码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-03-09 20:56:45      阅读:425      评论:0      收藏:0      [点我收藏+]

标签:

  • 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

标签:

原文地址:http://www.cnblogs.com/ymxiansen/p/5259554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!