/** * * common/param内にある算術ライブラリを * swig経由でスクリプトで使用するためのインターフェイスファイル * */ %module FenrirMath #define ENABLE_IOSTREAM 1 %{ #include #include #include "param/complex.h" #include "param/matrix.h" #include "algorithm/fft.h" #include "algorithm/window_function.h" %} %include std_string.i %include std_vector.i %include exception.i %exception { try { $action } catch (const std::exception& e) { SWIG_exception(SWIG_RuntimeError, e.what()); } } %define STR(x) #x %enddef %define MAKE_SETTER(name, type) %rename(STR(name ## =)) set_ ## name; type set_ ## name (const type &v) { return (self->name() = v); } %enddef %define MAKE_GETTER(name, type) %rename(STR(name)) get_ ## name; type get_ ## name () { return self->name(); } %enddef %define MAKE_TO_S(type) %extend type{ std::string to_s() const { std::stringstream s; s << (*self); return s.str(); } }; %enddef %feature("autodoc", "1"); %ignore Complex::real; %ignore Complex::imaginary; %ignore operator<<(std::ostream &, const Complex &); %include param/complex.h MAKE_TO_S(Complex); %extend Complex{ MAKE_SETTER(real, FloatT); MAKE_GETTER(real, FloatT); MAKE_SETTER(imaginary, FloatT); MAKE_GETTER(imaginary, FloatT); %alias set_imaginary "image="; %alias get_imaginary "image"; }; %define CONCRETIZE_COMPLEX(type, prefix) %template(prefix ## Complex) Complex; %enddef CONCRETIZE_COMPLEX(double, D); %ignore Array2D; %ignore Array2D_Dense; %ignore Array2D_Delegate; %ignore Array2D_CoFactor; %ignore Array2D_Transpose; %ignore Array2D_Partial; %ignore Matrix::storgae; %ignore operator<<(std::ostream &, const Matrix &); %ignore DelegatedMatrix::original; %ignore CoMatrix::CoMatrix; %ignore TransposedMatrix::TransposedMatrix; %ignore TransposedMatrix::untranspose; %ignore PartialMatrix::PartialMatrix; %ignore MatrixLU::MatrixLU; %ignore MatrixUD::MatrixUD; %{ template struct MatrixLU { Matrix LU; MatrixLU() : LU() {} MatrixLU(const Matrix &_LU) : LU(_LU) {} ~MatrixLU(){} PartialMatrix L() const { return LU.partial(LU.rows(), LU.columns() / 2, 0, 0); } PartialMatrix U() const { return LU.partial(LU.rows(), LU.columns() / 2, 0, LU.rows()); } }; template struct MatrixUD { Matrix UD; MatrixUD() : UD() {} MatrixUD(const Matrix &_UD) : UD(_UD) {} ~MatrixUD(){} PartialMatrix U() const { return UD.partial(UD.rows(), UD.columns() / 2, 0, 0); } PartialMatrix D() const { return UD.partial(UD.rows(), UD.columns() / 2, 0, UD.rows()); } }; %} %include param/matrix.h template struct MatrixLU { PartialMatrix L() const; PartialMatrix U() const; }; template struct MatrixUD { PartialMatrix U() const; PartialMatrix D() const; }; MAKE_TO_S(Matrix); %extend Matrix{ %ignore operator(); %alias rows "row_size"; %alias columns "column_size"; %alias isSquare "square?"; %alias isDiagonal "diagonal?"; %alias isSymmetric "symmetric?"; %alias isDifferentSize "different_size?"; static Matrix zero(const unsigned int &size){ return Matrix(size, size); } static Matrix identity(const unsigned int &size){ return Matrix::getI(size); } static Matrix unit(const unsigned int &size){ return Matrix::getI(size); } static Matrix I(const unsigned int &size){ return Matrix::getI(size); } static Matrix scalar(const unsigned int &size, const T &value){ return Matrix::getScalar(size, value); } %alias get_elm "[]"; T get_elm(const unsigned int i, const unsigned int j) { return self->operator()(i, j); } %alias set_elm "[]="; T set_elm(const unsigned int i, const unsigned int j, const T &v) { return self->operator()(i, j) = v; } %alias transpose "t"; %alias determinant "det"; %alias trace "tr"; PartialMatrix minor( const unsigned &from_row, const unsigned &row_size, const unsigned &from_col, const unsigned &col_size){ return self->partial(row_size, col_size, from_row, from_col); } %alias rowVector "row_vector"; %alias columnVector "column_vector"; std::vector > row_vectors(){ std::vector > res; for(unsigned int i = 0; i < self->rows(); i++){ res.push_back(Matrix(self->rowVector(i))); } return res; } std::vector > column_vectors(){ std::vector > res; for(unsigned int j = 0; j < self->columns(); j++){ res.push_back(Matrix(self->columnVector(j))); } return res; } %alias exchangeRows "exchange_rows!"; %alias exchangeColumns "exchange_columns!"; %alias inverse "inv"; MatrixLU decomposeLU(){ return MatrixLU(self->decomposeLU()); } MatrixUD decomposeUD(){ return MatrixUD(self->decomposeUD()); } } %define CONCRETIZE_MATRIX(type, prefix) %template(prefix ## Matrix) Matrix; %template(prefix ## DelegatedMatrix) DelegatedMatrix; %template(prefix ## CoMatrix) CoMatrix; %template(prefix ## TransposeMatrix) TransposedMatrix; %template(prefix ## PartialMatrix) PartialMatrix; %template(prefix ## Matrix ## Vector) std::vector >; %template(prefix ## MatrixLU) MatrixLU; %template(prefix ## MatrixUD) MatrixUD; %extend Matrix >{ %ignore hessenberg; %ignore eigen22; %ignore eigen; %ignore sqrt; } %enddef CONCRETIZE_MATRIX(double, D); CONCRETIZE_MATRIX(Complex, CD); %include algorithm/fft.h %include algorithm/window_function.h %define CONCRETIZE_FFT(type, prefix) %template(prefix ## Vector) std::vector; %template(prefix ## Complex ## Vector) std::vector >; %template(prefix ## _ft) FFT::ft; %template(prefix ## _ft2) FFT::ft; %template(prefix ## _fft_ct) FFT::fft_ct; %template(prefix ## _ift_no_divide) FFT::ift_no_divide; %template(prefix ## _ift) FFT::ift; %template(prefix ## _ifft_ct) FFT::ifft_ct; %template(prefix ## _fft) FFT::fft; %template(prefix ## _ifft) FFT::ifft; %template(prefix ## _rect) WindowFunction::rect; %template(prefix ## _gauss) WindowFunction::gauss; %template(prefix ## _hann) WindowFunction::hann; %template(prefix ## _hamming) WindowFunction::hamming; %enddef CONCRETIZE_FFT(double, D);