linear_dependent prints when not linearly dependent, like assert_equal. Added linear_independent to check the opposite.

release/4.3a0
Richard Roberts 2010-11-19 17:19:26 +00:00
parent 4e0b4021df
commit 739764ca8e
3 changed files with 45 additions and 8 deletions

View File

@ -175,17 +175,49 @@ bool assert_equal(const std::list<Matrix>& As, const std::list<Matrix>& Bs, doub
} }
/* ************************************************************************* */ /* ************************************************************************* */
bool linear_dependent(const Matrix& A, const Matrix& B, double tol) { static bool is_linear_dependent(const Matrix& A, const Matrix& B, double tol) {
// This local static function is used by linear_independent and
// linear_dependent just below.
size_t n1 = A.size2(), m1 = A.size1(); size_t n1 = A.size2(), m1 = A.size1();
size_t n2 = B.size2(), m2 = B.size1(); size_t n2 = B.size2(), m2 = B.size1();
if(m1!=m2 || n1!=n2) return false; bool dependent = true;
if(m1!=m2 || n1!=n2) dependent = false;
for(size_t i=0; i<m1; i++) { for(size_t i=0; dependent && i<m1; i++) {
if (!gtsam::linear_dependent(row_(A,i), row_(B,i), tol)) if (!gtsam::linear_dependent(row_(A,i), row_(B,i), tol))
dependent = false;
}
return dependent;
}
/* ************************************************************************* */
bool linear_independent(const Matrix& A, const Matrix& B, double tol) {
if(!is_linear_dependent(A, B, tol))
return true;
else {
cout << "not linearly dependent:" << endl;
print(A,"A = ");
print(B,"B = ");
if(A.size1()!=B.size1() || A.size2()!=B.size2())
cout << A.size1() << "x" << A.size2() << " != " << B.size1() << "x" << B.size2() << endl;
return false; return false;
} }
}
/* ************************************************************************* */
bool linear_dependent(const Matrix& A, const Matrix& B, double tol) {
if(is_linear_dependent(A, B, tol))
return true; return true;
else {
cout << "not linearly dependent:" << endl;
print(A,"A = ");
print(B,"B = ");
if(A.size1()!=B.size1() || A.size2()!=B.size2())
cout << A.size1() << "x" << A.size2() << " != " << B.size1() << "x" << B.size2() << endl;
return false;
}
} }
/* ************************************************************************* */ /* ************************************************************************* */

View File

@ -99,7 +99,12 @@ bool assert_equal(const Matrix& A, const Matrix& B, double tol = 1e-9);
bool assert_equal(const std::list<Matrix>& As, const std::list<Matrix>& Bs, double tol = 1e-9); bool assert_equal(const std::list<Matrix>& As, const std::list<Matrix>& Bs, double tol = 1e-9);
/** /**
* check whether the rows of two matrices are linear indepdent * check whether the rows of two matrices are linear independent
*/
bool linear_independent(const Matrix& A, const Matrix& B, double tol = 1e-9);
/**
* check whether the rows of two matrices are linear dependent
*/ */
bool linear_dependent(const Matrix& A, const Matrix& B, double tol = 1e-9); bool linear_dependent(const Matrix& A, const Matrix& B, double tol = 1e-9);

View File

@ -882,7 +882,7 @@ TEST( matrix, linear_dependent3 )
{ {
Matrix A = Matrix_(2, 3, 0.0, 2.0, 3.0, 4.0, 5.0, 6.0); Matrix A = Matrix_(2, 3, 0.0, 2.0, 3.0, 4.0, 5.0, 6.0);
Matrix B = Matrix_(2, 3, 0.0, -2.0, -3.0, 8.1, 10.0, 12.0); Matrix B = Matrix_(2, 3, 0.0, -2.0, -3.0, 8.1, 10.0, 12.0);
CHECK(!linear_dependent(A, B)); CHECK(linear_independent(A, B));
} }
/* ************************************************************************* */ /* ************************************************************************* */