26 #include <boost/tuple/tuple.hpp> 27 #include <boost/tokenizer.hpp> 46 size_t n1 = expected.cols(),
m1 = expected.rows();
47 size_t n2 = actual.cols(),
m2 = actual.rows();
49 cout <<
"not equal:" << endl;
50 print(expected,
"expected = ");
51 print(actual,
"actual = ");
53 cout <<
m1 <<
"," << n1 <<
" != " <<
m2 <<
"," << n2 << endl;
56 print(diff,
"actual - expected = ");
64 cout <<
"Erroneously equal:" << endl;
71 bool assert_equal(
const std::list<Matrix>& As,
const std::list<Matrix>& Bs,
double tol) {
72 if (As.size() != Bs.size())
return false;
74 list<Matrix>::const_iterator itA, itB;
75 itA = As.begin(); itB = Bs.begin();
76 for (; itA != As.end(); itA++, itB++)
87 size_t n1 = A.cols(),
m1 = A.rows();
88 size_t n2 = B.cols(),
m2 = B.rows();
90 bool dependent =
true;
91 if(
m1!=
m2 || n1!=n2) dependent =
false;
93 for(
size_t i=0; dependent &&
i<
m1;
i++) {
106 cout <<
"not linearly dependent:" << endl;
109 if(A.rows()!=B.rows() || A.cols()!=B.cols())
110 cout << A.rows() <<
"x" << A.cols() <<
" != " << B.rows() <<
"x" << B.cols() << endl;
120 cout <<
"not linearly dependent:" << endl;
123 if(A.rows()!=B.rows() || A.cols()!=B.cols())
124 cout << A.rows() <<
"x" << A.cols() <<
" != " << B.rows() <<
"x" << B.cols() << endl;
131 if (A.rows()!=v.size())
throw std::invalid_argument(
132 boost::str(boost::format(
"Matrix operator^ : A.m(%d)!=v.size(%d)") % A.rows() % v.size()));
136 return A.transpose() *
v;
167 fstream
stream(filename.c_str(), fstream::out | fstream::app);
179 while(getline(inputStream, line)) {
181 coeffs.push_back(vector<double>());
183 coeffs.back().reserve(width);
184 stringstream lineStream(line);
185 std::copy(istream_iterator<double>(lineStream), istream_iterator<double>(),
186 back_insert_iterator<vector<double> >(coeffs.back()));
188 width = coeffs.back().size();
189 if(coeffs.back().size() != width)
190 throw runtime_error(
"Error reading matrix from input stream, inconsistent numbers of elements in rows");
195 destinationMatrix.resize(height, width);
197 for(
const vector<double>& rowVec: coeffs) {
208 for (
size_t i = 0;
i<Hs.size(); ++
i) {
214 for (
size_t i = 0;
i<Hs.size(); ++
i) {
225 for (
size_t i = 0 ;
i < (
size_t) A.cols() ; ++
i ) {
226 v[
i] = A.col(
i).dot(A.col(
i));
235 const size_t m = A.rows(),
n = A.cols(), kprime =
min(m,
n);
237 Matrix Q=Matrix::Identity(m,m),
R(A);
241 for(
size_t j=0;
j < kprime;
j++){
248 for(
size_t k = 0 ; k < mm; k++)
253 boost::tie(beta,vjm) =
house(xjm);
256 for(
size_t k = 0 ; k <
m; k++)
257 v(k) = k<j ? 0.0 : vjm(k-j);
260 Matrix Qj = Matrix::Identity(m,m) - beta * v * v.transpose();
267 return make_pair(Q,
R);
271 list<boost::tuple<Vector, double, double> >
273 size_t m = A.rows(),
n = A.cols();
274 size_t maxRank =
min(m,
n);
277 list<boost::tuple<Vector, double, double> >
results;
280 Vector weights = sigmas.array().square().inverse();
286 for (
size_t j=0;
j<
n; ++
j) {
294 if (precision < 1
e-8)
continue;
298 for (
size_t j2=
j+1; j2<
n; ++j2)
299 r(j2) = pseudo.dot(A.col(j2));
302 double d = pseudo.dot(b);
309 if (results.size()>=maxRank)
break;
313 A -= a * r.transpose();
327 const size_t m = A.rows(),
n = A.cols(), kprime =
min(k,
min(m,
n));
329 for(
size_t j=0;
j < kprime;
j++) {
337 gttic(householder_update);
339 Vector w = beta * A.block(
j,
j,m-
j,
n-
j).transpose() * vjm;
340 A.block(
j,
j,m-
j,
n-
j) -= vjm * w.transpose();
341 gttoc(householder_update);
345 gttic(householder_vector_copy);
346 A.col(
j).segment(
j+1, m-(
j+1)) = vjm.segment(1, m-(
j+1));
347 gttoc(householder_vector_copy);
368 assert(L.rows() == L.cols());
378 assert(U.rows() == U.cols());
388 assert(U.rows() == U.cols());
401 va_start(ap, nrMatrices);
402 for(
size_t i = 0 ;
i < nrMatrices ;
i++) {
408 va_start(ap, nrMatrices);
411 for(
size_t i = 0 ;
i < nrMatrices ;
i++) {
413 for(
size_t d1 = 0; d1 < (
size_t) M->rows(); d1++)
415 A(vindex+d1,
d2) = (*M)(d1,
d2);
424 if (blocks.size() == 1)
return blocks.at(0);
425 DenseIndex nrows = 0, ncols = blocks.at(0).cols();
428 if (ncols !=
mat.cols())
429 throw invalid_argument(
"Matrix::stack(): column size mismatch!");
435 result.middleRows(cur_row,
mat.rows()) =
mat;
436 cur_row +=
mat.rows();
446 size_t dimA2 = n*matrices.size();
448 for(
const Matrix*
M: matrices) {
457 for(
const Matrix*
M: matrices) {
458 size_t row_len =
M->cols();
459 A.block(0, hindex, dimA1, row_len) = *
M;
469 vector<const Matrix *> matrices;
471 va_start(ap, nrMatrices);
472 for(
size_t i = 0 ;
i < nrMatrices ;
i++) {
474 matrices.push_back(M);
485 const double& vi =
v(
i);
507 const size_t n = A.cols();
509 for (
size_t j=0;
j<
n; ++
j) {
510 const double& vj =
v(
j);
515 for (
size_t j=0;
j<
n; ++
j)
541 Matrix inv = Matrix::Identity(A.rows(),A.rows());
543 return inv*inv.transpose();
553 Matrix inv = Matrix::Identity(A.rows(),A.rows());
555 return inv.transpose();
567 boost::tuple<int, double, Vector>
DLT(
const Matrix&
A,
double rank_tol) {
570 size_t n = A.rows(),
p = A.cols(),
m =
min(n,
p);
579 for (
size_t j = 0;
j <
m;
j++)
580 if (
s(
j) > rank_tol) rank++;
583 double error = m<
p ? 0 :
s(m-1);
584 return boost::tuple<int, double, Vector>((
int)rank, error,
Vector(
column(V,
p-1)));
589 Matrix E = Matrix::Identity(A.rows(),A.rows()), A_k = Matrix::Identity(A.rows(),A.rows());
590 for(
size_t k=1;k<=
K;k++) {
591 A_k = A_k*A/double(k);
601 const string firstline = label;
603 const string padding(firstline.size(),
' ');
604 const bool transposeMatrix = makeVectorHorizontal && matrix.cols() == 1 && matrix.rows() > 1;
605 const DenseIndex effectiveRows = transposeMatrix ? matrix.cols() : matrix.rows();
607 if(matrix.rows() > 0 && matrix.cols() > 0)
609 stringstream matrixPrinted;
611 matrixPrinted << matrix.transpose();
614 const std::string matrixStr = matrixPrinted.str();
615 boost::tokenizer<boost::char_separator<char> > tok(matrixStr, boost::char_separator<char>(
"\n"));
618 for(
const std::string& line: tok)
620 assert(row < effectiveRows);
623 ss <<
"[ " << line <<
" ]";
624 if(row < effectiveRows - 1)
629 ss <<
"Empty (" << matrix.rows() <<
"x" << matrix.cols() <<
")";
636 size_t rows = A.rows();
637 size_t cols = A.cols();
642 HCoeffsType hCoeffs(size);
643 RowVectorType temp(cols);
645 #if !EIGEN_VERSION_AT_LEAST(3,2,5)
std::string formatMatrixIndented(const std::string &label, const Matrix &matrix, bool makeVectorHorizontal)
void inplace_QR(Matrix &A)
boost::tuple< int, double, Vector > DLT(const Matrix &A, double rank_tol)
void zeroBelowDiagonal(MATRIX &A, size_t cols=0)
cout<< "Here is the matrix m:"<< endl<< m<< endl;JacobiSVD< MatrixXf > svd(m, ComputeThinU|ComputeThinV)
Matrix< RealScalar, Dynamic, Dynamic > M
static void run(MatrixQR &mat, HCoeffs &hCoeffs, Index maxBlockSize=32, typename MatrixQR::Scalar *tempData=0)
void save(const Matrix &A, const string &s, const string &filename)
double weightedPseudoinverse(const Vector &a, const Vector &weights, Vector &pseudo)
A thin wrapper around std::list that uses boost's fast_pool_allocator.
Matrix vector_scale(const Matrix &A, const Vector &v, bool inf_mask)
EIGEN_DEVICE_FUNC const CwiseBinaryOp< internal::scalar_boolean_xor_op, const Derived, const OtherDerived > operator^(const EIGEN_CURRENT_STORAGE_BASE_CLASS< OtherDerived > &other) const
Matrix diag(const std::vector< Matrix > &Hs)
const SingularValuesType & singularValues() const
A matrix or vector expression mapping an existing array of data.
bool assert_equal(const std::list< Matrix > &As, const std::list< Matrix > &Bs, double tol)
Traits::MatrixU matrixU() const
void vector_scale_inplace(const Vector &v, Matrix &A, bool inf_mask)
Rot2 R(Rot2::fromAngle(0.1))
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
Vector backSubstituteLower(const Matrix &L, const Vector &b, bool unit)
void print(const Matrix &A, const string &s)
ptrdiff_t DenseIndex
The index type for Eigen objects.
Matrix RtR(const Matrix &A)
Included from all GTSAM files.
Matrix expm(const Matrix &A, size_t K)
Matrix LLt(const Matrix &A)
const Eigen::IOFormat & matlabFormat()
void householder(const MatrixType &m)
Vector backSubstituteUpper(const Vector &b, const Matrix &U, bool unit)
constexpr int first(int i)
Implementation details for constexpr functions.
Tuple< Args... > make_tuple(Args...args)
Creates a tuple object, deducing the target type from the types of arguments.
istream & operator>>(istream &inputStream, Matrix &destinationMatrix)
pair< Matrix, Matrix > qr(const Matrix &A)
Standard Cholesky decomposition (LL^T) of a matrix and associated features.
void householder_(Matrix &A, size_t k, bool copy_vectors)
list< boost::tuple< Vector, double, double > > weighted_eliminate(Matrix &A, Vector &b, const Vector &sigmas)
static bool is_linear_dependent(const Matrix &A, const Matrix &B, double tol)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Matrix collect(size_t nrMatrices,...)
const MatrixVType & matrixV() const
const MatrixUType & matrixU() const
static std::stringstream ss
Matrix stack(const std::vector< Matrix > &blocks)
bool equal_with_abs_tol(const Eigen::DenseBase< MATRIX > &A, const Eigen::DenseBase< MATRIX > &B, double tol=1e-9)
typedef and functions to augment Eigen's VectorXd
bool linear_dependent(const Matrix &A, const Matrix &B, double tol)
Matrix inverse_square_root(const Matrix &A)
Vector columnNormSquare(const Matrix &A)
void insertSub(Eigen::MatrixBase< Derived1 > &fullMatrix, const Eigen::MatrixBase< Derived2 > &subMatrix, size_t i, size_t j)
The quaternion class used to represent 3D orientations and rotations.
std::map< std::string, Array< float, 1, 8, DontAlign|RowMajor > > results
Two-sided Jacobi SVD decomposition of a rectangular matrix.
bool assert_inequal(const Matrix &A, const Matrix &B, double tol)
pair< double, Vector > house(const Vector &x)
bool linear_independent(const Matrix &A, const Matrix &B, double tol)
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Traits::MatrixL matrixL() const
Matrix cholesky_inverse(const Matrix &A)
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
const MATRIX::ConstColXpr column(const MATRIX &A, size_t j)
double houseInPlace(Vector &v)