1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright 2018 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/multiprecision/eigen.hpp>
11 using namespace Eigen;
22 // expected results first:
23 Matrix<Num, 2, 2> r1, r2;
31 Matrix<Num, Dynamic, Dynamic> b(2, 2);
33 std::cout << "a + b =\n"
34 << a + b << std::endl;
35 BOOST_CHECK_EQUAL(a + b, r1);
36 std::cout << "a - b =\n"
37 << a - b << std::endl;
38 BOOST_CHECK_EQUAL(a - b, r2);
39 std::cout << "Doing a += b;" << std::endl;
41 std::cout << "Now a =\n"
43 Matrix<Num, 3, 1> v(1, 2, 3);
44 Matrix<Num, 3, 1> w(1, 0, 0);
45 std::cout << "-v + w - v =\n"
46 << -v + w - v << std::endl;
47 BOOST_CHECK_EQUAL(-v + w - v, r3);
55 Matrix<Num, 3, 1> v(1, 2, 3);
56 std::cout << "a * 2.5 =\n"
57 << a * 2.5 << std::endl;
58 std::cout << "0.1 * v =\n"
59 << 0.1 * v << std::endl;
60 std::cout << "Doing v *= 2;" << std::endl;
62 std::cout << "Now v =\n"
65 std::cout << "Doing v *= Num;" << std::endl;
67 std::cout << "Now v =\n"
69 typedef typename related_number<Num>::type related_type;
71 std::cout << "Doing v *= RelatedType;" << std::endl;
73 std::cout << "Now v =\n"
75 std::cout << "RelatedType * v =\n"
76 << r * v << std::endl;
77 std::cout << "Doing v *= RelatedType^2;" << std::endl;
79 std::cout << "Now v =\n"
81 std::cout << "RelatedType^2 * v =\n"
82 << r * r * v << std::endl;
89 Matrix<Num, Dynamic, Dynamic> a = Matrix<Num, Dynamic, Dynamic>::Random(2, 2);
90 cout << "Here is the matrix a\n"
92 cout << "Here is the matrix a^T\n"
93 << a.transpose() << endl;
94 cout << "Here is the conjugate of a\n"
95 << a.conjugate() << endl;
96 cout << "Here is the matrix a^*\n"
97 << a.adjoint() << endl;
103 Matrix<Num, 2, 2> mat;
106 Matrix<Num, 2, 1> u(-1, 1), v(2, 0);
107 std::cout << "Here is mat*mat:\n"
108 << mat * mat << std::endl;
109 std::cout << "Here is mat*u:\n"
110 << mat * u << std::endl;
111 std::cout << "Here is u^T*mat:\n"
112 << u.transpose() * mat << std::endl;
113 std::cout << "Here is u^T*v:\n"
114 << u.transpose() * v << std::endl;
115 std::cout << "Here is u*v^T:\n"
116 << u * v.transpose() << std::endl;
117 std::cout << "Let's multiply mat by itself" << std::endl;
119 std::cout << "Now mat is mat:\n"
127 Matrix<Num, 3, 1> v(1, 2, 3);
128 Matrix<Num, 3, 1> w(0, 1, 2);
129 cout << "Dot product: " << v.dot(w) << endl;
130 Num dp = v.adjoint() * w; // automatic conversion of the inner product to a scalar
131 cout << "Dot product via a matrix product: " << dp << endl;
132 cout << "Cross product:\n"
133 << v.cross(w) << endl;
140 Matrix<Num, 2, 2> mat;
143 cout << "Here is mat.sum(): " << mat.sum() << endl;
144 cout << "Here is mat.prod(): " << mat.prod() << endl;
145 cout << "Here is mat.mean(): " << mat.mean() << endl;
146 cout << "Here is mat.minCoeff(): " << mat.minCoeff() << endl;
147 cout << "Here is mat.maxCoeff(): " << mat.maxCoeff() << endl;
148 cout << "Here is mat.trace(): " << mat.trace() << endl;
156 Array<Num, Dynamic, Dynamic> m(2, 2);
158 // assign some values coefficient by coefficient
162 m(1, 1) = m(0, 1) + m(1, 0);
164 // print values to standard output
168 // using the comma-initializer is also allowed
172 // print values to standard output
180 Array<Num, Dynamic, Dynamic> a(3, 3);
181 Array<Num, Dynamic, Dynamic> b(3, 3);
190 cout << "a + b = " << endl
193 // Subtracting a scalar from an array
194 cout << "a - 2 = " << endl
202 Array<Num, Dynamic, Dynamic> a(2, 2);
203 Array<Num, Dynamic, Dynamic> b(2, 2);
208 cout << "a * b = " << endl
216 Array<Num, Dynamic, 1> a = Array<Num, Dynamic, 1>::Random(5);
218 cout << "a =" << endl
220 cout << "a.abs() =" << endl
222 cout << "a.abs().sqrt() =" << endl
223 << a.abs().sqrt() << endl;
224 cout << "a.min(a.abs().sqrt()) =" << endl
225 << (a.min)(a.abs().sqrt()) << endl;
232 Matrix<Num, Dynamic, Dynamic> m(2, 2);
233 Matrix<Num, Dynamic, Dynamic> n(2, 2);
234 Matrix<Num, Dynamic, Dynamic> result(2, 2);
240 cout << "-- Matrix m*n: --" << endl
243 result = m.array() * n.array();
244 cout << "-- Array m*n: --" << endl
247 result = m.cwiseProduct(n);
248 cout << "-- With cwiseProduct: --" << endl
251 result = m.array() + 4;
252 cout << "-- Array m + 4: --" << endl
261 Matrix<Num, Dynamic, Dynamic> m(2, 2);
262 Matrix<Num, Dynamic, Dynamic> n(2, 2);
263 Matrix<Num, Dynamic, Dynamic> result(2, 2);
269 result = (m.array() + 4).matrix() * m;
270 cout << "-- Combination 1: --" << endl
273 result = (m.array() * n.array()).matrix() * m;
274 cout << "-- Combination 2: --" << endl
283 Matrix<Num, Dynamic, Dynamic> m(4, 4);
288 cout << "Block in the middle" << endl;
289 cout << m.template block<2, 2>(1, 1) << endl
291 for (int i = 1; i <= 3; ++i)
293 cout << "Block of size " << i << "x" << i << endl;
294 cout << m.block(0, 0, i, i) << endl
306 Array<Num, 4, 4> a = Array<Num, 4, 4>::Constant(0.6);
307 cout << "Here is the array a:" << endl
310 a.template block<2, 2>(1, 1) = m;
311 cout << "Here is now a with m copied into its central 2x2 block:" << endl
314 a.block(0, 0, 2, 3) = a.block(2, 1, 2, 3);
315 cout << "Here is now a with bottom-right 2x3 block copied into top-left 2x2 block:" << endl
324 Eigen::Matrix<Num, Dynamic, Dynamic> m(3, 3);
328 cout << "Here is the matrix m:" << endl
330 cout << "2nd Row: " << m.row(1) << endl;
331 m.col(2) += 3 * m.col(0);
332 cout << "After adding 3 times the first column into the third column, the matrix m is:\n";
345 cout << "m.leftCols(2) =" << endl
346 << m.leftCols(2) << endl
348 cout << "m.bottomRows<2>() =" << endl
349 << m.template bottomRows<2>() << endl
351 m.topLeftCorner(1, 3) = m.bottomRightCorner(3, 1).transpose();
352 cout << "After assignment, m = " << endl
360 Array<Num, Dynamic, 1> v(6);
361 v << 1, 2, 3, 4, 5, 6;
362 cout << "v.head(3) =" << endl
365 cout << "v.tail<3>() = " << endl
366 << v.template tail<3>() << endl
368 v.segment(1, 4) *= 2;
369 cout << "after 'v.segment(1,4) *= 2', v =" << endl
377 Matrix<Num, 2, 2> mat;
380 cout << "Here is mat.sum(): " << mat.sum() << endl;
381 cout << "Here is mat.prod(): " << mat.prod() << endl;
382 cout << "Here is mat.mean(): " << mat.mean() << endl;
383 cout << "Here is mat.minCoeff(): " << mat.minCoeff() << endl;
384 cout << "Here is mat.maxCoeff(): " << mat.maxCoeff() << endl;
385 cout << "Here is mat.trace(): " << mat.trace() << endl;
387 BOOST_CHECK_EQUAL(mat.sum(), 10);
388 BOOST_CHECK_EQUAL(mat.prod(), 24);
389 BOOST_CHECK_EQUAL(mat.mean(), Num(5) / 2);
390 BOOST_CHECK_EQUAL(mat.minCoeff(), 1);
391 BOOST_CHECK_EQUAL(mat.maxCoeff(), 4);
392 BOOST_CHECK_EQUAL(mat.trace(), 5);
399 Matrix<Num, 2, 2> mat;
402 cout << "Here is mat.sum(): " << mat.sum() << endl;
403 cout << "Here is mat.prod(): " << mat.prod() << endl;
404 cout << "Here is mat.mean(): " << mat.mean() << endl;
405 //cout << "Here is mat.minCoeff(): " << mat.minCoeff() << endl;
406 //cout << "Here is mat.maxCoeff(): " << mat.maxCoeff() << endl;
407 cout << "Here is mat.trace(): " << mat.trace() << endl;
414 Matrix<Num, Dynamic, 1> v(2);
415 Matrix<Num, Dynamic, Dynamic> m(2, 2), n(2, 2);
422 cout << "v.squaredNorm() = " << v.squaredNorm() << endl;
423 cout << "v.norm() = " << v.norm() << endl;
424 cout << "v.lpNorm<1>() = " << v.template lpNorm<1>() << endl;
425 cout << "v.lpNorm<Infinity>() = " << v.template lpNorm<Infinity>() << endl;
427 cout << "m.squaredNorm() = " << m.squaredNorm() << endl;
428 cout << "m.norm() = " << m.norm() << endl;
429 cout << "m.lpNorm<1>() = " << m.template lpNorm<1>() << endl;
430 cout << "m.lpNorm<Infinity>() = " << m.template lpNorm<Infinity>() << endl;
439 A << 1, 2, 3, 4, 5, 6, 7, 8, 10;
441 cout << "Here is the matrix A:\n"
443 cout << "Here is the vector b:\n"
445 Matrix<Num, 3, 1> x = A.colPivHouseholderQr().solve(b);
446 cout << "The solution is:\n"
454 Matrix<Num, 2, 2> A, b;
457 cout << "Here is the matrix A:\n"
459 cout << "Here is the right hand side b:\n"
461 Matrix<Num, 2, 2> x = A.ldlt().solve(b);
462 cout << "The solution is:\n"
470 Matrix<Num, Dynamic, Dynamic> A = Matrix<Num, Dynamic, Dynamic>::Random(100, 100);
471 Matrix<Num, Dynamic, Dynamic> b = Matrix<Num, Dynamic, Dynamic>::Random(100, 50);
472 Matrix<Num, Dynamic, Dynamic> x = A.fullPivLu().solve(b);
473 Matrix<Num, Dynamic, Dynamic> axmb = A * x - b;
474 double relative_error = static_cast<double>(abs(axmb.norm() / b.norm())); // norm() is L2 norm
475 cout << "norm1 = " << axmb.norm() << endl;
476 cout << "norm2 = " << b.norm() << endl;
477 cout << "The relative error is:\n"
478 << relative_error << endl;
487 cout << "Here is the matrix A:\n"
489 SelfAdjointEigenSolver<Matrix<Num, 2, 2> > eigensolver(A);
490 if (eigensolver.info() != Success)
492 std::cout << "Eigenvalue solver failed!" << endl;
496 cout << "The eigenvalues of A are:\n"
497 << eigensolver.eigenvalues() << endl;
498 cout << "Here's a matrix whose columns are eigenvectors of A \n"
499 << "corresponding to these eigenvalues:\n"
500 << eigensolver.eigenvectors() << endl;
512 cout << "Here is the matrix A:\n"
514 cout << "The determinant of A is " << A.determinant() << endl;
515 cout << "The inverse of A is:\n"
516 << A.inverse() << endl;
520 void test_integer_type()
528 void test_float_type()
530 std::cout << "Epsilon = " << Eigen::NumTraits<Num>::epsilon() << std::endl;
531 std::cout << "Dummy Prec = " << Eigen::NumTraits<Num>::dummy_precision() << std::endl;
532 std::cout << "Highest = " << Eigen::NumTraits<Num>::highest() << std::endl;
533 std::cout << "Lowest = " << Eigen::NumTraits<Num>::lowest() << std::endl;
534 std::cout << "Digits10 = " << Eigen::NumTraits<Num>::digits10() << std::endl;
564 void test_float_type_2()
566 std::cout << "Epsilon = " << Eigen::NumTraits<Num>::epsilon() << std::endl;
567 std::cout << "Dummy Prec = " << Eigen::NumTraits<Num>::dummy_precision() << std::endl;
568 std::cout << "Highest = " << Eigen::NumTraits<Num>::highest() << std::endl;
569 std::cout << "Lowest = " << Eigen::NumTraits<Num>::lowest() << std::endl;
570 std::cout << "Digits10 = " << Eigen::NumTraits<Num>::digits10() << std::endl;
583 void test_float_type_3()
585 std::cout << "Epsilon = " << Eigen::NumTraits<Num>::epsilon() << std::endl;
586 std::cout << "Dummy Prec = " << Eigen::NumTraits<Num>::dummy_precision() << std::endl;
587 std::cout << "Highest = " << Eigen::NumTraits<Num>::highest() << std::endl;
588 std::cout << "Lowest = " << Eigen::NumTraits<Num>::lowest() << std::endl;
589 std::cout << "Digits10 = " << Eigen::NumTraits<Num>::digits10() << std::endl;
597 void test_complex_type()
599 std::cout << "Epsilon = " << Eigen::NumTraits<Num>::epsilon() << std::endl;
600 std::cout << "Dummy Prec = " << Eigen::NumTraits<Num>::dummy_precision() << std::endl;
601 std::cout << "Highest = " << Eigen::NumTraits<Num>::highest() << std::endl;
602 std::cout << "Lowest = " << Eigen::NumTraits<Num>::lowest() << std::endl;
603 std::cout << "Digits10 = " << Eigen::NumTraits<Num>::digits10() << std::endl;
625 // example23<Num>(); //requires comparisons.