00001 #ifndef SimTK_SIMMATRIX_SMALL_DEFS_THAT_NEED_BIG_H_ 00002 #define SimTK_SIMMATRIX_SMALL_DEFS_THAT_NEED_BIG_H_ 00003 00004 /* -------------------------------------------------------------------------- * 00005 * SimTK Core: SimTK Simmatrix(tm) * 00006 * -------------------------------------------------------------------------- * 00007 * This is part of the SimTK Core biosimulation toolkit originating from * 00008 * Simbios, the NIH National Center for Physics-Based Simulation of * 00009 * Biological Structures at Stanford, funded under the NIH Roadmap for * 00010 * Medical Research, grant U54 GM072970. See https://simtk.org. * 00011 * * 00012 * Portions copyright (c) 2005-7 Stanford University and the Authors. * 00013 * Authors: Michael Sherman * 00014 * Contributors: * 00015 * * 00016 * Permission is hereby granted, free of charge, to any person obtaining a * 00017 * copy of this software and associated documentation files (the "Software"), * 00018 * to deal in the Software without restriction, including without limitation * 00019 * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 00020 * and/or sell copies of the Software, and to permit persons to whom the * 00021 * Software is furnished to do so, subject to the following conditions: * 00022 * * 00023 * The above copyright notice and this permission notice shall be included in * 00024 * all copies or substantial portions of the Software. * 00025 * * 00026 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 00027 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 00028 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 00029 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 00030 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 00031 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 00032 * USE OR OTHER DEALINGS IN THE SOFTWARE. * 00033 * -------------------------------------------------------------------------- */ 00034 00040 #include "SimTKcommon/TemplatizedLapack.h" 00041 #include <vector> 00042 00043 namespace SimTK { 00044 00045 template <int M, int N, class ELT, int CS, int RS> 00046 typename Mat<M,N,ELT,CS,RS>::TInvert 00047 Mat<M,N,ELT,CS,RS>::invert() const { 00048 // We don't care if this is negated, but conjugated won't work. 00049 assert(CNT<ELT>::IsStdNumber || CNT<typename CNT<ELT>::TNeg>::IsStdNumber); 00050 assert(M == N); 00051 typedef typename CNT<ELT>::StdNumber Raw; 00052 00053 // Handle very small matrices directly. 00054 00055 if (M == 1) { 00056 assert((*this)(0, 0) != 0.0); 00057 TInvert mat(1.0/(*this)(0, 0)); 00058 return mat; 00059 } 00060 if (M == 2) { 00061 Raw d = (*this)(0, 0)*(*this)(1, 1) - (*this)(0, 1)*(*this)(1, 0); 00062 assert(d != 0.0); 00063 Raw dinv = 1.0/d; 00064 TInvert mat(dinv*(*this)(1, 1), -dinv*(*this)(0, 1), -dinv*(*this)(1, 0), dinv*(*this)(0, 0)); 00065 return mat; 00066 } 00067 00068 TInvert mat = *this; 00069 Raw* rawData = reinterpret_cast<Raw*>(&mat); 00070 int ipiv[M]; 00071 int info; 00072 Lapack::getrf<Raw>(M,M,rawData,M,&ipiv[0],info); 00073 assert(info==0); 00074 00075 // Calculate optimal size for work 00076 Raw workSz; 00077 Lapack::getri<Raw>(M,rawData,M,&ipiv[0],&workSz,-1,info); 00078 const int wsz = (int)CNT<Raw>::real(workSz); 00079 00080 std::vector<Raw> work(wsz); 00081 Lapack::getri<Raw>(M,rawData,M,&ipiv[0],&work[0],wsz,info); 00082 assert(info==0); 00083 return mat; 00084 } 00085 00086 } //namespace SimTK 00087 00088 00089 #endif // SimTK_SIMMATRIX_SMALL_DEFS_THAT_NEED_BIG_H_