C++_eigen function Library usage Note--matrix and Vector arithmetic

Source: Internet
Author: User

    • Addition and subtraction
    • Scalar Multiplication and division
    • Transposition
    • Matrix-matrix and Matrix-vector multiplication
    • Trace (the and of traces)
 
  • addition and subtraction
    • Binary operator + as ina+b
    • Binary Operator-as ina-b
    • Unary Operator-as in-a
    • Compound operator + = as ina+=b
    • Compound operator-= as in a-=b #include <iostream> #include <eigen/dense>using namespace Eigen;int main () { matrix2d a;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-=\n "<< A-B << std::endl;std::cout <<" Doing A + + +; "<< std::endl;a + = B;std::cout <&L T "Now a =\n" << a << Std::endl; Vector3D V (n/a); 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 inmatrix*scalar
    • Binary operator * as inscalar*matrix
    • Binary Operator/as inmatrix/scalar
    • Compound operator *= as inmatrix*=scalar
    • Compound operator/= as in matrix/=scalar #include <iostream> #include <eigen/dense>using namespace Eigen;int main () {  Matrix2d A; A << 1, 2, 3, 4;  Vector3D V (n/a);  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 MATRIXXCF a = Matrixxcf::random (2,2) cout << "Here's The Matrix a\n" << a << endl;cout << "Here I s 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 With its a 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 ina*b
    • Compound operator *= as ina*=b(This multiplies in the right:a*=bis equivalent toa = 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 << "H Ere is mat*u:\n "<< mat*u << std::endl;std::cout <<" Here's u^t*mat:\n "<< u.transpose () *mat < < Std::endl;std::cout << "Here's 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 (tracing and)#include <iostream> #include <eigen/dense>using namespace Std;int main () {Eigen::  matrix2d Mat; Mat << 1, 2, 3, 4;cout << "Here is Mat.trace ():" << mat.trace () << Endl;} Here is Mat.trace (): 5

C++_eigen function Library usage Note--matrix and Vector arithmetic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.