zoukankan      html  css  js  c++  java
  • 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 = " << a + b << std::endl;
        std::cout << "a - b = " << a - b << std::endl;
        std::cout << "Doing a += b;" << std::endl;
        a += b;
        std::cout << "Now a = " << a << std::endl;
        Vector3d v(1,2,3);
        Vector3d w(1,0,0);
        std::cout << "-v + w - v = " << -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 = " << a * 2.5 << std::endl;
          std::cout << "0.1 * v = " << 0.1 * v << std::endl;
          std::cout << "Doing v *= 2;" << std::endl;
          v *= 2;
          std::cout << "Now v = " << 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 " << a << endl;
      cout << "Here is the matrix a^T " << 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: " << a << endl;
    a.transposeInPlace();
    cout << "and after being transposed: " << 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: " << mat*mat << std::endl;
        std::cout << "Here is mat*u: " << mat*u << std::endl;
        std::cout << "Here is u^T*mat: " << u.transpose()*mat << std::endl;
        std::cout << "Here is u^T*v: " << u.transpose()*v << std::endl;
        std::cout << "Here is u*v^T: " << u*v.transpose() << std::endl;
        std::cout << "Let's multiply mat by itself" << std::endl;
        mat = mat*mat;
        std::cout << "Now mat is mat: " << 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
  • 相关阅读:
    #ACsaber ——简单排序、字符串加空格、数组中的行 ~20.10.22
    #堆排序 20.09.27
    #并查集 20.09.25
    #卡特兰数 #抽屉原理 #Nim游戏 ——杂记
    #扩展欧几里得算法 ——线性同余方程 ~20.9.4
    #周测 7 —— 数的划分 、逆序对 、排座椅 、棋盘
    117. 占卜DIY
    116. 飞行员兄弟
    115.给树染色
    112.雷达设备
  • 原文地址:https://www.cnblogs.com/ymxiansen/p/5259554.html
Copyright © 2011-2022 走看看