/* * 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 "aclMatrixOfElements.h" #include "aclVectorOfElements.h" #include "aclGenerators.h" #include "acl.h" #include "../aslUtilities.h" #include using namespace std; using asl::errorMessage; namespace acl { MatrixOfElements::MatrixOfElements(unsigned int nR, unsigned int nC): nRow(nR), nCol(nC), ve(nR*nC) { } unsigned int MatrixOfElements::ij2i(unsigned int i, unsigned int j) const { return i*nCol+j; } void MatrixOfElements::setElement(unsigned int i, unsigned int j, Element a) { ve.at(ij2i(i,j))=a; } void MatrixOfElements::setRow(unsigned int r, const VectorOfElements & b) { if(nCol != b.size()) errorMessage("Error: MatrixOfElements::setRow: size of b does not match number of columns"); if(nRow < r) errorMessage("Error: MatrixOfElements::setRow: r larger than number of rows"); for(unsigned int i(0); i < b.size(); ++i) setElement(r,i,b[i]); } void MatrixOfElements::setColumn(unsigned int c, const VectorOfElements & b) { if(nRow != b.size()) errorMessage("Error: MatrixOfElements::setColumn: size of b does not match number of rows"); if(nCol < c) errorMessage("Error: MatrixOfElements::setColumn: c larger than number of columns"); for(unsigned int i(0); i < b.size(); ++i) setElement(i,c,b[i]); } const Element MatrixOfElements::getElement(unsigned int i, unsigned int j) const { return ve.at(ij2i(i,j)); } const VectorOfElements MatrixOfElements::getVE(unsigned int i, unsigned int j) const { return subVE(ve, ij2i(i,j)); } const unsigned int MatrixOfElements::getNColumns() const { return nCol; } const unsigned int MatrixOfElements::getNRows() const { return nRow; } VectorOfElements & MatrixOfElements::getInternalVector() { return ve; } const VectorOfElements & MatrixOfElements::getInternalVector() const { return ve; } MatrixOfElements MatrixOfElements::operator= (const MatrixOfElements & m) { MatrixOfElements res(nRow, nCol); copy (ve = m.ve, res.ve); return res; } void copy(const MatrixOfElements & source, MatrixOfElements & destination) { if((source.getNRows() != destination.getNRows()) || (source.getNColumns() != destination.getNColumns())) errorMessage("copy: matricess have different sizes"); copy(source.getInternalVector(),destination.getInternalVector()); } MatrixOfElements operator+(const MatrixOfElements & a, const MatrixOfElements & b) { if (a.getNRows() != b.getNRows() || a.getNColumns() != b.getNColumns()) errorMessage("operator+ - the sizes of two MatrixOfElements are incompatible"); MatrixOfElements m(a.getNRows(),a.getNColumns()); copy(a.getInternalVector()+b.getInternalVector(),m.getInternalVector()); return m; } MatrixOfElements operator-(const MatrixOfElements & a, const MatrixOfElements & b) { if (a.getNRows() != b.getNRows() || a.getNColumns() != b.getNColumns()) errorMessage("operator- - the sizes of two MatrixOfElements are incompatible"); MatrixOfElements m(a.getNRows(),a.getNColumns()); copy(a.getInternalVector()-b.getInternalVector(),m.getInternalVector()); return m; } MatrixOfElements operator*(const MatrixOfElements & a, const MatrixOfElements & b) { if (a.getNColumns() != b.getNRows() && !(a.getNColumns()==1 && a.getNRows()==1) && !(b.getNColumns()==1 && b.getNRows()==1)) errorMessage("operator* - the sizes of two MatrixOfElements are incompatible"); MatrixOfElements m; if (a.getNColumns()==1 && a.getNRows()==1) { m.resize(b.getNRows(), b.getNColumns()); copy(b.getInternalVector() * a.getInternalVector(), m.getInternalVector()); } else { if (b.getNColumns()==1 && b.getNRows()==1) { m.resize(a.getNRows(), a.getNColumns()); copy(a.getInternalVector() * b.getInternalVector(), m.getInternalVector()); } else { m.resize(a.getNRows(),b.getNColumns()); for (unsigned int i(0); i < a.getNRows(); ++i) for (unsigned int j(0);j & a) { return generateME(&a[0], a.size()); } VectorOfElements getDiagonal(const MatrixOfElements & a) { if(a.getNColumns()!=a.getNRows()) errorMessage("Error: getDiagonal: the matrix is not square one"); unsigned int n(a.getNColumns()); VectorOfElements v(n); for(unsigned int i(0); i3) errorMessage("Error: det: the matrix size is larger than 3"); VectorOfElements v(1); if(m.getNColumns()==1) copy(m.getInternalVector(),v); if(m.getNColumns()==2) copy(m.getVE(0,0) * m.getVE(1,1) - m.getVE(0,1) * m.getVE(1,0), v); // copy(mad(m.getVE(0,0), m.getVE(1,1), - m.getVE(0,1) * m.getVE(1,0)), // v); if(m.getNColumns()==3) /* copy(mad(m.getVE(0,0) * m.getVE(1,1), m.getVE(2,2), mad(m.getVE(1,0) * m.getVE(2,1), m.getVE(0,2), mad(m.getVE(0,1) * m.getVE(1,2), m.getVE(2,0), -mad(m.getVE(0,2) * m.getVE(1,1), m.getVE(2,0), mad(m.getVE(1,0) * m.getVE(0,1), m.getVE(2,2), m.getVE(2,1) * m.getVE(1,2) * m.getVE(0,0)))))), v);*/ copy(m.getVE(0,0) * m.getVE(1,1) * m.getVE(2,2) + m.getVE(1,0) * m.getVE(2,1) * m.getVE(0,2) + m.getVE(0,1) * m.getVE(1,2) * m.getVE(2,0) - m.getVE(0,2) * m.getVE(1,1) * m.getVE(2,0) - m.getVE(1,0) * m.getVE(0,1) * m.getVE(2,2) - m.getVE(2,1) * m.getVE(1,2) * m.getVE(0,0), v); return v; } VectorOfElements solveSystem(const MatrixOfElements & a, const VectorOfElements & b) { if(a.getNColumns()!=a.getNRows()) errorMessage("Error: solveSystem: the matrix is not square one"); if(a.getNColumns()>3) errorMessage("Error: solveSystem: the matrix size is larger than 3"); if(a.getNColumns()!=b.size()) errorMessage("Error: solveSystem: size of b does not match the size of matrix"); VectorOfElements v(b.size()); auto d(det(a)); VectorOfElements db(b.size()); for(unsigned int i(0); i < b.size(); ++i) db[i] = (det(replaceColumn(a,b,i)))[0]; return db/d; } vector gcSolveSystem(const MatrixOfElements & a, const VectorOfElements & b, const VectorOfElements & x) { if(a.getNColumns()!=a.getNRows()) errorMessage("Error: solveSystem: the matrix is not square one"); if(a.getNColumns()>3) errorMessage("Error: solveSystem: the matrix size is larger than 3"); if(a.getNColumns()!=b.size()) errorMessage("Error: solveSystem: size of b does not match the size of matrix"); vector res(0); res.push_back((subVE(x,0) = 1./det(a))[0]); VectorOfElements db(b.size()); for(unsigned int i(1); i < b.size(); ++i) res.push_back((subVE(x,i) = subVE(x,0) * det(replaceColumn(a,b,i)))[0]); res.push_back((subVE(x,0) *= det(replaceColumn(a,b,0)))[0]); return res; } vector gcSolveSystemCG(const MatrixOfElements & a, const VectorOfElements & b, const VectorOfElements & x) { if(a.getNColumns()!=a.getNRows()) errorMessage("Error: solveSystem: the matrix is not square one"); if(a.getNColumns()!=b.size()) errorMessage("Error: solveSystem: size of b does not match the size of matrix"); if(a.getNColumns()!=x.size()) errorMessage("Error: solveSystem: size of x does not match the size of matrix"); TypeID type(getElementType(x)); auto p(generateVEPrivateVariable(b.size(),type)); auto r(generateVEPrivateVariable(b.size(),type)); auto a_p(generateVEPrivateVariable(b.size(),type)); auto alpha(generateVEPrivateVariable(1,type)); auto rr(generateVEPrivateVariable(1,type)); vector res(0); res << (x=b); res << (r=b-a*x); res << (p=r); res << (rr = r*r); for(unsigned int i(0); i 3) errorMessage("Error: generateMatrixCofactors: the matrix size is more than 3"); MatrixOfElements m(a.getNRows(), a.getNColumns()); int rfactor(-1); for(unsigned int i(0); i < a.getNRows(); ++i){ rfactor*=-1; int factor(-rfactor); for(unsigned int j(0); j < a.getNColumns(); ++j) { factor*=-1; m.setElement(i,j, (factor * matrixCofactor(a,i,j))[0] ); } } return m; } /// returns vector of elements for computing the inverse matrix for cases 2x2 and 3x3 \relates MatrixOfElements vector gcMatrixInversion(const MatrixOfElements & a, MatrixOfElements & inv) { if((a.getNRows() != inv.getNRows()) || (a.getNColumns() != inv.getNColumns())) errorMessage("Error: generateMatrixInversionCode: two matrices have different sizes"); auto mc(generateMatrixCofactors(a)); auto d(det(a)); unsigned int nr(a.getNRows()); unsigned int nc(a.getNColumns()); auto lastEl(inv.getVE(nr-1, nc-1)); auto op1(lastEl = 1./det(a)); auto op2(subVE(inv.getInternalVector(), 0, nr*nc-2) = subVE(mc.getInternalVector(), 0, nr*nc-2) * lastEl); auto op3(lastEl*=mc.getVE(nr-1, nc-1)); return cat(op1,op2,op3); } } // namespace acl