#ifndef MATRIX_H #define MATRIX_H #include #include #include #include #include #include #include #include #include /** Sign function **/ template T sign(T p){return ( p/abs(p) );} ///////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////// Hijack STL containers for our wicked means ///////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////// template using Vector = Vector; template using Matrix = Vector >; template void Resize(Vector & vec, int N) { vec.resize(N); } template void Resize(Matrix & mat, int N, int M) { mat.resize(N); for(int i=0;i void Size(Vector & vec, int &N) { N= vec.size(); } template void Size(Matrix & mat, int &N,int &M) { N= mat.size(); M= mat[0].size(); } template void SizeSquare(Matrix & mat, int &N) { int M; Size(mat,N,M); assert(N==M); } template void SizeSame(Matrix & mat1,Matrix &mat2, int &N1,int &M1) { int N2,M2; Size(mat1,N1,M1); Size(mat2,N2,M2); assert(N1==N2); assert(M1==M2); } //***************************************** //* (Complex) Vector operations * //***************************************** /**Conj of a Vector **/ template Vector conj(Vector p){ Vector q(p.size()); for(int i=0;i T norm(Vector p){ T sum = 0; for(int i=0;i T norm2(Vector p){ T sum = 0; for(int i=0;i T trace(Vector p){ T sum = 0; for(int i=0;i void Fill(Vector &p, T c){ for(int i=0;i void normalize(Vector &p){ T m = norm(p); if( abs(m) > 0.0) for(int i=0;i Vector times(Vector p, U s){ for(int i=0;i Vector times(U s, Vector p){ for(int i=0;i T inner(Vector a, Vector b){ T m = 0.; for(int i=0;i Vector add(Vector a, Vector b){ Vector m(a.size()); for(int i=0;i Vector sub(Vector a, Vector b){ Vector m(a.size()); for(int i=0;i void Fill(Matrix & mat, T&val) { int N,M; Size(mat,N,M); for(int i=0;i Transpose(Matrix & mat){ int N,M; Size(mat,N,M); Matrix C; Resize(C,M,N); for(int i=0;i void Unity(Matrix &mat){ int N; SizeSquare(mat,N); for(int i=0;i void PlusUnit(Matrix & A,T c){ int dim; SizeSquare(A,dim); for(int i=0;i HermitianConj(Matrix &mat){ int dim; SizeSquare(mat,dim); Matrix C; Resize(C,dim,dim); for(int i=0;i diag(Matrix &A) { int dim; SizeSquare(A,dim); Vector d; Resize(d,dim); for(int i=0;i operator *(Vector &B,Matrix &A) { int K,M,N; Size(B,K); Size(A,M,N); assert(K==M); Vector C; Resize(C,N); for(int j=0;j inv_diag(Matrix & A){ int dim; SizeSquare(A,dim); Vector d; Resize(d,dim); for(int i=0;i operator + (Matrix &A,Matrix &B) { int N,M ; SizeSame(A,B,N,M); Matrix C; Resize(C,N,M); for(int i=0;i operator- (Matrix & A,Matrix &B){ int N,M ; SizeSame(A,B,N,M); Matrix C; Resize(C,N,M); for(int i=0;i operator* (Matrix & A,T c){ int N,M; Size(A,N,M); Matrix C; Resize(C,N,M); for(int i=0;i operator* (Matrix &A,Matrix &B){ int K,L,N,M; Size(A,K,L); Size(B,N,M); assert(L==N); Matrix C; Resize(C,K,M); for(int i=0;i operator* (Matrix &A,Vector &B){ int M,N,K; Size(A,N,M); Size(B,K); assert(K==M); Vector C; Resize(C,N); for(int i=0;i T LargestDiag(Matrix &A) { int dim ; SizeSquare(A,dim); T ld = abs(A[0][0]); for(int i=1;i abs(ld) ){ld = cf;} } return ld; } /** Look for entries on the leading subdiagonal that are smaller than 'small' **/ template int Chop_subdiag(Matrix &A,T norm, int offset, U small) { int dim; SizeSquare(A,dim); for(int l = dim - 1 - offset; l >= 1; l--) { if((U)abs(A[l][l - 1]) < (U)small) { A[l][l-1]=(U)0.0; return l; } } return 0; } /** Look for entries on the leading subdiagonal that are smaller than 'small' **/ template int Chop_symm_subdiag(Matrix & A,T norm, int offset, U small) { int dim; SizeSquare(A,dim); for(int l = dim - 1 - offset; l >= 1; l--) { if((U)abs(A[l][l - 1]) < (U)small) { A[l][l - 1] = (U)0.0; A[l - 1][l] = (U)0.0; return l; } } return 0; } /**Assign a submatrix to a larger one**/ template void AssignSubMtx(Matrix & A,int row_st, int row_end, int col_st, int col_end, Matrix &S) { for(int i = row_st; i Matrix GetSubMtx(Matrix &A,int row_st, int row_end, int col_st, int col_end) { Matrix H; Resize(row_end - row_st,col_end-col_st); for(int i = row_st; i void AssignSubMtx(Matrix & A,int row_st, int row_end, int col_st, int col_end, Matrix &S) { for(int i = row_st; i T proj(Matrix A, Vector B){ int dim; SizeSquare(A,dim); int dimB; Size(B,dimB); assert(dimB==dim); T C = 0; for(int i=0;i q Q template void times(Vector &q, Matrix &Q) { int M; SizeSquare(Q,M); int N; Size(q,N); assert(M==N); times(q,Q,N); } /// q -> q Q template void times(multi1d &q, Matrix &Q, int N) { GridBase *grid = q[0]._grid; int M; SizeSquare(Q,M); int K; Size(q,K); assert(N S(N,grid ); for(int j=0;j