mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-07-11 22:03:01 -06:00
eigen_demo: Demo is now also test
git-svn-id: svn://ultimatepp.org/upp/trunk@14897 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
622c0d0147
commit
e1865d70fa
3 changed files with 270 additions and 308 deletions
|
|
@ -30,64 +30,66 @@ struct SerialTest {
|
|||
|
||||
CONSOLE_APP_MAIN
|
||||
{
|
||||
// https://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html
|
||||
Cout() << "\n\nTutorial page 1 - The Matrix class";
|
||||
StdLogSetup(LOG_COUT|LOG_FILE);
|
||||
|
||||
Cout() << "\n\nCoefficient accessors";
|
||||
UppLog() << "Eigen library demo";
|
||||
|
||||
// https://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html
|
||||
UppLog() << "\n\nTutorial page 1 - The Matrix class";
|
||||
|
||||
UppLog() << "\n\nCoefficient accessors";
|
||||
{
|
||||
MatrixXd m(2,2);
|
||||
m(0,0) = 3;
|
||||
m(1,0) = 2.5;
|
||||
m(0,1) = -1;
|
||||
m(1,1) = m(1,0) + m(0,1);
|
||||
Cout() << "\nHere is the matrix m:\n" << m;
|
||||
UppLog() << "\nHere is the matrix m:\n" << m;
|
||||
|
||||
VectorXd v(2);
|
||||
v(0) = 4;
|
||||
v(1) = v(0) - 1;
|
||||
Cout() << "\nHere is the vector v:\n" << v;
|
||||
UppLog() << "\nHere is the vector v:\n" << v;
|
||||
}
|
||||
Cout() << "\n\nResizing";
|
||||
UppLog() << "\n\nResizing";
|
||||
{
|
||||
MatrixXd m(2,5);
|
||||
m.resize(4,3);
|
||||
Cout() << "\nThe matrix m is of size " << m.rows() << "x" << m.cols();
|
||||
Cout() << "\nIt has " << m.size() << " coefficients";
|
||||
UppLog() << "\nThe matrix m is of size " << m.rows() << "x" << m.cols();
|
||||
UppLog() << "\nIt has " << m.size() << " coefficients";
|
||||
|
||||
VectorXd v(2);
|
||||
v.resize(5);
|
||||
Cout() << "\nThe vector v is of size " << v.size();
|
||||
Cout() << "\nAs a matrix, v is of size " << v.rows() << "x" << v.cols();
|
||||
UppLog() << "\nThe vector v is of size " << v.size();
|
||||
UppLog() << "\nAs a matrix, v is of size " << v.rows() << "x" << v.cols();
|
||||
}
|
||||
Cout() << "\n\nAssignment and resizing";
|
||||
UppLog() << "\n\nAssignment and resizing";
|
||||
{
|
||||
double _dat[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};// Assignment from C vector
|
||||
VectorXd dat = Map<VectorXd>(_dat, sizeof(_dat)/sizeof(double));
|
||||
Cout() << "\nC array data is " << dat.transpose();
|
||||
UppLog() << "\nC array data is " << dat.transpose();
|
||||
|
||||
const int dec = 5;
|
||||
VectorXd decimated = Map<VectorXd, 0, InnerStride<dec>>(dat.data(), 1+((dat.size()-1)/dec));
|
||||
Cout() << "\nDecimated " << decimated.transpose();
|
||||
UppLog() << "\nDecimated " << decimated.transpose();
|
||||
|
||||
VectorXd even = Map<VectorXd, 0, InnerStride<2>>(dat.data(), (dat.size()+1)/2);
|
||||
Cout() << "\nEven " << even.transpose();
|
||||
UppLog() << "\nEven " << even.transpose();
|
||||
|
||||
VectorXd odd = Map<VectorXd, 0, InnerStride<2>>(dat.data()+1, dat.size()/2);
|
||||
Cout() << "\nOdd " << odd.transpose();
|
||||
UppLog() << "\nOdd " << odd.transpose();
|
||||
|
||||
MatrixXf a(2,2);
|
||||
Cout() << "\na is of size " << a.rows() << "x" << a.cols();
|
||||
UppLog() << "\na is of size " << a.rows() << "x" << a.cols();
|
||||
MatrixXf b(3,3);
|
||||
a = b;
|
||||
Cout() << "\na is now of size " << a.rows() << "x" << a.cols();
|
||||
UppLog() << "\na is now of size " << a.rows() << "x" << a.cols();
|
||||
}
|
||||
Cout() << "\nPress enter to continue\n";
|
||||
ReadStdIn();
|
||||
|
||||
// https://eigen.tuxfamily.org/dox-devel/group__TutorialMatrixArithmetic.html
|
||||
Cout() << "\n\nTutorial page 2 - Matrix and vector arithmetic";
|
||||
UppLog() << "\n\nTutorial page 2 - Matrix and vector arithmetic";
|
||||
|
||||
Cout() << "\n\nAddition and subtraction";
|
||||
UppLog() << "\n\nAddition and subtraction";
|
||||
{
|
||||
Matrix2d a;
|
||||
a << 1, 2,
|
||||
|
|
@ -96,98 +98,96 @@ CONSOLE_APP_MAIN
|
|||
b << 2, 3,
|
||||
1, 4;
|
||||
|
||||
Cout() << "\na + b =\n" << a + b;
|
||||
Cout() << "\na - b =\n" << a - b;
|
||||
Cout() << "\nDoing a += b;";
|
||||
UppLog() << "\na + b =\n" << a + b;
|
||||
UppLog() << "\na - b =\n" << a - b;
|
||||
UppLog() << "\nDoing a += b;";
|
||||
a += b;
|
||||
Cout() << "\nNow a =\n" << a;
|
||||
UppLog() << "\nNow a =\n" << a;
|
||||
|
||||
Vector3d v(1,2,3);
|
||||
Vector3d w(1,0,0);
|
||||
Cout() << "\n-v + w - v =\n" << -v + w - v;
|
||||
UppLog() << "\n-v + w - v =\n" << -v + w - v;
|
||||
}
|
||||
Cout() << "\n\nScalar multiplication and division";
|
||||
UppLog() << "\n\nScalar multiplication and division";
|
||||
{
|
||||
Matrix2d a;
|
||||
a << 1, 2,
|
||||
3, 4;
|
||||
Vector3d v(1,2,3);
|
||||
Cout() << "\na * 2.5 =\n" << a * 2.5;
|
||||
Cout() << "\n0.1 * v =\n" << 0.1 * v;
|
||||
Cout() << "\nDoing v *= 2;";
|
||||
UppLog() << "\na * 2.5 =\n" << a * 2.5;
|
||||
UppLog() << "\n0.1 * v =\n" << 0.1 * v;
|
||||
UppLog() << "\nDoing v *= 2;";
|
||||
v *= 2;
|
||||
Cout() << "\nNow v =\n" << v;
|
||||
UppLog() << "\nNow v =\n" << v;
|
||||
}
|
||||
Cout() << "\n\nTransposition and conjugation";
|
||||
UppLog() << "\n\nTransposition and conjugation";
|
||||
{
|
||||
MatrixXcf a = MatrixXcf::Random(2,2);
|
||||
Cout() << "\nHere is the matrix a\n" << a;
|
||||
Cout() << "\nHere is the matrix a^T\n" << a.transpose();
|
||||
Cout() << "\nHere is the conjugate of a\n" << a.conjugate();
|
||||
Cout() << "\nHere is the matrix a^*\n" << a.adjoint();
|
||||
UppLog() << "\nHere is the matrix a\n" << a;
|
||||
UppLog() << "\nHere is the matrix a^T\n" << a.transpose();
|
||||
UppLog() << "\nHere is the conjugate of a\n" << a.conjugate();
|
||||
UppLog() << "\nHere is the matrix a^*\n" << a.adjoint();
|
||||
|
||||
VectorXd v(5);
|
||||
v << 1, 2, 3, 4, 5;
|
||||
Cout() << "\n\nInitial vector " << v.transpose();
|
||||
Cout() << "\nReversed vector " << v.reverse().transpose();
|
||||
UppLog() << "\n\nInitial vector " << v.transpose();
|
||||
UppLog() << "\nReversed vector " << v.reverse().transpose();
|
||||
}
|
||||
Cout() << "\n\nMatrix-matrix and matrix-vector multiplication";
|
||||
UppLog() << "\n\nMatrix-matrix and matrix-vector multiplication";
|
||||
{
|
||||
Matrix2d mat;
|
||||
mat << 1, 2,
|
||||
3, 4;
|
||||
Vector2d u(-1,1), v(2,0);
|
||||
Cout() << "\nHere is mat*mat:\n" << mat*mat;
|
||||
Cout() << "\nHere is mat*u:\n" << mat*u;
|
||||
Cout() << "\nHere is u^T*mat:\n" << u.transpose()*mat;
|
||||
Cout() << "\nHere is u^T*v:\n" << u.transpose()*v;
|
||||
Cout() << "\nHere is u*v^T:\n" << u*v.transpose();
|
||||
Cout() << "\nLet's multiply mat by itself";
|
||||
UppLog() << "\nHere is mat*mat:\n" << mat*mat;
|
||||
UppLog() << "\nHere is mat*u:\n" << mat*u;
|
||||
UppLog() << "\nHere is u^T*mat:\n" << u.transpose()*mat;
|
||||
UppLog() << "\nHere is u^T*v:\n" << u.transpose()*v;
|
||||
UppLog() << "\nHere is u*v^T:\n" << u*v.transpose();
|
||||
UppLog() << "\nLet's multiply mat by itself";
|
||||
mat *= mat;
|
||||
Cout() << "\nNow mat is mat:\n" << mat;
|
||||
UppLog() << "\nNow mat is mat:\n" << mat;
|
||||
}
|
||||
Cout() << "\n\nDot product and cross product";
|
||||
UppLog() << "\n\nDot product and cross product";
|
||||
{
|
||||
Vector3d v(1,2,3);
|
||||
Vector3d w(0,1,2);
|
||||
|
||||
Cout() << "\nDot product: " << v.dot(w);
|
||||
UppLog() << "\nDot product: " << v.dot(w);
|
||||
double dp = v.adjoint()*w; // automatic conversion of the inner product to a scalar
|
||||
Cout() << "\nDot product via a matrix product: " << dp;
|
||||
Cout() << "\nCross product:\n" << v.cross(w);
|
||||
UppLog() << "\nDot product via a matrix product: " << dp;
|
||||
UppLog() << "\nCross product:\n" << v.cross(w);
|
||||
}
|
||||
Cout() << "\n\nBasic arithmetic reduction operations";
|
||||
UppLog() << "\n\nBasic arithmetic reduction operations";
|
||||
{
|
||||
Eigen::Matrix2d mat;
|
||||
mat << 1, 2,
|
||||
3, 4;
|
||||
Cout() << "\nHere is mat.sum(): " << mat.sum();
|
||||
Cout() << "\nHere is mat.prod(): " << mat.prod();
|
||||
Cout() << "\nHere is mat.mean(): " << mat.mean();
|
||||
Cout() << "\nHere is mat.minCoeff(): " << mat.minCoeff();
|
||||
Cout() << "\nHere is mat.maxCoeff(): " << mat.maxCoeff();
|
||||
Cout() << "\nHere is mat.trace(): " << mat.trace();
|
||||
UppLog() << "\nHere is mat.sum(): " << mat.sum();
|
||||
UppLog() << "\nHere is mat.prod(): " << mat.prod();
|
||||
UppLog() << "\nHere is mat.mean(): " << mat.mean();
|
||||
UppLog() << "\nHere is mat.minCoeff(): " << mat.minCoeff();
|
||||
UppLog() << "\nHere is mat.maxCoeff(): " << mat.maxCoeff();
|
||||
UppLog() << "\nHere is mat.trace(): " << mat.trace();
|
||||
|
||||
Matrix3f m = Matrix3f::Random();
|
||||
ptrdiff_t i, j;
|
||||
float minOfM = m.minCoeff(&i, &j);
|
||||
Cout() << "\nHere is the matrix m:\n" << m;
|
||||
Cout() << "\nIts minimum coefficient (" << minOfM
|
||||
UppLog() << "\nHere is the matrix m:\n" << m;
|
||||
UppLog() << "\nIts minimum coefficient (" << minOfM
|
||||
<< ") is at position (" << i << "," << j << ")\n";
|
||||
|
||||
RowVector4i v = RowVector4i::Random();
|
||||
ptrdiff_t maxOfV = v.maxCoeff(&i);
|
||||
Cout() << "\nHere is the vector v: " << v;
|
||||
Cout() << "\nIts maximum coefficient (" << maxOfV
|
||||
UppLog() << "\nHere is the vector v: " << v;
|
||||
UppLog() << "\nIts maximum coefficient (" << maxOfV
|
||||
<< ") is at position " << i;
|
||||
}
|
||||
Cout() << "\nPress enter to continue\n";
|
||||
ReadStdIn();
|
||||
|
||||
// https://eigen.tuxfamily.org/dox/group__TutorialArrayClass.html
|
||||
Cout() << "\n\nTutorial page 3 - The Array class and coefficient-wise operations ";
|
||||
UppLog() << "\n\nTutorial page 3 - The Array class and coefficient-wise operations ";
|
||||
|
||||
Cout() << "\n\nAccessing values inside an Array";
|
||||
UppLog() << "\n\nAccessing values inside an Array";
|
||||
{
|
||||
ArrayXXf m(2,2);
|
||||
|
||||
|
|
@ -196,16 +196,16 @@ CONSOLE_APP_MAIN
|
|||
m(1,0) = 3.0; m(1,1) = m(0,1) + m(1,0);
|
||||
|
||||
// print values to standard output
|
||||
Cout() << "\n" << m;
|
||||
UppLog() << "\n" << m;
|
||||
|
||||
// using the comma-initializer is also allowed
|
||||
m << 1.0,2.0,
|
||||
3.0,4.0;
|
||||
|
||||
// print values to standard output
|
||||
Cout() << "\n" << m;
|
||||
UppLog() << "\n" << m;
|
||||
}
|
||||
Cout() << "\n\nAddition and subtraction";
|
||||
UppLog() << "\n\nAddition and subtraction";
|
||||
{
|
||||
ArrayXXf a(3,3);
|
||||
ArrayXXf b(3,3);
|
||||
|
|
@ -217,12 +217,12 @@ CONSOLE_APP_MAIN
|
|||
1,2,3;
|
||||
|
||||
// Adding two arrays
|
||||
Cout() << "\na + b = " << "\n" << a + b;
|
||||
UppLog() << "\na + b = " << "\n" << a + b;
|
||||
|
||||
// Subtracting a scalar from an array
|
||||
Cout() << "\na - 2 = " << "\n" << a - 2;
|
||||
UppLog() << "\na - 2 = " << "\n" << a - 2;
|
||||
}
|
||||
Cout() << "\n\nArray multiplication";
|
||||
UppLog() << "\n\nArray multiplication";
|
||||
{
|
||||
ArrayXXf a(2,2);
|
||||
ArrayXXf b(2,2);
|
||||
|
|
@ -230,18 +230,18 @@ CONSOLE_APP_MAIN
|
|||
3,4;
|
||||
b << 5,6,
|
||||
7,8;
|
||||
Cout() << "\na * b = " << "\n" << a * b;
|
||||
UppLog() << "\na * b = " << "\n" << a * b;
|
||||
}
|
||||
Cout() << "\n\nOther coefficient-wise operations";
|
||||
UppLog() << "\n\nOther coefficient-wise operations";
|
||||
{
|
||||
ArrayXf a = ArrayXf::Random(5);
|
||||
a *= 2;
|
||||
Cout() << "\na =" << "\n" << a;
|
||||
Cout() << "\na.abs() =" << "\n" << a.abs();
|
||||
Cout() << "\na.abs().sqrt() =" << "\n" << a.abs().sqrt();
|
||||
Cout() << "\na.min(a.abs().sqrt()) =" << "\n" << a.min(a.abs().sqrt());
|
||||
UppLog() << "\na =" << "\n" << a;
|
||||
UppLog() << "\na.abs() =" << "\n" << a.abs();
|
||||
UppLog() << "\na.abs().sqrt() =" << "\n" << a.abs().sqrt();
|
||||
UppLog() << "\na.min(a.abs().sqrt()) =" << "\n" << a.min(a.abs().sqrt());
|
||||
}
|
||||
Cout() << "\n\nConverting between array and matrix expressions";
|
||||
UppLog() << "\n\nConverting between array and matrix expressions";
|
||||
{
|
||||
MatrixXf m(2,2);
|
||||
MatrixXf n(2,2);
|
||||
|
|
@ -250,10 +250,10 @@ CONSOLE_APP_MAIN
|
|||
n << 5,6,
|
||||
7,8;
|
||||
|
||||
Cout() << "\n-- Matrix m*n: --" << "\n" << m * n;
|
||||
Cout() << "\n-- Array m*n: --" << "\n" << m.array() * n.array();
|
||||
Cout() << "\n-- With cwiseProduct: --" << "\n" << m.cwiseProduct(n);
|
||||
Cout() << "\n-- Array m + 4: --" << "\n" << m.array() + 4;
|
||||
UppLog() << "\n-- Matrix m*n: --" << "\n" << m * n;
|
||||
UppLog() << "\n-- Array m*n: --" << "\n" << m.array() * n.array();
|
||||
UppLog() << "\n-- With cwiseProduct: --" << "\n" << m.cwiseProduct(n);
|
||||
UppLog() << "\n-- Array m + 4: --" << "\n" << m.array() + 4;
|
||||
}
|
||||
{
|
||||
MatrixXf m(2,2);
|
||||
|
|
@ -263,27 +263,25 @@ CONSOLE_APP_MAIN
|
|||
n << 5,6,
|
||||
7,8;
|
||||
|
||||
Cout() << "\n-- Combination 1: --" << "\n" << (m.array() + 4).matrix() * m;
|
||||
Cout() << "\n-- Combination 2: --" << "\n" << (m.array() * n.array()).matrix() * m;
|
||||
UppLog() << "\n-- Combination 1: --" << "\n" << (m.array() + 4).matrix() * m;
|
||||
UppLog() << "\n-- Combination 2: --" << "\n" << (m.array() * n.array()).matrix() * m;
|
||||
}
|
||||
Cout() << "\nPress enter to continue\n";
|
||||
ReadStdIn();
|
||||
|
||||
// https://eigen.tuxfamily.org/dox/group__TutorialBlockOperations.html
|
||||
Cout() << "\n\nTutorial page 4 - Block operations";
|
||||
UppLog() << "\n\nTutorial page 4 - Block operations";
|
||||
|
||||
Cout() << "\n\nUsing block operations";
|
||||
UppLog() << "\n\nUsing block operations";
|
||||
{
|
||||
Eigen::MatrixXf m(4,4);
|
||||
m << 1, 2, 3, 4,
|
||||
5, 6, 7, 8,
|
||||
9,10,11,12,
|
||||
13,14,15,16;
|
||||
Cout() << "\nBlock in the middle\n";
|
||||
Cout() << m.block<2,2>(1,1);
|
||||
UppLog() << "\nBlock in the middle\n";
|
||||
UppLog() << m.block<2,2>(1,1);
|
||||
for (ptrdiff_t i = 1; i <= 3; ++i) {
|
||||
Cout() << "\nBlock of size " << i << "x" << i << "\n";
|
||||
Cout() << m.block(0, 0, i, i);
|
||||
UppLog() << "\nBlock of size " << i << "x" << i << "\n";
|
||||
UppLog() << m.block(0, 0, i, i);
|
||||
}
|
||||
}
|
||||
{
|
||||
|
|
@ -291,102 +289,100 @@ CONSOLE_APP_MAIN
|
|||
m << 1,2,
|
||||
3,4;
|
||||
Array44d a = Array44d::Constant(0.6);
|
||||
Cout() << "\nHere is the array a:\n" << a;
|
||||
UppLog() << "\nHere is the array a:\n" << a;
|
||||
a.block<2,2>(1,1) = m;
|
||||
Cout() << "\nHere is now a with m copied into its central 2x2 block:\n" << a;
|
||||
UppLog() << "\nHere is now a with m copied into its central 2x2 block:\n" << a;
|
||||
a.block(0,0,2,3) = a.block(2,1,2,3);
|
||||
Cout() << "\nHere is now a with bottom-right 2x3 block copied into top-left 2x2 block:\n" << a;
|
||||
UppLog() << "\nHere is now a with bottom-right 2x3 block copied into top-left 2x2 block:\n" << a;
|
||||
}
|
||||
Cout() << "\n\nColumns and rows";
|
||||
UppLog() << "\n\nColumns and rows";
|
||||
{
|
||||
Eigen::MatrixXf m(3,3);
|
||||
m << 1,2,3,
|
||||
4,5,6,
|
||||
7,8,9;
|
||||
Cout() << "\nHere is the matrix m:\n" << m;
|
||||
Cout() << "\n2nd Row: " << m.row(1);
|
||||
UppLog() << "\nHere is the matrix m:\n" << m;
|
||||
UppLog() << "\n2nd Row: " << m.row(1);
|
||||
m.col(2) += 3 * m.col(0);
|
||||
Cout() << "\nAfter adding 3 times the first column into the third column, the matrix m is:\n";
|
||||
Cout() << m;
|
||||
UppLog() << "\nAfter adding 3 times the first column into the third column, the matrix m is:\n";
|
||||
UppLog() << m;
|
||||
}
|
||||
Cout() << "\n\nCorner-related operations";
|
||||
UppLog() << "\n\nCorner-related operations";
|
||||
{
|
||||
Eigen::Matrix4f m;
|
||||
m << 1, 2, 3, 4,
|
||||
5, 6, 7, 8,
|
||||
9, 10,11,12,
|
||||
13,14,15,16;
|
||||
Cout() << "\nm.leftCols(2) =\n" << m.leftCols(2);
|
||||
Cout() << "\nm.bottomRows<2>() =\n" << m.bottomRows<2>();
|
||||
UppLog() << "\nm.leftCols(2) =\n" << m.leftCols(2);
|
||||
UppLog() << "\nm.bottomRows<2>() =\n" << m.bottomRows<2>();
|
||||
m.topLeftCorner(1,3) = m.bottomRightCorner(3,1).transpose();
|
||||
Cout() << "\nAfter assignment, m = \n" << m;
|
||||
UppLog() << "\nAfter assignment, m = \n" << m;
|
||||
}
|
||||
Cout() << "\n\nBlock operations for vectors";
|
||||
UppLog() << "\n\nBlock operations for vectors";
|
||||
{
|
||||
Eigen::ArrayXf v(6);
|
||||
v << 1, 2, 3, 4, 5, 6;
|
||||
Cout() << "\nv.head(3) =\n" << v.head(3);
|
||||
Cout() << "\nv.tail<3>() = \n" << v.tail<3>();
|
||||
UppLog() << "\nv.head(3) =\n" << v.head(3);
|
||||
UppLog() << "\nv.tail<3>() = \n" << v.tail<3>();
|
||||
v.segment(1,4) *= 2;
|
||||
Cout() << "\nafter 'v.segment(1,4) *= 2', v =\n" << v;
|
||||
UppLog() << "\nafter 'v.segment(1,4) *= 2', v =\n" << v;
|
||||
}
|
||||
Cout() << "\nPress enter to continue\n";
|
||||
ReadStdIn();
|
||||
|
||||
// https://eigen.tuxfamily.org/dox/group__TutorialAdvancedInitialization.html
|
||||
Cout() << "\n\nTutorial page 5 - Advanced initialization";
|
||||
UppLog() << "\n\nTutorial page 5 - Advanced initialization";
|
||||
|
||||
Cout() << "\n\nThe comma initializer";
|
||||
UppLog() << "\n\nThe comma initializer";
|
||||
{
|
||||
RowVectorXd vec1(3);
|
||||
vec1 << 1, 2, 3;
|
||||
Cout() << "\nvec1 = " << vec1;
|
||||
UppLog() << "\nvec1 = " << vec1;
|
||||
|
||||
RowVectorXd vec2(4);
|
||||
vec2 << 1, 4, 9, 16;
|
||||
Cout() << "\nvec2 = " << vec2;
|
||||
UppLog() << "\nvec2 = " << vec2;
|
||||
|
||||
RowVectorXd joined(7);
|
||||
joined << vec1, vec2;
|
||||
Cout() << "\njoined = " << joined;
|
||||
UppLog() << "\njoined = " << joined;
|
||||
|
||||
MatrixXf matA(2, 2);
|
||||
matA << 1, 2, 3, 4;
|
||||
MatrixXf matB(4, 4);
|
||||
matB << matA, matA/10, matA/10, matA;
|
||||
Cout() << matB;
|
||||
UppLog() << matB;
|
||||
|
||||
Matrix3f m;
|
||||
m.row(0) << 1, 2, 3;
|
||||
m.block(1,0,2,2) << 4, 5, 7, 8;
|
||||
m.col(2).tail(2) << 6, 9;
|
||||
Cout() << m;
|
||||
UppLog() << m;
|
||||
}
|
||||
Cout() << "\n\nSpecial matrices and arrays";
|
||||
UppLog() << "\n\nSpecial matrices and arrays";
|
||||
{
|
||||
Cout() << "\nA fixed-size array:\n";
|
||||
UppLog() << "\nA fixed-size array:\n";
|
||||
Array33f a1 = Array33f::Zero();
|
||||
Cout() << a1 << "\n\n";
|
||||
UppLog() << a1 << "\n\n";
|
||||
|
||||
Cout() << "\nA one-dimensional dynamic-size array:\n";
|
||||
UppLog() << "\nA one-dimensional dynamic-size array:\n";
|
||||
ArrayXf a2 = ArrayXf::Zero(3);
|
||||
Cout() << a2 << "\n\n";
|
||||
UppLog() << a2 << "\n\n";
|
||||
|
||||
Cout() << "\nA two-dimensional dynamic-size array:\n";
|
||||
UppLog() << "\nA two-dimensional dynamic-size array:\n";
|
||||
ArrayXXf a3 = ArrayXXf::Zero(3, 4);
|
||||
Cout() << a3 << "\n";
|
||||
UppLog() << a3 << "\n";
|
||||
|
||||
Cout() << "\nA two-dimensional dynamic-size array set to 1.23:\n";
|
||||
UppLog() << "\nA two-dimensional dynamic-size array set to 1.23:\n";
|
||||
MatrixXd a4 = MatrixXd::Constant(3, 4, 1.23);
|
||||
Cout() << a4 << "\n";
|
||||
UppLog() << a4 << "\n";
|
||||
|
||||
ArrayXXd table(10, 4);
|
||||
table.col(0) = ArrayXd::LinSpaced(10, 0, 90);
|
||||
table.col(1) = M_PI / 180 * table.col(0);
|
||||
table.col(2) = table.col(1).sin();
|
||||
table.col(3) = table.col(1).cos();
|
||||
Cout() << "\n Degrees Radians Sine Cosine\n";
|
||||
Cout() << table;
|
||||
UppLog() << "\n Degrees Radians Sine Cosine\n";
|
||||
UppLog() << table;
|
||||
|
||||
const ptrdiff_t size = 6;
|
||||
MatrixXd mat1(size, size);
|
||||
|
|
@ -394,42 +390,40 @@ CONSOLE_APP_MAIN
|
|||
mat1.topRightCorner(size/2, size/2) = MatrixXd::Identity(size/2, size/2);
|
||||
mat1.bottomLeftCorner(size/2, size/2) = MatrixXd::Identity(size/2, size/2);
|
||||
mat1.bottomRightCorner(size/2, size/2) = MatrixXd::Zero(size/2, size/2);
|
||||
Cout() << "\n" << mat1;
|
||||
UppLog() << "\n" << mat1;
|
||||
|
||||
MatrixXd mat2(size, size);
|
||||
mat2.topLeftCorner(size/2, size/2).setZero();
|
||||
mat2.topRightCorner(size/2, size/2).setIdentity();
|
||||
mat2.bottomLeftCorner(size/2, size/2).setIdentity();
|
||||
mat2.bottomRightCorner(size/2, size/2).setZero();
|
||||
Cout() << "\n" << mat2;
|
||||
UppLog() << "\n" << mat2;
|
||||
|
||||
MatrixXd mat3(size, size);
|
||||
mat3 << MatrixXd::Zero(size/2, size/2), MatrixXd::Identity(size/2, size/2),
|
||||
MatrixXd::Identity(size/2, size/2), MatrixXd::Zero(size/2, size/2);
|
||||
Cout() << "\n" << mat3;
|
||||
UppLog() << "\n" << mat3;
|
||||
}
|
||||
Cout() << "\n\nUsage as temporary objects";
|
||||
UppLog() << "\n\nUsage as temporary objects";
|
||||
{
|
||||
MatrixXd m = MatrixXd::Random(3,3);
|
||||
m = (m + MatrixXd::Constant(3,3,1.2)) * 50;
|
||||
Cout() << "\nm =\n" << m;
|
||||
UppLog() << "\nm =\n" << m;
|
||||
VectorXd v(3);
|
||||
v << 1, 2, 3;
|
||||
Cout() << "\nm * v =\n" << m * v;
|
||||
UppLog() << "\nm * v =\n" << m * v;
|
||||
}
|
||||
{
|
||||
MatrixXf mat = MatrixXf::Random(2, 3);
|
||||
Cout() << mat;
|
||||
UppLog() << mat;
|
||||
mat = (MatrixXf(2,2) << 0, 1, 1, 0).finished() * mat;
|
||||
Cout() << mat;
|
||||
UppLog() << mat;
|
||||
}
|
||||
Cout() << "\nPress enter to continue\n";
|
||||
ReadStdIn();
|
||||
|
||||
// https://eigen.tuxfamily.org/dox/group__TutorialLinearAlgebra.html
|
||||
Cout() << "\n\nTutorial page 6 - Linear algebra and decompositions";
|
||||
UppLog() << "\n\nTutorial page 6 - Linear algebra and decompositions";
|
||||
|
||||
Cout() << "\n\nBasic linear solving Ax = b";
|
||||
UppLog() << "\n\nBasic linear solving Ax = b";
|
||||
{
|
||||
Matrix3f A;
|
||||
Vector3f b;
|
||||
|
|
@ -437,87 +431,87 @@ CONSOLE_APP_MAIN
|
|||
4, 5, 6,
|
||||
7, 8,10;
|
||||
b << 3, 3, 4;
|
||||
Cout() << "\nHere is the matrix A:\n" << A;
|
||||
Cout() << "\nHere is the vector b:\n" << b;
|
||||
UppLog() << "\nHere is the matrix A:\n" << A;
|
||||
UppLog() << "\nHere is the vector b:\n" << b;
|
||||
Vector3f x = A.colPivHouseholderQr().solve(b);
|
||||
Cout() << "\nThe solution is:\n" << x;
|
||||
UppLog() << "\nThe solution is:\n" << x;
|
||||
}
|
||||
{
|
||||
Matrix2f A, b;
|
||||
A << 2, -1, -1, 3;
|
||||
b << 1, 2, 3, 1;
|
||||
Cout() << "\nHere is the matrix A:\n" << A;
|
||||
Cout() << "\nHere is the right hand side b:\n" << b;
|
||||
UppLog() << "\nHere is the matrix A:\n" << A;
|
||||
UppLog() << "\nHere is the right hand side b:\n" << b;
|
||||
Matrix2f x = A.ldlt().solve(b);
|
||||
Cout() << "\nThe solution is:\n" << x;
|
||||
UppLog() << "\nThe solution is:\n" << x;
|
||||
}
|
||||
Cout() << "\n\nChecking if a solution really exists";
|
||||
UppLog() << "\n\nChecking if a solution really exists";
|
||||
{
|
||||
MatrixXd A = MatrixXd::Random(100,100);
|
||||
MatrixXd b = MatrixXd::Random(100,50);
|
||||
MatrixXd x = A.fullPivLu().solve(b);
|
||||
double relative_error = (A*x - b).norm() / b.norm(); // norm() is L2 norm
|
||||
Cout() << "\nThe relative error is:\n" << relative_error;
|
||||
UppLog() << "\nThe relative error is:\n" << relative_error;
|
||||
}
|
||||
Cout() << "\n\nComputing eigenvalues and eigenvectors";
|
||||
UppLog() << "\n\nComputing eigenvalues and eigenvectors";
|
||||
{
|
||||
Matrix2f A;
|
||||
A << 1, 2, 2, 3;
|
||||
Cout() << "\nHere is the matrix A:\n" << A;
|
||||
UppLog() << "\nHere is the matrix A:\n" << A;
|
||||
SelfAdjointEigenSolver<Matrix2f> eigensolver(A);
|
||||
Cout() << "\nThe eigenvalues of A are:\n" << eigensolver.eigenvalues();
|
||||
Cout() << "\nHere's a matrix whose columns are eigenvectors of A "
|
||||
UppLog() << "\nThe eigenvalues of A are:\n" << eigensolver.eigenvalues();
|
||||
UppLog() << "\nHere's a matrix whose columns are eigenvectors of A "
|
||||
<< "corresponding to these eigenvalues:\n"
|
||||
<< eigensolver.eigenvectors();
|
||||
}
|
||||
Cout() << "\n\nComputing inverse and determinant";
|
||||
UppLog() << "\n\nComputing inverse and determinant";
|
||||
{
|
||||
Matrix3f A;
|
||||
A << 1, 2, 1,
|
||||
2, 1, 0,
|
||||
-1, 1, 2;
|
||||
Cout() << "\nHere is the matrix A:\n" << A;
|
||||
Cout() << "\nThe determinant of A is " << A.determinant();
|
||||
Cout() << "\nThe inverse of A is:\n" << A.inverse();
|
||||
UppLog() << "\nHere is the matrix A:\n" << A;
|
||||
UppLog() << "\nThe determinant of A is " << A.determinant();
|
||||
UppLog() << "\nThe inverse of A is:\n" << A.inverse();
|
||||
}
|
||||
Cout() << "\n\nLeast squares solving";
|
||||
UppLog() << "\n\nLeast squares solving";
|
||||
{
|
||||
MatrixXf A = MatrixXf::Random(5, 2);
|
||||
Cout() << "\nHere is the matrix A:\n" << A;
|
||||
UppLog() << "\nHere is the matrix A:\n" << A;
|
||||
VectorXf b = VectorXf::Random(5);
|
||||
Cout() << "\nHere is the right hand side b:\n" << b;
|
||||
Cout() << "\nThe least-squares solution is:\n"
|
||||
UppLog() << "\nHere is the right hand side b:\n" << b;
|
||||
UppLog() << "\nThe least-squares solution is:\n"
|
||||
<< A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b);
|
||||
}
|
||||
Cout() << "\n\nSeparating the computation from the construction";
|
||||
UppLog() << "\n\nSeparating the computation from the construction";
|
||||
{
|
||||
Matrix2f A, b;
|
||||
LLT<Matrix2f> llt;
|
||||
A << 2, -1, -1, 3;
|
||||
b << 1, 2, 3, 1;
|
||||
Cout() << "\nHere is the matrix A:\n" << A;
|
||||
Cout() << "\nHere is the right hand side b:\n" << b;
|
||||
Cout() << "\nComputing LLT decomposition...";
|
||||
UppLog() << "\nHere is the matrix A:\n" << A;
|
||||
UppLog() << "\nHere is the right hand side b:\n" << b;
|
||||
UppLog() << "\nComputing LLT decomposition...";
|
||||
llt.compute(A);
|
||||
Cout() << "\nThe solution is:\n" << llt.solve(b);
|
||||
UppLog() << "\nThe solution is:\n" << llt.solve(b);
|
||||
A(1,1)++;
|
||||
Cout() << "\nThe matrix A is now:\n" << A;
|
||||
Cout() << "\nComputing LLT decomposition...";
|
||||
UppLog() << "\nThe matrix A is now:\n" << A;
|
||||
UppLog() << "\nComputing LLT decomposition...";
|
||||
llt.compute(A);
|
||||
Cout() << "\nThe solution is now:\n" << llt.solve(b);
|
||||
UppLog() << "\nThe solution is now:\n" << llt.solve(b);
|
||||
}
|
||||
Cout() << "\n\nRank-revealing decompositions";
|
||||
UppLog() << "\n\nRank-revealing decompositions";
|
||||
{
|
||||
Matrix3f A;
|
||||
A << 1, 2, 5,
|
||||
2, 1, 4,
|
||||
3, 0, 3;
|
||||
Cout() << "\nHere is the matrix A:\n" << A;
|
||||
UppLog() << "\nHere is the matrix A:\n" << A;
|
||||
FullPivLU<Matrix3f> lu_decomp(A);
|
||||
Cout() << "\nThe rank of A is " << lu_decomp.rank();
|
||||
Cout() << "\nHere is a matrix whose columns form a basis of the null-space of A:\n"
|
||||
UppLog() << "\nThe rank of A is " << lu_decomp.rank();
|
||||
UppLog() << "\nHere is a matrix whose columns form a basis of the null-space of A:\n"
|
||||
<< lu_decomp.kernel();
|
||||
Cout() << "\nHere is a matrix whose columns form a basis of the column-space of A:\n"
|
||||
UppLog() << "\nHere is a matrix whose columns form a basis of the column-space of A:\n"
|
||||
<< lu_decomp.image(A); // yes, have to pass the original A
|
||||
}
|
||||
{
|
||||
|
|
@ -525,29 +519,27 @@ CONSOLE_APP_MAIN
|
|||
A << 2, 1,
|
||||
2, 0.9999999999;
|
||||
FullPivLU<Matrix2d> lu(A);
|
||||
Cout() << "\nBy default, the rank of A is found to be " << lu.rank();
|
||||
UppLog() << "\nBy default, the rank of A is found to be " << lu.rank();
|
||||
lu.setThreshold(1e-5);
|
||||
Cout() << "\nWith threshold 1e-5, the rank of A is found to be " << lu.rank();
|
||||
UppLog() << "\nWith threshold 1e-5, the rank of A is found to be " << lu.rank();
|
||||
}
|
||||
Cout() << "\nPress enter to continue\n";
|
||||
ReadStdIn();
|
||||
|
||||
// https://eigen.tuxfamily.org/dox/group__TutorialReductionsVisitorsBroadcasting.html
|
||||
Cout() << "\n\nTutorial page 7 - Reductions, visitors and broadcasting";
|
||||
UppLog() << "\n\nTutorial page 7 - Reductions, visitors and broadcasting";
|
||||
|
||||
Cout() << "\n\nReductions";
|
||||
UppLog() << "\n\nReductions";
|
||||
{
|
||||
Eigen::Matrix2d mat;
|
||||
mat << 1, 2,
|
||||
3, 4;
|
||||
Cout() << "\nHere is mat.sum(): " << mat.sum();
|
||||
Cout() << "\nHere is mat.prod(): " << mat.prod();
|
||||
Cout() << "\nHere is mat.mean(): " << mat.mean();
|
||||
Cout() << "\nHere is mat.minCoeff(): " << mat.minCoeff();
|
||||
Cout() << "\nHere is mat.maxCoeff(): " << mat.maxCoeff();
|
||||
Cout() << "\nHere is mat.trace(): " << mat.trace();
|
||||
UppLog() << "\nHere is mat.sum(): " << mat.sum();
|
||||
UppLog() << "\nHere is mat.prod(): " << mat.prod();
|
||||
UppLog() << "\nHere is mat.mean(): " << mat.mean();
|
||||
UppLog() << "\nHere is mat.minCoeff(): " << mat.minCoeff();
|
||||
UppLog() << "\nHere is mat.maxCoeff(): " << mat.maxCoeff();
|
||||
UppLog() << "\nHere is mat.trace(): " << mat.trace();
|
||||
}
|
||||
Cout() << "\n\nNorm computations";
|
||||
UppLog() << "\n\nNorm computations";
|
||||
{
|
||||
VectorXf v(2);
|
||||
MatrixXf m(2,2), n(2,2);
|
||||
|
|
@ -558,33 +550,33 @@ CONSOLE_APP_MAIN
|
|||
m << 1,-2,
|
||||
-3, 4;
|
||||
|
||||
Cout() << "\nv.squaredNorm() = " << v.squaredNorm();
|
||||
Cout() << "\nv.norm() = " << v.norm();
|
||||
Cout() << "\nv.lpNorm<1>() = " << v.lpNorm<1>();
|
||||
Cout() << "\nv.lpNorm<Infinity>() = " << v.lpNorm<Infinity>();
|
||||
UppLog() << "\nv.squaredNorm() = " << v.squaredNorm();
|
||||
UppLog() << "\nv.norm() = " << v.norm();
|
||||
UppLog() << "\nv.lpNorm<1>() = " << v.lpNorm<1>();
|
||||
UppLog() << "\nv.lpNorm<Infinity>() = " << v.lpNorm<Infinity>();
|
||||
|
||||
Cout() << "\n";
|
||||
Cout() << "\nm.squaredNorm() = " << m.squaredNorm();
|
||||
Cout() << "\nm.norm() = " << m.norm();
|
||||
Cout() << "\nm.lpNorm<1>() = " << m.lpNorm<1>();
|
||||
Cout() << "\nm.lpNorm<Infinity>() = " << m.lpNorm<Infinity>();
|
||||
UppLog() << "\n";
|
||||
UppLog() << "\nm.squaredNorm() = " << m.squaredNorm();
|
||||
UppLog() << "\nm.norm() = " << m.norm();
|
||||
UppLog() << "\nm.lpNorm<1>() = " << m.lpNorm<1>();
|
||||
UppLog() << "\nm.lpNorm<Infinity>() = " << m.lpNorm<Infinity>();
|
||||
}
|
||||
Cout() << "\n\nBoolean reductions";
|
||||
UppLog() << "\n\nBoolean reductions";
|
||||
{
|
||||
ArrayXXf a(2,2);
|
||||
|
||||
a << 1,2,
|
||||
3,4;
|
||||
|
||||
Cout() << "\n(a > 0).all() = " << (a > 0).all();
|
||||
Cout() << "\n(a > 0).any() = " << (a > 0).any();
|
||||
Cout() << "\n(a > 0).count() = " << (a > 0).count();
|
||||
Cout() << "\n";
|
||||
Cout() << "\n(a > 2).all() = " << (a > 2).all();
|
||||
Cout() << "\n(a > 2).any() = " << (a > 2).any();
|
||||
Cout() << "\n(a > 2).count() = " << (a > 2).count();
|
||||
UppLog() << "\n(a > 0).all() = " << (a > 0).all();
|
||||
UppLog() << "\n(a > 0).any() = " << (a > 0).any();
|
||||
UppLog() << "\n(a > 0).count() = " << (a > 0).count();
|
||||
UppLog() << "\n";
|
||||
UppLog() << "\n(a > 2).all() = " << (a > 2).all();
|
||||
UppLog() << "\n(a > 2).any() = " << (a > 2).any();
|
||||
UppLog() << "\n(a > 2).count() = " << (a > 2).count();
|
||||
}
|
||||
Cout() << "\n\nVisitors";
|
||||
UppLog() << "\n\nVisitors";
|
||||
{
|
||||
Eigen::MatrixXf m(2,2);
|
||||
|
||||
|
|
@ -599,25 +591,25 @@ CONSOLE_APP_MAIN
|
|||
MatrixXf::Index minRow, minCol;
|
||||
float min = m.minCoeff(&minRow, &minCol);
|
||||
|
||||
Cout() << "\nMax: " << max << ", at: " << maxRow << "," << maxCol;
|
||||
Cout() << "\nMin: " << min << ", at: " << minRow << "," << minCol;
|
||||
UppLog() << "\nMax: " << max << ", at: " << maxRow << "," << maxCol;
|
||||
UppLog() << "\nMin: " << min << ", at: " << minRow << "," << minCol;
|
||||
}
|
||||
Cout() << "\n\nPartial reductions";
|
||||
UppLog() << "\n\nPartial reductions";
|
||||
{
|
||||
Eigen::MatrixXf mat(2,4);
|
||||
mat << 1, 2, 6, 9,
|
||||
3, 1, 7, 2;
|
||||
|
||||
Cout() << "\nColumn's maximum: \n" << mat.colwise().maxCoeff();
|
||||
UppLog() << "\nColumn's maximum: \n" << mat.colwise().maxCoeff();
|
||||
}
|
||||
{
|
||||
Eigen::MatrixXf mat(2,4);
|
||||
mat << 1, 2, 6, 9,
|
||||
3, 1, 7, 2;
|
||||
|
||||
Cout() << "\nRow's maximum: \n" << mat.rowwise().maxCoeff();
|
||||
UppLog() << "\nRow's maximum: \n" << mat.rowwise().maxCoeff();
|
||||
}
|
||||
Cout() << "\n\nCombining partial reductions with other operations";
|
||||
UppLog() << "\n\nCombining partial reductions with other operations";
|
||||
{
|
||||
MatrixXf mat(2,4);
|
||||
mat << 1, 2, 6, 9,
|
||||
|
|
@ -626,13 +618,13 @@ CONSOLE_APP_MAIN
|
|||
MatrixXf::Index maxIndex;
|
||||
float maxNorm = mat.colwise().sum().maxCoeff(&maxIndex);
|
||||
|
||||
Cout() << "\nMaximum sum at position " << maxIndex;
|
||||
UppLog() << "\nMaximum sum at position " << maxIndex;
|
||||
|
||||
Cout() << "\nThe corresponding vector is: ";
|
||||
Cout() << "\n" << mat.col( maxIndex );
|
||||
Cout() << "\nAnd its sum is is: " << maxNorm;
|
||||
UppLog() << "\nThe corresponding vector is: ";
|
||||
UppLog() << "\n" << mat.col( maxIndex );
|
||||
UppLog() << "\nAnd its sum is is: " << maxNorm;
|
||||
}
|
||||
Cout() << "\n\nBroadcasting";
|
||||
UppLog() << "\n\nBroadcasting";
|
||||
{
|
||||
Eigen::MatrixXf mat(2,4);
|
||||
Eigen::VectorXf v(2);
|
||||
|
|
@ -646,8 +638,8 @@ CONSOLE_APP_MAIN
|
|||
//add v to each column of m
|
||||
mat.colwise() += v;
|
||||
|
||||
Cout() << "\nBroadcasting result: ";
|
||||
Cout() << "\n" << mat;
|
||||
UppLog() << "\nBroadcasting result: ";
|
||||
UppLog() << "\n" << mat;
|
||||
}
|
||||
{
|
||||
Eigen::MatrixXf mat(2,4);
|
||||
|
|
@ -661,10 +653,10 @@ CONSOLE_APP_MAIN
|
|||
//add v to each row of m
|
||||
mat.rowwise() += v.transpose();
|
||||
|
||||
Cout() << "\nBroadcasting result: ";
|
||||
Cout() << "\n" << mat;
|
||||
UppLog() << "\nBroadcasting result: ";
|
||||
UppLog() << "\n" << mat;
|
||||
}
|
||||
Cout() << "\n\nCombining broadcasting with other operations";
|
||||
UppLog() << "\n\nCombining broadcasting with other operations";
|
||||
{
|
||||
Eigen::MatrixXf m(2,4);
|
||||
Eigen::VectorXf v(2);
|
||||
|
|
@ -679,48 +671,41 @@ CONSOLE_APP_MAIN
|
|||
// find nearest neighbour
|
||||
(m.colwise() - v).colwise().squaredNorm().minCoeff(&index);
|
||||
|
||||
Cout() << "\nNearest neighbour is column " << index << ":";
|
||||
Cout() << "\n" << m.col(index);
|
||||
UppLog() << "\nNearest neighbour is column " << index << ":";
|
||||
UppLog() << "\n" << m.col(index);
|
||||
}
|
||||
|
||||
Cout() << "\nPress enter to continue\n";
|
||||
ReadStdIn();
|
||||
|
||||
Cout() << "\n\nSerializing tests";
|
||||
UppLog() << "\n\nSerializing tests";
|
||||
{
|
||||
SerialTest serialTest, serialTest_j, serialTest_x, serialTest_s;
|
||||
serialTest.m << 1, 2,
|
||||
4, 8;
|
||||
serialTest.v << 1, 2, 4;
|
||||
|
||||
StoreAsJsonFile(serialTest, AppendFileName(GetDesktopFolder(), "Json.txt"));
|
||||
LoadFromJsonFile(serialTest_j, AppendFileName(GetDesktopFolder(), "Json.txt"));
|
||||
Cout() << "\nJSON demo";
|
||||
StoreAsJsonFile(serialTest, GetExeDirFile("Json.txt"));
|
||||
LoadFromJsonFile(serialTest_j, GetExeDirFile("Json.txt"));
|
||||
UppLog() << "\nJSON demo";
|
||||
serialTest_j.Print();
|
||||
|
||||
StoreAsXMLFile(serialTest, "XMLdata", AppendFileName(GetDesktopFolder(), "Xml.txt"));
|
||||
LoadFromXMLFile(serialTest_x, AppendFileName(GetDesktopFolder(), "Xml.txt"));
|
||||
Cout() << "\nXML demo";
|
||||
StoreAsXMLFile(serialTest, "XMLdata", GetExeDirFile("Xml.txt"));
|
||||
LoadFromXMLFile(serialTest_x, GetExeDirFile("Xml.txt"));
|
||||
UppLog() << "\nXML demo";
|
||||
serialTest_x.Print();
|
||||
|
||||
StoreToFile(serialTest, AppendFileName(GetDesktopFolder(), "Serial.dat"));
|
||||
LoadFromFile(serialTest_s, AppendFileName(GetDesktopFolder(), "Serial.dat"));
|
||||
Cout() << "\nSerialization demo";
|
||||
StoreToFile(serialTest, GetExeDirFile("Serial.dat"));
|
||||
LoadFromFile(serialTest_s, GetExeDirFile("Serial.dat"));
|
||||
UppLog() << "\nSerialization demo";
|
||||
serialTest_s.Print();
|
||||
}
|
||||
|
||||
Cout() << "\nPress enter to continue\n";
|
||||
ReadStdIn();
|
||||
|
||||
NonLinearTests();
|
||||
|
||||
Cout() << "\nPress enter to continue\n";
|
||||
ReadStdIn();
|
||||
|
||||
FFTTests();
|
||||
|
||||
Cout() << "\n\nPress enter to end";
|
||||
#ifdef flagDEBUG
|
||||
Cout() << "\nPress enter key to end";
|
||||
ReadStdIn();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ using namespace Eigen;
|
|||
|
||||
void FFTTests()
|
||||
{
|
||||
Cout() << "\nFFT sample\nGets the FFT of equation"
|
||||
UppLog() << "\nFFT sample\nGets the FFT of equation"
|
||||
"\n f(t) = 2*sin(2*PI*t/50 - PI/3) + 5*sin(2*PI*t/30 - PI/2) + 30*sin(2*PI*t/10 - PI/5)"
|
||||
"\nsampled with a frequency of 14 samples/second";
|
||||
|
||||
|
|
@ -64,9 +64,9 @@ void FFTTests()
|
|||
<< 2*std::abs(freqbuf[i])/numData << csvSep
|
||||
<< 2*std::abs(freqbuf2[i])/numData;
|
||||
}
|
||||
String fftFileName = AppendFileName(GetDesktopFolder(), "fft.csv");
|
||||
Cout() << "\nFFT saved in '" << fftFileName << "'";
|
||||
SaveFile(fftFileName, str);
|
||||
String fftFileName = GetExeDirFile("fft.csv");
|
||||
UppLog() << "\nFFT saved in '" << fftFileName << "'";
|
||||
VERIFY(SaveFile(fftFileName, str));
|
||||
}
|
||||
|
||||
// Saving original and filtered series
|
||||
|
|
@ -76,9 +76,9 @@ void FFTTests()
|
|||
double t = 0;
|
||||
for (int i = 0; i < numData; ++i, t = i*1/samplingFrecuency)
|
||||
str << "\n" << t << csvSep << timebuf[i] << csvSep << timebuf2[i];;
|
||||
String dataFileName = AppendFileName(GetDesktopFolder(), "data.csv");
|
||||
Cout() << "\nSource data saved in '" << dataFileName << "'";
|
||||
SaveFile(dataFileName, str);
|
||||
String dataFileName = GetExeDirFile("data.csv");
|
||||
UppLog() << "\nSource data saved in '" << dataFileName << "'";
|
||||
VERIFY(SaveFile(dataFileName, str));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -48,12 +48,12 @@ struct Thurber_functor : NonLinearOptimizationFunctor<double> {
|
|||
VectorXd Thurber_functor::_x, Thurber_functor::_y;
|
||||
|
||||
void NonLinearOptimization() {
|
||||
Cout() << "\n\nNon linear equations optimization using Levenberg Marquardt based on Minpack\n"
|
||||
UppLog() << "\n\nNon linear equations optimization using Levenberg Marquardt based on Minpack\n"
|
||||
"(Given a set of non linear equations and a set of data longer than the equations, "
|
||||
"the program finds the equation coefficients that better fit with the equations)";
|
||||
|
||||
{
|
||||
Cout() << "\n\nEckerle4 equation\nSee: http://www.itl.nist.gov/div898/strd/nls/data/eckerle4.shtml";
|
||||
UppLog() << "\n\nEckerle4 equation\nSee: http://www.itl.nist.gov/div898/strd/nls/data/eckerle4.shtml";
|
||||
|
||||
VectorXd x(3);
|
||||
|
||||
|
|
@ -65,44 +65,39 @@ void NonLinearOptimization() {
|
|||
int ret = lm.minimize(x);
|
||||
if (ret == LevenbergMarquardtSpace::ImproperInputParameters ||
|
||||
ret == LevenbergMarquardtSpace::TooManyFunctionEvaluation)
|
||||
Cout() << "\nNo convergence!: " << ret;
|
||||
UppLog() << "\nNo convergence!: " << ret;
|
||||
else {
|
||||
if (VerifyIsApprox(lm.fvec.squaredNorm(), 1.4635887487E-03))
|
||||
Cout() << "\nNorm^2 is right";
|
||||
UppLog() << "\nNorm^2 is right";
|
||||
else
|
||||
Cout() << "\nNorm^2 is NOT right";
|
||||
UppLog() << "\nNorm^2 is NOT right";
|
||||
if (VerifyIsApprox(x[0], 1.5543827178) &&
|
||||
VerifyIsApprox(x[1], 4.0888321754) &&
|
||||
VerifyIsApprox(x[2], 4.5154121844E+02))
|
||||
Cout() << "\nNon-linear function optimization is right!";
|
||||
UppLog() << "\nNon-linear function optimization is right!";
|
||||
else
|
||||
Cout() << "\nNon-linear function optimization is NOT right!";
|
||||
UppLog() << "\nNon-linear function optimization is NOT right!";
|
||||
}
|
||||
}
|
||||
{
|
||||
Cout() << "\n\nThis is a simpler way, using NonLinearOptimization()";
|
||||
UppLog() << "\n\nThis is a simpler way, using NonLinearOptimization()";
|
||||
double x[] = {400.0, 405.0, 410.0, 415.0, 420.0, 425.0, 430.0, 435.0, 436.5, 438.0, 439.5, 441.0, 442.5, 444.0, 445.5, 447.0, 448.5, 450.0, 451.5, 453.0, 454.5, 456.0, 457.5, 459.0, 460.5, 462.0, 463.5, 465.0, 470.0, 475.0, 480.0, 485.0, 490.0, 495.0, 500.0};
|
||||
double f[] = {0.0001575, 0.0001699, 0.0002350, 0.0003102, 0.0004917, 0.0008710, 0.0017418, 0.0046400, 0.0065895, 0.0097302, 0.0149002, 0.0237310, 0.0401683, 0.0712559, 0.1264458, 0.2073413, 0.2902366, 0.3445623, 0.3698049, 0.3668534, 0.3106727, 0.2078154, 0.1164354, 0.0616764, 0.0337200, 0.0194023, 0.0117831, 0.0074357, 0.0022732, 0.0008800, 0.0004579, 0.0002345, 0.0001586, 0.0001143, 0.0000710};
|
||||
int num = sizeof(x)/sizeof(double);
|
||||
VectorXd y(3);
|
||||
y << 1., 10., 500.;
|
||||
if(!NonLinearOptimization(y, num, [&](const VectorXd &y, VectorXd &residual)->int {
|
||||
VERIFY(NonLinearOptimization(y, num, [&](const VectorXd &y, VectorXd &residual)->int {
|
||||
for(int i = 0; i < num; i++)
|
||||
residual[i] = y[0]/y[1] * exp(-0.5*(x[i]-y[2])*(x[i]-y[2])/(y[1]*y[1])) - f[i];
|
||||
return 0;
|
||||
}))
|
||||
Cout() << "\nNo convergence!";
|
||||
else {
|
||||
if (VerifyIsApprox(y[0], 1.5543827178) &&
|
||||
}));
|
||||
|
||||
VERIFY(VerifyIsApprox(y[0], 1.5543827178) &&
|
||||
VerifyIsApprox(y[1], 4.0888321754) &&
|
||||
VerifyIsApprox(y[2], 4.5154121844E+02))
|
||||
Cout() << "\nNon-linear function optimization is right!";
|
||||
else
|
||||
Cout() << "\nNon-linear function optimization is NOT right!";
|
||||
}
|
||||
VerifyIsApprox(y[2], 4.5154121844E+02));
|
||||
}
|
||||
{
|
||||
Cout() << "\n\nThurber equation\nSee: http://www.itl.nist.gov/div898/strd/nls/data/thurber.shtml\n";
|
||||
UppLog() << "\n\nThurber equation\nSee: http://www.itl.nist.gov/div898/strd/nls/data/thurber.shtml\n";
|
||||
|
||||
VectorXd x(7);
|
||||
x << 1000, 1000, 400, 40, 0.7, 0.3, 0.0; // Initial values
|
||||
|
|
@ -113,25 +108,19 @@ void NonLinearOptimization() {
|
|||
lm.parameters.ftol = 1.E4*NumTraits<double>::epsilon();
|
||||
lm.parameters.xtol = 1.E4*NumTraits<double>::epsilon();
|
||||
int ret = lm.minimize(x);
|
||||
if (ret == LevenbergMarquardtSpace::ImproperInputParameters ||
|
||||
ret == LevenbergMarquardtSpace::TooManyFunctionEvaluation)
|
||||
Cout() << "\nNo convergence!: " << ret;
|
||||
else {
|
||||
if (VerifyIsApprox(lm.fvec.squaredNorm(), 5.6427082397E+03))
|
||||
Cout() << "\nNorm^2 is right";
|
||||
else
|
||||
Cout() << "\nNorm^2 is NOT right";
|
||||
if (VerifyIsApprox(x[0], 1.2881396800E+03) &&
|
||||
|
||||
VERIFY(!(ret == LevenbergMarquardtSpace::ImproperInputParameters ||
|
||||
ret == LevenbergMarquardtSpace::TooManyFunctionEvaluation));
|
||||
|
||||
VERIFY(VerifyIsApprox(lm.fvec.squaredNorm(), 5.6427082397E+03));
|
||||
|
||||
VERIFY(VerifyIsApprox(x[0], 1.2881396800E+03) &&
|
||||
VerifyIsApprox(x[1], 1.4910792535E+03) &&
|
||||
VerifyIsApprox(x[2], 5.8323836877E+02) &&
|
||||
VerifyIsApprox(x[3], 7.5416644291E+01) &&
|
||||
VerifyIsApprox(x[4], 9.6629502864E-01) &&
|
||||
VerifyIsApprox(x[5], 3.9797285797E-01) &&
|
||||
VerifyIsApprox(x[6], 4.9727297349E-02))
|
||||
Cout() << "\nNon-linear function optimization is right!";
|
||||
else
|
||||
Cout() << "\nNon-linear function optimization FAILED!";
|
||||
}
|
||||
VerifyIsApprox(x[6], 4.9727297349E-02));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -158,7 +147,7 @@ struct Hybrd_functor : NonLinearOptimizationFunctor<double>
|
|||
};
|
||||
|
||||
void NonLinearSolving() {
|
||||
Cout() << "\n\nNon linear equation solving using the Powell hybrid method (\"dogleg\") based on Minpack. "
|
||||
UppLog() << "\n\nNon linear equation solving using the Powell hybrid method (\"dogleg\") based on Minpack. "
|
||||
<< "(Finds a zero of a system of n nonlinear equations in n variables)";
|
||||
|
||||
const int n = 9;
|
||||
|
|
@ -169,17 +158,15 @@ void NonLinearSolving() {
|
|||
Hybrd_functor functor;
|
||||
HybridNonLinearSolver<Hybrd_functor> solver(functor);
|
||||
int ret = solver.solveNumericalDiff(x);
|
||||
if (ret == HybridNonLinearSolverSpace::ImproperInputParameters ||
|
||||
|
||||
VERIFY(!(ret == HybridNonLinearSolverSpace::ImproperInputParameters ||
|
||||
ret == HybridNonLinearSolverSpace::TooManyFunctionEvaluation ||
|
||||
ret == HybridNonLinearSolverSpace::NotMakingProgressJacobian ||
|
||||
ret == HybridNonLinearSolverSpace::NotMakingProgressIterations)
|
||||
Cout() << "\nNo convergence!: " << ret;
|
||||
else {
|
||||
if (VerifyIsApprox(solver.fvec.blueNorm(), 1.192636e-08))
|
||||
Cout() << "\nNorm is right";
|
||||
else
|
||||
Cout() << "\nNorm is NOT right";
|
||||
if (VerifyIsApprox(x[0], -0.5706545) &&
|
||||
ret == HybridNonLinearSolverSpace::NotMakingProgressIterations));
|
||||
|
||||
VERIFY(VerifyIsApprox(solver.fvec.blueNorm(), 1.192636e-08));
|
||||
|
||||
VERIFY(VerifyIsApprox(x[0], -0.5706545) &&
|
||||
VerifyIsApprox(x[1], -0.6816283) &&
|
||||
VerifyIsApprox(x[2], -0.7017325) &&
|
||||
VerifyIsApprox(x[3], -0.7042129) &&
|
||||
|
|
@ -187,17 +174,13 @@ void NonLinearSolving() {
|
|||
VerifyIsApprox(x[5], -0.6918656) &&
|
||||
VerifyIsApprox(x[6], -0.665792) &&
|
||||
VerifyIsApprox(x[7], -0.5960342) &&
|
||||
VerifyIsApprox(x[8], -0.4164121))
|
||||
Cout() << "\nEquation solving is right!";
|
||||
else
|
||||
Cout() << "\nEquation solving FAILED!";
|
||||
}
|
||||
VerifyIsApprox(x[8], -0.4164121));
|
||||
|
||||
Cout() << "\n\nThis is a simpler way, using SolveNonLinearEquations()";
|
||||
UppLog() << "\n\nThis is a simpler way, using SolveNonLinearEquations()";
|
||||
|
||||
x.setConstant(n, -1.); // Initial values
|
||||
|
||||
if (!SolveNonLinearEquations(x, [&](const VectorXd &x, VectorXd &residual)->int {
|
||||
VERIFY(SolveNonLinearEquations(x, [&](const VectorXd &x, VectorXd &residual)->int {
|
||||
const ptrdiff_t n = x.size();
|
||||
|
||||
ASSERT(residual.size() == n);
|
||||
|
|
@ -211,10 +194,8 @@ void NonLinearSolving() {
|
|||
residual[k] = (3. - 2.*x[k])*x[k] - temp1 - 2.*temp2 + 1.;
|
||||
}
|
||||
return 0;
|
||||
}))
|
||||
Cout() << "\nNo convergence!: ";
|
||||
else {
|
||||
if (VerifyIsApprox(x[0], -0.5706545) &&
|
||||
})); // No convergence!
|
||||
VERIFY(VerifyIsApprox(x[0], -0.5706545) &&
|
||||
VerifyIsApprox(x[1], -0.6816283) &&
|
||||
VerifyIsApprox(x[2], -0.7017325) &&
|
||||
VerifyIsApprox(x[3], -0.7042129) &&
|
||||
|
|
@ -222,11 +203,7 @@ void NonLinearSolving() {
|
|||
VerifyIsApprox(x[5], -0.6918656) &&
|
||||
VerifyIsApprox(x[6], -0.665792) &&
|
||||
VerifyIsApprox(x[7], -0.5960342) &&
|
||||
VerifyIsApprox(x[8], -0.4164121))
|
||||
Cout() << "\nEquation solving is right!";
|
||||
else
|
||||
Cout() << "\nEquation solving FAILED!";
|
||||
}
|
||||
VerifyIsApprox(x[8], -0.4164121));
|
||||
}
|
||||
|
||||
void NonLinearTests() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue