Matrix Basics in Lpzrobot


/* compile this with g++ -Wall -lm -L. -lselforg  -o example matrixexample.cpp or use the Makefile*/
#include <iostream>
#include <math.h>
#include <selforg/matrix.h>
using namespace matrix;
using namespace std;


int main(){
  // matrix construction
  //  all matrices are initialised with 0 elements
  Matrix m0;    // 0-matrix (size is 0x0)
  m0.set(5,1);  // change matrix to be a 5x1 matrix (column vector)
  Matrix m1(2,3); // 2x3-matrix
  double data[9] = {1., 0., 1.,  0.1, 0.4, 0.5,  -2., 0.1, 2.};
  m1.set(3,3,data); // change matrix to be 3x3 matrix with initialised elements
  Matrix m2(3,3,data); // 3x3 matrix with initialised elements
  // Accessing and printing
  cout << m2.val(0,0) << endl; // Element with index 0,0
  m2.val(2,1) = 3.4;           // assign value to element at 2,1
  cout << m2.val(2,1) << endl; // Element with index 2,1
  cout << m2;                  // show matrix
  cout << m2.row(0);           // show first row which is row-vector
  cout << m2.column(2);        // show third column which is column-vector
  // Operations
  Matrix m3 = m2 + m2 - m2 * m2;         // addition subtraction and multiplication
  cout << m3;
  Matrix m4 = (m3^T) * (m3^-1) * (m3.multMT()); // transpose and invert and fast version of matrix*matrix^T
                                               // (parentheses needed because ^ bind less than *)
  Matrix m5 = m4*0.3;                    // multiplication with scalar (just right side)
  Matrix m6 = m4 & (m3.column(0)); // multiply m4 rowwise,
                                   // each row with the appropriate element of first column of m3
  Matrix m7 = m6.map(sin);         // apply sin function to all matrix elements
  cout << m7 <<endl<< endl;
  return 0;
}

lpz matrix::Matrix to Eigen::Matrix:
MatrixXd lpz_to_eigen(matrix::Matrix lpz){
  
  int number_row = lpz.getM();   //number of rows
  int number_column = lpz.getN();
  MatrixXd matrixC1(number_row,number_column);
  for(int i=0; i<number_row; i++)
  {
    for(int j=0;j<number_column;j++){
      matrixC1(i,j) = lpz.val(i,j);
    }
  }

  return matrixC1;
}
Convert Eigen to lpz matrix type:

using eigen_matrix.data()to get the pointer of the eigen::matrix to std::array

matrix::Matrix eigen_to_lpz(MatrixXd eigen_matrix){
  matrix::Matrix m;    // 0-matrix (size is 0x0)
  m.set(eigen_matrix.rows(),eigen_matrix.cols(),eigen_matrix.data());  // change/match matrix dimension
  return m;
}

Show case of previous :

Matrix m6 = m4 & (m3.column(0)); // multiply m4 rowwise,
                                   // each row with the appropriate element of first column of m3

m4:
1 0 1
0 1 0
1 0 1
m3:
0 1 0
1 0 1
0 1 0
m3.column(0):
0
1
0
multiply m4 row-wise (by 0,1,0 ):
0 0 0
0 1 0
0 0 0

Notice:

  1. if not using namespace matrix;then (m3^T)should be (m3^matrix::T)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值