Simbody  3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RowVectorBase.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMMATRIX_ROWVECTORBASE_H_
2 #define SimTK_SIMMATRIX_ROWVECTORBASE_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 
31 namespace SimTK {
32 
33 //==============================================================================
34 // ROW VECTOR BASE
35 //==============================================================================
42 template <class ELT> class RowVectorBase : public MatrixBase<ELT> {
43  typedef MatrixBase<ELT> Base;
44  typedef typename CNT<ELT>::Scalar Scalar;
45  typedef typename CNT<ELT>::Number Number;
46  typedef typename CNT<ELT>::StdNumber StdNumber;
47  typedef RowVectorBase<ELT> T;
48  typedef RowVectorBase<typename CNT<ELT>::TAbs> TAbs;
49  typedef RowVectorBase<typename CNT<ELT>::TNeg> TNeg;
50  typedef VectorView_<typename CNT<ELT>::THerm> THerm;
51 public:
52  // ------------------------------------------------------------------------
62 
65  explicit RowVectorBase(int n=0) : Base(MatrixCommitment::RowVector(), 1, n) {}
66 
70  RowVectorBase(const RowVectorBase& source) : Base(source) {}
71 
73  RowVectorBase(const TNeg& source) : Base(source) {}
74 
77  RowVectorBase(int n, const ELT& initialValue)
78  : Base(MatrixCommitment::RowVector(),1,n,initialValue) {}
79 
84  RowVectorBase(int n, const ELT* cppInitialValues)
85  : Base(MatrixCommitment::RowVector(),1,n,cppInitialValues) {}
87 
88  // ------------------------------------------------------------------------
97 
99  RowVectorBase(int n, int stride, const Scalar* s)
100  : Base(MatrixCommitment::RowVector(n), MatrixCharacter::RowVector(n),stride,s) { }
102  RowVectorBase(int n, int stride, Scalar* s)
103  : Base(MatrixCommitment::RowVector(n), MatrixCharacter::RowVector(n),stride,s) { }
105 
106  // ------------------------------------------------------------------------
113 
116  : Base(MatrixCommitment::RowVector(), h,s) { }
119  : Base(MatrixCommitment::RowVector(), h,s) { }
122  : Base(MatrixCommitment::RowVector(), h,d) { }
124 
125  // This gives the resulting rowvector type when (r(i) op P) is applied to each element.
126  // It will have element types which are the regular composite result of ELT op P.
127  template <class P> struct EltResult {
132  };
133 
137  Base::operator=(b); return *this;
138  }
139 
140  // default destructor
141 
142  RowVectorBase& operator*=(const StdNumber& t) {Base::operator*=(t); return *this;}
143  RowVectorBase& operator/=(const StdNumber& t) {Base::operator/=(t); return *this;}
145  RowVectorBase& operator-=(const RowVectorBase& r) {Base::operator-=(r); return *this;}
146 
147  template <class EE> RowVectorBase& operator=(const RowVectorBase<EE>& b)
148  { Base::operator=(b); return *this; }
149  template <class EE> RowVectorBase& operator+=(const RowVectorBase<EE>& b)
150  { Base::operator+=(b); return *this; }
151  template <class EE> RowVectorBase& operator-=(const RowVectorBase<EE>& b)
152  { Base::operator-=(b); return *this; }
153 
154  // default destructor
155 
159  RowVectorBase& operator=(const ELT& t) { Base::setTo(t); return *this; }
160 
165  template <class EE> RowVectorBase& colScaleInPlace(const VectorBase<EE>& v)
166  { Base::template colScaleInPlace<EE>(v); return *this; }
167  template <class EE> inline void colScale(const VectorBase<EE>& v, typename EltResult<EE>::Mul& out) const
168  { return Base::template colScale<EE>(v,out); }
169  template <class EE> inline typename EltResult<EE>::Mul colScale(const VectorBase<EE>& v) const
170  { typename EltResult<EE>::Mul out(ncol()); Base::template colScale<EE>(v,out); return out; }
171 
172 
173  // elementwise multiply
175  { Base::template elementwiseMultiplyInPlace<EE>(r); return *this; }
176  template <class EE> inline void elementwiseMultiply(const RowVectorBase<EE>& v, typename EltResult<EE>::Mul& out) const
177  { Base::template elementwiseMultiply<EE>(v,out); }
178  template <class EE> inline typename EltResult<EE>::Mul elementwiseMultiply(const RowVectorBase<EE>& v) const
179  { typename EltResult<EE>::Mul out(nrow()); Base::template elementwiseMultiply<EE>(v,out); return out; }
180 
181  // elementwise multiply from left
183  { Base::template elementwiseMultiplyFromLeftInPlace<EE>(r); return *this; }
184  template <class EE> inline void
186  const RowVectorBase<EE>& v,
188  {
189  Base::template elementwiseMultiplyFromLeft<EE>(v,out);
190  }
191  template <class EE> inline
192  typename RowVectorBase<EE>::template EltResult<ELT>::Mul
195  Base::template elementwiseMultiplyFromLeft<EE>(v,out);
196  return out;
197  }
198 
199  // elementwise divide
201  { Base::template elementwiseDivideInPlace<EE>(r); return *this; }
202  template <class EE> inline void elementwiseDivide(const RowVectorBase<EE>& v, typename EltResult<EE>::Dvd& out) const
203  { Base::template elementwiseDivide<EE>(v,out); }
204  template <class EE> inline typename EltResult<EE>::Dvd elementwiseDivide(const RowVectorBase<EE>& v) const
205  { typename EltResult<EE>::Dvd out(nrow()); Base::template elementwiseDivide<EE>(v,out); return out; }
206 
207  // elementwise divide from left
209  { Base::template elementwiseDivideFromLeftInPlace<EE>(r); return *this; }
210  template <class EE> inline void
212  (const RowVectorBase<EE>& v,
213  typename RowVectorBase<EE>::template EltResult<ELT>::Dvd& out) const {
214  Base::template elementwiseDivideFromLeft<EE>(v,out);
215  }
216  template <class EE> inline
217  typename RowVectorBase<EE>::template EltResult<ELT>::Dvd
220  Base::template elementwiseDivideFromLeft<EE>(v,out);
221  return out;
222  }
223 
224  // Implicit conversions are allowed to RowVector or Matrix, but not to Vector.
225  operator const RowVector_<ELT>&() const {return *reinterpret_cast<const RowVector_<ELT>*>(this);}
226  operator RowVector_<ELT>&() {return *reinterpret_cast< RowVector_<ELT>*>(this);}
227  operator const RowVectorView_<ELT>&() const {return *reinterpret_cast<const RowVectorView_<ELT>*>(this);}
228  operator RowVectorView_<ELT>&() {return *reinterpret_cast< RowVectorView_<ELT>*>(this);}
229 
230  operator const Matrix_<ELT>&() const {return *reinterpret_cast<const Matrix_<ELT>*>(this);}
231  operator Matrix_<ELT>&() {return *reinterpret_cast< Matrix_<ELT>*>(this);}
232  operator const MatrixView_<ELT>&() const {return *reinterpret_cast<const MatrixView_<ELT>*>(this);}
233  operator MatrixView_<ELT>&() {return *reinterpret_cast< MatrixView_<ELT>*>(this);}
234 
235 
236  // size() for RowVectors is Base::nelt() but returns int instead of ptrdiff_t.
237  int size() const {
238  assert(Base::nelt() <= (ptrdiff_t)std::numeric_limits<int>::max());
239  assert(Base::nrow()==1);
240  return (int)Base::nelt();
241  }
242  int nrow() const {assert(Base::nrow()==1); return Base::nrow();}
243  int ncol() const {assert(Base::nrow()==1); return Base::ncol();}
244  ptrdiff_t nelt() const {assert(Base::nrow()==1); return Base::nelt();}
245 
246  // Override MatrixBase operators to return the right shape
247  TAbs abs() const {
248  TAbs result; Base::abs(result); return result;
249  }
250 
251  // Override MatrixBase indexing operators
252  const ELT& operator[](int j) const {return *reinterpret_cast<const ELT*>(Base::getHelper().getElt(j));}
253  ELT& operator[](int j) {return *reinterpret_cast<ELT*> (Base::updHelper().updElt(j));}
254  const ELT& operator()(int j) const {return *reinterpret_cast<const ELT*>(Base::getHelper().getElt(j));}
255  ELT& operator()(int j) {return *reinterpret_cast<ELT*> (Base::updHelper().updElt(j));}
256 
257  // Block (contiguous subvector) creation
258  RowVectorView_<ELT> operator()(int j, int n) const {return Base::operator()(0,j,1,n).getAsRowVectorView();}
259  RowVectorView_<ELT> operator()(int j, int n) {return Base::operator()(0,j,1,n).updAsRowVectorView();}
260 
261  // Indexed view creation (arbitrary subvector). Indices must be monotonically increasing.
262  RowVectorView_<ELT> index(const Array_<int>& indices) const {
264  return RowVectorView_<ELT>(h);
265  }
268  return RowVectorView_<ELT>(h);
269  }
270 
271  RowVectorView_<ELT> operator()(const Array_<int>& indices) const {return index(indices);}
272  RowVectorView_<ELT> operator()(const Array_<int>& indices) {return updIndex(indices);}
273 
274  // Hermitian transpose.
275  THerm transpose() const {return Base::transpose().getAsVectorView();}
276  THerm updTranspose() {return Base::updTranspose().updAsVectorView();}
277 
278  THerm operator~() const {return transpose();}
279  THerm operator~() {return updTranspose();}
280 
281  const RowVectorBase& operator+() const {return *this; }
282 
283  // Negation
284 
285  const TNeg& negate() const {return *reinterpret_cast<const TNeg*>(this); }
286  TNeg& updNegate() {return *reinterpret_cast<TNeg*>(this); }
287 
288  const TNeg& operator-() const {return negate();}
289  TNeg& operator-() {return updNegate();}
290 
291  RowVectorBase& resize(int n) {Base::resize(1,n); return *this;}
292  RowVectorBase& resizeKeep(int n) {Base::resizeKeep(1,n); return *this;}
293 
294  //TODO: this is not re-locking the number of rows at 1.
295  void clear() {Base::clear(); Base::resize(1,0);}
296 
297  ELT sum() const {ELT s; Base::getHelper().sum(reinterpret_cast<Scalar*>(&s)); return s; } // add all the elements
299  return VectorIterator<ELT, RowVectorBase<ELT> >(*this, 0);
300  }
302  return VectorIterator<ELT, RowVectorBase<ELT> >(*this, size());
303  }
304 
305 protected:
306  // Create a RowVectorBase handle using a given helper rep.
307  explicit RowVectorBase(MatrixHelperRep<Scalar>* hrep) : Base(hrep) {}
308 
309 private:
310  // NO DATA MEMBERS ALLOWED
311 };
312 
313 } //namespace SimTK
314 
315 #endif // SimTK_SIMMATRIX_ROWVECTORBASE_H_
RowVectorBase & operator=(const RowVectorBase< EE > &b)
Definition: RowVectorBase.h:147
MatrixView_< EHerm > updTranspose()
Definition: BigMatrix.h:230
TNeg & updNegate()
Definition: RowVectorBase.h:286
RowVectorBase & operator-=(const RowVectorBase &r)
Definition: RowVectorBase.h:145
RowVectorBase(const RowVectorBase &source)
Copy constructor is a deep copy (not appropriate for views!).
Definition: RowVectorBase.h:70
RowVectorBase & operator*=(const StdNumber &t)
Definition: RowVectorBase.h:142
const MatrixHelper< Scalar > & getHelper() const
Definition: MatrixBase.h:865
RowVectorBase & operator-=(const RowVectorBase< EE > &b)
Definition: RowVectorBase.h:151
const TNeg & negate() const
Definition: RowVectorBase.h:285
RowVectorBase & resize(int n)
Definition: RowVectorBase.h:291
THerm operator~() const
Definition: RowVectorBase.h:278
void elementwiseDivide(const RowVectorBase< EE > &v, typename EltResult< EE >::Dvd &out) const
Definition: RowVectorBase.h:202
void elementwiseMultiplyFromLeft(const RowVectorBase< EE > &v, typename RowVectorBase< EE >::template EltResult< ELT >::Mul &out) const
Definition: RowVectorBase.h:185
MatrixView_< EHerm > transpose() const
Definition: BigMatrix.h:222
int ncol() const
Definition: RowVectorBase.h:243
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
RowVectorBase(int n, int stride, Scalar *s)
Construct a writable view into existing data.
Definition: RowVectorBase.h:102
VectorIterator< ELT, RowVectorBase< ELT > > end()
Definition: RowVectorBase.h:301
RowVectorBase(const TNeg &source)
Implicit conversion from compatible row vector with negated elements.
Definition: RowVectorBase.h:73
RowVectorBase & elementwiseDivideFromLeftInPlace(const RowVectorBase< EE > &r)
Definition: RowVectorBase.h:208
A MatrixCharacter is a set containing a value for each of the matrix characteristics except element t...
Definition: MatrixCharacteristics.h:596
RowVectorBase(const MatrixHelper< Scalar > &h, const typename MatrixHelper< Scalar >::DeepCopy &d)
Construct a new owner vector initialized with the data from the source.
Definition: RowVectorBase.h:121
RowVectorView_< ELT > operator()(int j, int n)
Definition: RowVectorBase.h:259
(Advanced) This class is identical to Matrix_ except that it has shallow (reference) copy and assignm...
Definition: BigMatrix.h:167
This is a dataless rehash of the MatrixBase class to specialize it for Vectors.
Definition: BigMatrix.h:164
THerm operator~()
Definition: RowVectorBase.h:279
RowVectorBase & elementwiseMultiplyInPlace(const RowVectorBase< EE > &r)
Definition: RowVectorBase.h:174
MatrixBase & operator=(const MatrixBase &b)
Definition: MatrixBase.h:201
RowVectorBase< EE >::template EltResult< ELT >::Mul elementwiseMultiplyFromLeft(const RowVectorBase< EE > &v) const
Definition: RowVectorBase.h:193
VectorView_< ELT > operator()(int j) const
Definition: MatrixBase.h:595
RowVectorBase & operator+=(const RowVectorBase &r)
Definition: RowVectorBase.h:144
ELT & operator[](int j)
Definition: RowVectorBase.h:253
S * updElt(int i, int j)
void elementwiseMultiply(const RowVectorBase< EE > &v, typename EltResult< EE >::Mul &out) const
Definition: RowVectorBase.h:176
MatrixHelper< Scalar > & updHelper()
Definition: MatrixBase.h:866
MatrixBase & operator*=(const StdNumber &t)
Definition: MatrixBase.h:290
Definition: RowVectorBase.h:127
ELT & operator()(int j)
Definition: RowVectorBase.h:255
ptrdiff_t nelt() const
Return the number of elements in the logical shape of this matrix.
Definition: MatrixBase.h:148
RowVectorView_< ELT > operator()(const Array_< int > &indices)
Definition: RowVectorBase.h:272
const ELT & operator[](int j) const
Definition: RowVectorBase.h:252
void clear()
Definition: RowVectorBase.h:295
RowVectorBase(MatrixHelper< Scalar > &h, const typename MatrixHelper< Scalar >::ShallowCopy &s)
Construct a writable view into the source data.
Definition: RowVectorBase.h:115
RowVectorBase(int n, const ELT &initialValue)
Construct an owner row vector of length n, with each element initialized to the given value...
Definition: RowVectorBase.h:77
THerm updTranspose()
Definition: RowVectorBase.h:276
RowVectorBase(MatrixHelperRep< Scalar > *hrep)
Definition: RowVectorBase.h:307
RowVectorBase(int n, int stride, const Scalar *s)
Construct a read-only view of existing data.
Definition: RowVectorBase.h:99
TAbs abs() const
abs() with the result as a function return.
Definition: MatrixBase.h:699
RowVectorBase< typename CNT< ELT >::template Result< P >::Sub > Sub
Definition: RowVectorBase.h:131
RowVectorBase< typename CNT< ELT >::template Result< P >::Mul > Mul
Definition: RowVectorBase.h:128
const TNeg & operator-() const
Definition: RowVectorBase.h:288
const ELT & operator()(int j) const
Definition: RowVectorBase.h:254
EltResult< EE >::Dvd elementwiseDivide(const RowVectorBase< EE > &v) const
Definition: RowVectorBase.h:204
MatrixBase & operator/=(const StdNumber &t)
Definition: MatrixBase.h:291
MatrixBase & operator+=(const MatrixBase &r)
Definition: MatrixBase.h:292
RowVectorBase & elementwiseDivideInPlace(const RowVectorBase< EE > &r)
Definition: RowVectorBase.h:200
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
RowVectorBase< typename CNT< ELT >::template Result< P >::Dvd > Dvd
Definition: RowVectorBase.h:129
MatrixBase & resize(int m, int n)
Change the size of this matrix.
Definition: MatrixBase.h:774
void clear()
This restores the MatrixBase to the state it would be in had it been constructed specifying only its ...
Definition: MatrixBase.h:288
RowVectorBase(const MatrixHelper< Scalar > &h, const typename MatrixHelper< Scalar >::ShallowCopy &s)
Construct a read-only view of the source data.
Definition: RowVectorBase.h:118
EltResult< EE >::Mul colScale(const VectorBase< EE > &v) const
Definition: RowVectorBase.h:169
VectorIterator< ELT, RowVectorBase< ELT > > begin()
Definition: RowVectorBase.h:298
THerm transpose() const
Definition: RowVectorBase.h:275
Represents a variable size row vector; much less common than the column vector type Vector_...
Definition: BigMatrix.h:174
MatrixBase & setTo(const ELT &t)
Fill every element in current allocation with given element (or NaN or 0).
Definition: MatrixBase.h:583
RowVectorView_< ELT > operator()(const Array_< int > &indices) const
Definition: RowVectorBase.h:271
int nrow() const
Return the number of rows m in the logical shape of this matrix.
Definition: MatrixBase.h:137
RowVectorBase & operator/=(const StdNumber &t)
Definition: RowVectorBase.h:143
RowVectorBase(int n=0)
Default constructor makes a 1x0 matrix locked at 1 row; you can provide an initial allocation if you ...
Definition: RowVectorBase.h:65
RowVectorBase & elementwiseMultiplyFromLeftInPlace(const RowVectorBase< EE > &r)
Definition: RowVectorBase.h:182
(Advanced) This class is identical to RowVector_ except that it has shallow (reference) copy and assi...
Definition: BigMatrix.h:173
int ncol() const
Return the number of columns n in the logical shape of this matrix.
Definition: MatrixBase.h:139
const S * getElt(int i, int j) const
MatrixBase & resizeKeep(int m, int n)
Change the size of this matrix, retaining as much of the old data as will fit.
Definition: MatrixBase.h:780
RowVectorBase(int n, const ELT *cppInitialValues)
Construct an owner vector of length n, with the elements initialized sequentially from a C++ array of...
Definition: RowVectorBase.h:84
const RowVectorBase & operator+() const
Definition: RowVectorBase.h:281
RowVectorView_< ELT > index(const Array_< int > &indices) const
Definition: RowVectorBase.h:262
const MatrixCommitment & getCharacterCommitment() const
Definition: MatrixBase.h:119
RowVectorView_< ELT > updIndex(const Array_< int > &indices)
Definition: RowVectorBase.h:266
EltResult< EE >::Mul elementwiseMultiply(const RowVectorBase< EE > &v) const
Definition: RowVectorBase.h:178
void colScale(const VectorBase< EE > &v, typename EltResult< EE >::Mul &out) const
Definition: RowVectorBase.h:167
int nrow() const
Definition: RowVectorBase.h:242
A MatrixCommitment provides a set of acceptable matrix characteristics.
Definition: MatrixCharacteristics.h:831
This is a dataless rehash of the MatrixBase class to specialize it for RowVectors.
Definition: BigMatrix.h:165
MatrixBase & operator-=(const MatrixBase &r)
Definition: MatrixBase.h:293
This is an iterator for iterating over the elements of a Vector_ or Vec object.
Definition: BigMatrix.h:176
RowVectorView_< ELT > operator()(int j, int n) const
Definition: RowVectorBase.h:258
RowVectorBase & operator=(const RowVectorBase &b)
Copy assignment is deep copy but behavior depends on type of lhs: if view, rhs must match...
Definition: RowVectorBase.h:136
TAbs abs() const
Definition: RowVectorBase.h:247
RowVectorBase & resizeKeep(int n)
Definition: RowVectorBase.h:292
ELT sum() const
Definition: RowVectorBase.h:297
RowVectorBase & operator+=(const RowVectorBase< EE > &b)
Definition: RowVectorBase.h:149
RowVectorBase & operator=(const ELT &t)
Fill current allocation with copies of element.
Definition: RowVectorBase.h:159
int size() const
Definition: RowVectorBase.h:237
RowVectorBase & colScaleInPlace(const VectorBase< EE > &v)
There's only one row here so it's a bit wierd to use colScale rather than elementwiseMultiply, but there's nothing really wrong with it.
Definition: RowVectorBase.h:165
This is the matrix class intended to appear in user code for large, variable size matrices...
Definition: BigMatrix.h:168
void sum(S *eltp) const
RowVectorBase< EE >::template EltResult< ELT >::Dvd elementwiseDivideFromLeft(const RowVectorBase< EE > &v) const
Definition: RowVectorBase.h:218
TNeg & operator-()
Definition: RowVectorBase.h:289
void elementwiseDivideFromLeft(const RowVectorBase< EE > &v, typename RowVectorBase< EE >::template EltResult< ELT >::Dvd &out) const
Definition: RowVectorBase.h:212
ptrdiff_t nelt() const
Definition: RowVectorBase.h:244
RowVectorBase< typename CNT< ELT >::template Result< P >::Add > Add
Definition: RowVectorBase.h:130