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()
      {
      Matrix2d mat;
      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

转载于:https://www.cnblogs.com/ymxiansen/p/5259554.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值