1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
// $Id: det_of_minor.cpp 3788 2016-02-09 15:50:06Z bradbell $
/* --------------------------------------------------------------------------
CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-16 Bradley M. Bell
CppAD is distributed under multiple licenses. This distribution is under
the terms of the
GNU General Public License Version 3.
A copy of this license is included in the COPYING file of this distribution.
Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
-------------------------------------------------------------------------- */
/*
$begin det_of_minor.cpp$$
$spell
det
Cpp
$$
$section Determinant of a Minor: Example and Test$$
$mindex det_of_minor$$
$code
$srcfile%speed/example/det_of_minor.cpp%0%// BEGIN C++%// END C++%1%$$
$$
$end
*/
// BEGIN C++
# include <vector>
# include <cstddef>
# include <cppad/speed/det_of_minor.hpp>
bool det_of_minor()
{ bool ok = true;
size_t i;
// dimension of the matrix A
size_t m = 3;
// index vectors set so minor is the entire matrix A
std::vector<size_t> r(m + 1);
std::vector<size_t> c(m + 1);
for(i= 0; i < m; i++)
{ r[i] = i+1;
c[i] = i+1;
}
r[m] = 0;
c[m] = 0;
// values in the matrix A
double data[] = {
1., 2., 3.,
3., 2., 1.,
2., 1., 2.
};
// construct vector a with the values of the matrix A
std::vector<double> a(data, data + 9);
// evaluate the determinant of A
size_t n = m; // minor has same dimension as A
double det = CppAD::det_of_minor(a, m, n, r, c);
// check the value of the determinant of A
ok &= (det == (double) (1*(2*2-1*1) - 2*(3*2-1*2) + 3*(3*1-2*2)) );
// minor where row 0 and column 1 are removed
r[m] = 1; // skip row index 0 by starting at row index 1
c[0] = 2; // skip column index 1 by pointing from index 0 to index 2
// evaluate determinant of the minor
n = m - 1; // dimension of the minor
det = CppAD::det_of_minor(a, m, m-1, r, c);
// check the value of the determinant of the minor
ok &= (det == (double) (3*2-1*2) );
return ok;
}
// END C++
|