liboctave

インストール(Ubuntu 10.10)

apt-get install octave3.2 octave3.2-headers

コンパイル

  • test.cpp の場合
g++ -c -I/usr/include/octave-3.2.4/octave test.cpp -o test.o
mkoctfile --link-stand-alone test.o -o test
  • OpenMPと併用 リンカに -lgomp を追加
    g++ -c -I/usr/include/octave-3.2.4/octave -fopenmp test.cpp -o test.o
    mkoctfile --link-stand-alone -lgomp test.o -o test
    

固有値・固有ベクトル・固有値分解に用いる関数・型

  • 複素数,0.7 + 0.3i
    Complex number = Complex (0.7, 0.3)
    
  • 複素数から実数部を取り出す
    double real( const Complex& z )
    
  • 複素数から虚数部を取り出す
    double imag( const Complex& z )
    

サンプル( C++ )

//  Eigen value
//
//  Last Updated : 2011/06/08 Wed 23:02:56                                  
//
#include <iostream>
#include <octave/config.h>
#include <octave/Matrix.h>
using namespace std;

int main(int argc, char *argv[])
{
  Matrix m( 2, 2 );
  m( 0, 0 ) = -1; m( 0, 1 ) = -3;
  m( 1, 0 ) = 0; m( 1, 1 ) = 2;
  cout << "Original Matrix" << endl << m << endl;

  EIG eig( m );
  cout << "Eigen Vectors" << endl << eig.eigenvectors() << endl;
  cout << "Eigen Values" << endl << eig.eigenvalues() << endl;
  cout << "Recomposed Matrix" << endl
       << eig.eigenvectors() * ComplexMatrix( ComplexDiagMatrix( eig.eigenvalues() ) ) * eig.eigenvectors().inverse() 
       << endl;

  return 0;
}

実行結果

Original Matrix
 -1 -3
 0 2

Eigen Vectors
 (1,0) (-0.707107,0)
 (0,0) (0.707107,0)

Eigen Values
(-1,0)
(2,0)

Recomposed Matrix
 (-1,0) (-3,0)
 (0,0) (2,0)

固有値aを実数部r、虚数部iへ取り出す場合、

ComplexColumnVector a = eig.eigenvalues();
double r = real( a( 0 ) );
double i = imag( a( 0 ) );

Makefile

OPT	= -O3
LFLAGS 	= -lm 
OCTFLAGS	= -I/usr/include/octave-3.2.4/octave
OCTLINK	= --link-stand-alone
GFLAGS 	= -Wall -g
CC 	= g++
MKOCT	= mkoctfile
PROGRAM = main
OBJS	= main.o

.c.o:
	$(CC) $(OPT) $(GFLAGS) $(OCTFLAGS) -c $<

all	: $(PROGRAM)
$(PROGRAM)	: $(OBJS)
	$(MKOCT) $(OCTLINK) $(OBJS) -o $@
clean	:
	rm -f $(OBJS) $(PROGRAM)

参考URL

最終更新:2011年06月14日 07:49