Simbody
3.5
|
00001 #ifndef SimTK_SIMMATRIX_ROWVECTORBASE_H_ 00002 #define SimTK_SIMMATRIX_ROWVECTORBASE_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 00031 namespace SimTK { 00032 00033 //============================================================================== 00034 // ROW VECTOR BASE 00035 //============================================================================== 00042 template <class ELT> class RowVectorBase : public MatrixBase<ELT> { 00043 typedef MatrixBase<ELT> Base; 00044 typedef typename CNT<ELT>::Scalar Scalar; 00045 typedef typename CNT<ELT>::Number Number; 00046 typedef typename CNT<ELT>::StdNumber StdNumber; 00047 typedef RowVectorBase<ELT> T; 00048 typedef RowVectorBase<typename CNT<ELT>::TAbs> TAbs; 00049 typedef RowVectorBase<typename CNT<ELT>::TNeg> TNeg; 00050 typedef VectorView_<typename CNT<ELT>::THerm> THerm; 00051 public: 00052 // ------------------------------------------------------------------------ 00062 00065 explicit RowVectorBase(int n=0) : Base(MatrixCommitment::RowVector(), 1, n) {} 00066 00070 RowVectorBase(const RowVectorBase& source) : Base(source) {} 00071 00073 RowVectorBase(const TNeg& source) : Base(source) {} 00074 00077 RowVectorBase(int n, const ELT& initialValue) 00078 : Base(MatrixCommitment::RowVector(),1,n,initialValue) {} 00079 00084 RowVectorBase(int n, const ELT* cppInitialValues) 00085 : Base(MatrixCommitment::RowVector(),1,n,cppInitialValues) {} 00087 00088 // ------------------------------------------------------------------------ 00097 00099 RowVectorBase(int n, int stride, const Scalar* s) 00100 : Base(MatrixCommitment::RowVector(n), MatrixCharacter::RowVector(n),stride,s) { } 00102 RowVectorBase(int n, int stride, Scalar* s) 00103 : Base(MatrixCommitment::RowVector(n), MatrixCharacter::RowVector(n),stride,s) { } 00105 00106 // ------------------------------------------------------------------------ 00113 00115 RowVectorBase(MatrixHelper<Scalar>& h, const typename MatrixHelper<Scalar>::ShallowCopy& s) 00116 : Base(MatrixCommitment::RowVector(), h,s) { } 00118 RowVectorBase(const MatrixHelper<Scalar>& h, const typename MatrixHelper<Scalar>::ShallowCopy& s) 00119 : Base(MatrixCommitment::RowVector(), h,s) { } 00121 RowVectorBase(const MatrixHelper<Scalar>& h, const typename MatrixHelper<Scalar>::DeepCopy& d) 00122 : Base(MatrixCommitment::RowVector(), h,d) { } 00124 00125 // This gives the resulting rowvector type when (r(i) op P) is applied to each element. 00126 // It will have element types which are the regular composite result of ELT op P. 00127 template <class P> struct EltResult { 00128 typedef RowVectorBase<typename CNT<ELT>::template Result<P>::Mul> Mul; 00129 typedef RowVectorBase<typename CNT<ELT>::template Result<P>::Dvd> Dvd; 00130 typedef RowVectorBase<typename CNT<ELT>::template Result<P>::Add> Add; 00131 typedef RowVectorBase<typename CNT<ELT>::template Result<P>::Sub> Sub; 00132 }; 00133 00136 RowVectorBase& operator=(const RowVectorBase& b) { 00137 Base::operator=(b); return *this; 00138 } 00139 00140 // default destructor 00141 00142 RowVectorBase& operator*=(const StdNumber& t) {Base::operator*=(t); return *this;} 00143 RowVectorBase& operator/=(const StdNumber& t) {Base::operator/=(t); return *this;} 00144 RowVectorBase& operator+=(const RowVectorBase& r) {Base::operator+=(r); return *this;} 00145 RowVectorBase& operator-=(const RowVectorBase& r) {Base::operator-=(r); return *this;} 00146 00147 template <class EE> RowVectorBase& operator=(const RowVectorBase<EE>& b) 00148 { Base::operator=(b); return *this; } 00149 template <class EE> RowVectorBase& operator+=(const RowVectorBase<EE>& b) 00150 { Base::operator+=(b); return *this; } 00151 template <class EE> RowVectorBase& operator-=(const RowVectorBase<EE>& b) 00152 { Base::operator-=(b); return *this; } 00153 00154 // default destructor 00155 00159 RowVectorBase& operator=(const ELT& t) { Base::setTo(t); return *this; } 00160 00165 template <class EE> RowVectorBase& colScaleInPlace(const VectorBase<EE>& v) 00166 { Base::template colScaleInPlace<EE>(v); return *this; } 00167 template <class EE> inline void colScale(const VectorBase<EE>& v, typename EltResult<EE>::Mul& out) const 00168 { return Base::template colScale<EE>(v,out); } 00169 template <class EE> inline typename EltResult<EE>::Mul colScale(const VectorBase<EE>& v) const 00170 { typename EltResult<EE>::Mul out(ncol()); Base::template colScale<EE>(v,out); return out; } 00171 00172 00173 // elementwise multiply 00174 template <class EE> RowVectorBase& elementwiseMultiplyInPlace(const RowVectorBase<EE>& r) 00175 { Base::template elementwiseMultiplyInPlace<EE>(r); return *this; } 00176 template <class EE> inline void elementwiseMultiply(const RowVectorBase<EE>& v, typename EltResult<EE>::Mul& out) const 00177 { Base::template elementwiseMultiply<EE>(v,out); } 00178 template <class EE> inline typename EltResult<EE>::Mul elementwiseMultiply(const RowVectorBase<EE>& v) const 00179 { typename EltResult<EE>::Mul out(nrow()); Base::template elementwiseMultiply<EE>(v,out); return out; } 00180 00181 // elementwise multiply from left 00182 template <class EE> RowVectorBase& elementwiseMultiplyFromLeftInPlace(const RowVectorBase<EE>& r) 00183 { Base::template elementwiseMultiplyFromLeftInPlace<EE>(r); return *this; } 00184 template <class EE> inline void 00185 elementwiseMultiplyFromLeft( 00186 const RowVectorBase<EE>& v, 00187 typename RowVectorBase<EE>::template EltResult<ELT>::Mul& out) const 00188 { 00189 Base::template elementwiseMultiplyFromLeft<EE>(v,out); 00190 } 00191 template <class EE> inline 00192 typename RowVectorBase<EE>::template EltResult<ELT>::Mul 00193 elementwiseMultiplyFromLeft(const RowVectorBase<EE>& v) const { 00194 typename RowVectorBase<EE>::template EltResult<ELT>::Mul out(nrow()); 00195 Base::template elementwiseMultiplyFromLeft<EE>(v,out); 00196 return out; 00197 } 00198 00199 // elementwise divide 00200 template <class EE> RowVectorBase& elementwiseDivideInPlace(const RowVectorBase<EE>& r) 00201 { Base::template elementwiseDivideInPlace<EE>(r); return *this; } 00202 template <class EE> inline void elementwiseDivide(const RowVectorBase<EE>& v, typename EltResult<EE>::Dvd& out) const 00203 { Base::template elementwiseDivide<EE>(v,out); } 00204 template <class EE> inline typename EltResult<EE>::Dvd elementwiseDivide(const RowVectorBase<EE>& v) const 00205 { typename EltResult<EE>::Dvd out(nrow()); Base::template elementwiseDivide<EE>(v,out); return out; } 00206 00207 // elementwise divide from left 00208 template <class EE> RowVectorBase& elementwiseDivideFromLeftInPlace(const RowVectorBase<EE>& r) 00209 { Base::template elementwiseDivideFromLeftInPlace<EE>(r); return *this; } 00210 template <class EE> inline void 00211 elementwiseDivideFromLeft 00212 (const RowVectorBase<EE>& v, 00213 typename RowVectorBase<EE>::template EltResult<ELT>::Dvd& out) const { 00214 Base::template elementwiseDivideFromLeft<EE>(v,out); 00215 } 00216 template <class EE> inline 00217 typename RowVectorBase<EE>::template EltResult<ELT>::Dvd 00218 elementwiseDivideFromLeft(const RowVectorBase<EE>& v) const { 00219 typename RowVectorBase<EE>::template EltResult<ELT>::Dvd out(nrow()); 00220 Base::template elementwiseDivideFromLeft<EE>(v,out); 00221 return out; 00222 } 00223 00224 // Implicit conversions are allowed to RowVector or Matrix, but not to Vector. 00225 operator const RowVector_<ELT>&() const {return *reinterpret_cast<const RowVector_<ELT>*>(this);} 00226 operator RowVector_<ELT>&() {return *reinterpret_cast< RowVector_<ELT>*>(this);} 00227 operator const RowVectorView_<ELT>&() const {return *reinterpret_cast<const RowVectorView_<ELT>*>(this);} 00228 operator RowVectorView_<ELT>&() {return *reinterpret_cast< RowVectorView_<ELT>*>(this);} 00229 00230 operator const Matrix_<ELT>&() const {return *reinterpret_cast<const Matrix_<ELT>*>(this);} 00231 operator Matrix_<ELT>&() {return *reinterpret_cast< Matrix_<ELT>*>(this);} 00232 operator const MatrixView_<ELT>&() const {return *reinterpret_cast<const MatrixView_<ELT>*>(this);} 00233 operator MatrixView_<ELT>&() {return *reinterpret_cast< MatrixView_<ELT>*>(this);} 00234 00235 00236 // size() for RowVectors is Base::nelt() but returns int instead of ptrdiff_t. 00237 int size() const { 00238 assert(Base::nelt() <= (ptrdiff_t)std::numeric_limits<int>::max()); 00239 assert(Base::nrow()==1); 00240 return (int)Base::nelt(); 00241 } 00242 int nrow() const {assert(Base::nrow()==1); return Base::nrow();} 00243 int ncol() const {assert(Base::nrow()==1); return Base::ncol();} 00244 ptrdiff_t nelt() const {assert(Base::nrow()==1); return Base::nelt();} 00245 00246 // Override MatrixBase operators to return the right shape 00247 TAbs abs() const { 00248 TAbs result; Base::abs(result); return result; 00249 } 00250 00251 // Override MatrixBase indexing operators 00252 const ELT& operator[](int j) const {return *reinterpret_cast<const ELT*>(Base::getHelper().getElt(j));} 00253 ELT& operator[](int j) {return *reinterpret_cast<ELT*> (Base::updHelper().updElt(j));} 00254 const ELT& operator()(int j) const {return *reinterpret_cast<const ELT*>(Base::getHelper().getElt(j));} 00255 ELT& operator()(int j) {return *reinterpret_cast<ELT*> (Base::updHelper().updElt(j));} 00256 00257 // Block (contiguous subvector) creation 00258 RowVectorView_<ELT> operator()(int j, int n) const {return Base::operator()(0,j,1,n).getAsRowVectorView();} 00259 RowVectorView_<ELT> operator()(int j, int n) {return Base::operator()(0,j,1,n).updAsRowVectorView();} 00260 00261 // Indexed view creation (arbitrary subvector). Indices must be monotonically increasing. 00262 RowVectorView_<ELT> index(const Array_<int>& indices) const { 00263 MatrixHelper<Scalar> h(Base::getHelper().getCharacterCommitment(), Base::getHelper(), indices); 00264 return RowVectorView_<ELT>(h); 00265 } 00266 RowVectorView_<ELT> updIndex(const Array_<int>& indices) { 00267 MatrixHelper<Scalar> h(Base::getHelper().getCharacterCommitment(), Base::updHelper(), indices); 00268 return RowVectorView_<ELT>(h); 00269 } 00270 00271 RowVectorView_<ELT> operator()(const Array_<int>& indices) const {return index(indices);} 00272 RowVectorView_<ELT> operator()(const Array_<int>& indices) {return updIndex(indices);} 00273 00274 // Hermitian transpose. 00275 THerm transpose() const {return Base::transpose().getAsVectorView();} 00276 THerm updTranspose() {return Base::updTranspose().updAsVectorView();} 00277 00278 THerm operator~() const {return transpose();} 00279 THerm operator~() {return updTranspose();} 00280 00281 const RowVectorBase& operator+() const {return *this; } 00282 00283 // Negation 00284 00285 const TNeg& negate() const {return *reinterpret_cast<const TNeg*>(this); } 00286 TNeg& updNegate() {return *reinterpret_cast<TNeg*>(this); } 00287 00288 const TNeg& operator-() const {return negate();} 00289 TNeg& operator-() {return updNegate();} 00290 00291 RowVectorBase& resize(int n) {Base::resize(1,n); return *this;} 00292 RowVectorBase& resizeKeep(int n) {Base::resizeKeep(1,n); return *this;} 00293 00294 //TODO: this is not re-locking the number of rows at 1. 00295 void clear() {Base::clear(); Base::resize(1,0);} 00296 00297 ELT sum() const {ELT s; Base::getHelper().sum(reinterpret_cast<Scalar*>(&s)); return s; } // add all the elements 00298 VectorIterator<ELT, RowVectorBase<ELT> > begin() { 00299 return VectorIterator<ELT, RowVectorBase<ELT> >(*this, 0); 00300 } 00301 VectorIterator<ELT, RowVectorBase<ELT> > end() { 00302 return VectorIterator<ELT, RowVectorBase<ELT> >(*this, size()); 00303 } 00304 00305 protected: 00306 // Create a RowVectorBase handle using a given helper rep. 00307 explicit RowVectorBase(MatrixHelperRep<Scalar>* hrep) : Base(hrep) {} 00308 00309 private: 00310 // NO DATA MEMBERS ALLOWED 00311 }; 00312 00313 } //namespace SimTK 00314 00315 #endif // SimTK_SIMMATRIX_ROWVECTORBASE_H_