/************************************************************************* * * * Vega FEM Simulation Library Version 3.1 * * * * "matrix" library , Copyright (C) 2007 CMU, 2009 MIT * * All rights reserved. * * * * Code author: Jernej Barbic * * http://www.jernejbarbic.com/code * * Research: Jernej Barbic, Doug L. James, Jovan Popovic * * Funding: NSF, Link Foundation, Singapore-MIT GAMBIT Game Lab * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the BSD-style license that is * * included with this library in the file LICENSE.txt * * * * This library 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 file * * LICENSE.TXT for more details. * * * *************************************************************************/ #include #include "matrix.h" #include "matrixIO.h" #include "matrixExp.h" template Matrix::Matrix(const char * filename) { if (ReadMatrixFromDisk(filename, &m, &n, &data) != 0) { printf("Error loading matrix from %s.\n", filename); throw 1; } freeDataInDestructor = true; } template Matrix::Matrix(int m_, int n_, const real * data_, bool makeInternalDataCopy, bool freeDataInDestructor_): m(m_), n(n_), freeDataInDestructor(freeDataInDestructor_) { if (makeInternalDataCopy) { data = (real*) malloc (sizeof(real) * m * n); memcpy(data, data_, sizeof(real) * m * n); } else data = (real*)data_; } template Matrix::Matrix (int m_, int n_, bool freeDataInDestructor_): m(m_), n(n_), freeDataInDestructor(freeDataInDestructor_) { data = (real*) calloc (m * n, sizeof(real)); } template Matrix::Matrix (int m_, int n_, real constEntry, bool freeDataInDestructor_): m(m_), n(n_), freeDataInDestructor(freeDataInDestructor_) { data = (real*) malloc (sizeof(real) * m * n); for(int i=0; i Matrix::Matrix(int m_, real diagonal, bool freeDataInDestructor_): m(m_), n(m_), freeDataInDestructor(freeDataInDestructor_) { data = (real*) calloc (m*m, sizeof(real)); for(int i=0; i Matrix::Matrix (int m_, const Matrix & vec, bool freeDataInDestructor_): m(m_), n(m_), freeDataInDestructor(freeDataInDestructor_) { if (m != vec.Getm()) throw 101; if (vec.Getn() != 1) throw 102; data = (real*) calloc (m*m, sizeof(real)); for(int i=0; i Matrix::Matrix(int m_, const real * diagonal, bool freeDataInDestructor_): m(m_), n(m_), freeDataInDestructor(freeDataInDestructor_) { data = (real*) calloc (m*m, sizeof(real)); for(int i=0; i Matrix::Matrix(const Matrix & mtx2) { m = mtx2.Getm(); n = mtx2.Getn(); data = (real*) malloc (sizeof(real) * m * n); memcpy(data, mtx2.GetData(), sizeof(real) * m * n); freeDataInDestructor = true; } template Matrix::~Matrix() { if (freeDataInDestructor) free(data); } template const Matrix Matrix::operator+ (const Matrix & mtx2) const { if ((m != mtx2.Getm()) || (n != mtx2.Getn())) { printf("Matrix size mismatch in Matrix::operator+ . m = %d, n = %d, mtx2.m = %d, mtx2.n = %d\n", m, n, mtx2.Getm(), mtx2.Getn()); throw 1; } real * output = SumMatrices(m, n, data, mtx2.GetData(), (real*)NULL); return Matrix(m,n,output,false); } template const Matrix Matrix::operator- (const Matrix & mtx2) const { if ((m != mtx2.Getm()) || (n != mtx2.Getn())) { printf("Matrix size mismatch in Matrix::operator- . m = %d, n = %d, mtx2.m = %d, mtx2.n = %d\n", m, n, mtx2.Getm(), mtx2.Getn()); throw 2; } real * output = SubtractMatrices(m, n, data, mtx2.GetData(), (real*)NULL); return Matrix(m,n,output,false); } template const Matrix Matrix::operator* (const Matrix & mtx2) const { if (n != mtx2.Getm()) { printf("Matrix size mismatch in Matrix::operator* . m = %d, n = %d, mtx2.m = %d, mtx2.n = %d\n", m, n, mtx2.Getm(), mtx2.Getn()); throw 3; } real * output = MultiplyMatrices(m, n, mtx2.Getn(), data, mtx2.GetData(), (real*)NULL); return Matrix(m,mtx2.Getn(),output,false); } // output = trans(this) * mtx2 template const Matrix Matrix::MultiplyT(const Matrix & mtx2) const { if (m != mtx2.Getm()) { printf("Matrix size mismatch in Matrix::MultiplyT . m = %d, n = %d, mtx2.m = %d, mtx2.n = %d\n", m, n, mtx2.Getm(), mtx2.Getn()); throw 31; } real * output = MultiplyMatricesT(n, m, mtx2.Getn(), data, mtx2.GetData(), (real*)NULL); return Matrix(n,mtx2.Getn(),output,false); } template Matrix & Matrix::operator= (const Matrix & mtx2) { if ((m != mtx2.Getm()) || (n != mtx2.Getn())) { printf("Matrix size mismatch in Matrix::operator= . m = %d, n = %d, mtx2.m = %d, mtx2.n = %d\n", m, n, mtx2.Getm(), mtx2.Getn()); throw 4; } if (this == &mtx2) // self-assignment return (*this); memcpy(data, mtx2.GetData(), sizeof(real) * m * n); return (*this); } template Matrix & Matrix::operator+= (const Matrix & mtx2) { if ((m != mtx2.Getm()) || (n != mtx2.Getn())) { printf("Matrix size mismatch in Matrix::operator+= . m = %d, n = %d, mtx2.m = %d, mtx2.n = %d\n", m, n, mtx2.Getm(), mtx2.Getn()); throw 5; } // works fine even if self-assignment SumMatrices(m, n, data, mtx2.GetData(), data); return (*this); } template Matrix & Matrix::operator-= (const Matrix & mtx2) { if ((m != mtx2.Getm()) || (n != mtx2.Getn())) { printf("Matrix size mismatch in Matrix::operator-= . m = %d, n = %d, mtx2.m = %d, mtx2.n = %d\n", m, n, mtx2.Getm(), mtx2.Getn()); throw 6; } // works fine even if self-assignment SubtractMatrices(m, n, data, mtx2.GetData(), data); return (*this); } template Matrix & Matrix::operator*= (const Matrix & mtx2) { if ((n != mtx2.Getm()) || (n != mtx2.Getn())) { printf("Matrix size mismatch in Matrix::operator*= . m = %d, n = %d, mtx2.m = %d, mtx2.n = %d\n", m, n, mtx2.Getm(), mtx2.Getn()); throw 7; } // works fine even if self-assignment real * buffer = MultiplyMatrices(m, n, n, data, mtx2.GetData()); memcpy(data, buffer, sizeof(real) * m * n); free(buffer); return (*this); } template Matrix & Matrix::operator*= (const real alpha) { int mn = m * n; for(int i=0; i real Matrix::MaxAbsEntry() const { real maxAbsEntry = 0.0; int mn = m * n; for(int i=0; i maxAbsEntry) maxAbsEntry = fabs(data[i]); } return maxAbsEntry; } template Matrix & Matrix::InPlaceTranspose() { InPlaceTransposeMatrix(m, n, data); // swap m,n int swap = m; m = n; n = swap; return (*this); } template void Matrix::Print(int numDigits) const { char formatString[6]; if (numDigits < 0) strcpy(formatString,"%G "); else sprintf(formatString,"%%.%df ", numDigits); for(int i=0; i void Matrix::SymmetricEigenDecomposition(Matrix & Q, Matrix & Lambda) { if (m != n) { printf("Matrix size mismatch in Matrix::SymmetricEigenDecomposition . m = %d, n = %d\n", m, n); throw 12; } real * QData = (real*) malloc(sizeof(real) * m * m); real * LambdaData = (real*) malloc(sizeof(real) * m); SymmetricMatrixEigenDecomposition(m, data, QData, LambdaData); Q = Matrix(m,m,QData,false); Lambda = Matrix(m,1,LambdaData,false); } template void Matrix::SVD(Matrix & U, Matrix & Sigma, Matrix & VT) { real * UData = (real*) malloc(sizeof(real) * m * MIN(m,n)); real * SigmaData = (real*) malloc(sizeof(real) * MIN(m,n)); real * VTData = (real*) malloc(sizeof(real) * MIN(m,n) * n); MatrixSVD(m, n, data, UData, SigmaData, VTData); U = Matrix(m, MIN(m,n), UData, false); Sigma = Matrix(MIN(m,n), 1, SigmaData, false); VT = Matrix(MIN(m,n), n, VTData, false); } template int Matrix::EigenDecomposition(Matrix & EigenVectors, Matrix & LambdaRe, Matrix & LambdaIm) { if (m != n) { printf("Matrix size mismatch in Matrix::EigenDecomposition . m = %d, n = %d\n", m, n); throw 13; } real * EigenVectorsData = (real*) malloc(sizeof(real) * m * m); real * LambdaReData = (real*) malloc(sizeof(real) * m); real * LambdaImData = (real*) malloc(sizeof(real) * m); int code = MatrixEigenDecomposition(m, data, EigenVectorsData, LambdaReData, LambdaImData); EigenVectors = Matrix(m,m,EigenVectorsData,false); LambdaRe = Matrix(m,1,LambdaReData,false); LambdaIm = Matrix(m,1,LambdaImData,false); return code; } template int Matrix::LUSolve(const Matrix & x, const Matrix & rhs) { if ((Getm() != Getn()) || (Getm() != x.Getm()) || (x.Getm() != rhs.Getm()) || (x.Getn() != rhs.Getn()) ) { printf("Matrix size mismatch in Matrix::LUSolve .\n"); throw 12; } int exitCode = 0; try { MatrixLUSolve(Getn(), rhs.Getn(), GetData(), x.GetData(), rhs.GetData()); } catch(int exceptionCode) { exitCode = exceptionCode; } return exitCode; } #ifdef USE_EXPOKIT template void Matrix::MExpv(real t, const Matrix & v, Matrix & w) { if ((Getm() != Getn()) || (Getm() != v.Getm()) || (Getm() != w.Getm()) || (v.Getn() != 1) || (w.Getn() != 1)) { printf("Matrix size mismatch in Matrix::MExpv .\n"); throw 13; } MatrixExpv(Getm(), GetData(), t, v.GetData(), w.GetData()); } #else template void Matrix::MExpv(real t, const Matrix & v, Matrix & w) { printf("Error: MExpv is not enabled.\n"); } #endif template int Matrix::Save(const char * filename) const { if (WriteMatrixToDisk(filename, m, n, data) != 0) { printf("Error writing matrix to %s.\n", filename); return 1; } return 0; } template void Matrix::SetSubmatrix(int I, int J, const Matrix & submatrix) { int subm = submatrix.Getm(); int subn = submatrix.Getn(); real * subdata = submatrix.GetData(); if ((I < 0) || (J < 0) || (I + subm > m) || (J + subn > n)) { printf("Error: matrix index out of bounds.\n"); throw 21; } for(int j=0; j void Matrix::RemoveColumns(int columnStart, int columnEnd) { // write everything to the right of columnEnd to the left int stride = columnEnd - columnStart; for(int column=columnEnd; column void Matrix::RemoveRows(int rowStart, int rowEnd) { InPlaceTransposeMatrix(m, n, data); int mBuf = m; m = n; n = mBuf; RemoveColumns(rowStart, rowEnd); InPlaceTransposeMatrix(m, n, data); mBuf = m; m = n; n = mBuf; } template void Matrix::RemoveRowsColumns(int columnStart, int columnEnd) { RemoveColumns(columnStart, columnEnd); RemoveRows(columnStart, columnEnd); } template void Matrix::AppendColumns(const Matrix & columns) { if (columns.Getm() != m) { printf("Error: mismatch in number of rows in AppendColumns.\n"); throw 41; } data = (real*) realloc (data, sizeof(real) * (m * (n + columns.Getn()))); memcpy(&data[ELT(m, 0, n)], columns.GetData(), sizeof(real) * m * columns.Getn()); n += columns.Getn(); } template void Matrix::AppendRows(const Matrix & rows) { if (rows.Getn() != n) { printf("Error: mismatch in number of columns in AppendRows.\n"); throw 42; } InPlaceTransposeMatrix(m, n, data); int mBuf = m; m = n; n = mBuf; Matrix rowsT = Transpose(rows); AppendColumns(rowsT); InPlaceTransposeMatrix(m, n, data); mBuf = m; m = n; n = mBuf; } template void Matrix::AppendRowsColumns(const Matrix & bottomLeftBlock, const Matrix & topRightBlock, const Matrix & bottomRightBlock) { if (topRightBlock.Getm() != m) { printf("Error: mismatch in AppendRowsColumns.\n"); throw 43; } if (bottomLeftBlock.Getn() != n) { printf("Error: mismatch in AppendRowsColumns.\n"); throw 44; } if (bottomRightBlock.Getm() != bottomLeftBlock.Getm()) { printf("Error: mismatch in AppendRowsColumns.\n"); throw 45; } if (bottomRightBlock.Getn() != topRightBlock.Getn()) { printf("Error: mismatch in AppendRowsColumns.\n"); throw 46; } AppendColumns(topRightBlock); Matrix blockMatrix(bottomLeftBlock.Getm(), n); real * blockMatrixData = blockMatrix.GetData(); memcpy(blockMatrixData, bottomLeftBlock.GetData(), sizeof(real) * (bottomLeftBlock.Getm() * bottomLeftBlock.Getn())); memcpy(&blockMatrixData[ELT(bottomLeftBlock.Getm(), 0, bottomLeftBlock.Getn())], bottomRightBlock.GetData(), sizeof(real) * (bottomRightBlock.Getm() * bottomRightBlock.Getn())); AppendRows(blockMatrix); } // === float === template Matrix::Matrix(const char * filename); template Matrix::Matrix(int m, int n, const float * data, bool makeInternalDataCopy, bool freeDataInDestructor); template Matrix::Matrix(int m, int n, bool freeDataInDestructor); template Matrix::Matrix(int m, int n, float constEntry, bool freeDataInDestructor); template Matrix::Matrix(int m, float diagonal, bool freeDataInDestructor); template Matrix::Matrix(int m, const float * diagonal, bool freeDataInDestructor); template Matrix::Matrix (int m, const Matrix & vec, bool freeDataInDestructor); template Matrix::Matrix(const Matrix & mtx2); template Matrix::~Matrix(); template const Matrix Matrix::operator+ (const Matrix & mtx2) const; template const Matrix Matrix::operator- (const Matrix & mtx2) const; template const Matrix Matrix::operator* (const Matrix & mtx2) const; template const Matrix Matrix::MultiplyT(const Matrix & mtx2) const; template Matrix & Matrix::operator= (const Matrix & mtx2); template Matrix & Matrix::operator+= (const Matrix & mtx2); template Matrix & Matrix::operator-= (const Matrix & mtx2); template Matrix & Matrix::operator*= (const Matrix & mtx2); template Matrix & Matrix::operator*= (const float alpha); template float Matrix::MaxAbsEntry() const; template Matrix & Matrix::InPlaceTranspose(); template void Matrix::SymmetricEigenDecomposition(Matrix & Q, Matrix & Lambda); template void Matrix::SVD(Matrix & U, Matrix & Sigma, Matrix & VT); template int Matrix::EigenDecomposition(Matrix & EigenVectors, Matrix & LambdaRe, Matrix & LambdaIm); template int Matrix::LUSolve(const Matrix & x, const Matrix & rhs); #ifdef USE_EXPOKIT template void Matrix::MExpv(float t, const Matrix & v, Matrix & w); #endif template void Matrix::Print(int numDigits) const; template int Matrix::Save(const char * filename) const; template void Matrix::SetSubmatrix(int I, int J, const Matrix & submatrix); template void Matrix::RemoveColumns(int columnStart, int columnEnd); template void Matrix::RemoveRows(int rowStart, int rowEnd); template void Matrix::RemoveRowsColumns(int columnStart, int columnEnd); template void Matrix::AppendRows(const Matrix & rows); template void Matrix::AppendColumns(const Matrix & columns); template void Matrix::AppendRowsColumns(const Matrix & bottomLeftBlock, const Matrix & topRightBlock, const Matrix & bottomRightBlock); // === double === template Matrix::Matrix(const char * filename); template Matrix::Matrix(int m, int n, const double * data, bool makeInternalDataCopy, bool freeDataInDestructor); template Matrix::Matrix(int m, int n, bool freeDataInDestructor); template Matrix::Matrix(int m, int n, double constEntry, bool freeDataInDestructor); template Matrix::Matrix(int m, double diagonal, bool freeDataInDestructor); template Matrix::Matrix(int m, const double * diagonal, bool freeDataInDestructor); template Matrix::Matrix (int m, const Matrix & vec, bool freeDataInDestructor); template Matrix::Matrix(const Matrix & mtx2); template Matrix::~Matrix(); template const Matrix Matrix::operator+ (const Matrix & mtx2) const; template const Matrix Matrix::operator- (const Matrix & mtx2) const; template const Matrix Matrix::operator* (const Matrix & mtx2) const; template Matrix & Matrix::operator*= (const double alpha); template const Matrix Matrix::MultiplyT(const Matrix & mtx2) const; template Matrix & Matrix::operator= (const Matrix & mtx2); template Matrix & Matrix::operator+= (const Matrix & mtx2); template Matrix & Matrix::operator-= (const Matrix & mtx2); template Matrix & Matrix::operator*= (const Matrix & mtx2); template double Matrix::MaxAbsEntry() const; template Matrix & Matrix::InPlaceTranspose(); template void Matrix::SymmetricEigenDecomposition(Matrix & Q, Matrix & Lambda); template void Matrix::SVD(Matrix & U, Matrix & Sigma, Matrix & VT); template int Matrix::EigenDecomposition(Matrix & EigenVectors, Matrix & LambdaRe, Matrix & LambdaIm); template int Matrix::LUSolve(const Matrix & x, const Matrix & rhs); #ifdef USE_EXPOKIT template void Matrix::MExpv(double t, const Matrix & v, Matrix & w); #endif template void Matrix::Print(int numDigits) const; template int Matrix::Save(const char * filename) const; template void Matrix::SetSubmatrix(int I, int J, const Matrix & submatrix); template void Matrix::RemoveColumns(int columnStart, int columnEnd); template void Matrix::RemoveRows(int rowStart, int rowEnd); template void Matrix::RemoveRowsColumns(int columnStart, int columnEnd); template void Matrix::AppendRows(const Matrix & rows); template void Matrix::AppendColumns(const Matrix & columns); template void Matrix::AppendRowsColumns(const Matrix & bottomLeftBlock, const Matrix & topRightBlock, const Matrix & bottomRightBlock);