2008-01-15 21:55:47 +08:00
|
|
|
#include <Eigen/Core>
|
|
|
|
|
|
|
|
USING_PART_OF_NAMESPACE_EIGEN
|
|
|
|
|
|
|
|
namespace Eigen {
|
|
|
|
|
2008-04-13 01:37:27 +08:00
|
|
|
/* Echelon a matrix in-place:
|
|
|
|
*
|
|
|
|
* Meta-Unrolled version, for small fixed-size matrices
|
|
|
|
*/
|
|
|
|
template<typename Derived, int Step>
|
|
|
|
struct unroll_echelon
|
2008-01-15 21:55:47 +08:00
|
|
|
{
|
2008-04-13 01:37:27 +08:00
|
|
|
enum { k = Step - 1,
|
|
|
|
Rows = Derived::RowsAtCompileTime,
|
|
|
|
Cols = Derived::ColsAtCompileTime,
|
|
|
|
CornerRows = Rows - k,
|
|
|
|
CornerCols = Cols - k
|
|
|
|
};
|
|
|
|
static void run(MatrixBase<Derived>& m)
|
2008-01-15 21:55:47 +08:00
|
|
|
{
|
2008-04-13 01:37:27 +08:00
|
|
|
unroll_echelon<Derived, Step-1>::run(m);
|
2008-01-15 21:55:47 +08:00
|
|
|
int rowOfBiggest, colOfBiggest;
|
2008-04-13 01:37:27 +08:00
|
|
|
m.template corner<CornerRows, CornerCols>(BottomRight)
|
2008-07-08 15:56:01 +08:00
|
|
|
.cwise().abs()
|
2008-03-26 16:48:04 +08:00
|
|
|
.maxCoeff(&rowOfBiggest, &colOfBiggest);
|
2008-01-15 21:55:47 +08:00
|
|
|
m.row(k).swap(m.row(k+rowOfBiggest));
|
|
|
|
m.col(k).swap(m.col(k+colOfBiggest));
|
2008-04-13 01:37:27 +08:00
|
|
|
m.template corner<CornerRows-1, CornerCols>(BottomRight)
|
2010-01-05 10:24:43 +08:00
|
|
|
-= m.col(k).template tail<CornerRows-1>()
|
|
|
|
* (m.row(k).template tail<CornerCols>() / m(k,k));
|
2008-04-13 01:37:27 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Derived>
|
|
|
|
struct unroll_echelon<Derived, 0>
|
|
|
|
{
|
|
|
|
static void run(MatrixBase<Derived>& m) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Echelon a matrix in-place:
|
|
|
|
*
|
|
|
|
* Non-unrolled version, for dynamic-size matrices.
|
|
|
|
* (this version works for all matrices, but in the fixed-size case the other
|
|
|
|
* version is faster).
|
|
|
|
*/
|
|
|
|
template<typename Derived>
|
|
|
|
struct unroll_echelon<Derived, Dynamic>
|
|
|
|
{
|
|
|
|
static void run(MatrixBase<Derived>& m)
|
|
|
|
{
|
2008-04-14 16:20:24 +08:00
|
|
|
for(int k = 0; k < m.diagonal().size() - 1; k++)
|
2008-04-13 01:37:27 +08:00
|
|
|
{
|
|
|
|
int rowOfBiggest, colOfBiggest;
|
|
|
|
int cornerRows = m.rows()-k, cornerCols = m.cols()-k;
|
|
|
|
m.corner(BottomRight, cornerRows, cornerCols)
|
2008-07-08 15:56:01 +08:00
|
|
|
.cwise().abs()
|
2008-04-13 01:37:27 +08:00
|
|
|
.maxCoeff(&rowOfBiggest, &colOfBiggest);
|
|
|
|
m.row(k).swap(m.row(k+rowOfBiggest));
|
|
|
|
m.col(k).swap(m.col(k+colOfBiggest));
|
|
|
|
m.corner(BottomRight, cornerRows-1, cornerCols)
|
2010-01-05 10:24:43 +08:00
|
|
|
-= m.col(k).tail(cornerRows-1) * (m.row(k).tail(cornerCols) / m(k,k));
|
2008-04-13 01:37:27 +08:00
|
|
|
}
|
2008-01-15 21:55:47 +08:00
|
|
|
}
|
2008-04-13 01:37:27 +08:00
|
|
|
};
|
2008-04-14 16:20:24 +08:00
|
|
|
|
2008-04-13 01:37:27 +08:00
|
|
|
using namespace std;
|
|
|
|
template<typename Derived>
|
|
|
|
void echelon(MatrixBase<Derived>& m)
|
|
|
|
{
|
|
|
|
const int size = DiagonalCoeffs<Derived>::SizeAtCompileTime;
|
|
|
|
const bool unroll = size <= 4;
|
2008-04-14 16:20:24 +08:00
|
|
|
unroll_echelon<Derived, unroll ? size-1 : Dynamic>::run(m);
|
2008-01-15 21:55:47 +08:00
|
|
|
}
|
|
|
|
|
2008-03-17 05:04:33 +08:00
|
|
|
template<typename Derived>
|
|
|
|
void doSomeRankPreservingOperations(MatrixBase<Derived>& m)
|
2008-01-15 21:55:47 +08:00
|
|
|
{
|
|
|
|
for(int a = 0; a < 3*(m.rows()+m.cols()); a++)
|
|
|
|
{
|
2008-03-17 15:35:22 +08:00
|
|
|
double d = ei_random<double>(-1,1);
|
|
|
|
int i = ei_random<int>(0,m.rows()-1); // i is a random row number
|
2008-01-15 21:55:47 +08:00
|
|
|
int j;
|
|
|
|
do {
|
2008-03-17 15:35:22 +08:00
|
|
|
j = ei_random<int>(0,m.rows()-1);
|
2008-01-15 21:55:47 +08:00
|
|
|
} while (i==j); // j is another one (must be different)
|
|
|
|
m.row(i) += d * m.row(j);
|
|
|
|
|
2008-03-17 15:35:22 +08:00
|
|
|
i = ei_random<int>(0,m.cols()-1); // i is a random column number
|
2008-01-15 21:55:47 +08:00
|
|
|
do {
|
2008-03-17 15:35:22 +08:00
|
|
|
j = ei_random<int>(0,m.cols()-1);
|
2008-01-15 21:55:47 +08:00
|
|
|
} while (i==j); // j is another one (must be different)
|
|
|
|
m.col(i) += d * m.col(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Eigen
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
int main(int, char **)
|
|
|
|
{
|
|
|
|
srand((unsigned int)time(0));
|
2008-04-14 16:20:24 +08:00
|
|
|
const int Rows = 6, Cols = 4;
|
2008-01-15 21:55:47 +08:00
|
|
|
typedef Matrix<double, Rows, Cols> Mat;
|
|
|
|
const int N = Rows < Cols ? Rows : Cols;
|
|
|
|
|
|
|
|
// start with a matrix m that's obviously of rank N-1
|
|
|
|
Mat m = Mat::identity(Rows, Cols); // args just in case of dyn. size
|
|
|
|
m.row(0) = m.row(1) = m.row(0) + m.row(1);
|
|
|
|
|
|
|
|
doSomeRankPreservingOperations(m);
|
|
|
|
|
|
|
|
// now m is still a matrix of rank N-1
|
|
|
|
cout << "Here's the matrix m:" << endl << m << endl;
|
|
|
|
|
2008-03-26 17:29:29 +08:00
|
|
|
cout << "Now let's echelon m (repeating many times for benchmarking purposes):" << endl;
|
|
|
|
for(int i = 0; i < 1000000; i++) echelon(m);
|
2008-01-15 21:55:47 +08:00
|
|
|
|
|
|
|
cout << "Now m is:" << endl << m << endl;
|
|
|
|
}
|