/* * Advanced Simulation Library * * Copyright 2015 Avtech Scientific * * * This file is part of Advanced Simulation Library (ASL). * * ASL is free software: you can redistribute it and/or modify it * under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, version 3 of the License. * * ASL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with ASL. If not, see . * */ #include"aslMatrices.h" using namespace std; namespace asl { template const AMatr & AMatr::operator=(const AMatr & a) { if(nCol != a.nCol || nRow != a.nRow ) resize(a.nRow, a.nCol); for(unsigned int i(0); i < nRow; ++i) for(unsigned int j(0); j < nCol; ++j) (*this)(i,j) = a(i,j); return *this; } template const AMatr & AMatr::operator=(const AMatr & a); template const AMatr & AMatr::operator=(const AMatr & a); template void AMatr::setRow(unsigned int r, const AVec & b) { if(nCol != b.getSize()) errorMessage("Error: AMatr::setRow: size of b does not match number of columns"); if(nRow < r) errorMessage("Error: AMatr::setRow: r larger than number of rows"); for(unsigned int i(0); i < b.getSize(); ++i) (*this)(r,i) = b[i]; } template void AMatr::setRow(unsigned int r, const AVec & b); template void AMatr::setRow(unsigned int r, const AVec & b); template void AMatr::setColumn(unsigned int c, const AVec & b) { if(nRow != b.getSize()) errorMessage("Error: AMatr::setColumn: size of b does not match number of rows"); if(nCol < c) errorMessage("Error: AMatr::setColumn: c larger than number of columns"); for(unsigned int i(0); i < b.getSize(); ++i) (*this)(i,c) = b[i]; } template void AMatr::setColumn(unsigned int c, const AVec & b); template void AMatr::setColumn(unsigned int c, const AVec & b); template ostream& operator<<(ostream &f, const AMatr & a) { for (unsigned int i(0); i < a.getNRow(); ++i){ for (unsigned int j(0); j < a.getNCol() - 1; ++j) f << a(i, j) << ' '; f << a(i, a.getNCol()-1) << endl; }; return f; } template ostream& operator<<(ostream &f,const AMatr & a); template ostream& operator<<(ostream &f,const AMatr & a); template const AMatr operator* (const AMatr &a, const AMatr & b) { AMatr c(a.getNRow(), b.getNCol()); T s(0); for (unsigned int i(0); i < a.getNRow(); ++i) for (unsigned int j(0); j < b.getNCol(); ++j) { for (unsigned int k(0); k operator* (const AMatr &a, const AMatr & b); template const AMatr operator* (const AMatr &a, const AMatr & b); template const AVec operator* (const AMatr &a, const AVec & b) { AVec c(a.getNRow()); T s(0); for (unsigned int i(0); i < a.getNRow(); ++i){ for (unsigned int k(0); k operator* (const AMatr &a, const AVec & b); template const AVec operator* (const AMatr &a, const AVec & b); template const AVec operator* (const AVec &a, const AMatr & b) { AVec c(b.getNCol()); T s(0); for (unsigned int j(0); j < b.getNCol(); ++j) { for (unsigned int k(0); k operator* (const AVec &a, const AMatr & b); template const AVec operator* (const AVec &a, const AMatr & b); template const AMatr operator* (const AMatr &a, const T & b) { AMatr c(a.getNRow(), a.getNCol()); c.getInternalVec() = a.getInternalVec() * b; return c; } template const AMatr operator* (const AMatr &a, const double & b); template const AMatr operator* (const AMatr &a, const float & b); template const AMatr operator* (const T &a, const AMatr & b) { AMatr c(b.getNRow(), b.getNCol()); c.getInternalVec() = b.getInternalVec() * a; return c; } template const AMatr operator* (const double &a, const AMatr & b); template const AMatr operator* (const float &a, const AMatr & b); template AMatr elementProduct(const AVec & a, const AVec & b) { AMatr m(a.getSize(),b.getSize()); for (unsigned int i(0); i < a.getSize(); ++i) for (unsigned int j(0);j elementProduct(const AVec & a, const AVec & b); template AMatr elementProduct(const AVec & a, const AVec & b); template const T trace(const AMatr &a) { T c(0); for (unsigned int i(0); i < a.getNRow(); ++i) c += a(i, i); return c; } template const double trace(const AMatr &a); template const float trace(const AMatr &a); template const T trace(const AMatr & a, const AMatr & b) { T c(0); for (unsigned int i(0); i < a.getNRow(); ++i) for (unsigned int j(0); j < a.getNCol(); ++j) c += a(i,j) * b(j,i); return c; } template const double trace(const AMatr & a, const AMatr & b); template const float trace(const AMatr & a, const AMatr & b); template AMatr makeAMatr(const AVec & a) { return {1,a.getSize(),a}; } template AMatr makeAMatr(const AVec & a); template AMatr makeAMatr(const AVec & a); template AMatr makeAMatr(const AVec & a, const AVec & b) { if(a.getSize() != b.getSize()) errorMessage("makeMatr: vectors have different sizes" ); AMatr m(2, a.getSize()); m.setRow(0,a); m.setRow(1,b); return m; } template AMatr makeAMatr(const AVec & a, const AVec & b); template AMatr makeAMatr(const AVec & a, const AVec & b); template AMatr makeAMatr(const AVec & a, const AVec & b, const AVec & c) { if((a.getSize() != b.getSize()) || (a.getSize() != c.getSize())) errorMessage("makeMatr: vectors have different sizes" ); AMatr m(3, a.getSize()); m.setRow(0,a); m.setRow(1,b); m.setRow(2,c); return m; } template AMatr makeAMatr(const AVec & a, const AVec & b, const AVec & c); template AMatr makeAMatr(const AVec & a, const AVec & b, const AVec & c); template AMatr makeAMatr(AVec *a, unsigned int n) { for(unsigned int i(1); i m(n, a[0].getSize()); for(unsigned int i(0); i makeAMatr(AVec *a, unsigned int n); template AMatr makeAMatr(AVec *a, unsigned int n); template AMatr makeAMatrUnit(unsigned int n) { AMatr m(n,n); for(unsigned int i(0); i makeAMatrUnit(unsigned int n); template AMatr makeAMatrUnit(unsigned int n); template AVec getDiagonal(const AMatr & a) { if(a.getNCol()!=a.getNRow()) errorMessage("Error: getDiagonal: the matrix is not square one"); unsigned int n(a.getNCol()); AVec v(n); for(unsigned int i(0); i getDiagonal(const AMatr & a); template AVec getDiagonal(const AMatr & a); template AVec getOffDiagonalUp(const AMatr & a) { if(a.getNCol()!=a.getNRow()) errorMessage("Error: getOffDiagonalUp: the matrix is not square one"); unsigned int n(a.getNCol()); AVec v((n*n-n)/2); unsigned int k(0); for(unsigned int i(1); i T det(const AMatr & m) { if(m.getNCol()!=m.getNRow()) errorMessage("Error: det: the matrix is not square one"); if(m.getNCol()>3) errorMessage("Error: det: the matrix size is larger than 3"); T v(0); if(m.getNCol()==1) v=m(0,0); if(m.getNCol()==2) v=m(0,0) * m(1,1) - m(0,1) * m(1,0); if(m.getNCol()==3) v=m(0,0) * m(1,1) * m(2,2) + m(1,0) * m(2,1) * m(0,2) + m(0,1) * m(1,2) * m(2,0) - m(0,2) * m(1,1) * m(2,0) - m(1,0) * m(0,1) * m(2,2) - m(2,1) * m(1,2) * m(0,0); return v; } template double det(const AMatr & m); template float det(const AMatr & m); template AVec solveSystem(const AMatr & a, const AVec & b) { if(a.getNCol()!=a.getNRow()) errorMessage("Error: solveSystem: the matrix is not square one"); if(a.getNCol()>3) errorMessage("Error: solveSystem: the matrix size is larger than 3"); if(a.getNCol()!=b.getSize()) errorMessage("Error: solveSystem: size of b does not match the size of matrix"); AVec v(b.getSize()); T d(det(a)); AVec db(b.getSize()); for(unsigned int i(0); i < b.getSize(); ++i) db[i] = det(replaceColumn(a,b,i)); return db/d; } template AVec solveSystem(const AMatr & a, const AVec & b); template AVec solveSystem(const AMatr & a, const AVec & b); template AMatr replaceRow(const AMatr & a, const AVec & b, unsigned int r) { AMatr m(a); m.setRow(r,b); return m; } template AMatr replaceColumn(const AMatr & a, const AVec & b, unsigned int c) { AMatr m(a); m.setColumn(c,b); return m; } template T matrixCofactor(const AMatr & a, unsigned int r, unsigned int c) { AMatr m(a.getNRow()-1, a.getNCol()-1); for(unsigned int i(0), im(0); im < m.getNRow(); ++i, ++im) { i += (i == r) ? 1 : 0; for(unsigned int j(0), jm(0); jm < m.getNCol(); ++j, ++jm) { j += (j == c) ? 1 : 0; m(im, jm) = a(i,j); } } return det(m); } template AMatr generateMatrixCofactors(const AMatr & a) { if(a.getNRow() != a.getNCol()) errorMessage("Error: generateMatrixCofactors: the matrix is not rectangular one"); if(a.getNRow() > 3) errorMessage("Error: generateMatrixCofactors: the matrix size is more than 3"); AMatr m(a.getNRow(), a.getNCol()); int rfactor(-1); for(unsigned int i(0); i < a.getNRow(); ++i){ rfactor*=-1; int factor(-rfactor); for(unsigned int j(0); j < a.getNCol(); ++j) { factor*=-1; m(i,j) = (factor * matrixCofactor(a,i,j)); } } return m; } template AMatr inverseMatrix(const AMatr & a) { AMatr mc(generateMatrixCofactors(a)); T d(det(a)); return mc/d; } template AMatr inverseMatrix(const AMatr & a); template AMatr inverseMatrix(const AMatr & a); double detSymMat(double a,double b, double c) { return a*b-c*c; } void getEValEVecMatSym2x2(double a,double b, double c, double & l1, double & l2, double & v1x, double & v1y, double & v2x, double & v2y) { double tr2((a+b)*.5); double det(detSymMat(a,b,c)); l1= tr2 + sqrt(tr2*tr2-det); l2= tr2 - sqrt(tr2*tr2-det); v1x=l1-b; v1y=c; v2x=l2-b; v2y=c; } double detSymMat(double a, double b, double c, double d, double e, double f) { return a*b*c+2.*d*e*f-b*e*e-a*f*f-c*d*d; } void getEValEVecMatSym3x3(double a, double b, double c, double d, double e, double f, double & l1, double & l2, double & l3, double & v1x, double & v1y, double & v1z, double & v2x, double & v2y, double & v2z, double & v3x, double & v3y, double & v3z) { // double tr(a+b+c); // double det(detSymMat(a,b,c,d,e,f)) } }