2007-12-11 18:07:18 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-23 02:25:33 +08:00
|
|
|
// for linear algebra.
|
2007-12-11 18:07:18 +08:00
|
|
|
//
|
2010-02-26 22:03:13 +08:00
|
|
|
// Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2007-12-11 18:07:18 +08:00
|
|
|
//
|
2008-02-28 23:44:45 +08:00
|
|
|
// Eigen is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
2008-03-03 19:02:52 +08:00
|
|
|
// License as published by the Free Software Foundation; either
|
2008-02-28 23:44:45 +08:00
|
|
|
// version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Alternatively, you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
2008-03-03 19:02:52 +08:00
|
|
|
// published by the Free Software Foundation; either version 2 of
|
2008-02-28 23:44:45 +08:00
|
|
|
// the License, or (at your option) any later version.
|
2007-12-11 18:07:18 +08:00
|
|
|
//
|
|
|
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2008-02-28 23:44:45 +08:00
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
|
|
|
// GNU General Public License for more details.
|
2007-12-11 18:07:18 +08:00
|
|
|
//
|
2008-03-03 19:02:52 +08:00
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
2008-02-28 23:44:45 +08:00
|
|
|
// License and a copy of the GNU General Public License along with
|
|
|
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
2007-12-11 18:07:18 +08:00
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
2008-03-03 18:52:44 +08:00
|
|
|
// check minor separately in order to avoid the possible creation of a zero-sized
|
|
|
|
// array. Comes from a compilation error with gcc-3.4 or gcc-4 with -ansi -pedantic.
|
2008-03-13 17:33:26 +08:00
|
|
|
// Another solution would be to declare the array like this: T m_data[Size==0?1:Size]; in ei_matrix_storage
|
2008-03-03 18:52:44 +08:00
|
|
|
// but this is probably not bad to raise such an error at compile time...
|
|
|
|
template<typename Scalar, int _Rows, int _Cols> struct CheckMinor
|
|
|
|
{
|
|
|
|
typedef Matrix<Scalar, _Rows, _Cols> MatrixType;
|
|
|
|
CheckMinor(MatrixType& m1, int r1, int c1)
|
|
|
|
{
|
|
|
|
int rows = m1.rows();
|
|
|
|
int cols = m1.cols();
|
|
|
|
|
|
|
|
Matrix<Scalar, Dynamic, Dynamic> mi = m1.minor(0,0).eval();
|
|
|
|
VERIFY_IS_APPROX(mi, m1.block(1,1,rows-1,cols-1));
|
|
|
|
mi = m1.minor(r1,c1);
|
|
|
|
VERIFY_IS_APPROX(mi.transpose(), m1.transpose().minor(c1,r1));
|
|
|
|
//check operator(), both constant and non-constant, on minor()
|
|
|
|
m1.minor(r1,c1)(0,0) = m1.minor(0,0)(0,0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Scalar> struct CheckMinor<Scalar,1,1>
|
|
|
|
{
|
|
|
|
typedef Matrix<Scalar, 1, 1> MatrixType;
|
|
|
|
CheckMinor(MatrixType&, int, int) {}
|
|
|
|
};
|
|
|
|
|
2010-02-26 22:03:13 +08:00
|
|
|
template<typename MatrixType> void block(const MatrixType& m)
|
2007-12-11 18:07:18 +08:00
|
|
|
{
|
|
|
|
/* this test covers the following files:
|
2010-02-26 22:03:13 +08:00
|
|
|
Row.h Column.h Block.h Minor.h
|
2007-12-11 18:07:18 +08:00
|
|
|
*/
|
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
2009-01-23 20:26:32 +08:00
|
|
|
typedef typename MatrixType::RealScalar RealScalar;
|
2008-03-13 01:17:36 +08:00
|
|
|
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
|
|
|
|
typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
|
2010-02-26 22:03:13 +08:00
|
|
|
typedef Matrix<Scalar, Dynamic, Dynamic> DynamicMatrixType;
|
|
|
|
typedef Matrix<Scalar, Dynamic, 1> DynamicVectorType;
|
|
|
|
|
2007-12-11 18:07:18 +08:00
|
|
|
int rows = m.rows();
|
|
|
|
int cols = m.cols();
|
2008-05-22 20:18:55 +08:00
|
|
|
|
2008-07-21 08:34:46 +08:00
|
|
|
MatrixType m1 = MatrixType::Random(rows, cols),
|
|
|
|
m2 = MatrixType::Random(rows, cols),
|
2007-12-11 18:07:18 +08:00
|
|
|
m3(rows, cols),
|
2008-07-21 08:34:46 +08:00
|
|
|
mzero = MatrixType::Zero(rows, cols),
|
2010-01-28 00:42:04 +08:00
|
|
|
ones = MatrixType::Ones(rows, cols);
|
2008-07-21 08:34:46 +08:00
|
|
|
VectorType v1 = VectorType::Random(rows),
|
|
|
|
v2 = VectorType::Random(rows),
|
|
|
|
v3 = VectorType::Random(rows),
|
|
|
|
vzero = VectorType::Zero(rows);
|
2007-12-11 18:07:18 +08:00
|
|
|
|
2008-02-28 20:38:12 +08:00
|
|
|
Scalar s1 = ei_random<Scalar>();
|
2008-03-03 19:02:52 +08:00
|
|
|
|
2008-02-28 20:38:12 +08:00
|
|
|
int r1 = ei_random<int>(0,rows-1);
|
|
|
|
int r2 = ei_random<int>(r1,rows-1);
|
|
|
|
int c1 = ei_random<int>(0,cols-1);
|
|
|
|
int c2 = ei_random<int>(c1,cols-1);
|
2008-03-03 19:02:52 +08:00
|
|
|
|
2007-12-11 18:07:18 +08:00
|
|
|
//check row() and col()
|
2010-02-26 22:03:13 +08:00
|
|
|
VERIFY_IS_EQUAL(m1.col(c1).transpose(), m1.transpose().row(c1));
|
2007-12-11 18:07:18 +08:00
|
|
|
//check operator(), both constant and non-constant, on row() and col()
|
|
|
|
m1.row(r1) += s1 * m1.row(r2);
|
|
|
|
m1.col(c1) += s1 * m1.col(c2);
|
2008-03-03 19:02:52 +08:00
|
|
|
|
2008-01-14 04:19:14 +08:00
|
|
|
//check block()
|
2007-12-11 18:07:18 +08:00
|
|
|
Matrix<Scalar,Dynamic,Dynamic> b1(1,1); b1(0,0) = m1(r1,c1);
|
2010-02-19 09:42:38 +08:00
|
|
|
|
2008-01-14 04:19:14 +08:00
|
|
|
RowVectorType br1(m1.block(r1,0,1,cols));
|
|
|
|
VectorType bc1(m1.block(0,c1,rows,1));
|
2010-02-26 22:03:13 +08:00
|
|
|
VERIFY_IS_EQUAL(b1, m1.block(r1,c1,1,1));
|
|
|
|
VERIFY_IS_EQUAL(m1.row(r1), br1);
|
|
|
|
VERIFY_IS_EQUAL(m1.col(c1), bc1);
|
2008-01-14 04:19:14 +08:00
|
|
|
//check operator(), both constant and non-constant, on block()
|
|
|
|
m1.block(r1,c1,r2-r1+1,c2-c1+1) = s1 * m2.block(0, 0, r2-r1+1,c2-c1+1);
|
|
|
|
m1.block(r1,c1,r2-r1+1,c2-c1+1)(r2-r1,c2-c1) = m2.block(0, 0, r2-r1+1,c2-c1+1)(0,0);
|
2008-05-22 20:18:55 +08:00
|
|
|
|
2007-12-11 18:07:18 +08:00
|
|
|
//check minor()
|
2008-03-13 01:17:36 +08:00
|
|
|
CheckMinor<Scalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> checkminor(m1,r1,c1);
|
2008-05-22 20:18:55 +08:00
|
|
|
|
|
|
|
const int BlockRows = EIGEN_ENUM_MIN(MatrixType::RowsAtCompileTime,2);
|
|
|
|
const int BlockCols = EIGEN_ENUM_MIN(MatrixType::ColsAtCompileTime,5);
|
|
|
|
if (rows>=5 && cols>=8)
|
|
|
|
{
|
2008-02-29 21:56:40 +08:00
|
|
|
// test fixed block() as lvalue
|
2008-05-22 20:18:55 +08:00
|
|
|
m1.template block<BlockRows,BlockCols>(1,1) *= s1;
|
2008-02-29 21:56:40 +08:00
|
|
|
// test operator() on fixed block() both as constant and non-constant
|
2008-05-22 20:18:55 +08:00
|
|
|
m1.template block<BlockRows,BlockCols>(1,1)(0, 3) = m1.template block<2,5>(1,1)(1,2);
|
2008-02-29 21:56:40 +08:00
|
|
|
// check that fixed block() and block() agree
|
2008-05-22 20:18:55 +08:00
|
|
|
Matrix<Scalar,Dynamic,Dynamic> b = m1.template block<BlockRows,BlockCols>(3,3);
|
2010-02-26 22:03:13 +08:00
|
|
|
VERIFY_IS_EQUAL(b, m1.block(3,3,BlockRows,BlockCols));
|
2007-12-11 18:07:18 +08:00
|
|
|
}
|
2008-08-10 04:06:25 +08:00
|
|
|
|
2008-09-09 17:30:23 +08:00
|
|
|
if (rows>2)
|
|
|
|
{
|
|
|
|
// test sub vectors
|
2010-02-26 22:03:13 +08:00
|
|
|
VERIFY_IS_EQUAL(v1.template head<2>(), v1.block(0,0,2,1));
|
|
|
|
VERIFY_IS_EQUAL(v1.template head<2>(), v1.head(2));
|
|
|
|
VERIFY_IS_EQUAL(v1.template head<2>(), v1.segment(0,2));
|
|
|
|
VERIFY_IS_EQUAL(v1.template head<2>(), v1.template segment<2>(0));
|
2008-09-09 17:30:23 +08:00
|
|
|
int i = rows-2;
|
2010-02-26 22:03:13 +08:00
|
|
|
VERIFY_IS_EQUAL(v1.template tail<2>(), v1.block(i,0,2,1));
|
|
|
|
VERIFY_IS_EQUAL(v1.template tail<2>(), v1.tail(2));
|
|
|
|
VERIFY_IS_EQUAL(v1.template tail<2>(), v1.segment(i,2));
|
|
|
|
VERIFY_IS_EQUAL(v1.template tail<2>(), v1.template segment<2>(i));
|
2008-09-09 17:30:23 +08:00
|
|
|
i = ei_random(0,rows-2);
|
2010-02-26 22:03:13 +08:00
|
|
|
VERIFY_IS_EQUAL(v1.segment(i,2), v1.template segment<2>(i));
|
2008-09-09 17:30:23 +08:00
|
|
|
}
|
|
|
|
|
2008-08-10 04:06:25 +08:00
|
|
|
// stress some basic stuffs with block matrices
|
2009-01-23 20:26:32 +08:00
|
|
|
VERIFY(ei_real(ones.col(c1).sum()) == RealScalar(rows));
|
|
|
|
VERIFY(ei_real(ones.row(r1).sum()) == RealScalar(cols));
|
2008-08-10 04:06:25 +08:00
|
|
|
|
2009-01-23 20:26:32 +08:00
|
|
|
VERIFY(ei_real(ones.col(c1).dot(ones.col(c2))) == RealScalar(rows));
|
|
|
|
VERIFY(ei_real(ones.row(r1).dot(ones.row(r2))) == RealScalar(cols));
|
2010-02-26 22:03:13 +08:00
|
|
|
|
|
|
|
// now test some block-inside-of-block.
|
|
|
|
|
|
|
|
// expressions with direct access
|
|
|
|
VERIFY_IS_EQUAL( (m1.block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2)) , (m1.block(r2,c2,rows-r2,cols-c2)) );
|
|
|
|
VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).row(0)) , (m1.row(r1).segment(c1,c2-c1+1)) );
|
|
|
|
VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).col(0)) , (m1.col(c1).segment(r1,r2-r1+1)) );
|
|
|
|
VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0)) , (m1.row(r1).segment(c1,c2-c1+1)) );
|
|
|
|
VERIFY_IS_EQUAL( (m1.transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0)) , (m1.row(r1).segment(c1,c2-c1+1)) );
|
|
|
|
|
|
|
|
// expressions without direct access
|
|
|
|
VERIFY_IS_EQUAL( ((m1+m2).block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2)) , ((m1+m2).block(r2,c2,rows-r2,cols-c2)) );
|
|
|
|
VERIFY_IS_EQUAL( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).row(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)) );
|
|
|
|
VERIFY_IS_EQUAL( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).col(0)) , ((m1+m2).col(c1).segment(r1,r2-r1+1)) );
|
|
|
|
VERIFY_IS_EQUAL( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)) );
|
|
|
|
VERIFY_IS_EQUAL( ((m1+m2).transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)) );
|
|
|
|
|
|
|
|
// evaluation into plain matrices from expressions with direct access (stress MapBase)
|
|
|
|
DynamicMatrixType dm;
|
|
|
|
DynamicVectorType dv;
|
|
|
|
dm.setZero();
|
|
|
|
dm = m1.block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2);
|
|
|
|
VERIFY_IS_EQUAL(dm, (m1.block(r2,c2,rows-r2,cols-c2)));
|
|
|
|
dm.setZero();
|
|
|
|
dv.setZero();
|
|
|
|
dm = m1.block(r1,c1,r2-r1+1,c2-c1+1).row(0).transpose();
|
|
|
|
dv = m1.row(r1).segment(c1,c2-c1+1);
|
|
|
|
VERIFY_IS_EQUAL(dv, dm);
|
|
|
|
dm.setZero();
|
|
|
|
dv.setZero();
|
|
|
|
dm = m1.col(c1).segment(r1,r2-r1+1);
|
|
|
|
dv = m1.block(r1,c1,r2-r1+1,c2-c1+1).col(0);
|
|
|
|
VERIFY_IS_EQUAL(dv, dm);
|
|
|
|
dm.setZero();
|
|
|
|
dv.setZero();
|
|
|
|
dm = m1.block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0);
|
|
|
|
dv = m1.row(r1).segment(c1,c2-c1+1);
|
|
|
|
VERIFY_IS_EQUAL(dv, dm);
|
|
|
|
dm.setZero();
|
|
|
|
dv.setZero();
|
|
|
|
dm = m1.row(r1).segment(c1,c2-c1+1).transpose();
|
|
|
|
dv = m1.transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0);
|
|
|
|
VERIFY_IS_EQUAL(dv, dm);
|
2007-12-11 18:07:18 +08:00
|
|
|
}
|
|
|
|
|
2009-08-31 23:39:56 +08:00
|
|
|
|
|
|
|
template<typename MatrixType>
|
|
|
|
void compare_using_data_and_stride(const MatrixType& m)
|
|
|
|
{
|
|
|
|
int rows = m.rows();
|
|
|
|
int cols = m.cols();
|
|
|
|
int size = m.size();
|
2010-02-19 09:42:38 +08:00
|
|
|
int innerStride = m.innerStride();
|
|
|
|
int outerStride = m.outerStride();
|
|
|
|
int rowStride = m.rowStride();
|
|
|
|
int colStride = m.colStride();
|
2009-08-31 23:39:56 +08:00
|
|
|
const typename MatrixType::Scalar* data = m.data();
|
|
|
|
|
|
|
|
for(int j=0;j<cols;++j)
|
|
|
|
for(int i=0;i<rows;++i)
|
2010-02-26 22:03:13 +08:00
|
|
|
VERIFY(m.coeff(i,j) == data[i*rowStride + j*colStride]);
|
2010-02-19 09:42:38 +08:00
|
|
|
|
|
|
|
if(!MatrixType::IsVectorAtCompileTime)
|
|
|
|
{
|
|
|
|
for(int j=0;j<cols;++j)
|
|
|
|
for(int i=0;i<rows;++i)
|
2010-02-26 22:03:13 +08:00
|
|
|
VERIFY(m.coeff(i,j) == data[(MatrixType::Flags&RowMajorBit)
|
|
|
|
? i*outerStride + j*innerStride
|
|
|
|
: j*outerStride + i*innerStride]);
|
2010-02-19 09:42:38 +08:00
|
|
|
}
|
2009-08-31 23:39:56 +08:00
|
|
|
|
|
|
|
if(MatrixType::IsVectorAtCompileTime)
|
|
|
|
{
|
2010-02-26 22:03:13 +08:00
|
|
|
VERIFY(innerStride == int((&m.coeff(1))-(&m.coeff(0))));
|
2009-08-31 23:39:56 +08:00
|
|
|
for (int i=0;i<size;++i)
|
2010-02-26 22:03:13 +08:00
|
|
|
VERIFY(m.coeff(i) == data[i*innerStride]);
|
2009-08-31 23:39:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename MatrixType>
|
|
|
|
void data_and_stride(const MatrixType& m)
|
|
|
|
{
|
|
|
|
int rows = m.rows();
|
|
|
|
int cols = m.cols();
|
|
|
|
|
|
|
|
int r1 = ei_random<int>(0,rows-1);
|
|
|
|
int r2 = ei_random<int>(r1,rows-1);
|
|
|
|
int c1 = ei_random<int>(0,cols-1);
|
|
|
|
int c2 = ei_random<int>(c1,cols-1);
|
|
|
|
|
|
|
|
MatrixType m1 = MatrixType::Random(rows, cols);
|
|
|
|
compare_using_data_and_stride(m1.block(r1, c1, r2-r1+1, c2-c1+1));
|
2010-02-26 10:01:52 +08:00
|
|
|
compare_using_data_and_stride(m1.transpose().block(c1, r1, c2-c1+1, r2-r1+1));
|
2009-08-31 23:39:56 +08:00
|
|
|
compare_using_data_and_stride(m1.row(r1));
|
|
|
|
compare_using_data_and_stride(m1.col(c1));
|
2010-02-26 10:01:52 +08:00
|
|
|
compare_using_data_and_stride(m1.row(r1).transpose());
|
|
|
|
compare_using_data_and_stride(m1.col(c1).transpose());
|
2009-08-31 23:39:56 +08:00
|
|
|
}
|
|
|
|
|
2010-02-26 22:03:13 +08:00
|
|
|
void test_block()
|
2008-05-22 20:18:55 +08:00
|
|
|
{
|
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
2010-02-26 22:03:13 +08:00
|
|
|
CALL_SUBTEST_1( block(Matrix<float, 1, 1>()) );
|
|
|
|
CALL_SUBTEST_2( block(Matrix4d()) );
|
|
|
|
CALL_SUBTEST_3( block(MatrixXcf(3, 3)) );
|
|
|
|
CALL_SUBTEST_4( block(MatrixXi(8, 12)) );
|
|
|
|
CALL_SUBTEST_5( block(MatrixXcd(20, 20)) );
|
|
|
|
CALL_SUBTEST_6( block(MatrixXf(20, 20)) );
|
|
|
|
|
|
|
|
CALL_SUBTEST_8( block(Matrix<float,Dynamic,4>(3, 4)) );
|
2010-01-28 00:42:04 +08:00
|
|
|
|
2010-02-19 09:42:38 +08:00
|
|
|
#ifndef EIGEN_DEFAULT_TO_ROW_MAJOR
|
2009-10-29 06:19:29 +08:00
|
|
|
CALL_SUBTEST_6( data_and_stride(MatrixXf(ei_random(5,50), ei_random(5,50))) );
|
|
|
|
CALL_SUBTEST_7( data_and_stride(Matrix<int,Dynamic,Dynamic,RowMajor>(ei_random(5,50), ei_random(5,50))) );
|
2010-02-19 09:42:38 +08:00
|
|
|
#endif
|
2008-05-22 20:18:55 +08:00
|
|
|
}
|
|
|
|
}
|