行列演算(加減乗算)を行いたい


Tips

解説

MatrixもしくはComplexMatrixクラスを利用します。

サンプルプログラム

filemain.cpp

#include <iostream>
#include <octave/config.h>
#include <octave/CColVector.h>
#include <octave/CMatrix.h>

using namespace std; 

int main()
{
  cout.precision(3);
  cout.setf(ios::fixed); 

  Complex const IU(0.0, 1.0);

  ComplexMatrix A(2, 3), B(3,2);
  ComplexColumnVector v(3);

  A(0,0) = 1.0; A(0,1) = 2.0; A(0,2) = 3.0;
  A(1,0) = 4.0; A(1,1) = 5.0; A(1,2) = 6.0;

  B(0,0) = 1.0*IU; B(0,1) = 4.0*IU;
  B(1,0) = 2.0*IU; B(1,1) = 5.0*IU;
  B(2,0) = 3.0*IU; B(2,1) = 6.0*IU; 

  v(0) = 7.0;
  v(1) = 8.0;
  v(2) = 9.0; 

  cout << "A = " << endl << A << endl;
  cout << "A.rows() = " << A.rows() 
       << ", A.cols() = " << A.cols() << endl << endl;
  cout << "B = " << endl << B << endl;
  cout << "v = " << endl << v << endl;
  cout << "A*B = " << endl << A*B << endl;
  cout << "A*v = " << endl << A*v << endl;
  cout << "2*A+B^(t) = " << endl << 2.0*A + B.transpose() << endl;
}

出力

A = 
 (1.000,0.000) (2.000,0.000) (3.000,0.000)
 (4.000,0.000) (5.000,0.000) (6.000,0.000)

A.rows() = 2, A.cols() = 3

B = 
 (0.000,1.000) (0.000,4.000)
 (0.000,2.000) (0.000,5.000)
 (0.000,3.000) (0.000,6.000)

v = 
(7.000,0.000)
(8.000,0.000)
(9.000,0.000)

A*B = 
 (0.000,14.000) (0.000,32.000)
 (0.000,32.000) (0.000,77.000)

A*v = 
(50.000,0.000)
(122.000,0.000)

2*A+B^(t) = 
 (2.000,1.000) (4.000,2.000) (6.000,3.000)
 (8.000,4.000) (10.000,5.000) (12.000,6.000)

コメント