#include "vec.h" #include "smat.h" #include "mat.h" //MISC template extern const NRMat diagonalmatrix(const NRVec &x); template extern const NRVec lineof(const NRMat &x, const int i); template extern const NRVec columnof(const NRMat &x, const int i); template extern const NRVec diagonalof(const NRMat &x); //more efficient commutator for a special case of full matrices template inline const NRMat commutator ( const NRMat &x, const NRMat &y, const bool trx=0, const bool tryy=0) { NRMat r(trx?x.ncols():x.nrows(), tryy?y.nrows():y.ncols()); r.gemm((T)0,x,trx?'t':'n',y,tryy?'t':'n',(T)1); r.gemm((T)1,y,tryy?'t':'n',x,trx?'t':'n',(T)-1); return r; } //more efficient commutator for a special case of full matrices template inline const NRMat anticommutator ( const NRMat &x, const NRMat &y, const bool trx=0, const bool tryy=0) { NRMat r(trx?x.ncols():x.nrows(), tryy?y.nrows():y.ncols()); r.gemm((T)0,x,trx?'t':'n',y,tryy?'t':'n',(T)1); r.gemm((T)1,y,tryy?'t':'n',x,trx?'t':'n',(T)1); return r; } ////////////////////// // LAPACK interface // ////////////////////// #define declare_la(T) \ extern const NRVec diagofproduct(const NRMat &a, const NRMat &b,\ bool trb=0, bool conjb=0); \ extern T trace2(const NRMat &a, const NRMat &b, bool trb=0); \ extern T trace2(const NRSMat &a, const NRSMat &b, const bool diagscaled=0);\ extern void linear_solve(NRMat &a, NRMat *b, double *det=0); \ extern void linear_solve(NRSMat &a, NRMat *b, double *det=0); \ extern void diagonalize(NRMat &a, NRVec &w, const bool eivec=1,\ const bool corder=1); \ extern void diagonalize(NRSMat &a, NRVec &w, NRMat *v, const bool corder=1);\ extern void singular_decomposition(NRMat &a, NRMat *u, NRVec &s,\ NRMat *v, const bool corder=1); declare_la(double) declare_la(complex) // Separate declarations extern void gdiagonalize(NRMat &a, NRVec &wr, NRVec &wi, NRMat *vl, NRMat *vr, const bool corder=1); extern void gdiagonalize(NRMat &a, NRVec< complex > &w, NRMat< complex >*vl, NRMat< complex > *vr); extern NRMat matrixfunction(NRSMat a, double (*f) (double)); extern NRMat matrixfunction(NRMat a, complex (*f)(const complex &),const bool adjust=0); //functions on matrices inline NRMat sqrt(const NRSMat &a) { return matrixfunction(a,&sqrt); } inline NRMat log(const NRSMat &a) { return matrixfunction(a,&log); } extern NRMat log(const NRMat &a); extern const NRMat realpart(const NRMat< complex >&); extern const NRMat imagpart(const NRMat< complex >&); extern const NRMat< complex > realmatrix (const NRMat&); extern const NRMat< complex > imagmatrix (const NRMat&); //inverse by means of linear solve, preserving rhs intact template const NRMat inverse(NRMat a, T *det=0) { #ifdef DEBUG if(a.nrows()!=a.ncols()) laerror("inverse() for non-square matrix"); #endif NRMat result(a.nrows(),a.nrows()); result = (T)1.; linear_solve(a, &result, det); return result; }