Simbody  3.7
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 MatrixView_<ELT> TView;
64  typedef Matrix_<ENeg> TNeg;
65 
66 public:
67  Matrix_() : Base() { }
68  explicit Matrix_(const MatrixCommitment& mc) : Base(mc) {}
69 
70  // Copy constructor is deep.
71  Matrix_(const Matrix_& src) : Base(src) { }
72 
73  // Assignment is a deep copy and will also allow reallocation if this Matrix
74  // doesn't have a view.
75  Matrix_& operator=(const Matrix_& src) {
76  Base::operator=(src); return *this;
77  }
78 
79  // Force a deep copy of the view or whatever this is.
80  // Note that this is an implicit conversion.
81  Matrix_(const Base& v) : Base(v) {} // e.g., MatrixView
82 
83  // Allow implicit conversion from a source matrix that
84  // has a negated version of ELT.
85  Matrix_(const BaseNeg& v) : Base(v) {}
86 
87  // TODO: implicit conversion from conjugate. This is trickier
88  // since real elements are their own conjugate so you'll get
89  // duplicate methods defined from Matrix_(BaseHerm) and Matrix_(Base).
90 
91  Matrix_(int m, int n) : Base(MatrixCommitment(), m, n) {}
92 
93  Matrix_(int m, int n, const ELT* cppInitialValuesByRow)
94  : Base(MatrixCommitment(), m, n, cppInitialValuesByRow) {}
95  Matrix_(int m, int n, const ELT& initialValue)
96  : Base(MatrixCommitment(), m, n, initialValue) {}
97 
98  Matrix_(int m, int n, int leadingDim, const S* data) // read only
99  : Base(MatrixCommitment(), MatrixCharacter::LapackFull(m,n),
100  leadingDim, data) {}
101  Matrix_(int m, int n, int leadingDim, S* data) // writable
102  : Base(MatrixCommitment(), MatrixCharacter::LapackFull(m,n),
103  leadingDim, data) {}
104 
106  template <int M, int N, int CS, int RS>
107  explicit Matrix_(const Mat<M,N,ELT,CS,RS>& mat)
108  : Base(MatrixCommitment(), M, N)
109  { for (int i = 0; i < M; ++i)
110  for (int j = 0; j < N; ++j)
111  this->updElt(i, j) = mat(i, j); }
112 
113  Matrix_& operator=(const ELT& v) { Base::operator=(v); return *this; }
114 
115  template <class EE> Matrix_& operator=(const MatrixBase<EE>& m)
116  { Base::operator=(m); return*this; }
117  template <class EE> Matrix_& operator+=(const MatrixBase<EE>& m)
118  { Base::operator+=(m); return*this; }
119  template <class EE> Matrix_& operator-=(const MatrixBase<EE>& m)
120  { Base::operator-=(m); return*this; }
121 
122  Matrix_& operator*=(const StdNumber& t) { Base::operator*=(t); return *this; }
123  Matrix_& operator/=(const StdNumber& t) { Base::operator/=(t); return *this; }
124  Matrix_& operator+=(const ELT& r) { this->updDiag() += r; return *this; }
125  Matrix_& operator-=(const ELT& r) { this->updDiag() -= r; return *this; }
126 
127  const TNeg& negate() const {return *reinterpret_cast<const TNeg*>(this); }
128  TNeg& updNegate() {return *reinterpret_cast<TNeg*>(this); }
129 
130  const TNeg& operator-() const {return negate();}
131  TNeg& operator-() {return updNegate();}
132 
133  // Functions to be used for Scripting in MATLAB and languages that do not support operator overloading
135  std::string toString() const {
136  std::stringstream stream;
137  stream << (*this) ;
138  return stream.str();
139  }
141  const ELT& get(int i,int j) const { return this->getElt(i,j); }
143  void set(int i,int j, const ELT& value) { this->updElt(i,j)=value; }
144 
145 private:
146  // NO DATA MEMBERS ALLOWED
147 };
148 
149 } //namespace SimTK
150 
151 #endif // SimTK_SIMMATRIX_MATRIX_H_
Matrix_(const MatrixCommitment &mc)
Definition: Matrix_.h:68
Matrix_(int m, int n, int leadingDim, S *data)
Definition: Matrix_.h:101
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 &v)
Definition: Matrix_.h:113
Matrix_ & operator/=(const StdNumber &t)
Definition: Matrix_.h:123
K::Scalar Scalar
Definition: CompositeNumericalTypes.h:160
A MatrixCharacter is a set containing a value for each of the matrix characteristics except element t...
Definition: MatrixCharacteristics.h:597
TNeg & operator-()
Definition: Matrix_.h:131
std::string toString() const
toString() returns a string representation of the Matrix_.
Definition: Matrix_.h:135
MatrixBase & operator/=(const StdNumber &t)
Definition: MatrixBase.h:290
ELT & updElt(int i, int j)
Definition: MatrixBase.h:656
Matrix_ & operator-=(const MatrixBase< EE > &m)
Definition: Matrix_.h:119
Matrix_ & operator+=(const MatrixBase< EE > &m)
Definition: Matrix_.h:117
Matrix_(int m, int n)
Definition: Matrix_.h:91
MatrixBase & operator-=(const MatrixBase &r)
Definition: MatrixBase.h:292
MatrixBase & operator*=(const StdNumber &t)
Definition: MatrixBase.h:289
Matrix_(const BaseNeg &v)
Definition: Matrix_.h:85
Matrix_ & operator=(const MatrixBase< EE > &m)
Definition: Matrix_.h:115
Matrix_ & operator*=(const StdNumber &t)
Definition: Matrix_.h:122
Matrix_(int m, int n, int leadingDim, const S *data)
Definition: Matrix_.h:98
Matrix_ & operator=(const Matrix_ &src)
Definition: Matrix_.h:75
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 Mat< M, N, ELT, CS, RS > &mat)
Convert a Mat to a Matrix_.
Definition: Matrix_.h:107
EStdNumber StdNumber
Definition: MatrixBase.h:99
Matrix_()
Definition: Matrix_.h:67
This is the matrix class intended to appear in user code for large, variable size matrices...
Definition: BigMatrix.h:168
Matrix_(const Base &v)
Definition: Matrix_.h:81
MatrixBase & operator+=(const MatrixBase &r)
Definition: MatrixBase.h:291
K::StdNumber StdNumber
Definition: CompositeNumericalTypes.h:163
const TNeg & operator-() const
Definition: Matrix_.h:130
const ELT & getElt(int i, int j) const
Element selection for stored elements.
Definition: MatrixBase.h:655
Matrix_ & operator-=(const ELT &r)
Definition: Matrix_.h:125
Matrix_(int m, int n, const ELT &initialValue)
Definition: Matrix_.h:95
MatrixBase & operator=(const MatrixBase &b)
Definition: MatrixBase.h:200
K::TNeg TNeg
Definition: CompositeNumericalTypes.h:139
Matrix_ & operator+=(const ELT &r)
Definition: Matrix_.h:124
Matrix_(int m, int n, const ELT *cppInitialValuesByRow)
Definition: Matrix_.h:93
A MatrixCommitment provides a set of acceptable matrix characteristics.
Definition: MatrixCharacteristics.h:832
TNeg & updNegate()
Definition: Matrix_.h:128
This class represents a small matrix whose size is known at compile time, containing elements of any ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:620
const TNeg & negate() const
Definition: Matrix_.h:127
K::Number Number
Definition: CompositeNumericalTypes.h:162
K::THerm THerm
Definition: CompositeNumericalTypes.h:144
Matrix_(const Matrix_ &src)
Definition: Matrix_.h:71