C++_Eigen函数库用法笔记——Matrix and Vector Arithmetic

Posted 一鸣先森

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++_Eigen函数库用法笔记——Matrix and Vector Arithmetic相关的知识,希望对你有一定的参考价值。

  • Addition and subtraction
  • Scalar multiplication and division
  • Transposition
  • Matrix-matrix and matrix-vector multiplication
  • Trace(求迹的和)
 

  • Addition and subtraction
    • binary operator + as in a+b
    • binary operator - as in a-b
    • unary operator - as in -a
    • compound operator += as in a+=b
    • compound operator -= as in a-=b
      #include <iostream>
      #include <Eigen/Dense>
      using namespace Eigen;
      int main()
      {
      a << 1, 2,
      3, 4;
      MatrixXd b(2,2);
      b << 2, 3,
      1, 4;
      std::cout << "a + b =\n" << a + b << std::endl;
      std::cout << "a - b =\n" << a - b << std::endl;
      std::cout << "Doing a += b;" << std::endl;
      a += b;
      std::cout << "Now a =\n" << a << std::endl;
      Vector3d v(1,2,3);
      Vector3d w(1,0,0);
      std::cout << "-v + w - v =\n" << -v + w - v << std::endl;
      }
      a + b =
      3 5
      4 8
      a - b =
      -1 -1
       2  0
      Doing a += b;
      Now a =
      3 5
      4 8
      -v + w - v =
      -1
      -4
      -6
  • Scalar multiplication and division
    • binary operator * as in matrix*scalar
    • binary operator * as in scalar*matrix
    • binary operator / as in matrix/scalar
    • compound operator *= as in matrix*=scalar
    • compound operator /= as in matrix/=scalar
      #include <iostream>
      #include <Eigen/Dense>
      using namespace Eigen;
      int main()
      {
        a << 1, 2,
             3, 4;
      Vector3d v(1,2,3);
        std::cout << "a * 2.5 =\n" << a * 2.5 << std::endl;
        std::cout << "0.1 * v =\n" << 0.1 * v << std::endl;
        std::cout << "Doing v *= 2;" << std::endl;
        v *= 2;
        std::cout << "Now v =\n" << v << std::endl;
      }
      a * 2.5 =
      2.5   5
      7.5  10
      0.1 * v =
      0.1
      0.2
      0.3
      Doing v *= 2;
      Now v =
      2
      4
      6
  • Transposition
    cout << "Here is the matrix a\n" << a << endl;
    cout << "Here is the matrix a^T\n" << a.transpose() << endl;
    Here is the matrix a
     (-0.211,0.68) (-0.605,0.823)
     (0.597,0.566)  (0.536,-0.33)
    Here is the matrix a^T
     (-0.211,0.68)  (0.597,0.566)
    (-0.605,0.823)  (0.536,-0.33)
          the instruction a = a.transpose() does not replace a with its transpose
          For in-place transposition,simply use the transposeInPlace()                
MatrixXf a(2,3); a << 1, 2, 3, 4, 5, 6;
cout << "Here is the initial matrix a:\n" << a << endl;
a.transposeInPlace();
cout << "and after being transposed:\n" << a << endl;
  • Matrix-matrix and matrix-vector multiplication
    • binary operator * as in a*b
    • compound operator *= as in a*=b (this multiplies on the right: a*=b is equivalent to a = a*b
      #include <iostream>
      #include <Eigen/Dense>
      using namespace Eigen;
      int main()
      {
      mat << 1, 2,
      3, 4;
      Vector2d u(-1,1), v(2,0);
      std::cout << "Here is mat*mat:\n" << mat*mat << std::endl;
      std::cout << "Here is mat*u:\n" << mat*u << std::endl;
      std::cout << "Here is u^T*mat:\n" << u.transpose()*mat << std::endl;
      std::cout << "Here is u^T*v:\n" << u.transpose()*v << std::endl;
      std::cout << "Here is u*v^T:\n" << u*v.transpose() << std::endl;
      std::cout << "Let‘s multiply mat by itself" << std::endl;
      mat = mat*mat;
      std::cout << "Now mat is mat:\n" << mat << std::endl;
      }
      Here is mat*mat:
       7 10
      15 22
      Here is mat*u:
      1
      1
      Here is u^T*mat:
      2 2
      Here is u^T*v:
      -2
      Here is u*v^T:
      -2 -0
       2  0
      Let‘s multiply mat by itself
      Now mat is mat:
       7 10
      15 22
  • Trace(求迹的和)
    #include <iostream>
    #include <Eigen/Dense>
    using namespace std;
    int main()
    {
      mat << 1, 2,
             3, 4;
    cout << "Here is mat.trace(): " << mat.trace() << endl;
    }
    Here is mat.trace():     5

以上是关于C++_Eigen函数库用法笔记——Matrix and Vector Arithmetic的主要内容,如果未能解决你的问题,请参考以下文章

Eigen库笔记整理

Eigen库的使用笔记

Eigen向量和矩阵的用法1(C++)

eigen 笔记2

从函数返回两个浮点数组和 Eigen::Matrix3f

将动态矩阵转换为 Eigen 中的固定矩阵