Simbody  3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Matrix_.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMMATRIX_MATRIX_H_
2 #define SimTK_SIMMATRIX_MATRIX_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2005-13 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
30 namespace SimTK {
31 
32 //==============================================================================
33 // MATRIX
34 //==============================================================================
50 //------------------------------------------------------------------------------
51 template <class ELT> class Matrix_ : public MatrixBase<ELT> {
52  typedef typename CNT<ELT>::Scalar S;
53  typedef typename CNT<ELT>::Number Number;
54  typedef typename CNT<ELT>::StdNumber StdNumber;
55 
56  typedef typename CNT<ELT>::TNeg ENeg;
57  typedef typename CNT<ELT>::THerm EHerm;
58 
59  typedef MatrixBase<ELT> Base;
60  typedef MatrixBase<ENeg> BaseNeg;
61  typedef MatrixBase<EHerm> BaseHerm;
62 
63  typedef Matrix_<ELT> T;
64  typedef MatrixView_<ELT> TView;
65  typedef Matrix_<ENeg> TNeg;
66 
67 public:
68  Matrix_() : Base() { }
69  explicit Matrix_(const MatrixCommitment& mc) : Base(mc) {}
70 
71  // Copy constructor is deep.
72  Matrix_(const Matrix_& src) : Base(src) { }
73 
74  // Assignment is a deep copy and will also allow reallocation if this Matrix
75  // doesn't have a view.
76  Matrix_& operator=(const Matrix_& src) {
77  Base::operator=(src); return *this;
78  }
79 
80  // Force a deep copy of the view or whatever this is.
81  // Note that this is an implicit conversion.
82  Matrix_(const Base& v) : Base(v) {} // e.g., MatrixView
83 
84  // Allow implicit conversion from a source matrix that
85  // has a negated version of ELT.
86  Matrix_(const BaseNeg& v) : Base(v) {}
87 
88  // TODO: implicit conversion from conjugate. This is trickier
89  // since real elements are their own conjugate so you'll get
90  // duplicate methods defined from Matrix_(BaseHerm) and Matrix_(Base).
91 
92  Matrix_(int m, int n) : Base(MatrixCommitment(), m, n) {}
93 
94  Matrix_(int m, int n, const ELT* cppInitialValuesByRow)
95  : Base(MatrixCommitment(), m, n, cppInitialValuesByRow) {}
96  Matrix_(int m, int n, const ELT& initialValue)
97  : Base(MatrixCommitment(), m, n, initialValue) {}
98 
99  Matrix_(int m, int n, int leadingDim, const S* data) // read only
100  : Base(MatrixCommitment(), MatrixCharacter::LapackFull(m,n),
101  leadingDim, data) {}
102  Matrix_(int m, int n, int leadingDim, S* data) // writable
103  : Base(MatrixCommitment(), MatrixCharacter::LapackFull(m,n),
104  leadingDim, data) {}
105 
107  template <int M, int N, int CS, int RS>
108  explicit Matrix_(const Mat<M,N,ELT,CS,RS>& mat)
109  : Base(MatrixCommitment(), M, N)
110  { for (int i = 0; i < M; ++i)
111  for (int j = 0; j < N; ++j)
112  this->updElt(i, j) = mat(i, j); }
113 
114  Matrix_& operator=(const ELT& v) { Base::operator=(v); return *this; }
115 
116  template <class EE> Matrix_& operator=(const MatrixBase<EE>& m)
117  { Base::operator=(m); return*this; }
118  template <class EE> Matrix_& operator+=(const MatrixBase<EE>& m)
119  { Base::operator+=(m); return*this; }
120  template <class EE> Matrix_& operator-=(const MatrixBase<EE>& m)
121  { Base::operator-=(m); return*this; }
122 
123  Matrix_& operator*=(const StdNumber& t) { Base::operator*=(t); return *this; }
124  Matrix_& operator/=(const StdNumber& t) { Base::operator/=(t); return *this; }
125  Matrix_& operator+=(const ELT& r) { this->updDiag() += r; return *this; }
126  Matrix_& operator-=(const ELT& r) { this->updDiag() -= r; return *this; }
127 
128  const TNeg& negate() const {return *reinterpret_cast<const TNeg*>(this); }
129  TNeg& updNegate() {return *reinterpret_cast<TNeg*>(this); }
130 
131  const TNeg& operator-() const {return negate();}
132  TNeg& operator-() {return updNegate();}
133 
134  // Functions to be used for Scripting in MATLAB and languages that do not support operator overloading
136  std::string toString() const {
137  std::stringstream stream;
138  stream << (*this) ;
139  return stream.str();
140  }
142  const ELT& get(int i,int j) const { return this->getElt(i,j); }
144  void set(int i,int j, const ELT& value) { this->updElt(i,j)=value; }
145 
146 private:
147  // NO DATA MEMBERS ALLOWED
148 };
149 
150 } //namespace SimTK
151 
152 #endif // SimTK_SIMMATRIX_MATRIX_H_
Matrix_ & operator*=(const StdNumber &t)
Definition: Matrix_.h:123
Matrix_(const Mat< M, N, ELT, CS, RS > &mat)
Convert a Mat to a Matrix_.
Definition: Matrix_.h:108
Matrix_(int m, int n, int leadingDim, const S *data)
Definition: Matrix_.h:99
Matrix_ & operator=(const MatrixBase< EE > &m)
Definition: Matrix_.h:116
Matrix_ & operator=(const Matrix_ &src)
Definition: Matrix_.h:76
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
Matrix_ & operator-=(const ELT &r)
Definition: Matrix_.h:126
A MatrixCharacter is a set containing a value for each of the matrix characteristics except element t...
Definition: MatrixCharacteristics.h:596
std::string toString() const
toString() returns a string representation of the Matrix_.
Definition: Matrix_.h:136
Matrix_(int m, int n)
Definition: Matrix_.h:92
MatrixBase & operator=(const MatrixBase &b)
Definition: MatrixBase.h:201
TNeg & operator-()
Definition: Matrix_.h:132
Matrix_(const BaseNeg &v)
Definition: Matrix_.h:86
Matrix_ & operator+=(const MatrixBase< EE > &m)
Definition: Matrix_.h:118
MatrixBase & operator*=(const StdNumber &t)
Definition: MatrixBase.h:290
Matrix_(const Base &v)
Definition: Matrix_.h:82
This is the common base class for Simbody's Vector_ and Matrix_ classes for handling large...
Definition: BigMatrix.h:163
Matrix_(int m, int n, const ELT &initialValue)
Definition: Matrix_.h:96
Matrix_(const Matrix_ &src)
Definition: Matrix_.h:72
Matrix_ & operator-=(const MatrixBase< EE > &m)
Definition: Matrix_.h:120
ELT & updElt(int i, int j)
Definition: MatrixBase.h:657
MatrixBase & operator/=(const StdNumber &t)
Definition: MatrixBase.h:291
MatrixBase & operator+=(const MatrixBase &r)
Definition: MatrixBase.h:292
const TNeg & operator-() const
Definition: Matrix_.h:131
TNeg & updNegate()
Definition: Matrix_.h:129
Matrix_(int m, int n, int leadingDim, S *data)
Definition: Matrix_.h:102
VectorView_< ELT > updDiag()
Select main diagonal (of largest leading square if rectangular) and return it as a writable view of t...
Definition: BigMatrix.h:245
Matrix_(const MatrixCommitment &mc)
Definition: Matrix_.h:69
Matrix_(int m, int n, const ELT *cppInitialValuesByRow)
Definition: Matrix_.h:94
void set(int i, int j, const ELT &value)
Variant of indexing operator that's scripting friendly to set entry (i, j)
Definition: Matrix_.h:144
This class represents a small matrix whose size is known at compile time, containing elements of any ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:591
const ELT & getElt(int i, int j) const
Element selection for stored elements.
Definition: MatrixBase.h:656
Matrix_()
Definition: Matrix_.h:68
const TNeg & negate() const
Definition: Matrix_.h:128
Matrix_ & operator+=(const ELT &r)
Definition: Matrix_.h:125
A MatrixCommitment provides a set of acceptable matrix characteristics.
Definition: MatrixCharacteristics.h:831
MatrixBase & operator-=(const MatrixBase &r)
Definition: MatrixBase.h:293
Matrix_ & operator=(const ELT &v)
Definition: Matrix_.h:114
Matrix_ & operator/=(const StdNumber &t)
Definition: Matrix_.h:124
This is the matrix class intended to appear in user code for large, variable size matrices...
Definition: BigMatrix.h:168