Simbody  3.5
Matrix_.h
Go to the documentation of this file.
00001 #ifndef SimTK_SIMMATRIX_MATRIX_H_
00002 #define SimTK_SIMMATRIX_MATRIX_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                       Simbody(tm): SimTKcommon                             *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK 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/home/simbody.  *
00011  *                                                                            *
00012  * Portions copyright (c) 2005-13 Stanford University and the Authors.        *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors:                                                              *
00015  *                                                                            *
00016  * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
00017  * not use this file except in compliance with the License. You may obtain a  *
00018  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
00019  *                                                                            *
00020  * Unless required by applicable law or agreed to in writing, software        *
00021  * distributed under the License is distributed on an "AS IS" BASIS,          *
00022  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
00023  * See the License for the specific language governing permissions and        *
00024  * limitations under the License.                                             *
00025  * -------------------------------------------------------------------------- */
00026 
00030 namespace SimTK {
00031 
00032 //==============================================================================
00033 //                                MATRIX
00034 //==============================================================================
00050 //------------------------------------------------------------------------------
00051 template <class ELT> class Matrix_ : public MatrixBase<ELT> {
00052     typedef typename CNT<ELT>::Scalar       S;
00053     typedef typename CNT<ELT>::Number       Number;
00054     typedef typename CNT<ELT>::StdNumber    StdNumber;
00055 
00056     typedef typename CNT<ELT>::TNeg         ENeg;
00057     typedef typename CNT<ELT>::THerm        EHerm;
00058 
00059     typedef MatrixBase<ELT>     Base;
00060     typedef MatrixBase<ENeg>    BaseNeg;
00061     typedef MatrixBase<EHerm>   BaseHerm;
00062 
00063     typedef Matrix_<ELT>        T;
00064     typedef MatrixView_<ELT>    TView;
00065     typedef Matrix_<ENeg>       TNeg;
00066 
00067 public:
00068     Matrix_() : Base() { }
00069     explicit Matrix_(const MatrixCommitment& mc) : Base(mc) {}
00070 
00071     // Copy constructor is deep.
00072     Matrix_(const Matrix_& src) : Base(src) { }
00073 
00074     // Assignment is a deep copy and will also allow reallocation if this Matrix
00075     // doesn't have a view.
00076     Matrix_& operator=(const Matrix_& src) { 
00077         Base::operator=(src); return *this;
00078     }
00079 
00080     // Force a deep copy of the view or whatever this is.
00081     // Note that this is an implicit conversion.
00082     Matrix_(const Base& v) : Base(v) {}   // e.g., MatrixView
00083 
00084     // Allow implicit conversion from a source matrix that
00085     // has a negated version of ELT.
00086     Matrix_(const BaseNeg& v) : Base(v) {}
00087 
00088     // TODO: implicit conversion from conjugate. This is trickier
00089     // since real elements are their own conjugate so you'll get
00090     // duplicate methods defined from Matrix_(BaseHerm) and Matrix_(Base).
00091 
00092     Matrix_(int m, int n) : Base(MatrixCommitment(), m, n) {}
00093 
00094     Matrix_(int m, int n, const ELT* cppInitialValuesByRow) 
00095     :   Base(MatrixCommitment(), m, n, cppInitialValuesByRow) {}
00096     Matrix_(int m, int n, const ELT& initialValue) 
00097     :   Base(MatrixCommitment(), m, n, initialValue) {}
00098     
00099     Matrix_(int m, int n, int leadingDim, const S* data) // read only
00100     :   Base(MatrixCommitment(), MatrixCharacter::LapackFull(m,n), 
00101              leadingDim, data) {}
00102     Matrix_(int m, int n, int leadingDim, S* data) // writable
00103     :   Base(MatrixCommitment(), MatrixCharacter::LapackFull(m,n), 
00104              leadingDim, data) {}
00105     
00107     template <int M, int N, int CS, int RS>
00108     explicit Matrix_(const Mat<M,N,ELT,CS,RS>& mat)
00109     :   Base(MatrixCommitment(), M, N)
00110     {   for (int i = 0; i < M; ++i)
00111             for (int j = 0; j < N; ++j)
00112                 this->updElt(i, j) = mat(i, j); }
00113 
00114     Matrix_& operator=(const ELT& v) { Base::operator=(v); return *this; }
00115 
00116     template <class EE> Matrix_& operator=(const MatrixBase<EE>& m)
00117       { Base::operator=(m); return*this; }
00118     template <class EE> Matrix_& operator+=(const MatrixBase<EE>& m)
00119       { Base::operator+=(m); return*this; }
00120     template <class EE> Matrix_& operator-=(const MatrixBase<EE>& m)
00121       { Base::operator-=(m); return*this; }
00122 
00123     Matrix_& operator*=(const StdNumber& t) { Base::operator*=(t); return *this; }
00124     Matrix_& operator/=(const StdNumber& t) { Base::operator/=(t); return *this; }
00125     Matrix_& operator+=(const ELT& r)       { this->updDiag() += r; return *this; }
00126     Matrix_& operator-=(const ELT& r)       { this->updDiag() -= r; return *this; }  
00127 
00128     const TNeg& negate()    const {return *reinterpret_cast<const TNeg*>(this); }
00129     TNeg&       updNegate()       {return *reinterpret_cast<TNeg*>(this); }
00130 
00131     const TNeg& operator-() const {return negate();}
00132     TNeg&       operator-()       {return updNegate();}
00133    
00134     // Functions to be used for Scripting in MATLAB and languages that do not support operator overloading
00136     std::string toString() const {
00137         std::stringstream stream;
00138         stream <<  (*this) ;
00139         return stream.str(); 
00140     }
00142     const ELT& get(int i,int j) const { return this->getElt(i,j); }
00144     void       set(int i,int j, const ELT& value)       { this->updElt(i,j)=value; }
00145 
00146 private:
00147     // NO DATA MEMBERS ALLOWED
00148 };
00149 
00150 } //namespace SimTK
00151 
00152 #endif // SimTK_SIMMATRIX_MATRIX_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines