// $Id: fmatrix.h,v 1.1.1.1 2000/11/02 08:47:16 fritzi Exp $ // This file is part of the DEAL Library // DEAL is Copyright(1995) by // Roland Becker, Guido Kanschat, Franz-Theo Suttmeier #ifndef __fmatrix_h #define __fmatrix_h #ifndef __stdio_h #include #endif #ifndef __dvector_h #include #endif #ifndef __vector_h #include #endif #ifndef __ivector_h #include #endif #ifndef __errors_h #include #endif /* CLASS FMatrix Full matrix of T */ template class FMatrix { T val[M][N]; void init() { for (int i=0;i=M), IntError(IntError::Range,i)); THROW2((j<0) || (j>=N), IntError(IntError::Range,i)); return el(i,j); } ////////// // // Read-Write access to matrix elements // T& operator() (int i, int j) { THROW2((i<0) || (i>=M), IntError(IntError::Range,i)); THROW2((j<0) || (j>=N), IntError(IntError::Range,i)); return el(i,j); } // GROUP: Copying from other matrices ////////// // // Assignment operator // FMatrix& operator = (const FMatrix&); ////////// void add(double s, const FMatrix& src); ////////// void add_diag(double s, const FMatrix& src); ////////// void Tadd(double s, const FMatrix& src); // GROUP: Manipulation of rows and columns ////////// // // Add different rows of a matrix // // a(i,.) += s * a(j,.) // void add_row(int i, double s, int j); ////////// // // Add different rows of a matrix // // a(i,.) += s * a(j,.) + t * a(k,.) // void add_row(int i, double s, int j, double t, int k); ////////// // // Add different columns of a matrix // // a(.,i) += s * a(.,j) // void add_col(int i, double s, int j); ////////// // // Add different columns of a matrix // // a(.,i) += s * a(.,j) + t * a(.,k) // void add_col(int i, double s, int j, double t, int k); ////////// // // Exchange contents of rows i and j // void swap_row(int i, int j); ////////// // // Exchange contents of columns i and j // void swap_col(int i, int j); // GROUP: Multiplication Routines ////////// // // Matrix-matrix-multiplication // // dst = this * src // void mmult(FMatrix& dst, const FMatrix& src) const; ////////// // // Application of a matrix to a vector. // // [flag] adding determines if the result is copied to dst or added. // // dst (+)= this * src // void vmult(dVector& dst, const dVector& src,const int adding = 0) const; void vmult(Vector& dst, const Vector& src,const int adding = 0) const; ////////// void gsmult(dVector& dst, const dVector& src,const iVector& gl) const; ////////// // // Application of the transpose matrix to a vector. // // [flag] adding determines if the result is copied to dst or added. // // dst (+)= src * this // void Tvmult(dVector& dst,const dVector& src,const int adding=0) const; void Tvmult(Vector& dst,const Vector& src,const int adding=0) const; ////////// // // Calculation of the residual
// dst = right - this * src
// and return of its norm. // double residual(dVector& dst, const dVector& src, const dVector& right) const; double residual(Vector& dst, const Vector& src, const Vector& right) const; ////////// // // Inversion of lower triangle // void forward(dVector& dst, const dVector& src) const; void forward(Vector& dst, const Vector& src) const; // GROUP: Solvers ////////// // // Inversion of upper triangle // void backward(dVector& dst, const dVector& src) const; void backward(Vector& dst, const Vector& src) const; // GROUP: Inversion Techniques ////////// // // Replace this by its inverse matrix calculated with Gauß-Jordan algorithm. // void gauss_jordan(); ////////// // // QR - factorization of a matrix. // The orthogonal transformation Q is applied to the vector y and the // matrix. After execution of householder, the upper triangle contains // the resulting matrix R, the lower the incomplete factorization matrices. // void householder(dVector& y); void householder(Vector& y); ////////// // // Least - Squares - Approximation by QR-factorization. // // double least_squares(dVector& dst, dVector& src); double least_squares(Vector& dst, Vector& src); // GROUP: I/O ////////// // // Output of the matrix in user-defined format. // void print(FILE* fp, const char* format = 0) const; }; template void FMatrix::vmult(dVector& dst, const dVector& src,const int adding) const { THROW2 (dst.n() != M, Error(Error::Dimension,"FMatrix::vmult")); if(!adding) dst.reinit(M); for (int i=0;i void FMatrix::vmult(Vector& dst, const Vector& src,const int adding) const { THROW2 (dst.n() != M, Error(Error::Dimension,"FMatrix::vmult")); if(!adding) dst.reinit(M); for (int i=0;i void FMatrix::gsmult(dVector& dst, const dVector& src,const iVector& gl) const { THROW2 (gl.n() != dst.n(), Error(Error::Dimension,"FMatrix::gsmult")); for (int i=0;i void FMatrix::Tvmult(dVector& dst, const dVector& src, const int adding) const { THROW2 (dst.n() != M, Error(Error::Dimension,"FMatrix::vmult")); if(!adding) dst.reinit(M); for (int i=0;i void FMatrix::Tvmult(Vector& dst, const Vector& src, const int adding) const { THROW2 (dst.n() != M, Error(Error::Dimension,"FMatrix::vmult")); if(!adding) dst.reinit(M); for (int i=0;i double FMatrix::residual(dVector& dst, const dVector& src, const dVector& right) const { THROW2 (dst.n() != M, Error(Error::Dimension,"FMatrix::vmult")); double s,res = 0.; for (int i=0;i double FMatrix::residual(Vector& dst, const Vector& src, const Vector& right) const { THROW2 (dst.n() != M, Error(Error::Dimension,"FMatrix::vmult")); double s,res = 0.; for (int i=0;i void FMatrix::forward(dVector& dst, const dVector& src) const { int i,j; int nu = MIN(m(),n()); double s; for (i=0;i void FMatrix::backward(dVector& dst, const dVector& src) const { int i,j; int nu = MIN(m(),n()); double s; for (i=nu-1;i>=0;i--) { s = src(i); for (j=i+1;j void FMatrix::forward(Vector& dst, const Vector& src) const { int i,j; int nu = MIN(m(),n()); double s; for (i=0;i void FMatrix::backward(Vector& dst, const Vector& src) const { int i,j; int nu = MIN(m(),n()); double s; for (i=nu-1;i>=0;i--) { s = src(i); for (j=i+1;j FMatrix& FMatrix::operator = (const FMatrix& B) { reinit(B); int nn = n()*m(); for (int i=0;i void FMatrix::add_row(int i, double s, int j) { int k; for (k=0;k void FMatrix::add_row(int i, double s, int j, double t, int k) { int l; for (l=0;l void FMatrix::add_col(int i, double s, int j) { int k; for (k=0;k void FMatrix::add_col(int i, double s, int j, double t, int k) { int l; for (l=0;l void FMatrix::swap_row(int i, int j) { int k; double s; for (k=0;k void FMatrix::swap_col(int i, int j) { int k; double s; for (k=0;k void FMatrix::mmult(FMatrix& dst, const FMatrix& src) const { int i,j,k; double s = 1.; THROW1 (m() != src.n(), Error(Error::Dimension,"mmult")); dst.reinit(n(), src.m()); for (i=0;i void FMatrix::print(FILE* f, const char* format) const { if (!format) format = " %5.2f"; int i,j; for (i=0;i void FMatrix::add(double s,const FMatrix& src) { THROW1 (M != src.m(), Error(Error::Dimension,"add")); THROW1 (N != src.n(), Error(Error::Dimension,"add")); for (int i=0;i void FMatrix::add_diag(double s,const FMatrix& src) { THROW1 (M != src.m(), Error(Error::Dimension,"sadd")); THROW1 (N != src.n(), Error(Error::Dimension,"sadd")); for (int i=0;i void FMatrix::Tadd(double s,const FMatrix& src) { THROW1 (M != n(), Error(Error::Dimension,"Tsadd")); THROW1 (N != src.m(), Error(Error::Dimension,"Tsadd")); THROW1 (N != src.n(), Error(Error::Dimension,"Tsadd")); for (int i=0;i void FMatrix::gauss_jordan() { THROW1(n()!=m(), Error(Error::Dimension,"gauss_jordan")); iVector p(n()); int i,j,k,r; double max, hr; for (i=0;i max) { max = ABS(el(i,j)); r = i; } } THROW1(max<1.e-16,Error(Error::MatrixNotRegular, "gauss_jordan")); // Zeilentausch if (r>j) { for (k=0;k void FMatrix::householder(dVector& y) { // m > n, y.n() = m for (int j=0 ; j double FMatrix::least_squares(dVector& dst, dVector& src) { // m > n, m = src.n, n = dst.n householder(src); backward(dst, src); double sum = 0.; for (int i=n() ; i void FMatrix::householder(Vector& y) { // m > n, y.n() = m for (int j=0 ; j double FMatrix::least_squares(Vector& dst, Vector& src) { // m > n, m = src.n, n = dst.n householder(src); backward(dst, src); double sum = 0.; for (int i=n() ; i