Simbody  3.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BigMatrix.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMMATRIX_BIGMATRIX_H_
2 #define SimTK_SIMMATRIX_BIGMATRIX_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-12 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 
159 // TODO: matrix expression templates for delaying operator execution.
160 
161 #include "SimTKcommon/Scalar.h"
162 #include "SimTKcommon/SmallMatrix.h"
163 
166 
167 #include <iostream>
168 #include <cassert>
169 #include <complex>
170 #include <cstddef>
171 #include <limits>
172 
173 namespace SimTK {
174 
175 template <class ELT> class MatrixBase;
176 template <class ELT> class VectorBase;
177 template <class ELT> class RowVectorBase;
178 
179 template <class T=Real> class MatrixView_;
180 template <class T=Real> class DeadMatrixView_;
181 template <class T=Real> class Matrix_;
182 template <class T=Real> class DeadMatrix_;
183 
184 template <class T=Real> class VectorView_;
185 template <class T=Real> class DeadVectorView_;
186 template <class T=Real> class Vector_;
187 template <class T=Real> class DeadVector_;
188 
189 template <class T=Real> class RowVectorView_;
190 template <class T=Real> class DeadRowVectorView_;
191 template <class T=Real> class RowVector_;
192 template <class T=Real> class DeadRowVector_;
193 
194 template <class ELT, class VECTOR_CLASS> class VectorIterator;
195 
196 // -------------------------------- MatrixBase --------------------------------
218 // ----------------------------------------------------------------------------
219 template <class ELT> class MatrixBase {
220 public:
221  // These typedefs are handy, but despite appearances you cannot
222  // treat a MatrixBase as a composite numerical type. That is,
223  // CNT<MatrixBase> will not compile, or if it does it won't be
224  // meaningful.
225 
226  typedef ELT E;
227  typedef typename CNT<E>::TNeg ENeg;
229  typedef typename CNT<E>::TReal EReal;
230  typedef typename CNT<E>::TImag EImag;
231  typedef typename CNT<E>::TComplex EComplex;
232  typedef typename CNT<E>::THerm EHerm;
233  typedef typename CNT<E>::TPosTrans EPosTrans;
234 
235  typedef typename CNT<E>::TAbs EAbs;
236  typedef typename CNT<E>::TStandard EStandard;
237  typedef typename CNT<E>::TInvert EInvert;
238  typedef typename CNT<E>::TNormalize ENormalize;
239  typedef typename CNT<E>::TSqHermT ESqHermT;
240  typedef typename CNT<E>::TSqTHerm ESqTHerm;
241 
242  typedef typename CNT<E>::Scalar EScalar;
243  typedef typename CNT<E>::Number ENumber;
244  typedef typename CNT<E>::StdNumber EStdNumber;
245  typedef typename CNT<E>::Precision EPrecision;
247 
248  typedef EScalar Scalar; // the underlying Scalar type
249  typedef ENumber Number; // negator removed from Scalar
250  typedef EStdNumber StdNumber; // conjugate goes to complex
251  typedef EPrecision Precision; // complex removed from StdNumber
252  typedef EScalarNormSq ScalarNormSq; // type of scalar^2
253 
254  typedef MatrixBase<E> T;
262 
267  typedef MatrixBase<ESqHermT> TSqHermT; // ~Mat*Mat
268  typedef MatrixBase<ESqTHerm> TSqTHerm; // Mat*~Mat
269 
271  const MatrixCharacter& getMatrixCharacter() const {return helper.getMatrixCharacter();}
272 
275  void commitTo(const MatrixCommitment& mc)
276  { helper.commitTo(mc); }
277 
278  // This gives the resulting matrix type when (m(i,j) op P) is applied to each element.
279  // It will have element types which are the regular composite result of E op P.
280  template <class P> struct EltResult {
281  typedef MatrixBase<typename CNT<E>::template Result<P>::Mul> Mul;
282  typedef MatrixBase<typename CNT<E>::template Result<P>::Dvd> Dvd;
283  typedef MatrixBase<typename CNT<E>::template Result<P>::Add> Add;
284  typedef MatrixBase<typename CNT<E>::template Result<P>::Sub> Sub;
285  };
286 
288  int nrow() const {return helper.nrow();}
290  int ncol() const {return helper.ncol();}
291 
299  ptrdiff_t nelt() const {return helper.nelt();}
300 
303 
304  enum {
306  CppNScalarsPerElement = sizeof(E) / sizeof(Scalar)
307  };
308 
313 
316  MatrixBase(int m, int n)
318 
324  explicit MatrixBase(const MatrixCommitment& commitment)
325  : helper(NScalarsPerElement,CppNScalarsPerElement,commitment) {}
326 
327 
332  MatrixBase(const MatrixCommitment& commitment, int m, int n)
333  : helper(NScalarsPerElement,CppNScalarsPerElement,commitment,m,n) {}
334 
337  : helper(b.helper.getCharacterCommitment(),
338  b.helper, typename MatrixHelper<Scalar>::DeepCopy()) { }
339 
342  MatrixBase(const TNeg& b)
343  : helper(b.helper.getCharacterCommitment(),
344  b.helper, typename MatrixHelper<Scalar>::DeepCopy()) { }
345 
349  helper.copyAssign(b.helper);
350  return *this;
351  }
352  MatrixBase& operator=(const MatrixBase& b) { return copyAssign(b); }
353 
354 
364  helper.writableViewAssign(const_cast<MatrixHelper<Scalar>&>(src.helper));
365  return *this;
366  }
367 
368  // default destructor
369 
376  MatrixBase(const MatrixCommitment& commitment, int m, int n, const ELT& initialValue)
377  : helper(NScalarsPerElement, CppNScalarsPerElement, commitment, m, n)
378  { helper.fillWith(reinterpret_cast<const Scalar*>(&initialValue)); }
379 
390  MatrixBase(const MatrixCommitment& commitment, int m, int n,
391  const ELT* cppInitialValuesByRow)
392  : helper(NScalarsPerElement, CppNScalarsPerElement, commitment, m, n)
393  { helper.copyInByRowsFromCpp(reinterpret_cast<const Scalar*>(cppInitialValuesByRow)); }
394 
406 
408  MatrixBase(const MatrixCommitment& commitment,
409  const MatrixCharacter& character,
410  int spacing, const Scalar* data) // read only data
412  commitment, character, spacing, data) {}
413 
415  MatrixBase(const MatrixCommitment& commitment,
416  const MatrixCharacter& character,
417  int spacing, Scalar* data) // writable data
419  commitment, character, spacing, data) {}
421 
422  // Create a new MatrixBase from an existing helper. Both shallow and deep copies are possible.
423  MatrixBase(const MatrixCommitment& commitment,
424  MatrixHelper<Scalar>& source,
425  const typename MatrixHelper<Scalar>::ShallowCopy& shallow)
426  : helper(commitment, source, shallow) {}
427  MatrixBase(const MatrixCommitment& commitment,
428  const MatrixHelper<Scalar>& source,
429  const typename MatrixHelper<Scalar>::ShallowCopy& shallow)
430  : helper(commitment, source, shallow) {}
431  MatrixBase(const MatrixCommitment& commitment,
432  const MatrixHelper<Scalar>& source,
433  const typename MatrixHelper<Scalar>::DeepCopy& deep)
434  : helper(commitment, source, deep) {}
435 
439  void clear() {helper.clear();}
440 
441  MatrixBase& operator*=(const StdNumber& t) { helper.scaleBy(t); return *this; }
442  MatrixBase& operator/=(const StdNumber& t) { helper.scaleBy(StdNumber(1)/t); return *this; }
443  MatrixBase& operator+=(const MatrixBase& r) { helper.addIn(r.helper); return *this; }
444  MatrixBase& operator-=(const MatrixBase& r) { helper.subIn(r.helper); return *this; }
445 
446  template <class EE> MatrixBase(const MatrixBase<EE>& b)
447  : helper(MatrixCommitment(),b.helper, typename MatrixHelper<Scalar>::DeepCopy()) { }
448 
449  template <class EE> MatrixBase& operator=(const MatrixBase<EE>& b)
450  { helper = b.helper; return *this; }
451  template <class EE> MatrixBase& operator+=(const MatrixBase<EE>& b)
452  { helper.addIn(b.helper); return *this; }
453  template <class EE> MatrixBase& operator-=(const MatrixBase<EE>& b)
454  { helper.subIn(b.helper); return *this; }
455 
466  MatrixBase& operator=(const ELT& t) {
467  setToZero(); updDiag().setTo(t);
468  return *this;
469  }
470 
476  template <class S> inline MatrixBase&
477  scalarAssign(const S& s) {
478  setToZero(); updDiag().setTo(s);
479  return *this;
480  }
481 
485  template <class S> inline MatrixBase&
486  scalarAddInPlace(const S& s) {
487  updDiag().elementwiseAddScalarInPlace(s);
488  }
489 
490 
494  template <class S> inline MatrixBase&
495  scalarSubtractInPlace(const S& s) {
496  updDiag().elementwiseSubtractScalarInPlace(s);
497  }
498 
501  template <class S> inline MatrixBase&
503  negateInPlace();
504  updDiag().elementwiseAddScalarInPlace(s); // yes, add
505  }
506 
513  template <class S> inline MatrixBase&
514  scalarMultiplyInPlace(const S&);
515 
519  template <class S> inline MatrixBase&
521 
528  template <class S> inline MatrixBase&
529  scalarDivideInPlace(const S&);
530 
534  template <class S> inline MatrixBase&
535  scalarDivideFromLeftInPlace(const S&);
536 
537 
540  template <class EE> inline MatrixBase&
542 
545  template <class EE> inline void
546  rowScale(const VectorBase<EE>& r, typename EltResult<EE>::Mul& out) const;
547 
548  template <class EE> inline typename EltResult<EE>::Mul
549  rowScale(const VectorBase<EE>& r) const {
550  typename EltResult<EE>::Mul out(nrow(), ncol()); rowScale(r,out); return out;
551  }
552 
555  template <class EE> inline MatrixBase&
557 
558  template <class EE> inline void
559  colScale(const VectorBase<EE>& c, typename EltResult<EE>::Mul& out) const;
560 
561  template <class EE> inline typename EltResult<EE>::Mul
562  colScale(const VectorBase<EE>& c) const {
563  typename EltResult<EE>::Mul out(nrow(), ncol()); colScale(c,out); return out;
564  }
565 
566 
571  template <class ER, class EC> inline MatrixBase&
573 
574  template <class ER, class EC> inline void
575  rowAndColScale(const VectorBase<ER>& r, const VectorBase<EC>& c,
576  typename EltResult<typename VectorBase<ER>::template EltResult<EC>::Mul>::Mul& out) const;
577 
578  template <class ER, class EC> inline typename EltResult<typename VectorBase<ER>::template EltResult<EC>::Mul>::Mul
579  rowAndColScale(const VectorBase<ER>& r, const VectorBase<EC>& c) const {
581  out(nrow(), ncol());
582  rowAndColScale(r,c,out); return out;
583  }
584 
592  template <class S> inline MatrixBase&
593  elementwiseAssign(const S& s);
594 
597  { return elementwiseAssign<Real>(Real(s)); }
598 
601 
602  void elementwiseInvert(MatrixBase<typename CNT<E>::TInvert>& out) const;
603 
606  elementwiseInvert(out);
607  return out;
608  }
609 
617  template <class S> inline MatrixBase&
618  elementwiseAddScalarInPlace(const S& s);
619 
620  template <class S> inline void
621  elementwiseAddScalar(const S& s, typename EltResult<S>::Add&) const;
622 
623  template <class S> inline typename EltResult<S>::Add
624  elementwiseAddScalar(const S& s) const {
625  typename EltResult<S>::Add out(nrow(), ncol());
626  elementwiseAddScalar(s,out);
627  return out;
628  }
629 
637  template <class S> inline MatrixBase&
639 
640  template <class S> inline void
641  elementwiseSubtractScalar(const S& s, typename EltResult<S>::Sub&) const;
642 
643  template <class S> inline typename EltResult<S>::Sub
644  elementwiseSubtractScalar(const S& s) const {
645  typename EltResult<S>::Sub out(nrow(), ncol());
647  return out;
648  }
649 
658  template <class S> inline MatrixBase&
660 
661  template <class S> inline void
663  const S&,
664  typename MatrixBase<S>::template EltResult<E>::Sub&) const;
665 
666  template <class S> inline typename MatrixBase<S>::template EltResult<E>::Sub
667  elementwiseSubtractFromScalar(const S& s) const {
669  elementwiseSubtractFromScalar<S>(s,out);
670  return out;
671  }
672 
674  template <class EE> inline MatrixBase&
676 
677  template <class EE> inline void
678  elementwiseMultiply(const MatrixBase<EE>&, typename EltResult<EE>::Mul&) const;
679 
680  template <class EE> inline typename EltResult<EE>::Mul
682  typename EltResult<EE>::Mul out(nrow(), ncol());
683  elementwiseMultiply<EE>(m,out);
684  return out;
685  }
686 
688  template <class EE> inline MatrixBase&
690 
691  template <class EE> inline void
693  const MatrixBase<EE>&,
694  typename MatrixBase<EE>::template EltResult<E>::Mul&) const;
695 
696  template <class EE> inline typename MatrixBase<EE>::template EltResult<E>::Mul
698  typename EltResult<EE>::Mul out(nrow(), ncol());
699  elementwiseMultiplyFromLeft<EE>(m,out);
700  return out;
701  }
702 
704  template <class EE> inline MatrixBase&
706 
707  template <class EE> inline void
708  elementwiseDivide(const MatrixBase<EE>&, typename EltResult<EE>::Dvd&) const;
709 
710  template <class EE> inline typename EltResult<EE>::Dvd
712  typename EltResult<EE>::Dvd out(nrow(), ncol());
713  elementwiseDivide<EE>(m,out);
714  return out;
715  }
716 
718  template <class EE> inline MatrixBase&
720 
721  template <class EE> inline void
723  const MatrixBase<EE>&,
724  typename MatrixBase<EE>::template EltResult<E>::Dvd&) const;
725 
726  template <class EE> inline typename MatrixBase<EE>::template EltResult<EE>::Dvd
729  elementwiseDivideFromLeft<EE>(m,out);
730  return out;
731  }
732 
734  MatrixBase& setTo(const ELT& t) {helper.fillWith(reinterpret_cast<const Scalar*>(&t)); return *this;}
736  MatrixBase& setToZero() {helper.fillWithScalar(StdNumber(0)); return *this;}
737 
738  // View creating operators. TODO: these should be DeadMatrixViews
739  inline RowVectorView_<ELT> row(int i) const; // select a row
740  inline RowVectorView_<ELT> updRow(int i);
741  inline VectorView_<ELT> col(int j) const; // select a column
742  inline VectorView_<ELT> updCol(int j);
743 
744  RowVectorView_<ELT> operator[](int i) const {return row(i);}
746  VectorView_<ELT> operator()(int j) const {return col(j);}
747  VectorView_<ELT> operator()(int j) {return updCol(j);}
748 
749  // Select a block.
750  inline MatrixView_<ELT> block(int i, int j, int m, int n) const;
751  inline MatrixView_<ELT> updBlock(int i, int j, int m, int n);
752 
753  MatrixView_<ELT> operator()(int i, int j, int m, int n) const
754  { return block(i,j,m,n); }
755  MatrixView_<ELT> operator()(int i, int j, int m, int n)
756  { return updBlock(i,j,m,n); }
757 
758  // Hermitian transpose.
759  inline MatrixView_<EHerm> transpose() const;
761 
764 
767  inline VectorView_<ELT> diag() const;
770  inline VectorView_<ELT> updDiag();
774 
775  // Create a view of the real or imaginary elements. TODO
776  //inline MatrixView_<EReal> real() const;
777  //inline MatrixView_<EReal> updReal();
778  //inline MatrixView_<EImag> imag() const;
779  //inline MatrixView_<EImag> updImag();
780 
781  // Overload "real" and "imag" for both read and write as a nod to convention. TODO
782  //MatrixView_<EReal> real() {return updReal();}
783  //MatrixView_<EReal> imag() {return updImag();}
784 
785  // TODO: this routine seems ill-advised but I need it for the IVM port at the moment
786  TInvert invert() const { // return a newly-allocated inverse; dump negator
787  TInvert m(*this);
788  m.helper.invertInPlace();
789  return m; // TODO - bad: makes an extra copy
790  }
791 
792  void invertInPlace() {helper.invertInPlace();}
793 
795  void dump(const char* msg=0) const {
796  helper.dump(msg);
797  }
798 
799 
800  // This routine is useful for implementing friendlier Matrix expressions and operators.
801  // It maps closely to the Level-3 BLAS family of pxxmm() routines like sgemm(). The
802  // operation performed assumes that "this" is the result, and that "this" has
803  // already been sized correctly to receive the result. We'll compute
804  // this = beta*this + alpha*A*B
805  // If beta is 0 then "this" can be uninitialized. If alpha is 0 we promise not
806  // to look at A or B. The routine can work efficiently even if A and/or B are transposed
807  // by their views, so an expression like
808  // C += s * ~A * ~B
809  // can be performed with the single equivalent call
810  // C.matmul(1., s, Tr(A), Tr(B))
811  // where Tr(A) indicates a transposed view of the original A.
812  // The ultimate efficiency of this call depends on the data layout and views which
813  // are used for the three matrices.
814  // NOTE: neither A nor B can be the same matrix as 'this', nor views of the same data
815  // which would expose elements of 'this' that will be modified by this operation.
816  template <class ELT_A, class ELT_B>
817  MatrixBase& matmul(const StdNumber& beta, // applied to 'this'
818  const StdNumber& alpha, const MatrixBase<ELT_A>& A, const MatrixBase<ELT_B>& B)
819  {
820  helper.matmul(beta,alpha,A.helper,B.helper);
821  return *this;
822  }
823 
832  const ELT& getElt(int i, int j) const { return *reinterpret_cast<const ELT*>(helper.getElt(i,j)); }
833  ELT& updElt(int i, int j) { return *reinterpret_cast< ELT*>(helper.updElt(i,j)); }
834 
835  const ELT& operator()(int i, int j) const {return getElt(i,j);}
836  ELT& operator()(int i, int j) {return updElt(i,j);}
837 
842  void getAnyElt(int i, int j, ELT& value) const
843  { helper.getAnyElt(i,j,reinterpret_cast<Scalar*>(&value)); }
844  ELT getAnyElt(int i, int j) const {ELT e; getAnyElt(i,j,e); return e;}
845 
848  // TODO: very slow! Should be optimized at least for the case
849  // where ELT is a Scalar.
851  const int nr=nrow(), nc=ncol();
852  ScalarNormSq sum(0);
853  for(int j=0;j<nc;++j)
854  for (int i=0; i<nr; ++i)
855  sum += CNT<E>::scalarNormSqr((*this)(i,j));
856  return sum;
857  }
858 
862  // TODO: very slow! Should be optimized at least for the case
863  // where ELT is a Scalar.
864  void abs(TAbs& mabs) const {
865  const int nr=nrow(), nc=ncol();
866  mabs.resize(nr,nc);
867  for(int j=0;j<nc;++j)
868  for (int i=0; i<nr; ++i)
869  mabs(i,j) = CNT<E>::abs((*this)(i,j));
870  }
871 
875  TAbs abs() const { TAbs mabs; abs(mabs); return mabs; }
876 
888  const int nr=nrow(), nc=ncol();
889  TStandard mstd(nr, nc);
890  for(int j=0;j<nc;++j)
891  for (int i=0; i<nr; ++i)
892  mstd(i,j) = CNT<E>::standardize((*this)(i,j));
893  return mstd;
894  }
895 
899  ScalarNormSq normSqr() const { return scalarNormSqr(); }
900  // TODO -- not good; unnecessary overflow
901  typename CNT<ScalarNormSq>::TSqrt
903 
907  typename CNT<ScalarNormSq>::TSqrt
908  normRMS() const {
909  if (!CNT<ELT>::IsScalar)
910  SimTK_THROW1(Exception::Cant, "normRMS() only defined for scalar elements");
911  if (nelt() == 0)
912  return typename CNT<ScalarNormSq>::TSqrt(0);
914  }
915 
918  const int cols = ncol();
919  RowVector_<ELT> row(cols);
920  for (int j = 0; j < cols; ++j)
921  helper.colSum(j, reinterpret_cast<Scalar*>(&row[j]));
922  return row;
923  }
925  RowVector_<ELT> sum() const {return colSum();}
926 
929  const int rows = nrow();
930  Vector_<ELT> col(rows);
931  for (int i = 0; i < rows; ++i)
932  helper.rowSum(i, reinterpret_cast<Scalar*>(&col[i]));
933  return col;
934  }
935 
936  //TODO: make unary +/- return a self-view so they won't reallocate?
937  const MatrixBase& operator+() const {return *this; }
938  const TNeg& negate() const {return *reinterpret_cast<const TNeg*>(this); }
939  TNeg& updNegate() {return *reinterpret_cast<TNeg*>(this); }
940 
941  const TNeg& operator-() const {return negate();}
942  TNeg& operator-() {return updNegate();}
943 
944  MatrixBase& negateInPlace() {(*this) *= EPrecision(-1); return *this;}
945 
950  MatrixBase& resize(int m, int n) { helper.resize(m,n); return *this; }
956  MatrixBase& resizeKeep(int m, int n) { helper.resizeKeep(m,n); return *this; }
957 
958  // This prevents shape changes in a Matrix that would otherwise allow it. No harm if is
959  // are called on a Matrix that is locked already; it always succeeds.
960  void lockShape() {helper.lockShape();}
961 
962  // This allows shape changes again for a Matrix which was constructed to allow them
963  // but had them locked with the above routine. No harm if this is called on a Matrix
964  // that is already unlocked, but it is not allowed to call this on a Matrix which
965  // *never* allowed resizing. An exception will be thrown in that case.
966  void unlockShape() {helper.unlockShape();}
967 
968  // An assortment of handy conversions
969  const MatrixView_<ELT>& getAsMatrixView() const { return *reinterpret_cast<const MatrixView_<ELT>*>(this); }
970  MatrixView_<ELT>& updAsMatrixView() { return *reinterpret_cast< MatrixView_<ELT>*>(this); }
971  const Matrix_<ELT>& getAsMatrix() const { return *reinterpret_cast<const Matrix_<ELT>*>(this); }
972  Matrix_<ELT>& updAsMatrix() { return *reinterpret_cast< Matrix_<ELT>*>(this); }
973 
975  { assert(ncol()==1); return *reinterpret_cast<const VectorView_<ELT>*>(this); }
977  { assert(ncol()==1); return *reinterpret_cast< VectorView_<ELT>*>(this); }
978  const Vector_<ELT>& getAsVector() const
979  { assert(ncol()==1); return *reinterpret_cast<const Vector_<ELT>*>(this); }
981  { assert(ncol()==1); return *reinterpret_cast< Vector_<ELT>*>(this); }
983  { assert(ncol()==1); return *reinterpret_cast<const VectorBase<ELT>*>(this); }
985  { assert(ncol()==1); return *reinterpret_cast< VectorBase<ELT>*>(this); }
986 
988  { assert(nrow()==1); return *reinterpret_cast<const RowVectorView_<ELT>*>(this); }
990  { assert(nrow()==1); return *reinterpret_cast< RowVectorView_<ELT>*>(this); }
992  { assert(nrow()==1); return *reinterpret_cast<const RowVector_<ELT>*>(this); }
994  { assert(nrow()==1); return *reinterpret_cast< RowVector_<ELT>*>(this); }
996  { assert(nrow()==1); return *reinterpret_cast<const RowVectorBase<ELT>*>(this); }
998  { assert(nrow()==1); return *reinterpret_cast< RowVectorBase<ELT>*>(this); }
999 
1000  // Access to raw data. We have to return the raw data
1001  // pointer as pointer-to-scalar because we may pack the elements tighter
1002  // than a C++ array would.
1003 
1008 
1012  int getPackedSizeofElement() const {return NScalarsPerElement*sizeof(Scalar);}
1013 
1014  bool hasContiguousData() const {return helper.hasContiguousData();}
1015  ptrdiff_t getContiguousScalarDataLength() const {
1016  return helper.getContiguousDataLength();
1017  }
1019  return helper.getContiguousData();
1020  }
1022  return helper.updContiguousData();
1023  }
1024  void replaceContiguousScalarData(Scalar* newData, ptrdiff_t length, bool takeOwnership) {
1025  helper.replaceContiguousData(newData,length,takeOwnership);
1026  }
1027  void replaceContiguousScalarData(const Scalar* newData, ptrdiff_t length) {
1028  helper.replaceContiguousData(newData,length);
1029  }
1030  void swapOwnedContiguousScalarData(Scalar* newData, ptrdiff_t length, Scalar*& oldData) {
1031  helper.swapOwnedContiguousData(newData,length,oldData);
1032  }
1033 
1038  explicit MatrixBase(MatrixHelperRep<Scalar>* hrep) : helper(hrep) {}
1039 
1040 protected:
1041  const MatrixHelper<Scalar>& getHelper() const {return helper;}
1042  MatrixHelper<Scalar>& updHelper() {return helper;}
1043 
1044 private:
1045  MatrixHelper<Scalar> helper; // this is just one pointer
1046 
1047  template <class EE> friend class MatrixBase;
1048 };
1049 
1050 
1051 
1052 // -------------------------------- VectorBase --------------------------------
1056 // ----------------------------------------------------------------------------
1057 template <class ELT> class VectorBase : public MatrixBase<ELT> {
1058  typedef MatrixBase<ELT> Base;
1059  typedef typename Base::ScalarNormSq ScalarNormSq;
1060  typedef typename Base::EAbs EAbs;
1061  typedef typename CNT<ELT>::Scalar Scalar;
1062  typedef typename CNT<ELT>::Number Number;
1063  typedef typename CNT<ELT>::StdNumber StdNumber;
1064  typedef VectorBase<ELT> T;
1065  typedef VectorBase<typename CNT<ELT>::TAbs> TAbs;
1066  typedef VectorBase<typename CNT<ELT>::TNeg> TNeg;
1068 public:
1069  // ------------------------------------------------------------------------
1079 
1082  explicit VectorBase(int m=0) : Base(MatrixCommitment::Vector(), m, 1) {}
1083 
1087  VectorBase(const VectorBase& source) : Base(source) {}
1088 
1090  VectorBase(const TNeg& source) : Base(source) {}
1091 
1094  VectorBase(int m, const ELT& initialValue)
1095  : Base(MatrixCommitment::Vector(),m,1,initialValue) {}
1096 
1101  VectorBase(int m, const ELT* cppInitialValues)
1102  : Base(MatrixCommitment::Vector(),m,1,cppInitialValues) {}
1104 
1105  // ------------------------------------------------------------------------
1114 
1116  VectorBase(int m, int stride, const Scalar* s)
1117  : Base(MatrixCommitment::Vector(m), MatrixCharacter::Vector(m),stride,s) { }
1119  VectorBase(int m, int stride, Scalar* s)
1120  : Base(MatrixCommitment::Vector(m), MatrixCharacter::Vector(m),stride,s) { }
1122 
1123  // ------------------------------------------------------------------------
1130 
1133  : Base(MatrixCommitment::Vector(), h,s) { }
1136  : Base(MatrixCommitment::Vector(), h,s) { }
1139  : Base(MatrixCommitment::Vector(), h,d) { }
1141 
1142  // This gives the resulting vector type when (v[i] op P) is applied to each element.
1143  // It will have element types which are the regular composite result of ELT op P.
1144  template <class P> struct EltResult {
1149  };
1150 
1154  Base::operator=(b); return *this;
1155  }
1156 
1157  // default destructor
1158 
1159 
1160  VectorBase& operator*=(const StdNumber& t) { Base::operator*=(t); return *this; }
1161  VectorBase& operator/=(const StdNumber& t) { Base::operator/=(t); return *this; }
1162  VectorBase& operator+=(const VectorBase& r) { Base::operator+=(r); return *this; }
1163  VectorBase& operator-=(const VectorBase& r) { Base::operator-=(r); return *this; }
1164 
1165 
1166  template <class EE> VectorBase& operator=(const VectorBase<EE>& b)
1167  { Base::operator=(b); return *this; }
1168  template <class EE> VectorBase& operator+=(const VectorBase<EE>& b)
1169  { Base::operator+=(b); return *this; }
1170  template <class EE> VectorBase& operator-=(const VectorBase<EE>& b)
1171  { Base::operator-=(b); return *this; }
1172 
1173 
1177  VectorBase& operator=(const ELT& t) { Base::setTo(t); return *this; }
1178 
1183  template <class EE> VectorBase& rowScaleInPlace(const VectorBase<EE>& v)
1184  { Base::template rowScaleInPlace<EE>(v); return *this; }
1185  template <class EE> inline void rowScale(const VectorBase<EE>& v, typename EltResult<EE>::Mul& out) const
1186  { Base::rowScale(v,out); }
1187  template <class EE> inline typename EltResult<EE>::Mul rowScale(const VectorBase<EE>& v) const
1188  { typename EltResult<EE>::Mul out(nrow()); Base::rowScale(v,out); return out; }
1189 
1194  typename CNT<ScalarNormSq>::TSqrt
1195  normRMS(int* worstOne=0) const {
1196  if (!CNT<ELT>::IsScalar)
1198  "Vector::normRMS() only defined for scalar elements.");
1199  const int n = nrow();
1200  if (n == 0) {
1201  if (worstOne) *worstOne = -1;
1202  return typename CNT<ScalarNormSq>::TSqrt(0);
1203  }
1204 
1205  ScalarNormSq sumsq = 0;
1206  if (worstOne) {
1207  *worstOne = 0;
1208  ScalarNormSq maxsq = 0;
1209  for (int i=0; i<n; ++i) {
1210  const ScalarNormSq v2 = square((*this)[i]);
1211  if (v2 > maxsq) maxsq=v2, *worstOne=i;
1212  sumsq += v2;
1213  }
1214  } else { // don't track the worst element
1215  for (int i=0; i<n; ++i) {
1216  const ScalarNormSq v2 = square((*this)[i]);
1217  sumsq += v2;
1218  }
1219  }
1220 
1221  return CNT<ScalarNormSq>::sqrt(sumsq/n);
1222  }
1223 
1229  template <class EE>
1230  typename CNT<ScalarNormSq>::TSqrt
1231  weightedNormRMS(const VectorBase<EE>& w, int* worstOne=0) const {
1234  "Vector::weightedNormRMS() only defined for scalar elements"
1235  " and weights.");
1236  const int n = nrow();
1237  assert(w.nrow()==n);
1238  if (n == 0) {
1239  if (worstOne) *worstOne = -1;
1240  return typename CNT<ScalarNormSq>::TSqrt(0);
1241  }
1242 
1243  ScalarNormSq sumsq = 0;
1244  if (worstOne) {
1245  *worstOne = 0;
1246  ScalarNormSq maxsq = 0;
1247  for (int i=0; i<n; ++i) {
1248  const ScalarNormSq wv2 = square(w[i]*(*this)[i]);
1249  if (wv2 > maxsq) maxsq=wv2, *worstOne=i;
1250  sumsq += wv2;
1251  }
1252  } else { // don't track the worst element
1253  for (int i=0; i<n; ++i) {
1254  const ScalarNormSq wv2 = square(w[i]*(*this)[i]);
1255  sumsq += wv2;
1256  }
1257  }
1258 
1259  return CNT<ScalarNormSq>::sqrt(sumsq/n);
1260  }
1261 
1266  EAbs normInf(int* worstOne=0) const {
1267  if (!CNT<ELT>::IsScalar)
1269  "Vector::normInf() only defined for scalar elements.");
1270  const int n = nrow();
1271  if (n == 0) {
1272  if (worstOne) *worstOne = -1;
1273  return EAbs(0);
1274  }
1275 
1276  EAbs maxabs = 0;
1277  if (worstOne) {
1278  *worstOne = 0;
1279  for (int i=0; i<n; ++i) {
1280  const EAbs a = std::abs((*this)[i]);
1281  if (a > maxabs) maxabs=a, *worstOne=i;
1282  }
1283  } else { // don't track the worst element
1284  for (int i=0; i<n; ++i) {
1285  const EAbs a = std::abs((*this)[i]);
1286  if (a > maxabs) maxabs=a;
1287  }
1288  }
1289 
1290  return maxabs;
1291  }
1292 
1298  template <class EE>
1299  EAbs weightedNormInf(const VectorBase<EE>& w, int* worstOne=0) const {
1302  "Vector::weightedNormInf() only defined for scalar elements"
1303  " and weights.");
1304  const int n = nrow();
1305  assert(w.nrow()==n);
1306  if (n == 0) {
1307  if (worstOne) *worstOne = -1;
1308  return EAbs(0);
1309  }
1310 
1311  EAbs maxabs = 0;
1312  if (worstOne) {
1313  *worstOne = 0;
1314  for (int i=0; i<n; ++i) {
1315  const EAbs wv = std::abs(w[i]*(*this)[i]);
1316  if (wv > maxabs) maxabs=wv, *worstOne=i;
1317  }
1318  } else { // don't track the worst element
1319  for (int i=0; i<n; ++i) {
1320  const EAbs wv = std::abs(w[i]*(*this)[i]);
1321  if (wv > maxabs) maxabs=wv;
1322  }
1323  }
1324 
1325  return maxabs;
1326  }
1327 
1331  return *this;
1332  }
1333 
1335  void elementwiseInvert(VectorBase<typename CNT<ELT>::TInvert>& out) const {
1337  }
1338 
1343  return out;
1344  }
1345 
1346  // elementwise multiply
1348  { Base::template elementwiseMultiplyInPlace<EE>(r); return *this; }
1349  template <class EE> inline void elementwiseMultiply(const VectorBase<EE>& v, typename EltResult<EE>::Mul& out) const
1350  { Base::template elementwiseMultiply<EE>(v,out); }
1351  template <class EE> inline typename EltResult<EE>::Mul elementwiseMultiply(const VectorBase<EE>& v) const
1352  { typename EltResult<EE>::Mul out(nrow()); Base::template elementwiseMultiply<EE>(v,out); return out; }
1353 
1354  // elementwise multiply from left
1356  { Base::template elementwiseMultiplyFromLeftInPlace<EE>(r); return *this; }
1357  template <class EE> inline void
1359  const VectorBase<EE>& v,
1360  typename VectorBase<EE>::template EltResult<ELT>::Mul& out) const
1361  {
1362  Base::template elementwiseMultiplyFromLeft<EE>(v,out);
1363  }
1364  template <class EE> inline typename VectorBase<EE>::template EltResult<ELT>::Mul
1366  {
1368  Base::template elementwiseMultiplyFromLeft<EE>(v,out);
1369  return out;
1370  }
1371 
1372  // elementwise divide
1373  template <class EE> VectorBase& elementwiseDivideInPlace(const VectorBase<EE>& r)
1374  { Base::template elementwiseDivideInPlace<EE>(r); return *this; }
1375  template <class EE> inline void elementwiseDivide(const VectorBase<EE>& v, typename EltResult<EE>::Dvd& out) const
1376  { Base::template elementwiseDivide<EE>(v,out); }
1377  template <class EE> inline typename EltResult<EE>::Dvd elementwiseDivide(const VectorBase<EE>& v) const
1378  { typename EltResult<EE>::Dvd out(nrow()); Base::template elementwiseDivide<EE>(v,out); return out; }
1379 
1380  // elementwise divide from left
1382  { Base::template elementwiseDivideFromLeftInPlace<EE>(r); return *this; }
1383  template <class EE> inline void
1385  const VectorBase<EE>& v,
1386  typename VectorBase<EE>::template EltResult<ELT>::Dvd& out) const
1387  {
1388  Base::template elementwiseDivideFromLeft<EE>(v,out);
1389  }
1390  template <class EE> inline typename VectorBase<EE>::template EltResult<ELT>::Dvd
1392  {
1394  Base::template elementwiseDivideFromLeft<EE>(v,out);
1395  return out;
1396  }
1397 
1398  // Implicit conversions are allowed to Vector or Matrix, but not to RowVector.
1399  operator const Vector_<ELT>&() const { return *reinterpret_cast<const Vector_<ELT>*>(this); }
1400  operator Vector_<ELT>&() { return *reinterpret_cast< Vector_<ELT>*>(this); }
1401  operator const VectorView_<ELT>&() const { return *reinterpret_cast<const VectorView_<ELT>*>(this); }
1402  operator VectorView_<ELT>&() { return *reinterpret_cast< VectorView_<ELT>*>(this); }
1403 
1404  operator const Matrix_<ELT>&() const { return *reinterpret_cast<const Matrix_<ELT>*>(this); }
1405  operator Matrix_<ELT>&() { return *reinterpret_cast< Matrix_<ELT>*>(this); }
1406  operator const MatrixView_<ELT>&() const { return *reinterpret_cast<const MatrixView_<ELT>*>(this); }
1407  operator MatrixView_<ELT>&() { return *reinterpret_cast< MatrixView_<ELT>*>(this); }
1408 
1409 
1410  // size() for Vectors is Base::nelt() but returns int instead of ptrdiff_t.
1411  int size() const {
1412  assert(Base::nelt() <= (ptrdiff_t)std::numeric_limits<int>::max());
1413  assert(Base::ncol()==1);
1414  return (int)Base::nelt();
1415  }
1416  int nrow() const {assert(Base::ncol()==1); return Base::nrow();}
1417  int ncol() const {assert(Base::ncol()==1); return Base::ncol();}
1418  ptrdiff_t nelt() const {assert(Base::ncol()==1); return Base::nelt();}
1419 
1420  // Override MatrixBase operators to return the right shape
1421  TAbs abs() const {TAbs result; Base::abs(result); return result;}
1422 
1423  // Override MatrixBase indexing operators
1424  const ELT& operator[](int i) const {return *reinterpret_cast<const ELT*>(Base::getHelper().getElt(i));}
1425  ELT& operator[](int i) {return *reinterpret_cast<ELT*> (Base::updHelper().updElt(i));}
1426  const ELT& operator()(int i) const {return *reinterpret_cast<const ELT*>(Base::getHelper().getElt(i));}
1427  ELT& operator()(int i) {return *reinterpret_cast<ELT*> (Base::updHelper().updElt(i));}
1428 
1429  // Block (contiguous subvector) view creation
1430  VectorView_<ELT> operator()(int i, int m) const {return Base::operator()(i,0,m,1).getAsVectorView();}
1431  VectorView_<ELT> operator()(int i, int m) {return Base::operator()(i,0,m,1).updAsVectorView();}
1432 
1433  // Indexed view creation (arbitrary subvector). Indices must be
1434  // monotonically increasing.
1435  VectorView_<ELT> index(const Array_<int>& indices) const {
1437  Base::getHelper(), indices);
1438  return VectorView_<ELT>(h);
1439  }
1442  Base::updHelper(), indices);
1443  return VectorView_<ELT>(h);
1444  }
1445 
1446  VectorView_<ELT> operator()(const Array_<int>& indices) const {return index(indices);}
1447  VectorView_<ELT> operator()(const Array_<int>& indices) {return updIndex(indices);}
1448 
1449  // Hermitian transpose.
1450  THerm transpose() const {return Base::transpose().getAsRowVectorView();}
1451  THerm updTranspose() {return Base::updTranspose().updAsRowVectorView();}
1452 
1453  THerm operator~() const {return transpose();}
1455 
1456  const VectorBase& operator+() const {return *this; }
1457 
1458  // Negation
1459 
1460  const TNeg& negate() const {return *reinterpret_cast<const TNeg*>(this); }
1461  TNeg& updNegate() {return *reinterpret_cast<TNeg*>(this); }
1462 
1463  const TNeg& operator-() const {return negate();}
1464  TNeg& operator-() {return updNegate();}
1465 
1466  VectorBase& resize(int m) {Base::resize(m,1); return *this;}
1467  VectorBase& resizeKeep(int m) {Base::resizeKeep(m,1); return *this;}
1468 
1469  //TODO: this is not re-locking the number of columns at 1.
1470  void clear() {Base::clear(); Base::resize(0,1);}
1471 
1472  ELT sum() const {ELT s; Base::getHelper().sum(reinterpret_cast<Scalar*>(&s)); return s; } // add all the elements
1474  return VectorIterator<ELT, VectorBase<ELT> >(*this, 0);
1475  }
1477  return VectorIterator<ELT, VectorBase<ELT> >(*this, size());
1478  }
1479 
1480 protected:
1481  // Create a VectorBase handle using a given helper rep.
1482  explicit VectorBase(MatrixHelperRep<Scalar>* hrep) : Base(hrep) {}
1483 
1484 private:
1485  // NO DATA MEMBERS ALLOWED
1486 };
1487 
1488 
1489 
1490 // ------------------------------- RowVectorBase ------------------------------
1494 // ----------------------------------------------------------------------------
1495 template <class ELT> class RowVectorBase : public MatrixBase<ELT> {
1496  typedef MatrixBase<ELT> Base;
1497  typedef typename CNT<ELT>::Scalar Scalar;
1498  typedef typename CNT<ELT>::Number Number;
1499  typedef typename CNT<ELT>::StdNumber StdNumber;
1500  typedef RowVectorBase<ELT> T;
1501  typedef RowVectorBase<typename CNT<ELT>::TAbs> TAbs;
1502  typedef RowVectorBase<typename CNT<ELT>::TNeg> TNeg;
1503  typedef VectorView_<typename CNT<ELT>::THerm> THerm;
1504 public:
1505  // ------------------------------------------------------------------------
1515 
1518  explicit RowVectorBase(int n=0) : Base(MatrixCommitment::RowVector(), 1, n) {}
1519 
1523  RowVectorBase(const RowVectorBase& source) : Base(source) {}
1524 
1526  RowVectorBase(const TNeg& source) : Base(source) {}
1527 
1530  RowVectorBase(int n, const ELT& initialValue)
1531  : Base(MatrixCommitment::RowVector(),1,n,initialValue) {}
1532 
1537  RowVectorBase(int n, const ELT* cppInitialValues)
1538  : Base(MatrixCommitment::RowVector(),1,n,cppInitialValues) {}
1540 
1541  // ------------------------------------------------------------------------
1550 
1552  RowVectorBase(int n, int stride, const Scalar* s)
1553  : Base(MatrixCommitment::RowVector(n), MatrixCharacter::RowVector(n),stride,s) { }
1555  RowVectorBase(int n, int stride, Scalar* s)
1556  : Base(MatrixCommitment::RowVector(n), MatrixCharacter::RowVector(n),stride,s) { }
1558 
1559  // ------------------------------------------------------------------------
1566 
1569  : Base(MatrixCommitment::RowVector(), h,s) { }
1572  : Base(MatrixCommitment::RowVector(), h,s) { }
1575  : Base(MatrixCommitment::RowVector(), h,d) { }
1577 
1578  // This gives the resulting rowvector type when (r(i) op P) is applied to each element.
1579  // It will have element types which are the regular composite result of ELT op P.
1580  template <class P> struct EltResult {
1585  };
1586 
1590  Base::operator=(b); return *this;
1591  }
1592 
1593  // default destructor
1594 
1595  RowVectorBase& operator*=(const StdNumber& t) {Base::operator*=(t); return *this;}
1596  RowVectorBase& operator/=(const StdNumber& t) {Base::operator/=(t); return *this;}
1598  RowVectorBase& operator-=(const RowVectorBase& r) {Base::operator-=(r); return *this;}
1599 
1600  template <class EE> RowVectorBase& operator=(const RowVectorBase<EE>& b)
1601  { Base::operator=(b); return *this; }
1602  template <class EE> RowVectorBase& operator+=(const RowVectorBase<EE>& b)
1603  { Base::operator+=(b); return *this; }
1604  template <class EE> RowVectorBase& operator-=(const RowVectorBase<EE>& b)
1605  { Base::operator-=(b); return *this; }
1606 
1607  // default destructor
1608 
1612  RowVectorBase& operator=(const ELT& t) { Base::setTo(t); return *this; }
1613 
1618  template <class EE> RowVectorBase& colScaleInPlace(const VectorBase<EE>& v)
1619  { Base::template colScaleInPlace<EE>(v); return *this; }
1620  template <class EE> inline void colScale(const VectorBase<EE>& v, typename EltResult<EE>::Mul& out) const
1621  { return Base::template colScale<EE>(v,out); }
1622  template <class EE> inline typename EltResult<EE>::Mul colScale(const VectorBase<EE>& v) const
1623  { typename EltResult<EE>::Mul out(ncol()); Base::template colScale<EE>(v,out); return out; }
1624 
1625 
1626  // elementwise multiply
1628  { Base::template elementwiseMultiplyInPlace<EE>(r); return *this; }
1629  template <class EE> inline void elementwiseMultiply(const RowVectorBase<EE>& v, typename EltResult<EE>::Mul& out) const
1630  { Base::template elementwiseMultiply<EE>(v,out); }
1631  template <class EE> inline typename EltResult<EE>::Mul elementwiseMultiply(const RowVectorBase<EE>& v) const
1632  { typename EltResult<EE>::Mul out(nrow()); Base::template elementwiseMultiply<EE>(v,out); return out; }
1633 
1634  // elementwise multiply from left
1636  { Base::template elementwiseMultiplyFromLeftInPlace<EE>(r); return *this; }
1637  template <class EE> inline void
1639  const RowVectorBase<EE>& v,
1641  {
1642  Base::template elementwiseMultiplyFromLeft<EE>(v,out);
1643  }
1644  template <class EE> inline
1645  typename RowVectorBase<EE>::template EltResult<ELT>::Mul
1648  Base::template elementwiseMultiplyFromLeft<EE>(v,out);
1649  return out;
1650  }
1651 
1652  // elementwise divide
1654  { Base::template elementwiseDivideInPlace<EE>(r); return *this; }
1655  template <class EE> inline void elementwiseDivide(const RowVectorBase<EE>& v, typename EltResult<EE>::Dvd& out) const
1656  { Base::template elementwiseDivide<EE>(v,out); }
1657  template <class EE> inline typename EltResult<EE>::Dvd elementwiseDivide(const RowVectorBase<EE>& v) const
1658  { typename EltResult<EE>::Dvd out(nrow()); Base::template elementwiseDivide<EE>(v,out); return out; }
1659 
1660  // elementwise divide from left
1662  { Base::template elementwiseDivideFromLeftInPlace<EE>(r); return *this; }
1663  template <class EE> inline void
1665  (const RowVectorBase<EE>& v,
1666  typename RowVectorBase<EE>::template EltResult<ELT>::Dvd& out) const {
1667  Base::template elementwiseDivideFromLeft<EE>(v,out);
1668  }
1669  template <class EE> inline
1670  typename RowVectorBase<EE>::template EltResult<ELT>::Dvd
1673  Base::template elementwiseDivideFromLeft<EE>(v,out);
1674  return out;
1675  }
1676 
1677  // Implicit conversions are allowed to RowVector or Matrix, but not to Vector.
1678  operator const RowVector_<ELT>&() const {return *reinterpret_cast<const RowVector_<ELT>*>(this);}
1679  operator RowVector_<ELT>&() {return *reinterpret_cast< RowVector_<ELT>*>(this);}
1680  operator const RowVectorView_<ELT>&() const {return *reinterpret_cast<const RowVectorView_<ELT>*>(this);}
1681  operator RowVectorView_<ELT>&() {return *reinterpret_cast< RowVectorView_<ELT>*>(this);}
1682 
1683  operator const Matrix_<ELT>&() const {return *reinterpret_cast<const Matrix_<ELT>*>(this);}
1684  operator Matrix_<ELT>&() {return *reinterpret_cast< Matrix_<ELT>*>(this);}
1685  operator const MatrixView_<ELT>&() const {return *reinterpret_cast<const MatrixView_<ELT>*>(this);}
1686  operator MatrixView_<ELT>&() {return *reinterpret_cast< MatrixView_<ELT>*>(this);}
1687 
1688 
1689  // size() for RowVectors is Base::nelt() but returns int instead of ptrdiff_t.
1690  int size() const {
1691  assert(Base::nelt() <= (ptrdiff_t)std::numeric_limits<int>::max());
1692  assert(Base::nrow()==1);
1693  return (int)Base::nelt();
1694  }
1695  int nrow() const {assert(Base::nrow()==1); return Base::nrow();}
1696  int ncol() const {assert(Base::nrow()==1); return Base::ncol();}
1697  ptrdiff_t nelt() const {assert(Base::nrow()==1); return Base::nelt();}
1698 
1699  // Override MatrixBase operators to return the right shape
1700  TAbs abs() const {
1701  TAbs result; Base::abs(result); return result;
1702  }
1703 
1704  // Override MatrixBase indexing operators
1705  const ELT& operator[](int j) const {return *reinterpret_cast<const ELT*>(Base::getHelper().getElt(j));}
1706  ELT& operator[](int j) {return *reinterpret_cast<ELT*> (Base::updHelper().updElt(j));}
1707  const ELT& operator()(int j) const {return *reinterpret_cast<const ELT*>(Base::getHelper().getElt(j));}
1708  ELT& operator()(int j) {return *reinterpret_cast<ELT*> (Base::updHelper().updElt(j));}
1709 
1710  // Block (contiguous subvector) creation
1711  RowVectorView_<ELT> operator()(int j, int n) const {return Base::operator()(0,j,1,n).getAsRowVectorView();}
1712  RowVectorView_<ELT> operator()(int j, int n) {return Base::operator()(0,j,1,n).updAsRowVectorView();}
1713 
1714  // Indexed view creation (arbitrary subvector). Indices must be monotonically increasing.
1715  RowVectorView_<ELT> index(const Array_<int>& indices) const {
1717  return RowVectorView_<ELT>(h);
1718  }
1721  return RowVectorView_<ELT>(h);
1722  }
1723 
1724  RowVectorView_<ELT> operator()(const Array_<int>& indices) const {return index(indices);}
1725  RowVectorView_<ELT> operator()(const Array_<int>& indices) {return updIndex(indices);}
1726 
1727  // Hermitian transpose.
1728  THerm transpose() const {return Base::transpose().getAsVectorView();}
1729  THerm updTranspose() {return Base::updTranspose().updAsVectorView();}
1730 
1731  THerm operator~() const {return transpose();}
1733 
1734  const RowVectorBase& operator+() const {return *this; }
1735 
1736  // Negation
1737 
1738  const TNeg& negate() const {return *reinterpret_cast<const TNeg*>(this); }
1739  TNeg& updNegate() {return *reinterpret_cast<TNeg*>(this); }
1740 
1741  const TNeg& operator-() const {return negate();}
1742  TNeg& operator-() {return updNegate();}
1743 
1744  RowVectorBase& resize(int n) {Base::resize(1,n); return *this;}
1745  RowVectorBase& resizeKeep(int n) {Base::resizeKeep(1,n); return *this;}
1746 
1747  //TODO: this is not re-locking the number of rows at 1.
1748  void clear() {Base::clear(); Base::resize(1,0);}
1749 
1750  ELT sum() const {ELT s; Base::getHelper().sum(reinterpret_cast<Scalar*>(&s)); return s; } // add all the elements
1752  return VectorIterator<ELT, RowVectorBase<ELT> >(*this, 0);
1753  }
1755  return VectorIterator<ELT, RowVectorBase<ELT> >(*this, size());
1756  }
1757 
1758 protected:
1759  // Create a RowVectorBase handle using a given helper rep.
1760  explicit RowVectorBase(MatrixHelperRep<Scalar>* hrep) : Base(hrep) {}
1761 
1762 private:
1763  // NO DATA MEMBERS ALLOWED
1764 };
1765 
1766 
1767 
1768 // ------------------------------- MatrixView_ --------------------------------
1774 // ----------------------------------------------------------------------------
1775 template <class ELT> class MatrixView_ : public MatrixBase<ELT> {
1776  typedef MatrixBase<ELT> Base;
1777  typedef typename CNT<ELT>::Scalar S;
1778  typedef typename CNT<ELT>::StdNumber StdNumber;
1779 public:
1780  // Default construction is suppressed.
1781  // Uses default destructor.
1782 
1783  // Create a MatrixView_ handle using a given helper rep.
1784  explicit MatrixView_(MatrixHelperRep<S>* hrep) : Base(hrep) {}
1785 
1786  // Copy constructor is shallow. CAUTION: despite const argument, this preserves writability
1787  // if it was present in the source. This is necessary to allow temporary views to be
1788  // created and used as lvalues.
1790  : Base(MatrixCommitment(),
1791  const_cast<MatrixHelper<S>&>(m.getHelper()),
1792  typename MatrixHelper<S>::ShallowCopy()) {}
1793 
1794  // Copy assignment is deep but not reallocating.
1796  Base::operator=(m); return *this;
1797  }
1798 
1799  // Copy construction and copy assignment from a DeadMatrixView steals the helper.
1802 
1803  // Ask for shallow copy
1804  MatrixView_(const MatrixHelper<S>& h) : Base(MatrixCommitment(), h, typename MatrixHelper<S>::ShallowCopy()) { }
1805  MatrixView_(MatrixHelper<S>& h) : Base(MatrixCommitment(), h, typename MatrixHelper<S>::ShallowCopy()) { }
1806 
1807  MatrixView_& operator=(const Matrix_<ELT>& v) { Base::operator=(v); return *this; }
1808  MatrixView_& operator=(const ELT& e) { Base::operator=(e); return *this; }
1809 
1810  template <class EE> MatrixView_& operator=(const MatrixBase<EE>& m)
1811  { Base::operator=(m); return *this; }
1812  template <class EE> MatrixView_& operator+=(const MatrixBase<EE>& m)
1813  { Base::operator+=(m); return *this; }
1814  template <class EE> MatrixView_& operator-=(const MatrixBase<EE>& m)
1815  { Base::operator-=(m); return *this; }
1816 
1817  MatrixView_& operator*=(const StdNumber& t) { Base::operator*=(t); return *this; }
1818  MatrixView_& operator/=(const StdNumber& t) { Base::operator/=(t); return *this; }
1819  MatrixView_& operator+=(const ELT& r) { this->updDiag() += r; return *this; }
1820  MatrixView_& operator-=(const ELT& r) { this->updDiag() -= r; return *this; }
1821 
1822  operator const Matrix_<ELT>&() const { return *reinterpret_cast<const Matrix_<ELT>*>(this); }
1823  operator Matrix_<ELT>&() { return *reinterpret_cast<Matrix_<ELT>*>(this); }
1824 
1825 private:
1826  // NO DATA MEMBERS ALLOWED
1827  MatrixView_(); // default constructor suppressed (what's it a view of?)
1828 };
1829 
1830 
1831 
1832 // ----------------------------- DeadMatrixView_ ------------------------------
1836 // ----------------------------------------------------------------------------
1837 template <class ELT> class DeadMatrixView_ : public MatrixView_<ELT> {
1838  typedef MatrixView_<ELT> Base;
1839  typedef typename CNT<ELT>::Scalar S;
1840  typedef typename CNT<ELT>::StdNumber StdNumber;
1841 public:
1842  // Default construction is suppressed.
1843  // Uses default destructor.
1844 
1845  // All functionality is passed through to MatrixView_.
1846  explicit DeadMatrixView_(MatrixHelperRep<S>* hrep) : Base(hrep) {}
1847  DeadMatrixView_(const Base& m) : Base(m) {}
1849  Base::operator=(m); return *this;
1850  }
1851 
1852  // Ask for shallow copy
1855 
1856  DeadMatrixView_& operator=(const Matrix_<ELT>& v) { Base::operator=(v); return *this; }
1857  DeadMatrixView_& operator=(const ELT& e) { Base::operator=(e); return *this; }
1858 
1859  template <class EE> DeadMatrixView_& operator=(const MatrixBase<EE>& m)
1860  { Base::operator=(m); return *this; }
1861  template <class EE> DeadMatrixView_& operator+=(const MatrixBase<EE>& m)
1862  { Base::operator+=(m); return *this; }
1863  template <class EE> DeadMatrixView_& operator-=(const MatrixBase<EE>& m)
1864  { Base::operator-=(m); return *this; }
1865 
1866  DeadMatrixView_& operator*=(const StdNumber& t) { Base::operator*=(t); return *this; }
1867  DeadMatrixView_& operator/=(const StdNumber& t) { Base::operator/=(t); return *this; }
1868  DeadMatrixView_& operator+=(const ELT& r) { this->updDiag() += r; return *this; }
1869  DeadMatrixView_& operator-=(const ELT& r) { this->updDiag() -= r; return *this; }
1870 
1871 private:
1872  // NO DATA MEMBERS ALLOWED
1873  DeadMatrixView_(); // default constructor suppressed (what's it a view of?)
1874 };
1875 
1876 template <class ELT> inline
1878 : Base(dead.updHelper().stealRep()) {}
1879 
1880 template <class ELT> inline MatrixView_<ELT>&
1882  if (Base::getHelper().getCharacterCommitment().isSatisfiedBy(dead.getMatrixCharacter()))
1883  Base::updHelper().replaceRep(dead.updHelper().stealRep());
1884  else
1885  Base::operator=(dead);
1886  return *this;
1887 }
1888 
1889 
1890 // ---------------------------------- Matrix_ ---------------------------------
1893 // ----------------------------------------------------------------------------
1894 template <class ELT> class Matrix_ : public MatrixBase<ELT> {
1895  typedef typename CNT<ELT>::Scalar S;
1896  typedef typename CNT<ELT>::Number Number;
1897  typedef typename CNT<ELT>::StdNumber StdNumber;
1898 
1899  typedef typename CNT<ELT>::TNeg ENeg;
1900  typedef typename CNT<ELT>::THerm EHerm;
1901 
1902  typedef MatrixBase<ELT> Base;
1903  typedef MatrixBase<ENeg> BaseNeg;
1904  typedef MatrixBase<EHerm> BaseHerm;
1905 
1906  typedef Matrix_<ELT> T;
1907  typedef MatrixView_<ELT> TView;
1908  typedef Matrix_<ENeg> TNeg;
1909 
1910 public:
1911  Matrix_() : Base() { }
1912  Matrix_(const MatrixCommitment& mc) : Base(mc) {}
1913 
1914  // Copy constructor is deep.
1915  Matrix_(const Matrix_& src) : Base(src) { }
1916 
1917  // Assignment is a deep copy and will also allow reallocation if this Matrix
1918  // doesn't have a view.
1919  Matrix_& operator=(const Matrix_& src) {
1920  Base::operator=(src); return *this;
1921  }
1922 
1923  // Force a deep copy of the view or whatever this is.
1924  // Note that this is an implicit conversion.
1925  Matrix_(const Base& v) : Base(v) {} // e.g., MatrixView
1926 
1927  // Allow implicit conversion from a source matrix that
1928  // has a negated version of ELT.
1929  Matrix_(const BaseNeg& v) : Base(v) {}
1930 
1931  // TODO: implicit conversion from conjugate. This is trickier
1932  // since real elements are their own conjugate so you'll get
1933  // duplicate methods defined from Matrix_(BaseHerm) and Matrix_(Base).
1934 
1935  Matrix_(int m, int n) : Base(MatrixCommitment(), m, n) {}
1936 
1937  Matrix_(int m, int n, const ELT* cppInitialValuesByRow)
1938  : Base(MatrixCommitment(), m, n, cppInitialValuesByRow) {}
1939  Matrix_(int m, int n, const ELT& initialValue)
1940  : Base(MatrixCommitment(), m, n, initialValue) {}
1941 
1942  Matrix_(int m, int n, int leadingDim, const S* data) // read only
1943  : Base(MatrixCommitment(), MatrixCharacter::LapackFull(m,n),
1944  leadingDim, data) {}
1945  Matrix_(int m, int n, int leadingDim, S* data) // writable
1946  : Base(MatrixCommitment(), MatrixCharacter::LapackFull(m,n),
1947  leadingDim, data) {}
1948 
1950  template <int M, int N, int CS, int RS>
1951  explicit Matrix_(const Mat<M,N,ELT,CS,RS>& mat)
1952  : Base(MatrixCommitment(), M, N)
1953  { for (int i = 0; i < M; ++i)
1954  for (int j = 0; j < N; ++j)
1955  this->updElt(i, j) = mat(i, j); }
1956 
1957  Matrix_& operator=(const ELT& v) { Base::operator=(v); return *this; }
1958 
1959  template <class EE> Matrix_& operator=(const MatrixBase<EE>& m)
1960  { Base::operator=(m); return*this; }
1961  template <class EE> Matrix_& operator+=(const MatrixBase<EE>& m)
1962  { Base::operator+=(m); return*this; }
1963  template <class EE> Matrix_& operator-=(const MatrixBase<EE>& m)
1964  { Base::operator-=(m); return*this; }
1965 
1966  Matrix_& operator*=(const StdNumber& t) { Base::operator*=(t); return *this; }
1967  Matrix_& operator/=(const StdNumber& t) { Base::operator/=(t); return *this; }
1968  Matrix_& operator+=(const ELT& r) { this->updDiag() += r; return *this; }
1969  Matrix_& operator-=(const ELT& r) { this->updDiag() -= r; return *this; }
1970 
1971  const TNeg& negate() const {return *reinterpret_cast<const TNeg*>(this); }
1972  TNeg& updNegate() {return *reinterpret_cast<TNeg*>(this); }
1973 
1974  const TNeg& operator-() const {return negate();}
1975  TNeg& operator-() {return updNegate();}
1976 
1977  // Functions to be used for Scripting in MATLAB and languages that do not support operator overloading
1979  std::string toString() const {
1980  std::stringstream stream;
1981  stream << (*this) ;
1982  return stream.str();
1983  }
1985  const ELT& get(int i,int j) const { return this->getElt(i,j); }
1987  void set(int i,int j, const ELT& value) { this->updElt(i,j)=value; }
1988 
1989 private:
1990  // NO DATA MEMBERS ALLOWED
1991 };
1992 
1993 
1994 
1995 // -------------------------------- VectorView_ -------------------------------
2001 // ----------------------------------------------------------------------------
2002 template <class ELT> class VectorView_ : public VectorBase<ELT> {
2003  typedef VectorBase<ELT> Base;
2004  typedef typename CNT<ELT>::Scalar S;
2005  typedef typename CNT<ELT>::Number Number;
2006  typedef typename CNT<ELT>::StdNumber StdNumber;
2007  typedef VectorView_<ELT> T;
2008  typedef VectorView_< typename CNT<ELT>::TNeg > TNeg;
2009  typedef RowVectorView_< typename CNT<ELT>::THerm > THerm;
2010 public:
2011  // Default construction is suppressed.
2012  // Uses default destructor.
2013 
2014  // Create a VectorView_ handle using a given helper rep.
2015  explicit VectorView_(MatrixHelperRep<S>* hrep) : Base(hrep) {}
2016 
2017  // Copy constructor is shallow. CAUTION: despite const argument, this preserves writability
2018  // if it was present in the source. This is necessary to allow temporary views to be
2019  // created and used as lvalues.
2021  : Base(const_cast<MatrixHelper<S>&>(v.getHelper()), typename MatrixHelper<S>::ShallowCopy()) { }
2022 
2023  // Copy assignment is deep but not reallocating.
2025  Base::operator=(v); return *this;
2026  }
2027 
2028  // Ask for shallow copy
2029  explicit VectorView_(const MatrixHelper<S>& h) : Base(h, typename MatrixHelper<S>::ShallowCopy()) { }
2030  explicit VectorView_(MatrixHelper<S>& h) : Base(h, typename MatrixHelper<S>::ShallowCopy()) { }
2031 
2032  VectorView_& operator=(const Base& b) { Base::operator=(b); return *this; }
2033 
2034  VectorView_& operator=(const ELT& v) { Base::operator=(v); return *this; }
2035 
2036  template <class EE> VectorView_& operator=(const VectorBase<EE>& m)
2037  { Base::operator=(m); return*this; }
2038  template <class EE> VectorView_& operator+=(const VectorBase<EE>& m)
2039  { Base::operator+=(m); return*this; }
2040  template <class EE> VectorView_& operator-=(const VectorBase<EE>& m)
2041  { Base::operator-=(m); return*this; }
2042 
2043  VectorView_& operator*=(const StdNumber& t) { Base::operator*=(t); return *this; }
2044  VectorView_& operator/=(const StdNumber& t) { Base::operator/=(t); return *this; }
2045  VectorView_& operator+=(const ELT& b) { this->elementwiseAddScalarInPlace(b); return *this; }
2046  VectorView_& operator-=(const ELT& b) { this->elementwiseSubtractScalarInPlace(b); return *this; }
2047 
2048 private:
2049  // NO DATA MEMBERS ALLOWED
2050  VectorView_(); // default construction suppressed (what's it a View of?)
2051 };
2052 
2053 
2054 
2055 // ---------------------------------- Vector_ ---------------------------------
2059 // ----------------------------------------------------------------------------
2060 template <class ELT> class Vector_ : public VectorBase<ELT> {
2061  typedef typename CNT<ELT>::Scalar S;
2062  typedef typename CNT<ELT>::Number Number;
2063  typedef typename CNT<ELT>::StdNumber StdNumber;
2064  typedef typename CNT<ELT>::TNeg ENeg;
2065  typedef VectorBase<ELT> Base;
2066  typedef VectorBase<ENeg> BaseNeg;
2067 public:
2068  Vector_() : Base() {} // 0x1 reallocatable
2069  // Uses default destructor.
2070 
2071  // Copy constructor is deep.
2072  Vector_(const Vector_& src) : Base(src) {}
2073 
2074  // Implicit conversions.
2075  Vector_(const Base& src) : Base(src) {} // e.g., VectorView
2076  Vector_(const BaseNeg& src) : Base(src) {}
2077 
2078  // Copy assignment is deep and can be reallocating if this Vector
2079  // has no View.
2080  Vector_& operator=(const Vector_& src) {
2081  Base::operator=(src); return*this;
2082  }
2083 
2084 
2085  explicit Vector_(int m) : Base(m) { }
2086  Vector_(int m, const ELT* cppInitialValues) : Base(m, cppInitialValues) {}
2087  Vector_(int m, const ELT& initialValue) : Base(m, initialValue) {}
2088 
2093  Vector_(int m, const S* cppData, bool): Base(m, Base::CppNScalarsPerElement, cppData) {}
2094  Vector_(int m, S* cppData, bool): Base(m, Base::CppNScalarsPerElement, cppData) {}
2095 
2099  Vector_(int m, int stride, const S* data, bool) : Base(m, stride, data) {}
2100  Vector_(int m, int stride, S* data, bool) : Base(m, stride, data) {}
2101 
2103  template <int M>
2104  explicit Vector_(const Vec<M,ELT>& v) : Base(M) {
2105  for (int i = 0; i < M; ++i)
2106  this->updElt(i, 0) = v(i);
2107  }
2108 
2109  Vector_& operator=(const ELT& v) { Base::operator=(v); return *this; }
2110 
2111  template <class EE> Vector_& operator=(const VectorBase<EE>& m)
2112  { Base::operator=(m); return*this; }
2113  template <class EE> Vector_& operator+=(const VectorBase<EE>& m)
2114  { Base::operator+=(m); return*this; }
2115  template <class EE> Vector_& operator-=(const VectorBase<EE>& m)
2116  { Base::operator-=(m); return*this; }
2117 
2118  Vector_& operator*=(const StdNumber& t) { Base::operator*=(t); return *this; }
2119  Vector_& operator/=(const StdNumber& t) { Base::operator/=(t); return *this; }
2120  Vector_& operator+=(const ELT& b) { this->elementwiseAddScalarInPlace(b); return *this; }
2121  Vector_& operator-=(const ELT& b) { this->elementwiseSubtractScalarInPlace(b); return *this; }
2122 
2123  // Functions to be used for Scripting in MATLAB and languages that do not support operator overloading
2125  std::string toString() const {
2126  std::stringstream stream;
2127  stream << (*this) ;
2128  return stream.str();
2129  }
2131  const ELT& get(int i) const { return (*this)[i]; }
2133  void set (int i, const ELT& value) { (*this)[i]=value; }
2134 
2135 private:
2136  // NO DATA MEMBERS ALLOWED
2137 };
2138 
2139 
2140 
2141 // ------------------------------ RowVectorView_ ------------------------------
2147 // ----------------------------------------------------------------------------
2148 template <class ELT> class RowVectorView_ : public RowVectorBase<ELT> {
2149  typedef RowVectorBase<ELT> Base;
2150  typedef typename CNT<ELT>::Scalar S;
2151  typedef typename CNT<ELT>::Number Number;
2152  typedef typename CNT<ELT>::StdNumber StdNumber;
2153  typedef RowVectorView_<ELT> T;
2154  typedef RowVectorView_< typename CNT<ELT>::TNeg > TNeg;
2155  typedef VectorView_< typename CNT<ELT>::THerm > THerm;
2156 public:
2157  // Default construction is suppressed.
2158  // Uses default destructor.
2159 
2160  // Create a RowVectorView_ handle using a given helper rep.
2161  explicit RowVectorView_(MatrixHelperRep<S>* hrep) : Base(hrep) {}
2162 
2163  // Copy constructor is shallow. CAUTION: despite const argument, this preserves writability
2164  // if it was present in the source. This is necessary to allow temporary views to be
2165  // created and used as lvalues.
2167  : Base(const_cast<MatrixHelper<S>&>(r.getHelper()), typename MatrixHelper<S>::ShallowCopy()) { }
2168 
2169  // Copy assignment is deep but not reallocating.
2171  Base::operator=(r); return *this;
2172  }
2173 
2174  // Ask for shallow copy
2175  explicit RowVectorView_(const MatrixHelper<S>& h) : Base(h, typename MatrixHelper<S>::ShallowCopy()) { }
2176  explicit RowVectorView_(MatrixHelper<S>& h) : Base(h, typename MatrixHelper<S>::ShallowCopy()) { }
2177 
2178  RowVectorView_& operator=(const Base& b) { Base::operator=(b); return *this; }
2179 
2180  RowVectorView_& operator=(const ELT& v) { Base::operator=(v); return *this; }
2181 
2182  template <class EE> RowVectorView_& operator=(const RowVectorBase<EE>& m)
2183  { Base::operator=(m); return*this; }
2184  template <class EE> RowVectorView_& operator+=(const RowVectorBase<EE>& m)
2185  { Base::operator+=(m); return*this; }
2186  template <class EE> RowVectorView_& operator-=(const RowVectorBase<EE>& m)
2187  { Base::operator-=(m); return*this; }
2188 
2189  RowVectorView_& operator*=(const StdNumber& t) { Base::operator*=(t); return *this; }
2190  RowVectorView_& operator/=(const StdNumber& t) { Base::operator/=(t); return *this; }
2191  RowVectorView_& operator+=(const ELT& b) { this->elementwiseAddScalarInPlace(b); return *this; }
2192  RowVectorView_& operator-=(const ELT& b) { this->elementwiseSubtractScalarInPlace(b); return *this; }
2193 
2194 private:
2195  // NO DATA MEMBERS ALLOWED
2196  RowVectorView_(); // default construction suppressed (what is it a view of?)
2197 };
2198 
2199 
2200 
2201 // -------------------------------- RowVector_ --------------------------------
2206 // ----------------------------------------------------------------------------
2207 template <class ELT> class RowVector_ : public RowVectorBase<ELT> {
2208  typedef typename CNT<ELT>::Scalar S;
2209  typedef typename CNT<ELT>::Number Number;
2210  typedef typename CNT<ELT>::StdNumber StdNumber;
2211  typedef typename CNT<ELT>::TNeg ENeg;
2212 
2213  typedef RowVectorBase<ELT> Base;
2214  typedef RowVectorBase<ENeg> BaseNeg;
2215 public:
2216  RowVector_() : Base() {} // 1x0 reallocatable
2217  // Uses default destructor.
2218 
2219  // Copy constructor is deep.
2220  RowVector_(const RowVector_& src) : Base(src) {}
2221 
2222  // Implicit conversions.
2223  RowVector_(const Base& src) : Base(src) {} // e.g., RowVectorView
2224  RowVector_(const BaseNeg& src) : Base(src) {}
2225 
2226  // Copy assignment is deep and can be reallocating if this RowVector
2227  // has no View.
2229  Base::operator=(src); return*this;
2230  }
2231 
2232 
2233  explicit RowVector_(int n) : Base(n) { }
2234  RowVector_(int n, const ELT* cppInitialValues) : Base(n, cppInitialValues) {}
2235  RowVector_(int n, const ELT& initialValue) : Base(n, initialValue) {}
2236 
2241  RowVector_(int n, const S* cppData, bool): Base(n, Base::CppNScalarsPerElement, cppData) {}
2242  RowVector_(int n, S* cppData, bool): Base(n, Base::CppNScalarsPerElement, cppData) {}
2243 
2247  RowVector_(int n, int stride, const S* data, bool) : Base(n, stride, data) {}
2248  RowVector_(int n, int stride, S* data, bool) : Base(n, stride, data) {}
2249 
2251  template <int M>
2252  explicit RowVector_(const Row<M,ELT>& v) : Base(M) {
2253  for (int i = 0; i < M; ++i)
2254  this->updElt(0, i) = v(i);
2255  }
2256 
2257  RowVector_& operator=(const ELT& v) { Base::operator=(v); return *this; }
2258 
2259  template <class EE> RowVector_& operator=(const RowVectorBase<EE>& b)
2260  { Base::operator=(b); return*this; }
2261  template <class EE> RowVector_& operator+=(const RowVectorBase<EE>& b)
2262  { Base::operator+=(b); return*this; }
2263  template <class EE> RowVector_& operator-=(const RowVectorBase<EE>& b)
2264  { Base::operator-=(b); return*this; }
2265 
2266  RowVector_& operator*=(const StdNumber& t) { Base::operator*=(t); return *this; }
2267  RowVector_& operator/=(const StdNumber& t) { Base::operator/=(t); return *this; }
2268  RowVector_& operator+=(const ELT& b) { this->elementwiseAddScalarInPlace(b); return *this; }
2269  RowVector_& operator-=(const ELT& b) { this->elementwiseSubtractScalarInPlace(b); return *this; }
2270 
2271 private:
2272  // NO DATA MEMBERS ALLOWED
2273 };
2274 
2275 
2276 
2277 // ------------------------ MatrixBase definitions ----------------------------
2278 
2279 template <class ELT> inline MatrixView_<ELT>
2280 MatrixBase<ELT>::block(int i, int j, int m, int n) const {
2281  SimTK_INDEXCHECK(i,nrow()+1,"MatrixBase::block()");
2282  SimTK_INDEXCHECK(j,ncol()+1,"MatrixBase::block()");
2283  SimTK_SIZECHECK(i+m,nrow(),"MatrixBase::block()");
2284  SimTK_SIZECHECK(j+n,ncol(),"MatrixBase::block()");
2285 
2286  MatrixHelper<Scalar> h(MatrixCommitment(),helper,i,j,m,n);
2287  return MatrixView_<ELT>(h.stealRep());
2288 }
2289 
2290 template <class ELT> inline MatrixView_<ELT>
2291 MatrixBase<ELT>::updBlock(int i, int j, int m, int n) {
2292  SimTK_INDEXCHECK(i,nrow()+1,"MatrixBase::updBlock()");
2293  SimTK_INDEXCHECK(j,ncol()+1,"MatrixBase::updBlock()");
2294  SimTK_SIZECHECK(i+m,nrow(),"MatrixBase::updBlock()");
2295  SimTK_SIZECHECK(j+n,ncol(),"MatrixBase::updBlock()");
2296 
2297  MatrixHelper<Scalar> h(MatrixCommitment(),helper,i,j,m,n);
2298  return MatrixView_<ELT>(h.stealRep());
2299 }
2300 
2301 template <class E> inline MatrixView_<typename CNT<E>::THerm>
2304  h(MatrixCommitment(),
2305  helper, typename MatrixHelper<typename CNT<Scalar>::THerm>::TransposeView());
2307 }
2308 
2309 template <class E> inline MatrixView_<typename CNT<E>::THerm>
2312  h(MatrixCommitment(),
2313  helper, typename MatrixHelper<typename CNT<Scalar>::THerm>::TransposeView());
2315 }
2316 
2317 template <class E> inline VectorView_<E>
2320  helper, typename MatrixHelper<Scalar>::DiagonalView());
2321  return VectorView_<E>(h.stealRep());
2322 }
2323 
2324 template <class E> inline VectorView_<E>
2327  helper, typename MatrixHelper<Scalar>::DiagonalView());
2328  return VectorView_<E>(h.stealRep());
2329 }
2330 
2331 template <class ELT> inline VectorView_<ELT>
2332 MatrixBase<ELT>::col(int j) const {
2333  SimTK_INDEXCHECK(j,ncol(),"MatrixBase::col()");
2334 
2336  helper,0,j,nrow(),1);
2337  return VectorView_<ELT>(h.stealRep());
2338 }
2339 
2340 template <class ELT> inline VectorView_<ELT>
2342  SimTK_INDEXCHECK(j,ncol(),"MatrixBase::updCol()");
2343 
2345  helper,0,j,nrow(),1);
2346  return VectorView_<ELT>(h.stealRep());
2347 }
2348 
2349 template <class ELT> inline RowVectorView_<ELT>
2350 MatrixBase<ELT>::row(int i) const {
2351  SimTK_INDEXCHECK(i,nrow(),"MatrixBase::row()");
2352 
2354  helper,i,0,1,ncol());
2355  return RowVectorView_<ELT>(h.stealRep());
2356 }
2357 
2358 template <class ELT> inline RowVectorView_<ELT>
2360  SimTK_INDEXCHECK(i,nrow(),"MatrixBase::updRow()");
2361 
2363  helper,i,0,1,ncol());
2364  return RowVectorView_<ELT>(h.stealRep());
2365 }
2366 
2367 // M = diag(v) * M; v must have nrow() elements.
2368 // That is, M[i] *= v[i].
2369 template <class ELT> template <class EE> inline MatrixBase<ELT>&
2371  assert(v.nrow() == nrow());
2372  for (int i=0; i < nrow(); ++i)
2373  (*this)[i] *= v[i];
2374  return *this;
2375 }
2376 
2377 template <class ELT> template <class EE> inline void
2379  assert(v.nrow() == nrow());
2380  out.resize(nrow(), ncol());
2381  for (int j=0; j<ncol(); ++j)
2382  for (int i=0; i<nrow(); ++i)
2383  out(i,j) = (*this)(i,j) * v[i];
2384 }
2385 
2386 // M = M * diag(v); v must have ncol() elements
2387 // That is, M(i) *= v[i]
2388 template <class ELT> template <class EE> inline MatrixBase<ELT>&
2390  assert(v.nrow() == ncol());
2391  for (int j=0; j < ncol(); ++j)
2392  (*this)(j) *= v[j];
2393  return *this;
2394 }
2395 
2396 template <class ELT> template <class EE> inline void
2398  assert(v.nrow() == ncol());
2399  out.resize(nrow(), ncol());
2400  for (int j=0; j<ncol(); ++j)
2401  for (int i=0; i<nrow(); ++i)
2402  out(i,j) = (*this)(i,j) * v[j];
2403 }
2404 
2405 
2406 // M(i,j) *= r[i]*c[j]; r must have nrow() elements; c must have ncol() elements
2407 template <class ELT> template <class ER, class EC> inline MatrixBase<ELT>&
2409  assert(r.nrow()==nrow() && c.nrow()==ncol());
2410  for (int j=0; j<ncol(); ++j)
2411  for (int i=0; i<nrow(); ++i)
2412  (*this)(i,j) *= (r[i]*c[j]);
2413  return *this;
2414 }
2415 
2416 template <class ELT> template <class ER, class EC> inline void
2418  const VectorBase<ER>& r,
2419  const VectorBase<EC>& c,
2420  typename EltResult<typename VectorBase<ER>::template EltResult<EC>::Mul>::Mul&
2421  out) const
2422 {
2423  assert(r.nrow()==nrow() && c.nrow()==ncol());
2424  out.resize(nrow(), ncol());
2425  for (int j=0; j<ncol(); ++j)
2426  for (int i=0; i<nrow(); ++i)
2427  out(i,j) = (*this)(i,j) * (r[i]*c[j]);
2428 }
2429 
2430 // M(i,j) = s
2431 template <class ELT> template <class S> inline MatrixBase<ELT>&
2433  for (int j=0; j<ncol(); ++j)
2434  for (int i=0; i<nrow(); ++i)
2435  (*this)(i,j) = s;
2436  return *this;
2437 }
2438 
2439 // Set M(i,j) = M(i,j)^-1.
2440 template <class ELT> inline MatrixBase<ELT>&
2442  const int nr=nrow(), nc=ncol();
2443  for (int j=0; j<nc; ++j)
2444  for (int i=0; i<nr; ++i) {
2445  ELT& e = updElt(i,j);
2446  e = CNT<ELT>::invert(e);
2447  }
2448  return *this;
2449 }
2450 
2451 template <class ELT> inline void
2453  const int nr=nrow(), nc=ncol();
2454  out.resize(nr,nc);
2455  for (int j=0; j<nc; ++j)
2456  for (int i=0; i<nr; ++i)
2457  out(i,j) = CNT<ELT>::invert((*this)(i,j));
2458 }
2459 
2460 // M(i,j) += s
2461 template <class ELT> template <class S> inline MatrixBase<ELT>&
2463  for (int j=0; j<ncol(); ++j)
2464  for (int i=0; i<nrow(); ++i)
2465  (*this)(i,j) += s;
2466  return *this;
2467 }
2468 
2469 template <class ELT> template <class S> inline void
2471  const S& s,
2472  typename MatrixBase<ELT>::template EltResult<S>::Add& out) const
2473 {
2474  const int nr=nrow(), nc=ncol();
2475  out.resize(nr,nc);
2476  for (int j=0; j<nc; ++j)
2477  for (int i=0; i<nr; ++i)
2478  out(i,j) = (*this)(i,j) + s;
2479 }
2480 
2481 // M(i,j) -= s
2482 template <class ELT> template <class S> inline MatrixBase<ELT>&
2484  for (int j=0; j<ncol(); ++j)
2485  for (int i=0; i<nrow(); ++i)
2486  (*this)(i,j) -= s;
2487  return *this;
2488 }
2489 
2490 template <class ELT> template <class S> inline void
2492  const S& s,
2493  typename MatrixBase<ELT>::template EltResult<S>::Sub& out) const
2494 {
2495  const int nr=nrow(), nc=ncol();
2496  out.resize(nr,nc);
2497  for (int j=0; j<nc; ++j)
2498  for (int i=0; i<nr; ++i)
2499  out(i,j) = (*this)(i,j) - s;
2500 }
2501 
2502 // M(i,j) = s - M(i,j)
2503 template <class ELT> template <class S> inline MatrixBase<ELT>&
2505  const int nr=nrow(), nc=ncol();
2506  for (int j=0; j<nc; ++j)
2507  for (int i=0; i<nr; ++i) {
2508  ELT& e = updElt(i,j);
2509  e = s - e;
2510  }
2511  return *this;
2512 }
2513 
2514 template <class ELT> template <class S> inline void
2516  const S& s,
2517  typename MatrixBase<S>::template EltResult<ELT>::Sub& out) const
2518 {
2519  const int nr=nrow(), nc=ncol();
2520  out.resize(nr,nc);
2521  for (int j=0; j<nc; ++j)
2522  for (int i=0; i<nr; ++i)
2523  out(i,j) = s - (*this)(i,j);
2524 }
2525 
2526 // M(i,j) *= R(i,j); R must have same dimensions as this
2527 template <class ELT> template <class EE> inline MatrixBase<ELT>&
2529  const int nr=nrow(), nc=ncol();
2530  assert(r.nrow()==nr && r.ncol()==nc);
2531  for (int j=0; j<nc; ++j)
2532  for (int i=0; i<nr; ++i)
2533  (*this)(i,j) *= r(i,j);
2534  return *this;
2535 }
2536 
2537 template <class ELT> template <class EE> inline void
2539  const MatrixBase<EE>& r,
2540  typename MatrixBase<ELT>::template EltResult<EE>::Mul& out) const
2541 {
2542  const int nr=nrow(), nc=ncol();
2543  assert(r.nrow()==nr && r.ncol()==nc);
2544  out.resize(nr,nc);
2545  for (int j=0; j<nc; ++j)
2546  for (int i=0; i<nr; ++i)
2547  out(i,j) = (*this)(i,j) * r(i,j);
2548 }
2549 
2550 // M(i,j) = R(i,j) * M(i,j); R must have same dimensions as this
2551 template <class ELT> template <class EE> inline MatrixBase<ELT>&
2553  const int nr=nrow(), nc=ncol();
2554  assert(r.nrow()==nr && r.ncol()==nc);
2555  for (int j=0; j<nc; ++j)
2556  for (int i=0; i<nr; ++i) {
2557  ELT& e = updElt(i,j);
2558  e = r(i,j) * e;
2559  }
2560  return *this;
2561 }
2562 
2563 template <class ELT> template <class EE> inline void
2565  const MatrixBase<EE>& r,
2566  typename MatrixBase<EE>::template EltResult<ELT>::Mul& out) const
2567 {
2568  const int nr=nrow(), nc=ncol();
2569  assert(r.nrow()==nr && r.ncol()==nc);
2570  out.resize(nr,nc);
2571  for (int j=0; j<nc; ++j)
2572  for (int i=0; i<nr; ++i)
2573  out(i,j) = r(i,j) * (*this)(i,j);
2574 }
2575 
2576 // M(i,j) /= R(i,j); R must have same dimensions as this
2577 template <class ELT> template <class EE> inline MatrixBase<ELT>&
2579  const int nr=nrow(), nc=ncol();
2580  assert(r.nrow()==nr && r.ncol()==nc);
2581  for (int j=0; j<nc; ++j)
2582  for (int i=0; i<nr; ++i)
2583  (*this)(i,j) /= r(i,j);
2584  return *this;
2585 }
2586 
2587 template <class ELT> template <class EE> inline void
2589  const MatrixBase<EE>& r,
2590  typename MatrixBase<ELT>::template EltResult<EE>::Dvd& out) const
2591 {
2592  const int nr=nrow(), nc=ncol();
2593  assert(r.nrow()==nr && r.ncol()==nc);
2594  out.resize(nr,nc);
2595  for (int j=0; j<nc; ++j)
2596  for (int i=0; i<nr; ++i)
2597  out(i,j) = (*this)(i,j) / r(i,j);
2598 }
2599 // M(i,j) = R(i,j) / M(i,j); R must have same dimensions as this
2600 template <class ELT> template <class EE> inline MatrixBase<ELT>&
2602  const int nr=nrow(), nc=ncol();
2603  assert(r.nrow()==nr && r.ncol()==nc);
2604  for (int j=0; j<nc; ++j)
2605  for (int i=0; i<nr; ++i) {
2606  ELT& e = updElt(i,j);
2607  e = r(i,j) / e;
2608  }
2609  return *this;
2610 }
2611 
2612 template <class ELT> template <class EE> inline void
2614  const MatrixBase<EE>& r,
2615  typename MatrixBase<EE>::template EltResult<ELT>::Dvd& out) const
2616 {
2617  const int nr=nrow(), nc=ncol();
2618  assert(r.nrow()==nr && r.ncol()==nc);
2619  out.resize(nr,nc);
2620  for (int j=0; j<nc; ++j)
2621  for (int i=0; i<nr; ++i)
2622  out(i,j) = r(i,j) / (*this)(i,j);
2623 }
2624 
2625 /*
2626 template <class ELT> inline MatrixView_< typename CNT<ELT>::TReal >
2627 MatrixBase<ELT>::real() const {
2628  if (!CNT<ELT>::IsComplex) { // known at compile time
2629  return MatrixView_< typename CNT<ELT>::TReal >( // this is just ELT
2630  MatrixHelper(helper,0,0,nrow(),ncol())); // a view of the whole matrix
2631  }
2632  // Elements are complex -- helper uses underlying precision (real) type.
2633  MatrixHelper<Precision> h(helper,typename MatrixHelper<Precision>::RealView);
2634  return MatrixView_< typename CNT<ELT>::TReal >(h);
2635 }
2636 */
2637 
2638 
2639 // ----------------------------------------------------------------------------
2643 
2644 // + and - allow mixed element types, but will fail to compile if the elements aren't
2645 // compatible. At run time these will fail if the dimensions are incompatible.
2646 template <class E1, class E2>
2647 Matrix_<typename CNT<E1>::template Result<E2>::Add>
2649  return Matrix_<typename CNT<E1>::template Result<E2>::Add>(l) += r;
2650 }
2651 
2652 template <class E>
2653 Matrix_<E> operator+(const MatrixBase<E>& l, const typename CNT<E>::T& r) {
2654  return Matrix_<E>(l) += r;
2655 }
2656 
2657 template <class E>
2658 Matrix_<E> operator+(const typename CNT<E>::T& l, const MatrixBase<E>& r) {
2659  return Matrix_<E>(r) += l;
2660 }
2661 
2662 template <class E1, class E2>
2663 Matrix_<typename CNT<E1>::template Result<E2>::Sub>
2665  return Matrix_<typename CNT<E1>::template Result<E2>::Sub>(l) -= r;
2666 }
2667 
2668 template <class E>
2669 Matrix_<E> operator-(const MatrixBase<E>& l, const typename CNT<E>::T& r) {
2670  return Matrix_<E>(l) -= r;
2671 }
2672 
2673 template <class E>
2674 Matrix_<E> operator-(const typename CNT<E>::T& l, const MatrixBase<E>& r) {
2675  Matrix_<E> temp(r.nrow(), r.ncol());
2676  temp = l;
2677  return (temp -= r);
2678 }
2679 
2680 // Scalar multiply and divide. You might wish the scalar could be
2681 // a templatized type "E2", but that would create horrible ambiguities since
2682 // E2 would match not only scalar types but everything else including
2683 // matrices.
2684 template <class E> Matrix_<E>
2685 operator*(const MatrixBase<E>& l, const typename CNT<E>::StdNumber& r)
2686  { return Matrix_<E>(l)*=r; }
2687 
2688 template <class E> Matrix_<E>
2689 operator*(const typename CNT<E>::StdNumber& l, const MatrixBase<E>& r)
2690  { return Matrix_<E>(r)*=l; }
2691 
2692 template <class E> Matrix_<E>
2693 operator/(const MatrixBase<E>& l, const typename CNT<E>::StdNumber& r)
2694  { return Matrix_<E>(l)/=r; }
2695 
2696 // Handle ints explicitly.
2697 template <class E> Matrix_<E>
2698 operator*(const MatrixBase<E>& l, int r)
2699  { return Matrix_<E>(l)*= typename CNT<E>::StdNumber(r); }
2700 
2701 template <class E> Matrix_<E>
2702 operator*(int l, const MatrixBase<E>& r)
2703  { return Matrix_<E>(r)*= typename CNT<E>::StdNumber(l); }
2704 
2705 template <class E> Matrix_<E>
2706 operator/(const MatrixBase<E>& l, int r)
2707  { return Matrix_<E>(l)/= typename CNT<E>::StdNumber(r); }
2708 
2710 
2714 
2715 template <class E1, class E2>
2716 Vector_<typename CNT<E1>::template Result<E2>::Add>
2718  return Vector_<typename CNT<E1>::template Result<E2>::Add>(l) += r;
2719 }
2720 template <class E>
2721 Vector_<E> operator+(const VectorBase<E>& l, const typename CNT<E>::T& r) {
2722  return Vector_<E>(l) += r;
2723 }
2724 template <class E>
2725 Vector_<E> operator+(const typename CNT<E>::T& l, const VectorBase<E>& r) {
2726  return Vector_<E>(r) += l;
2727 }
2728 template <class E1, class E2>
2729 Vector_<typename CNT<E1>::template Result<E2>::Sub>
2731  return Vector_<typename CNT<E1>::template Result<E2>::Sub>(l) -= r;
2732 }
2733 template <class E>
2734 Vector_<E> operator-(const VectorBase<E>& l, const typename CNT<E>::T& r) {
2735  return Vector_<E>(l) -= r;
2736 }
2737 template <class E>
2738 Vector_<E> operator-(const typename CNT<E>::T& l, const VectorBase<E>& r) {
2739  Vector_<E> temp(r.size());
2740  temp = l;
2741  return (temp -= r);
2742 }
2743 
2744 // Scalar multiply and divide.
2745 
2746 template <class E> Vector_<E>
2747 operator*(const VectorBase<E>& l, const typename CNT<E>::StdNumber& r)
2748  { return Vector_<E>(l)*=r; }
2749 
2750 template <class E> Vector_<E>
2751 operator*(const typename CNT<E>::StdNumber& l, const VectorBase<E>& r)
2752  { return Vector_<E>(r)*=l; }
2753 
2754 template <class E> Vector_<E>
2755 operator/(const VectorBase<E>& l, const typename CNT<E>::StdNumber& r)
2756  { return Vector_<E>(l)/=r; }
2757 
2758 // Handle ints explicitly
2759 template <class E> Vector_<E>
2760 operator*(const VectorBase<E>& l, int r)
2761  { return Vector_<E>(l)*= typename CNT<E>::StdNumber(r); }
2762 
2763 template <class E> Vector_<E>
2764 operator*(int l, const VectorBase<E>& r)
2765  { return Vector_<E>(r)*= typename CNT<E>::StdNumber(l); }
2766 
2767 template <class E> Vector_<E>
2768 operator/(const VectorBase<E>& l, int r)
2769  { return Vector_<E>(l)/= typename CNT<E>::StdNumber(r); }
2770 
2771 // These are fancier "scalars"; whether they are allowed depends on
2772 // whether the element type and the CNT are compatible.
2773 
2774 // Vector * Vec
2775 template <class E1, int M, class E2, int S>
2776 Vector_<typename CNT<E1>::template Result< Vec<M,E2,S> >::Mul>
2777 operator*(const VectorBase<E1>& v, const Vec<M,E2,S>& s) {
2778  Vector_<typename CNT<E1>::template Result< Vec<M,E2,S> >::Mul> res(v.nrow());
2779  for (int i=0; i < v.nrow(); ++i)
2780  res[i] = v[i]*s;
2781  return res;
2782 }
2783 
2784 // Vec * Vector
2785 template <class E1, int M, class E2, int S>
2786 Vector_<typename Vec<M,E2,S>::template Result<E1>::Mul>
2787 operator*(const Vec<M,E2,S>& s, const VectorBase<E1>& v) {
2788  Vector_<typename Vec<M,E2,S>::template Result<E1>::Mul> res(v.nrow());
2789  for (int i=0; i < v.nrow(); ++i)
2790  res[i] = s*v[i];
2791  return res;
2792 }
2793 
2794 // Vector * Row
2795 template <class E1, int N, class E2, int S>
2796 Vector_<typename CNT<E1>::template Result< Row<N,E2,S> >::Mul>
2797 operator*(const VectorBase<E1>& v, const Row<N,E2,S>& s) {
2798  Vector_<typename CNT<E1>::template Result< Row<N,E2,S> >::Mul> res(v.nrow());
2799  for (int i=0; i < v.nrow(); ++i)
2800  res[i] = v[i]*s;
2801  return res;
2802 }
2803 
2804 // Row * Vector
2805 template <class E1, int N, class E2, int S>
2806 Vector_<typename Row<N,E2,S>::template Result<E1>::Mul>
2807 operator*(const Row<N,E2,S>& s, const VectorBase<E1>& v) {
2808  Vector_<typename Row<N,E2,S>::template Result<E1>::Mul> res(v.nrow());
2809  for (int i=0; i < v.nrow(); ++i)
2810  res[i] = s*v[i];
2811  return res;
2812 }
2813 
2814 // Vector * Mat
2815 template <class E1, int M, int N, class E2, int S1, int S2>
2816 Vector_<typename CNT<E1>::template Result< Mat<M,N,E2,S1,S2> >::Mul>
2818  Vector_<typename CNT<E1>::template Result< Mat<M,N,E2,S1,S2> >::Mul> res(v.nrow());
2819  for (int i=0; i < v.nrow(); ++i)
2820  res[i] = v[i]*s;
2821  return res;
2822 }
2823 
2824 // Mat * Vector
2825 template <class E1, int M, int N, class E2, int S1, int S2>
2826 Vector_<typename Mat<M,N,E2,S1,S2>::template Result<E1>::Mul>
2828  Vector_<typename Mat<M,N,E2,S1,S2>::template Result<E1>::Mul> res(v.nrow());
2829  for (int i=0; i < v.nrow(); ++i)
2830  res[i] = s*v[i];
2831  return res;
2832 }
2833 
2834 // Vector * SymMat
2835 template <class E1, int M, class E2, int S>
2836 Vector_<typename CNT<E1>::template Result< SymMat<M,E2,S> >::Mul>
2838  Vector_<typename CNT<E1>::template Result< SymMat<M,E2,S> >::Mul> res(v.nrow());
2839  for (int i=0; i < v.nrow(); ++i)
2840  res[i] = v[i]*s;
2841  return res;
2842 }
2843 
2844 // SymMat * Vector
2845 template <class E1, int M, class E2, int S>
2846 Vector_<typename SymMat<M,E2,S>::template Result<E1>::Mul>
2848  Vector_<typename SymMat<M,E2,S>::template Result<E1>::Mul> res(v.nrow());
2849  for (int i=0; i < v.nrow(); ++i)
2850  res[i] = s*v[i];
2851  return res;
2852 }
2853 
2855 
2859 
2860 template <class E1, class E2>
2861 RowVector_<typename CNT<E1>::template Result<E2>::Add>
2863  return RowVector_<typename CNT<E1>::template Result<E2>::Add>(l) += r;
2864 }
2865 template <class E>
2866 RowVector_<E> operator+(const RowVectorBase<E>& l, const typename CNT<E>::T& r) {
2867  return RowVector_<E>(l) += r;
2868 }
2869 template <class E>
2870 RowVector_<E> operator+(const typename CNT<E>::T& l, const RowVectorBase<E>& r) {
2871  return RowVector_<E>(r) += l;
2872 }
2873 template <class E1, class E2>
2874 RowVector_<typename CNT<E1>::template Result<E2>::Sub>
2876  return RowVector_<typename CNT<E1>::template Result<E2>::Sub>(l) -= r;
2877 }
2878 template <class E>
2879 RowVector_<E> operator-(const RowVectorBase<E>& l, const typename CNT<E>::T& r) {
2880  return RowVector_<E>(l) -= r;
2881 }
2882 template <class E>
2883 RowVector_<E> operator-(const typename CNT<E>::T& l, const RowVectorBase<E>& r) {
2884  RowVector_<E> temp(r.size());
2885  temp = l;
2886  return (temp -= r);
2887 }
2888 
2889 // Scalar multiply and divide
2890 
2891 template <class E> RowVector_<E>
2892 operator*(const RowVectorBase<E>& l, const typename CNT<E>::StdNumber& r)
2893  { return RowVector_<E>(l)*=r; }
2894 
2895 template <class E> RowVector_<E>
2896 operator*(const typename CNT<E>::StdNumber& l, const RowVectorBase<E>& r)
2897  { return RowVector_<E>(r)*=l; }
2898 
2899 template <class E> RowVector_<E>
2900 operator/(const RowVectorBase<E>& l, const typename CNT<E>::StdNumber& r)
2901  { return RowVector_<E>(l)/=r; }
2902 
2903 // Handle ints explicitly.
2904 template <class E> RowVector_<E>
2905 operator*(const RowVectorBase<E>& l, int r)
2906  { return RowVector_<E>(l)*= typename CNT<E>::StdNumber(r); }
2907 
2908 template <class E> RowVector_<E>
2909 operator*(int l, const RowVectorBase<E>& r)
2910  { return RowVector_<E>(r)*= typename CNT<E>::StdNumber(l); }
2911 
2912 template <class E> RowVector_<E>
2913 operator/(const RowVectorBase<E>& l, int r)
2914  { return RowVector_<E>(l)/= typename CNT<E>::StdNumber(r); }
2915 
2916 
2917 // These are fancier "scalars"; whether they are allowed depends on
2918 // whether the element type and the CNT are compatible.
2919 
2920 // RowVector * Vec
2921 template <class E1, int M, class E2, int S>
2922 RowVector_<typename CNT<E1>::template Result< Vec<M,E2,S> >::Mul>
2924  RowVector_<typename CNT<E1>::template Result< Vec<M,E2,S> >::Mul> res(v.ncol());
2925  for (int i=0; i < v.ncol(); ++i)
2926  res[i] = v[i]*s;
2927  return res;
2928 }
2929 
2930 // Vec * RowVector
2931 template <class E1, int M, class E2, int S>
2932 RowVector_<typename Vec<M,E2,S>::template Result<E1>::Mul>
2934  RowVector_<typename Vec<M,E2,S>::template Result<E1>::Mul> res(v.ncol());
2935  for (int i=0; i < v.ncol(); ++i)
2936  res[i] = s*v[i];
2937  return res;
2938 }
2939 
2940 // RowVector * Row
2941 template <class E1, int N, class E2, int S>
2942 RowVector_<typename CNT<E1>::template Result< Row<N,E2,S> >::Mul>
2944  RowVector_<typename CNT<E1>::template Result< Row<N,E2,S> >::Mul> res(v.ncol());
2945  for (int i=0; i < v.ncol(); ++i)
2946  res[i] = v[i]*s;
2947  return res;
2948 }
2949 
2950 // Row * RowVector
2951 template <class E1, int N, class E2, int S>
2952 RowVector_<typename Row<N,E2,S>::template Result<E1>::Mul>
2954  RowVector_<typename Row<N,E2,S>::template Result<E1>::Mul> res(v.ncol());
2955  for (int i=0; i < v.ncol(); ++i)
2956  res[i] = s*v[i];
2957  return res;
2958 }
2959 
2960 // RowVector * Mat
2961 template <class E1, int M, int N, class E2, int S1, int S2>
2962 RowVector_<typename CNT<E1>::template Result< Mat<M,N,E2,S1,S2> >::Mul>
2964  RowVector_<typename CNT<E1>::template Result< Mat<M,N,E2,S1,S2> >::Mul> res(v.ncol());
2965  for (int i=0; i < v.ncol(); ++i)
2966  res[i] = v[i]*s;
2967  return res;
2968 }
2969 
2970 // Mat * RowVector
2971 template <class E1, int M, int N, class E2, int S1, int S2>
2972 RowVector_<typename Mat<M,N,E2,S1,S2>::template Result<E1>::Mul>
2974  RowVector_<typename Mat<M,N,E2,S1,S2>::template Result<E1>::Mul> res(v.ncol());
2975  for (int i=0; i < v.ncol(); ++i)
2976  res[i] = s*v[i];
2977  return res;
2978 }
2979 
2980 // RowVector * SymMat
2981 template <class E1, int M, class E2, int S>
2982 RowVector_<typename CNT<E1>::template Result< SymMat<M,E2,S> >::Mul>
2984  RowVector_<typename CNT<E1>::template Result< SymMat<M,E2,S> >::Mul> res(v.ncol());
2985  for (int i=0; i < v.ncol(); ++i)
2986  res[i] = v[i]*s;
2987  return res;
2988 }
2989 
2990 // SymMat * RowVector
2991 template <class E1, int M, class E2, int S>
2992 RowVector_<typename SymMat<M,E2,S>::template Result<E1>::Mul>
2994  RowVector_<typename SymMat<M,E2,S>::template Result<E1>::Mul> res(v.ncol());
2995  for (int i=0; i < v.ncol(); ++i)
2996  res[i] = s*v[i];
2997  return res;
2998 }
2999 
3001 
3002 
3007 
3008  // TODO: these should use LAPACK!
3009 
3010 // Dot product
3011 template <class E1, class E2>
3012 typename CNT<E1>::template Result<E2>::Mul
3014  assert(r.ncol() == v.nrow());
3015  typename CNT<E1>::template Result<E2>::Mul sum(0);
3016  for (int j=0; j < r.ncol(); ++j)
3017  sum += r(j) * v[j];
3018  return sum;
3019 }
3020 
3021 template <class E1, class E2>
3022 Vector_<typename CNT<E1>::template Result<E2>::Mul>
3024  assert(m.ncol() == v.nrow());
3025  Vector_<typename CNT<E1>::template Result<E2>::Mul> res(m.nrow());
3026  for (int i=0; i< m.nrow(); ++i)
3027  res[i] = m[i]*v;
3028  return res;
3029 }
3030 
3031 template <class E1, class E2>
3032 Matrix_<typename CNT<E1>::template Result<E2>::Mul>
3033 operator*(const MatrixBase<E1>& m1, const MatrixBase<E2>& m2) {
3034  assert(m1.ncol() == m2.nrow());
3035  Matrix_<typename CNT<E1>::template Result<E2>::Mul>
3036  res(m1.nrow(),m2.ncol());
3037 
3038  for (int j=0; j < res.ncol(); ++j)
3039  for (int i=0; i < res.nrow(); ++i)
3040  res(i,j) = m1[i] * m2(j);
3041 
3042  return res;
3043 }
3044 
3046 
3047 // This "private" static method is used to implement VectorView's
3048 // fillVectorViewFromStream() and Vector's readVectorFromStream()
3049 // namespace-scope static methods, which are in turn used to implement
3050 // VectorView's and
3051 // Vector's stream extraction operators ">>". This method has to be in the
3052 // header file so that we don't need to pass streams through the API, but it
3053 // is not intended for use by users and has no Doxygen presence, unlike
3054 // fillArrayFromStream() and readArrayFromStream() and (more commonly)
3055 // the extraction operators.
3056 template <class T> static inline
3057 std::istream& readVectorFromStreamHelper
3058  (std::istream& in, bool isFixedSize, Vector_<T>& out)
3059 {
3060  // If already failed, bad, or eof, set failed bit and return without
3061  // touching the Vector.
3062  if (!in.good()) {in.setstate(std::ios::failbit); return in;}
3063 
3064  // If the passed-in Vector isn't resizeable, then we have to treat it as
3065  // a fixed size VectorView regardless of the setting of the isFixedSize
3066  // argument.
3067  if (!out.isResizeable())
3068  isFixedSize = true; // might be overriding the argument here
3069 
3070  // numRequired will be ignored unless isFixedSize==true.
3071  const int numRequired = isFixedSize ? out.size() : 0;
3072 
3073  if (!isFixedSize)
3074  out.clear(); // We're going to replace the entire contents of the Array.
3075 
3076  // Skip initial whitespace. If that results in eof this may be a successful
3077  // read of a 0-length, unbracketed Vector. That is OK for either a
3078  // variable-length Vector or a fixed-length VectorView of length zero.
3079  std::ws(in); if (in.fail()) return in;
3080  if (in.eof()) {
3081  if (isFixedSize && numRequired != 0)
3082  in.setstate(std::ios_base::failbit); // zero elements not OK
3083  return in;
3084  }
3085 
3086  // Here the stream is good and the next character is non-white.
3087  assert(in.good());
3088 
3089  // Use this for raw i/o (peeks and gets).
3090  typename std::iostream::int_type ch;
3091  const typename std::iostream::int_type EOFch =
3092  std::iostream::traits_type::eof();
3093 
3094  // First we'll look for the optional "~". If found, the brackets become
3095  // required.
3096  bool tildeFound = false;
3097  ch = in.peek(); if (in.fail()) return in;
3098  assert(ch != EOFch); // we already checked above
3099  if ((char)ch == '~') {
3100  tildeFound = true;
3101  in.get(); // absorb the tilde
3102  // Eat whitespace after the tilde to see what's next.
3103  if (in.good()) std::ws(in);
3104  // If we hit eof after the tilde we don't like the formatting.
3105  if (!in.good()) {in.setstate(std::ios_base::failbit); return in;}
3106  }
3107 
3108  // Here the stream is good, the next character is non-white, and we
3109  // might have seen a tilde.
3110  assert(in.good());
3111 
3112  // Now see if the sequence is bare or surrounded by (), or [].
3113  bool lookForCloser = true;
3114  char openBracket, closeBracket;
3115  ch = in.peek(); if (in.fail()) return in;
3116  assert(ch != EOFch); // we already checked above
3117 
3118  openBracket = (char)ch;
3119  if (openBracket=='(') {in.get(); closeBracket = ')';}
3120  else if (openBracket=='[') {in.get(); closeBracket = ']';}
3121  else lookForCloser = false;
3122 
3123  // If we found a tilde, the opening bracket was mandatory. If we didn't
3124  // find one then we reject the formatting.
3125  if (tildeFound && !lookForCloser)
3126  { in.setstate(std::ios_base::failbit); return in;}
3127 
3128  // If lookForCloser is true, then closeBracket contains the terminating
3129  // delimiter, otherwise we're not going to quit until eof.
3130 
3131  // Eat whitespace after the opening bracket to see what's next.
3132  if (in.good()) std::ws(in);
3133 
3134  // If we're at eof now it must be because the open bracket was the
3135  // last non-white character in the stream, which is an error.
3136  if (!in.good()) {
3137  if (in.eof()) {
3138  assert(lookForCloser); // or we haven't read anything that could eof
3139  in.setstate(std::ios::failbit);
3140  }
3141  return in;
3142  }
3143 
3144  // istream is good and next character is non-white; ready to read first
3145  // value or terminator.
3146 
3147  // We need to figure out whether the elements are space- or comma-
3148  // separated and then insist on consistency.
3149  bool commaOK = true, commaRequired = false;
3150  bool terminatorSeen = false;
3151  int nextIndex = 0;
3152  while (true) {
3153  char c;
3154 
3155  // Here at the top of this loop, we have already successfully read
3156  // n=nextIndex values of type T. For fixed-size reads, it might be
3157  // the case that n==numRequired already, but we still may need to
3158  // look for a closing bracket before we can declare victory.
3159  // The stream is good() (not at eof) but it might be the case that
3160  // there is nothing but white space left; we don't know yet because
3161  // if we have satisfied the fixed-size count and are not expecting
3162  // a terminator then we should quit without absorbing the trailing
3163  // white space.
3164  assert(in.good());
3165 
3166  // Look for closing bracket before trying to read value.
3167  if (lookForCloser) {
3168  // Eat white space to find the closing bracket.
3169  std::ws(in); if (!in.good()) break; // eof?
3170  ch = in.peek(); assert(ch != EOFch);
3171  if (!in.good()) break;
3172  c = (char)ch;
3173  if (c == closeBracket) {
3174  in.get(); // absorb the closing bracket
3175  terminatorSeen = true;
3176  break;
3177  }
3178  // next char not a closing bracket; fall through
3179  }
3180 
3181  // We didn't look or didn't find a closing bracket. The istream is good
3182  // but we might be looking at white space.
3183 
3184  // If we already got all the elements we want, break for final checks.
3185  if (isFixedSize && (nextIndex == numRequired))
3186  break; // that's a full count.
3187 
3188  // Look for comma before value, except the first time.
3189  if (commaOK && nextIndex != 0) {
3190  // Eat white space to find the comma.
3191  std::ws(in); if (!in.good()) break; // eof?
3192  ch = in.peek(); assert(ch != EOFch);
3193  if (!in.good()) break;
3194  c = (char)ch;
3195  if (c == ',') {
3196  in.get(); // absorb comma
3197  commaRequired = true; // all commas from now on
3198  } else { // next char not a comma
3199  if (commaRequired) // bad, e.g.: v1, v2, v3 v4
3200  { in.setstate(std::ios::failbit); break; }
3201  else commaOK = false; // saw: v1 v2 (no commas now)
3202  }
3203  if (!in.good()) break; // might be eof
3204  }
3205 
3206  // No closing bracket yet; don't have enough elements; skipped comma
3207  // if any; istream is good; might be looking at white space.
3208  assert(in.good());
3209 
3210  // Now read in an element of type T.
3211  // The extractor T::operator>>() will ignore leading white space.
3212  if (!isFixedSize)
3213  out.resizeKeep(out.size()+1); // grow by one (default consructed)
3214  in >> out[nextIndex]; if (in.fail()) break;
3215  ++nextIndex;
3216 
3217  if (!in.good()) break; // might be eof
3218  }
3219 
3220  // We will get here under a number of circumstances:
3221  // - the fail bit is set in the istream, or
3222  // - we reached eof
3223  // - we saw a closing brace
3224  // - we got all the elements we wanted (for a fixed-size read)
3225  // Note that it is possible that we consumed everything except some
3226  // trailing white space (meaning we're not technically at eof), but
3227  // for consistency with built-in operator>>()'s we won't try to absorb
3228  // that trailing white space.
3229 
3230  if (!in.fail()) {
3231  if (lookForCloser && !terminatorSeen)
3232  in.setstate(std::ios::failbit); // missing terminator
3233 
3234  if (isFixedSize && nextIndex != numRequired)
3235  in.setstate(std::ios::failbit); // wrong number of values
3236  }
3237 
3238  return in;
3239 }
3240 
3241 
3242 
3243 //------------------------------------------------------------------------------
3244 // RELATED GLOBAL OPERATORS
3245 //------------------------------------------------------------------------------
3246 // These are logically part of the Matrix_<T> class but are not actually
3247 // class members; that is, they are in the SimTK namespace.
3248 
3256 
3260 template <class E> inline void
3261 writeUnformatted(std::ostream& o, const VectorBase<E>& v) {
3262  const int sz = v.size();
3263  for (int i=0; i < sz; ++i) {
3264  if (i != 0) o << " ";
3265  writeUnformatted(o, v[i]);
3266  }
3267 }
3270 template <class E> inline void
3271 writeUnformatted(std::ostream& o, const VectorView_<E>& v)
3272 { writeUnformatted(o, static_cast< const VectorBase<E> >(v)); }
3273 
3276 template <class E> inline void
3277 writeUnformatted(std::ostream& o, const Vector_<E>& v)
3278 { writeUnformatted(o, static_cast< const VectorBase<E> >(v)); }
3279 
3283 template <class E> inline void
3284 writeUnformatted(std::ostream& o, const RowVectorBase<E>& v)
3285 { writeUnformatted(o, ~v); }
3286 
3289 template <class E> inline void
3290 writeUnformatted(std::ostream& o, const RowVectorView_<E>& v)
3291 { writeUnformatted(o, static_cast< const RowVectorBase<E> >(v)); }
3292 
3295 template <class E> inline void
3296 writeUnformatted(std::ostream& o, const RowVector_<E>& v)
3297 { writeUnformatted(o, static_cast< const RowVectorBase<E> >(v)); }
3298 
3302 template <class E> inline void
3303 writeUnformatted(std::ostream& o, const MatrixBase<E>& v) {
3304  const int nr = v.nrow();
3305  for (int i=0; i < nr; ++i) {
3306  if (i != 0) o << std::endl;
3307  writeUnformatted(o, v[i]);
3308  }
3309 }
3312 template <class E> inline void
3313 writeUnformatted(std::ostream& o, const MatrixView_<E>& v)
3314 { writeUnformatted(o, static_cast< const MatrixBase<E> >(v)); }
3315 
3318 template <class E> inline void
3319 writeUnformatted(std::ostream& o, const Matrix_<E>& v)
3320 { writeUnformatted(o, static_cast< const MatrixBase<E> >(v)); }
3321 
3324 template <class E> inline bool
3325 readUnformatted(std::istream& in, VectorView_<E>& v) {
3326  for (int i=0; i < v.size(); ++i)
3327  if (!readUnformatted(in, v[i])) return false;
3328  return true;
3329 }
3330 
3332 template <class E> inline bool
3333 readUnformatted(std::istream& in, Vector_<E>& v) {
3334  if (!v.isResizeable())
3335  return readUnformatted(in, v.updAsVectorView());
3336 
3337  Array_<E,int> a;
3338  if (!readUnformatted(in,a)) return false;
3339  v.resize(a.size());
3340  for (int i=0; i<a.size(); ++i)
3341  v[i] = a[i];
3342  return true;
3343 }
3344 
3347 template <class E> inline bool
3348 readUnformatted(std::istream& in, RowVectorView_<E>& v)
3349 { VectorView_<E> vt(~v);
3350  return readUnformatted<E>(in, vt); }
3351 
3354 template <class E> inline bool
3355 readUnformatted(std::istream& in, RowVector_<E>& v)
3356 { Vector_<E> vt(~v);
3357  return readUnformatted<E>(in, vt); }
3358 
3363 template <class E> inline bool
3364 readUnformatted(std::istream& in, MatrixView_<E>& v) {
3365  for (int row=0; row < v.nrow(); ++row) {
3366  RowVectorView_<E> oneRow(v[row]);
3367  if (!readUnformatted<E>(in, oneRow)) return false;
3368  }
3369  return true;
3370 }
3371 
3376 template <class E> inline bool
3377 fillUnformatted(std::istream& in, Matrix_<E>& v) {
3378  return readUnformatted<E>(in, v.updAsMatrixView());
3379 }
3380 
3383 template <class E> inline bool
3384 readUnformatted(std::istream& in, Matrix_<E>& v) {
3385  SimTK_ASSERT_ALWAYS(!"implemented",
3386  "SimTK::readUnformatted(istream, Matrix) is not implemented; try"
3387  " SimTK::fillUnformatted(istream, Matrix) instead.");
3388  return false;
3389 }
3390 
3397 template <class T> inline std::ostream&
3398 operator<<(std::ostream& o, const VectorBase<T>& v)
3399 { o << "~[";
3400  if (v.size()) {
3401  o << v[0];
3402  for (int i=1; i < v.size(); ++i) o << " " << v[i];
3403  }
3404  return o << "]";
3405 }
3406 
3413 template <class T> inline std::ostream&
3414 operator<<(std::ostream& o, const RowVectorBase<T>& v)
3415 { o << "[";
3416  if (v.size()) {
3417  o << v[0];
3418  for (int i=1; i < v.size(); ++i) o << " " << v[i];
3419  }
3420  return o << "]";
3421 }
3422 
3430 template <class T> inline std::ostream&
3431 operator<<(std::ostream& o, const MatrixBase<T>& m) {
3432  for (int i=0;i<m.nrow();++i)
3433  o << std::endl << m[i];
3434  if (m.nrow()) o << std::endl;
3435  return o;
3436 }
3437 
3438 
3470 template <class T> static inline
3471 std::istream& readVectorFromStream(std::istream& in, Vector_<T>& out)
3472 { return readVectorFromStreamHelper<T>(in, false /*variable sizez*/, out); }
3473 
3474 
3475 
3500 template <class T> static inline
3501 std::istream& fillVectorFromStream(std::istream& in, Vector_<T>& out)
3502 { return readVectorFromStreamHelper<T>(in, true /*fixed size*/, out); }
3503 
3508 template <class T> static inline
3509 std::istream& fillVectorViewFromStream(std::istream& in, VectorView_<T>& out)
3510 { return readVectorFromStreamHelper<T>(in, true /*fixed size*/, out); }
3511 
3512 
3522 template <class T> inline
3523 std::istream& operator>>(std::istream& in, Vector_<T>& out)
3524 { return readVectorFromStream<T>(in, out); }
3525 
3533 template <class T> inline
3534 std::istream& operator>>(std::istream& in, VectorView_<T>& out)
3535 { return fillVectorViewFromStream<T>(in, out); }
3536 
3539 // Friendly abbreviations for vectors and matrices with scalar elements.
3540 
3547 
3554 
3561 
3568 
3575 
3582 
3583 
3588 template <class ELT, class VECTOR_CLASS>
3589 class VectorIterator {
3590 public:
3591  typedef ELT value_type;
3592  typedef ptrdiff_t difference_type;
3593  typedef ELT& reference;
3594  typedef ELT* pointer;
3595  typedef std::random_access_iterator_tag iterator_category;
3596  VectorIterator(VECTOR_CLASS& vector, ptrdiff_t index) : vector(vector), index(index) {
3597  }
3598  VectorIterator(const VectorIterator& iter) : vector(iter.vector), index(iter.index) {
3599  }
3601  vector = iter.vector;
3602  index = iter.index;
3603  return *this;
3604  }
3605  ELT& operator*() {
3606  assert (index >= 0 && index < vector.size());
3607  return vector[(int)index];
3608  }
3609  ELT& operator[](ptrdiff_t i) {
3610  assert (i >= 0 && i < vector.size());
3611  return vector[(int)i];
3612  }
3614  assert (index < vector.size());
3615  ++index;
3616  return *this;
3617  }
3619  assert (index < vector.size());
3620  VectorIterator current = *this;
3621  ++index;
3622  return current;
3623  }
3625  assert (index > 0);
3626  --index;
3627  return *this;
3628  }
3630  assert (index > 0);
3631  VectorIterator current = *this;
3632  --index;
3633  return current;
3634  }
3635  bool operator<(VectorIterator iter) const {
3636  return (index < iter.index);
3637  }
3638  bool operator>(VectorIterator iter) const {
3639  return (index > iter.index);
3640  }
3641  bool operator<=(VectorIterator iter) const {
3642  return (index <= iter.index);
3643  }
3644  bool operator>=(VectorIterator iter) const {
3645  return (index >= iter.index);
3646  }
3647  ptrdiff_t operator-(VectorIterator iter) const {
3648  return (index - iter.index);
3649  }
3650  VectorIterator operator-(ptrdiff_t n) const {
3651  return VectorIterator(vector, index-n);
3652  }
3653  VectorIterator operator+(ptrdiff_t n) const {
3654  return VectorIterator(vector, index+n);
3655  }
3656  bool operator==(VectorIterator iter) const {
3657  return (index == iter.index);
3658  }
3659  bool operator!=(VectorIterator iter) const {
3660  return (index != iter.index);
3661  }
3662 private:
3663  VECTOR_CLASS& vector;
3664  ptrdiff_t index;
3665 };
3666 
3667 } //namespace SimTK
3668 
3669 #endif //SimTK_SIMMATRIX_BIGMATRIX_H_
Here we define class MatrixHelper<S>, the scalar-type templatized helper class for the more general...
Definition: MatrixHelper.h:79
VectorView_< ELT > operator()(int j) const
Definition: BigMatrix.h:746
Vector_< Complex > ComplexVector
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3544
void elementwiseDivide(const VectorBase< EE > &v, typename EltResult< EE >::Dvd &out) const
Definition: BigMatrix.h:1375
RowVectorBase(int n=0)
Default constructor makes a 1x0 matrix locked at 1 row; you can provide an initial allocation if you ...
Definition: BigMatrix.h:1518
void unlockShape()
Definition: BigMatrix.h:966
CNT< E >::Precision EPrecision
Definition: BigMatrix.h:245
VectorBase & operator-=(const VectorBase &r)
Definition: BigMatrix.h:1163
Inertia_< P > operator/(const Inertia_< P > &i, const P &r)
Divide an inertia matrix by a scalar.
Definition: MassProperties.h:616
VectorIterator< ELT, VectorBase< ELT > > begin()
Definition: BigMatrix.h:1473
RowVectorBase & operator+=(const RowVectorBase< EE > &b)
Definition: BigMatrix.h:1602
bool operator!=(VectorIterator iter) const
Definition: BigMatrix.h:3659
PhiMatrixTranspose transpose(const PhiMatrix &phi)
Definition: SpatialAlgebra.h:720
MatrixBase & setToNaN()
Definition: BigMatrix.h:735
EltResult< EE >::Dvd elementwiseDivide(const RowVectorBase< EE > &v) const
Definition: BigMatrix.h:1657
Vector_ & operator-=(const ELT &b)
Definition: BigMatrix.h:2121
DeadMatrixView_(MatrixHelper< S > &h)
Definition: BigMatrix.h:1854
VectorView_(const VectorView_ &v)
Definition: BigMatrix.h:2020
MatrixView_< ELT > operator()(int i, int j, int m, int n)
Definition: BigMatrix.h:755
VectorBase & operator/=(const StdNumber &t)
Definition: BigMatrix.h:1161
RowVector_(const RowVector_ &src)
Definition: BigMatrix.h:2220
RowVectorBase & elementwiseMultiplyFromLeftInPlace(const RowVectorBase< EE > &r)
Definition: BigMatrix.h:1635
K::ScalarNormSq ScalarNormSq
Definition: CompositeNumericalTypes.h:166
Vector_< ELT > & updAsVector()
Definition: BigMatrix.h:980
const VectorView_< ELT > & getAsVectorView() const
Definition: BigMatrix.h:974
void elementwiseDivide(const RowVectorBase< EE > &v, typename EltResult< EE >::Dvd &out) const
Definition: BigMatrix.h:1655
const MatrixCharacter & getMatrixCharacter() const
Definition: BigMatrix.h:271
MatrixBase< ESqTHerm > TSqTHerm
Definition: BigMatrix.h:268
MatrixBase(const MatrixCommitment &commitment, int m, int n, const ELT *cppInitialValuesByRow)
Initializing constructor with the initially-allocated elements initialized from a C++ array of elemen...
Definition: BigMatrix.h:390
EScalarNormSq ScalarNormSq
Definition: BigMatrix.h:252
Matrix_ & operator*=(const StdNumber &t)
Definition: BigMatrix.h:1966
RowVectorView_< fComplex > fComplexRowVectorView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3566
#define SimTK_SIZECHECK(sz, maxsz, where)
Definition: ExceptionMacros.h:146
VectorView_ & operator-=(const ELT &b)
Definition: BigMatrix.h:2046
RowVector_(int n)
Definition: BigMatrix.h:2233
const TNeg & negate() const
Definition: BigMatrix.h:938
K::TReal TReal
Definition: CompositeNumericalTypes.h:141
Here we declare the detailed interface to the Simmatrix classes.
void clear()
Definition: BigMatrix.h:1748
RowVectorBase(const MatrixHelper< Scalar > &h, const typename MatrixHelper< Scalar >::ShallowCopy &s)
Construct a read-only view of the source data.
Definition: BigMatrix.h:1571
THerm updTranspose()
Definition: BigMatrix.h:1451
VectorView_< Real > VectorView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3548
MatrixBase & copyAssign(const MatrixBase &b)
Copy assignment is a deep copy but behavior depends on type of lhs: if view, rhs must match...
Definition: BigMatrix.h:348
void resize(int m, int n)
DeadMatrixView_ & operator*=(const StdNumber &t)
Definition: BigMatrix.h:1866
MatrixBase< EStandard > TStandard
Definition: BigMatrix.h:264
CNT< E >::TNormalize ENormalize
Definition: BigMatrix.h:238
RowVectorView_< ELT > operator()(int j, int n) const
Definition: BigMatrix.h:1711
Vector_ & operator=(const VectorBase< EE > &m)
Definition: BigMatrix.h:2111
void writeUnformatted(std::ostream &o, const VectorBase< E > &v)
Specialize for VectorBase<E> to delegate to element type E, with spaces separating the elements...
Definition: BigMatrix.h:3261
RS is total spacing between rows in memory (default 1)
Definition: SymMat.h:71
const TNeg & negate() const
Definition: BigMatrix.h:1971
MatrixView_ & operator=(const MatrixView_ &m)
Definition: BigMatrix.h:1795
DeadMatrixView_ & operator=(const Base &m)
Definition: BigMatrix.h:1848
VectorIterator & operator=(const VectorIterator &iter)
Definition: BigMatrix.h:3600
const VectorBase< ELT > & getAsVectorBase() const
Definition: BigMatrix.h:982
This is the Vector class intended to appear in user code.
Definition: BigMatrix.h:186
RowVectorBase(const TNeg &source)
Implicit conversion from compatible row vector with negated elements.
Definition: BigMatrix.h:1526
CNT< E >::TImag EImag
Definition: BigMatrix.h:230
This is a dataless rehash of the MatrixBase class to specialize it for RowVectors.
Definition: BigMatrix.h:177
MatrixView_< Real > MatrixView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3576
const MatrixCommitment & getCharacterCommitment() const
Definition: BigMatrix.h:270
Matrix_< fComplex > fComplexMatrix
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3573
VectorBase(const MatrixHelper< Scalar > &h, const typename MatrixHelper< Scalar >::DeepCopy &d)
Construct a new owner vector initialized with the data from the source.
Definition: BigMatrix.h:1138
RowVectorView_< ELT > operator()(int j, int n)
Definition: BigMatrix.h:1712
VectorBase & operator=(const VectorBase< EE > &b)
Definition: BigMatrix.h:1166
RowVectorView_(MatrixHelper< S > &h)
Definition: BigMatrix.h:2176
Matrix_ & operator+=(const ELT &r)
Definition: BigMatrix.h:1968
DeadMatrixView_(const Base &m)
Definition: BigMatrix.h:1847
void resizeKeep(int m, int n)
RowVectorBase< EE >::template EltResult< ELT >::Dvd elementwiseDivideFromLeft(const RowVectorBase< EE > &v) const
Definition: BigMatrix.h:1671
RowVectorBase< EE >::template EltResult< ELT >::Mul elementwiseMultiplyFromLeft(const RowVectorBase< EE > &v) const
Definition: BigMatrix.h:1646
void elementwiseMultiplyFromLeft(const RowVectorBase< EE > &v, typename RowVectorBase< EE >::template EltResult< ELT >::Mul &out) const
Definition: BigMatrix.h:1638
MatrixHelper & copyAssign(const MatrixHelper &source)
DeadMatrixView_ & operator/=(const StdNumber &t)
Definition: BigMatrix.h:1867
std::istream & operator>>(std::istream &in, VectorView_< T > &out)
Read a (fixed size n) VectorView_<T> from a stream as a sequence of space- or comma-separated values ...
Definition: BigMatrix.h:3534
RowVectorBase & operator=(const ELT &t)
Fill current allocation with copies of element.
Definition: BigMatrix.h:1612
THerm operator~() const
Definition: BigMatrix.h:1731
Definition: BigMatrix.h:192
Matrix_(const Base &v)
Definition: BigMatrix.h:1925
VectorView_(MatrixHelper< S > &h)
Definition: BigMatrix.h:2030
EltResult< EE >::Mul colScale(const VectorBase< EE > &c) const
Definition: BigMatrix.h:562
CNT< E >::TNeg ENeg
Definition: BigMatrix.h:227
K::TSqrt TSqrt
Definition: CompositeNumericalTypes.h:154
VectorBase< ELT > & updAsVectorBase()
Definition: BigMatrix.h:984
bool readUnformatted(std::istream &in, Array_< T, X > &v)
Specialization of readUnformatted() for variable-length Array_<T,X>; continues reading whitespace-sep...
Definition: Array.h:3379
Vector_ & operator+=(const VectorBase< EE > &m)
Definition: BigMatrix.h:2113
RowVector_< double > dRowVector
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3557
MatrixHelper< Scalar > & updHelper()
Definition: BigMatrix.h:1042
MatrixBase & elementwiseSubtractScalarInPlace(const S &s)
Set M(i,j)-=s for every element of M and some value s.
CNT< E >::TStandard EStandard
Definition: BigMatrix.h:236
Definition: BigMatrix.h:280
std::random_access_iterator_tag iterator_category
Definition: BigMatrix.h:3595
static TSqrt sqrt(const K &t)
Definition: CompositeNumericalTypes.h:239
void commitTo(const MatrixCommitment &mc)
Change the handle commitment for this matrix handle; only allowed if the handle is currently clear...
Definition: BigMatrix.h:275
CNT< E >::THerm EHerm
Definition: BigMatrix.h:232
#define SimTK_ASSERT_ALWAYS(cond, msg)
Definition: ExceptionMacros.h:349
VectorBase & operator=(const VectorBase &b)
Copy assignment is deep copy but behavior depends on type of lhs: if view, rhs must match...
Definition: BigMatrix.h:1153
THerm operator~()
Definition: BigMatrix.h:1732
ScalarNormSq normSqr() const
This is the scalar Frobenius norm, and its square.
Definition: BigMatrix.h:899
RowVectorView_< ELT > row(int i) const
Definition: BigMatrix.h:2350
Matrix_ & operator=(const ELT &v)
Definition: BigMatrix.h:1957
VectorView_ & operator+=(const VectorBase< EE > &m)
Definition: BigMatrix.h:2038
const MatrixCharacter & getMatrixCharacter() const
MatrixBase< typename CNT< E >::TInvert > elementwiseInvert() const
Definition: BigMatrix.h:604
K::Scalar Scalar
Definition: CompositeNumericalTypes.h:160
Matrix_< Real > Matrix
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3569
Vector_(int m, const S *cppData, bool)
Construct a Vector which uses borrowed space with assumed element-to-element stride equal to the C++ ...
Definition: BigMatrix.h:2093
void elementwiseDivide(const MatrixBase< EE > &, typename EltResult< EE >::Dvd &) const
RowVectorView_< float > fRowVectorView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3563
MatrixView_(MatrixHelperRep< S > *hrep)
Definition: BigMatrix.h:1784
A MatrixCharacter is a set containing a value for each of the matrix characteristics except element t...
Definition: MatrixCharacteristics.h:603
void set(int i, const ELT &value)
Variant of operator[] that's scripting friendly to set ith entry.
Definition: BigMatrix.h:2133
void colScale(const VectorBase< EE > &v, typename EltResult< EE >::Mul &out) const
Definition: BigMatrix.h:1620
K::TNormalize TNormalize
Definition: CompositeNumericalTypes.h:158
VectorIterator operator-(ptrdiff_t n) const
Definition: BigMatrix.h:3650
bool operator<(VectorIterator iter) const
Definition: BigMatrix.h:3635
TStandard standardize() const
Return a Matrix of the same shape and contents as this one but with the element type converted to one...
Definition: BigMatrix.h:887
RowVectorView_< dComplex > dComplexRowVectorView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3567
MatrixView_ & operator/=(const StdNumber &t)
Definition: BigMatrix.h:1818
MatrixBase()
The default constructor builds a 0x0 matrix managed by a helper that understands how many scalars the...
Definition: BigMatrix.h:312
Matrix_ & operator-=(const ELT &r)
Definition: BigMatrix.h:1969
VectorIterator(VECTOR_CLASS &vector, ptrdiff_t index)
Definition: BigMatrix.h:3596
VectorView_< Complex > ComplexVectorView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3551
CNT< E >::TComplex EComplex
Definition: BigMatrix.h:231
MatrixBase & elementwiseDivideFromLeftInPlace(const MatrixBase< EE > &)
M(i,j) = R(i,j) / M(i,j); R must have same dimensions as this.
MatrixBase & rowAndColScaleInPlace(const VectorBase< ER > &r, const VectorBase< EC > &c)
M = diag(r) * M * diag(c); r must have nrow() elements; must have ncol() elements.
EAbs weightedNormInf(const VectorBase< EE > &w, int *worstOne=0) const
Return the weighted infinity norm (max absolute value) WInf of a Vector of scalars, with optional return of the index of the weighted element of largest absolute value.
Definition: BigMatrix.h:1299
MatrixView_ & operator-=(const ELT &r)
Definition: BigMatrix.h:1820
VectorView_(MatrixHelperRep< S > *hrep)
Definition: BigMatrix.h:2015
MatrixBase & elementwiseMultiplyFromLeftInPlace(const MatrixBase< EE > &)
M(i,j) = R(i,j) * M(i,j); R must have same dimensions as this.
const TNeg & operator-() const
Definition: BigMatrix.h:1463
Vector_(int m)
Definition: BigMatrix.h:2085
MatrixBase & elementwiseAssign(const S &s)
Set M(i,j)=s for every element of M and some value s.
MatrixView_< EHerm > operator~()
Definition: BigMatrix.h:763
Vector_ & operator+=(const ELT &b)
Definition: BigMatrix.h:2120
void replaceContiguousScalarData(const Scalar *newData, ptrdiff_t length)
Definition: BigMatrix.h:1027
Matrix_ & operator=(const Matrix_ &src)
Definition: BigMatrix.h:1919
VectorView_< ELT > updCol(int j)
Definition: BigMatrix.h:2341
int size() const
Definition: BigMatrix.h:1411
MatrixBase & operator/=(const StdNumber &t)
Definition: BigMatrix.h:442
void writeUnformatted(std::ostream &o, const Vector_< E > &v)
Raw serialization of Vector_<E>; same as VectorBase<E>.
Definition: BigMatrix.h:3277
CNT< ScalarNormSq >::TSqrt normRMS() const
We only allow RMS norm if the elements are scalars.
Definition: BigMatrix.h:908
void replaceContiguousScalarData(Scalar *newData, ptrdiff_t length, bool takeOwnership)
Definition: BigMatrix.h:1024
MatrixBase & resizeKeep(int m, int n)
Change the size of this matrix, retaining as much of the old data as will fit.
Definition: BigMatrix.h:956
VectorView_< ELT > operator()(int i, int m) const
Definition: BigMatrix.h:1430
MatrixView_< dComplex > dComplexMatrixView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3581
Scalar * updContiguousScalarData()
Definition: BigMatrix.h:1021
VectorBase< typename CNT< ELT >::template Result< P >::Add > Add
Definition: BigMatrix.h:1147
MatrixBase< EHerm > THerm
Definition: BigMatrix.h:260
K::TImag TImag
Definition: CompositeNumericalTypes.h:142
EltResult< EE >::Mul elementwiseMultiply(const VectorBase< EE > &v) const
Definition: BigMatrix.h:1351
CNT< E >::Number ENumber
Definition: BigMatrix.h:243
Definition: Exception.h:297
RowVectorView_ & operator=(const RowVectorView_ &r)
Definition: BigMatrix.h:2170
RowVectorView_< ELT > updRow(int i)
Definition: BigMatrix.h:2359
ELT & updElt(int i, int j)
Definition: BigMatrix.h:833
ELT E
Definition: BigMatrix.h:226
int getNScalarsPerElement() const
This is the number of consecutive scalars used to represent one element of type ELT.
Definition: BigMatrix.h:1007
VectorView_ & operator-=(const VectorBase< EE > &m)
Definition: BigMatrix.h:2040
RowVectorBase(MatrixHelper< Scalar > &h, const typename MatrixHelper< Scalar >::ShallowCopy &s)
Construct a writable view into the source data.
Definition: BigMatrix.h:1568
ptrdiff_t getContiguousScalarDataLength() const
Definition: BigMatrix.h:1015
int ncol() const
Definition: BigMatrix.h:1417
MatrixView_ & operator=(const ELT &e)
Definition: BigMatrix.h:1808
MatrixBase & viewAssign(const MatrixBase &src)
View assignment is a shallow copy, meaning that we disconnect the MatrixBase from whatever it used to...
Definition: BigMatrix.h:363
TNeg & operator-()
Definition: BigMatrix.h:942
RowVector_(int n, const ELT *cppInitialValues)
Definition: BigMatrix.h:2234
DeadMatrixView_ & operator=(const Matrix_< ELT > &v)
Definition: BigMatrix.h:1856
Matrix_(const Matrix_ &src)
Definition: BigMatrix.h:1915
VectorBase< typename CNT< ELT >::template Result< P >::Mul > Mul
Definition: BigMatrix.h:1145
bool fillUnformatted(std::istream &in, Matrix_< E > &v)
Read in new values for a Matrix without changing its size, from a stream of whitespace-separated toke...
Definition: BigMatrix.h:3377
Definition: BigMatrix.h:185
EltResult< S >::Sub elementwiseSubtractScalar(const S &s) const
Definition: BigMatrix.h:644
ptrdiff_t nelt() const
EltResult< typename VectorBase< ER >::template EltResult< EC >::Mul >::Mul rowAndColScale(const VectorBase< ER > &r, const VectorBase< EC > &c) const
Definition: BigMatrix.h:579
MatrixBase(const MatrixCommitment &commitment, const MatrixCharacter &character, int spacing, const Scalar *data)
Construct a read-only view of pre-existing data.
Definition: BigMatrix.h:408
MatrixBase(const MatrixCommitment &commitment, MatrixHelper< Scalar > &source, const typename MatrixHelper< Scalar >::ShallowCopy &shallow)
Definition: BigMatrix.h:423
void elementwiseMultiply(const VectorBase< EE > &v, typename EltResult< EE >::Mul &out) const
Definition: BigMatrix.h:1349
size_type size() const
Return the current number of elements stored in this array.
Definition: Array.h:2014
void elementwiseMultiplyFromLeft(const MatrixBase< EE > &, typename MatrixBase< EE >::template EltResult< E >::Mul &) const
Definition: BigMatrix.h:2564
This is a MatrixView_ with the additional property that we are about to delete it.
Definition: BigMatrix.h:180
VectorBase< EE >::template EltResult< ELT >::Mul elementwiseMultiplyFromLeft(const VectorBase< EE > &v) const
Definition: BigMatrix.h:1365
void elementwiseDivideFromLeft(const MatrixBase< EE > &, typename MatrixBase< EE >::template EltResult< E >::Dvd &) const
Definition: BigMatrix.h:2613
VectorView_< ELT > operator()(int j)
Definition: BigMatrix.h:747
EltResult< EE >::Mul elementwiseMultiply(const RowVectorBase< EE > &v) const
Definition: BigMatrix.h:1631
MatrixBase(const MatrixCommitment &commitment, int m, int n)
This constructor takes a handle commitment and allocates the default matrix for that kind of commitme...
Definition: BigMatrix.h:332
RowVectorBase & operator-=(const RowVectorBase &r)
Definition: BigMatrix.h:1598
void colSum(int j, S *eltp) const
Vector_< double > dVector
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3543
void swapOwnedContiguousData(S *newData, ptrdiff_t length, S *&oldData)
void writeUnformatted(std::ostream &o, const VectorView_< E > &v)
Raw serialization of VectorView_<E>; same as VectorBase<E>.
Definition: BigMatrix.h:3271
MatrixBase & scalarMultiplyInPlace(const S &)
Set M(i,j) = M(i,j)*S for some "scalar" S.
void elementwiseMultiplyFromLeft(const VectorBase< EE > &v, typename VectorBase< EE >::template EltResult< ELT >::Mul &out) const
Definition: BigMatrix.h:1358
THerm transpose() const
Definition: BigMatrix.h:1728
VectorView_ & operator*=(const StdNumber &t)
Definition: BigMatrix.h:2043
Vector_< dComplex > dComplexVector
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3546
RowVectorView_ & operator+=(const RowVectorBase< EE > &m)
Definition: BigMatrix.h:2184
VectorView_< double > dVectorView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3550
CNT< E >::TWithoutNegator EWithoutNegator
Definition: BigMatrix.h:228
RowVector_< fComplex > fComplexRowVector
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3559
MatrixBase & operator-=(const MatrixBase &r)
Definition: BigMatrix.h:444
MatrixBase & operator*=(const StdNumber &t)
Definition: BigMatrix.h:441
S * updElt(int i, int j)
void scaleBy(const StdNumber &)
Vector_< float > fVector
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3542
Matrix_< ELT > & updAsMatrix()
Definition: BigMatrix.h:972
static TStandard standardize(const K &t)
Definition: CompositeNumericalTypes.h:241
Vector_ & operator*=(const StdNumber &t)
Definition: BigMatrix.h:2118
CNT< ScalarNormSq >::TSqrt norm() const
Definition: BigMatrix.h:902
void matmul(const StdNumber &beta, const StdNumber &alpha, const MatrixHelper< SA > &A, const MatrixHelper< SB > &B)
VectorView_< float > fVectorView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3549
void getAnyElt(int i, int j, ELT &value) const
This returns a copy of the element value for any position in the logical matrix, regardless of whethe...
Definition: BigMatrix.h:842
RowVector_(int n, const ELT &initialValue)
Definition: BigMatrix.h:2235
RowVectorBase & operator+=(const RowVectorBase &r)
Definition: BigMatrix.h:1597
RowVectorView_ & operator=(const RowVectorBase< EE > &m)
Definition: BigMatrix.h:2182
TNeg & operator-()
Definition: BigMatrix.h:1464
DeadMatrixView_ & operator=(const ELT &e)
Definition: BigMatrix.h:1857
std::istream & operator>>(std::istream &in, Vector_< T > &out)
Read Vector_<T> from a stream as a sequence of space- or comma-separated values of type T...
Definition: BigMatrix.h:3523
VectorBase & operator-=(const VectorBase< EE > &b)
Definition: BigMatrix.h:1170
VectorBase & operator+=(const VectorBase &r)
Definition: BigMatrix.h:1162
ELT & operator()(int j)
Definition: BigMatrix.h:1708
unsigned char square(unsigned char u)
Definition: Scalar.h:351
VectorView_< ELT > updIndex(const Array_< int > &indices)
Definition: BigMatrix.h:1440
void elementwiseDivideFromLeft(const VectorBase< EE > &v, typename VectorBase< EE >::template EltResult< ELT >::Dvd &out) const
Definition: BigMatrix.h:1384
Definition: BigMatrix.h:1580
RowVectorBase & operator=(const RowVectorBase< EE > &b)
Definition: BigMatrix.h:1600
void abs(TAbs &mabs) const
abs() is elementwise absolute value; that is, the return value has the same dimension as this Matrix ...
Definition: BigMatrix.h:864
const Matrix_< ELT > & getAsMatrix() const
Definition: BigMatrix.h:971
VectorBase & operator+=(const VectorBase< EE > &b)
Definition: BigMatrix.h:1168
const RowVector_< ELT > & getAsRowVector() const
Definition: BigMatrix.h:991
This class is identical to a RowVector_; it is used only to manage the C++ rules for when copy constr...
Definition: BigMatrix.h:189
Matrix_< dComplex > dComplexMatrix
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3574
CNT< E >::TInvert EInvert
Definition: BigMatrix.h:237
RowVectorView_< ELT > operator[](int i)
Definition: BigMatrix.h:745
void set(int i, int j, const ELT &value)
Variant of indexing operator that's scripting friendly to set entry (i, j)
Definition: BigMatrix.h:1987
RowVector_(int n, S *cppData, bool)
Definition: BigMatrix.h:2242
Vector_< fComplex > fComplexVector
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3545
VectorBase(const VectorBase &source)
Copy constructor is a deep copy (not appropriate for views!).
Definition: BigMatrix.h:1087
Vector_(int m, S *cppData, bool)
Definition: BigMatrix.h:2094
void writeUnformatted(std::ostream &o, const RowVector_< E > &v)
Raw serialization of RowVector_<E>; same as Vector_<E>.
Definition: BigMatrix.h:3296
Matrix_()
Definition: BigMatrix.h:1911
MatrixBase< typename CNT< E >::template Result< P >::Sub > Sub
Definition: BigMatrix.h:284
RowVectorBase(const RowVectorBase &source)
Copy constructor is a deep copy (not appropriate for views!).
Definition: BigMatrix.h:1523
ELEM sum(const VectorBase< ELEM > &v)
Definition: VectorMath.h:147
VectorIterator operator+(ptrdiff_t n) const
Definition: BigMatrix.h:3653
RowVector_< ELT > & updAsRowVector()
Definition: BigMatrix.h:993
VectorBase & elementwiseDivideFromLeftInPlace(const VectorBase< EE > &r)
Definition: BigMatrix.h:1381
K::TSqTHerm TSqTHerm
Definition: CompositeNumericalTypes.h:147
MatrixBase< EComplex > TComplex
Definition: BigMatrix.h:259
VectorBase< typename CNT< ELT >::template Result< P >::Dvd > Dvd
Definition: BigMatrix.h:1146
Matrix_ & operator=(const MatrixBase< EE > &m)
Definition: BigMatrix.h:1959
void copyInByRowsFromCpp(const S *elts)
Vector_ & operator-=(const VectorBase< EE > &m)
Definition: BigMatrix.h:2115
const TNeg & operator-() const
Definition: BigMatrix.h:1741
VectorBase(const MatrixHelper< Scalar > &h, const typename MatrixHelper< Scalar >::ShallowCopy &s)
Construct a read-only view of the source data.
Definition: BigMatrix.h:1135
RowVector_ & operator+=(const ELT &b)
Definition: BigMatrix.h:2268
void fillWith(const S *eltp)
VectorBase(const TNeg &source)
Implicit conversion from compatible vector with negated elements.
Definition: BigMatrix.h:1090
RowVectorBase(int n, const ELT &initialValue)
Construct an owner row vector of length n, with each element initialized to the given value...
Definition: BigMatrix.h:1530
CNT< E >::StdNumber EStdNumber
Definition: BigMatrix.h:244
MatrixView_< EHerm > transpose() const
Definition: BigMatrix.h:2302
MatrixView_< ELT > updBlock(int i, int j, int m, int n)
Definition: BigMatrix.h:2291
VectorBase & rowScaleInPlace(const VectorBase< EE > &v)
There's only one column here so it's a bit weird to use rowScale rather than elementwiseMultiply, but there's nothing really wrong with it.
Definition: BigMatrix.h:1183
This is a fixed length column vector designed for no-overhead inline computation. ...
Definition: Vec.h:131
DeadMatrixView_ & operator+=(const MatrixBase< EE > &m)
Definition: BigMatrix.h:1861
void writeUnformatted(std::ostream &o, const MatrixView_< E > &v)
Raw serialization of MatrixView_<E>; same as MatrixBase<E>.
Definition: BigMatrix.h:3313
MatrixView_ & operator+=(const MatrixBase< EE > &m)
Definition: BigMatrix.h:1812
Vector_(const Base &src)
Definition: BigMatrix.h:2075
This is an iterator for iterating over the elements of a Vector.
Definition: BigMatrix.h:194
Vector_(const Vec< M, ELT > &v)
Convert a Vec to a Vector_.
Definition: BigMatrix.h:2104
EltResult< S >::Add elementwiseAddScalar(const S &s) const
Definition: BigMatrix.h:624
THerm operator~()
Definition: BigMatrix.h:1454
VectorView_< ELT > operator()(const Array_< int > &indices)
Definition: BigMatrix.h:1447
VectorView_< ELT > diag()
This non-const version of diag() is an alternate name for updDiag() available for historical reasons...
Definition: BigMatrix.h:773
int nrow() const
Return the number of rows m in the logical shape of this matrix.
Definition: BigMatrix.h:288
void rowSum(int i, S *eltp) const
Definition: BigMatrix.h:182
This is the default commitment for a row vector.
Definition: MatrixCharacteristics.h:1010
ELT sum() const
Definition: BigMatrix.h:1472
RowVector_< ELT > colSum() const
Form the column sums of this matrix, returned as a RowVector.
Definition: BigMatrix.h:917
MatrixBase< EImag > TImag
Definition: BigMatrix.h:258
void elementwiseSubtractScalar(const S &s, typename EltResult< S >::Sub &) const
K::Precision Precision
Definition: CompositeNumericalTypes.h:164
RowVectorBase(MatrixHelperRep< Scalar > *hrep)
Definition: BigMatrix.h:1760
Matrix_(const MatrixCommitment &mc)
Definition: BigMatrix.h:1912
RowVector_ & operator=(const ELT &v)
Definition: BigMatrix.h:2257
VectorView_< ELT > operator()(const Array_< int > &indices) const
Definition: BigMatrix.h:1446
VectorView_ & operator=(const Base &b)
Definition: BigMatrix.h:2032
TNeg & updNegate()
Definition: BigMatrix.h:939
RowVector_ & operator-=(const ELT &b)
Definition: BigMatrix.h:2269
VectorIterator operator++(int)
Definition: BigMatrix.h:3618
Vector_< ELT > rowSum() const
Form the row sums of this matrix, returned as a Vector.
Definition: BigMatrix.h:928
Vector_< Real > Vector
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3541
RowVectorView_ & operator=(const Base &b)
Definition: BigMatrix.h:2178
void writeUnformatted(std::ostream &o, const RowVectorView_< E > &v)
Raw serialization of RowVectorView_<E>; same as VectorView_<E>.
Definition: BigMatrix.h:3290
MatrixBase< E > TPosTrans
Definition: BigMatrix.h:261
VectorBase & elementwiseMultiplyFromLeftInPlace(const VectorBase< EE > &r)
Definition: BigMatrix.h:1355
RowVectorBase & elementwiseMultiplyInPlace(const RowVectorBase< EE > &r)
Definition: BigMatrix.h:1627
RowVector_ & operator*=(const StdNumber &t)
Definition: BigMatrix.h:2266
This class is identical to a Matrix_; it is used only to manage the C++ rules for when copy construct...
Definition: BigMatrix.h:179
void dump(const char *msg=0) const
Matlab-compatible debug output.
Definition: BigMatrix.h:795
Inertia_< P > operator*(const Inertia_< P > &i, const P &r)
Multiply an inertia matrix by a scalar.
Definition: MassProperties.h:588
ScalarNormSq scalarNormSqr() const
Scalar norm square is sum( squares of all scalars ).
Definition: BigMatrix.h:850
RowVector_< ELT > sum() const
Alternate name for colSum(); behaves like the Matlab function sum().
Definition: BigMatrix.h:925
EltResult< EE >::Mul elementwiseMultiply(const MatrixBase< EE > &m) const
Definition: BigMatrix.h:681
void elementwiseDivide(const Row< 1, E1, S1 > &r1, const Row< 1, E2, S2 > &r2, Row< 1, typename CNT< E1 >::template Result< E2 >::Dvd > &result)
Definition: Row.h:90
RowVectorBase< typename CNT< ELT >::template Result< P >::Sub > Sub
Definition: BigMatrix.h:1584
MatrixBase & elementwiseAssign(int s)
Overloaded to allow an integer argument, which is converted to Real.
Definition: BigMatrix.h:596
VectorIterator operator--()
Definition: BigMatrix.h:3624
RowVectorBase< typename CNT< ELT >::template Result< P >::Mul > Mul
Definition: BigMatrix.h:1581
RowVectorView_ & operator-=(const ELT &b)
Definition: BigMatrix.h:2192
DeadMatrixView_ & operator=(const MatrixBase< EE > &m)
Definition: BigMatrix.h:1859
RowVector_(int n, int stride, const S *data, bool)
Borrowed-space construction with explicit stride supplied as "number of scalars between elements"...
Definition: BigMatrix.h:2247
MatrixView_< fComplex > fComplexMatrixView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3580
const ELT & operator[](int j) const
Definition: BigMatrix.h:1705
K::TInvert TInvert
Definition: CompositeNumericalTypes.h:157
MatrixBase & scalarAddInPlace(const S &s)
Add a scalar to M's diagonal.
Definition: BigMatrix.h:486
const RowVectorBase< ELT > & getAsRowVectorBase() const
Definition: BigMatrix.h:995
MatrixView_< ELT > block(int i, int j, int m, int n) const
Definition: BigMatrix.h:2280
CNT< E >::TSqHermT ESqHermT
Definition: BigMatrix.h:239
MatrixView_< EHerm > updTranspose()
Definition: BigMatrix.h:2310
Matrix_(int m, int n, const ELT &initialValue)
Definition: BigMatrix.h:1939
EltResult< EE >::Mul rowScale(const VectorBase< EE > &v) const
Definition: BigMatrix.h:1187
VectorView_ & operator=(const ELT &v)
Definition: BigMatrix.h:2034
MatrixView_(const MatrixView_ &m)
Definition: BigMatrix.h:1789
RowVectorView_< ELT > updIndex(const Array_< int > &indices)
Definition: BigMatrix.h:1719
This is a user-includable header which includes everything needed to make use of SimMatrix Scalar cod...
VectorView_ & operator=(const VectorView_ &v)
Definition: BigMatrix.h:2024
EltResult< EE >::Mul colScale(const VectorBase< EE > &v) const
Definition: BigMatrix.h:1622
RowVector_ & operator/=(const StdNumber &t)
Definition: BigMatrix.h:2267
VectorBase(MatrixHelper< Scalar > &h, const typename MatrixHelper< Scalar >::ShallowCopy &s)
Construct a writable view into the source data.
Definition: BigMatrix.h:1132
CNT< E >::TReal EReal
Definition: BigMatrix.h:229
VectorView_< ELT > updDiag()
Select main diagonal (of largest leading square if rectangular) and return it as a writable view of t...
Definition: BigMatrix.h:2325
Matrix_(int m, int n, int leadingDim, const S *data)
Definition: BigMatrix.h:1942
void subIn(const MatrixHelper &)
Vector_ & operator=(const ELT &v)
Definition: BigMatrix.h:2109
MatrixBase(const MatrixCommitment &commitment, const MatrixCharacter &character, int spacing, Scalar *data)
Construct a writable view of pre-existing data.
Definition: BigMatrix.h:415
VectorBase & resizeKeep(int m)
Definition: BigMatrix.h:1467
TNeg & operator-()
Definition: BigMatrix.h:1742
ENumber Number
Definition: BigMatrix.h:249
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
MatrixBase & scalarSubtractFromLeftInPlace(const S &s)
Set M(i,i) = S - M(i,i), M(i,j) = -M(i,j) for i!=j.
Definition: BigMatrix.h:502
MatrixBase< typename CNT< E >::template Result< P >::Add > Add
Definition: BigMatrix.h:283
RowVectorBase< ELT > & updAsRowVectorBase()
Definition: BigMatrix.h:997
RowVectorBase< typename CNT< ELT >::template Result< P >::Dvd > Dvd
Definition: BigMatrix.h:1582
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: BigMatrix.h:1618
VectorView_(const MatrixHelper< S > &h)
Definition: BigMatrix.h:2029
RowVectorBase & operator-=(const RowVectorBase< EE > &b)
Definition: BigMatrix.h:1604
MatrixBase(const TNeg &b)
Implicit conversion from matrix with negated elements (otherwise this is just like the copy construct...
Definition: BigMatrix.h:342
MatrixBase< typename CNT< E >::template Result< P >::Dvd > Dvd
Definition: BigMatrix.h:282
RowVectorView_(const RowVectorView_ &r)
Definition: BigMatrix.h:2166
TNeg & updNegate()
Definition: BigMatrix.h:1461
VectorBase & elementwiseDivideInPlace(const VectorBase< EE > &r)
Definition: BigMatrix.h:1373
RowVectorBase & resize(int n)
Definition: BigMatrix.h:1744
MatrixBase< E > T
Definition: BigMatrix.h:254
VectorIterator< ELT, RowVectorBase< ELT > > begin()
Definition: BigMatrix.h:1751
#define SimTK_THROW1(exc, a1)
Definition: Exception.h:311
void rowScale(const VectorBase< EE > &r, typename EltResult< EE >::Mul &out) const
Return type is a new matrix which will have the same dimensions as 'this' but will have element types...
TNeg & operator-()
Definition: BigMatrix.h:1975
RowVectorView_ & operator-=(const RowVectorBase< EE > &m)
Definition: BigMatrix.h:2186
Vector_ & operator=(const Vector_ &src)
Definition: BigMatrix.h:2080
VectorView_< ELT > index(const Array_< int > &indices) const
Definition: BigMatrix.h:1435
VectorBase(int m, int stride, Scalar *s)
Construct a writable view into existing data.
Definition: BigMatrix.h:1119
VectorIterator< ELT, RowVectorBase< ELT > > end()
Definition: BigMatrix.h:1754
MatrixBase< EInvert > TInvert
Definition: BigMatrix.h:265
RowVectorBase & elementwiseDivideInPlace(const RowVectorBase< EE > &r)
Definition: BigMatrix.h:1653
MatrixView_ & operator=(const Matrix_< ELT > &v)
Definition: BigMatrix.h:1807
int nrow() const
Definition: BigMatrix.h:1416
const ELT & getElt(int i, int j) const
Element selection for stored elements.
Definition: BigMatrix.h:832
MatrixBase< ENormalize > TNormalize
Definition: BigMatrix.h:266
CNT< E >::TPosTrans EPosTrans
Definition: BigMatrix.h:233
MatrixBase< EAbs > TAbs
Definition: BigMatrix.h:263
int nrow() const
Definition: BigMatrix.h:1695
RowVector_ & operator-=(const RowVectorBase< EE > &b)
Definition: BigMatrix.h:2263
const RowVectorBase & operator+() const
Definition: BigMatrix.h:1734
void lockShape()
Definition: BigMatrix.h:960
RowVectorBase & elementwiseDivideFromLeftInPlace(const RowVectorBase< EE > &r)
Definition: BigMatrix.h:1661
RowVectorBase & operator/=(const StdNumber &t)
Definition: BigMatrix.h:1596
K::TPosTrans TPosTrans
Definition: CompositeNumericalTypes.h:145
EStdNumber StdNumber
Definition: BigMatrix.h:250
MatrixHelperRep< S > * stealRep()
Definition: MatrixHelper.h:341
VectorIterator operator--(int)
Definition: BigMatrix.h:3629
RowVectorView_(const MatrixHelper< S > &h)
Definition: BigMatrix.h:2175
RowVectorView_< Real > RowVectorView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3562
TInvert invert() const
Definition: BigMatrix.h:786
RowVector_ & operator=(const RowVector_ &src)
Definition: BigMatrix.h:2228
MatrixBase< EWithoutNegator > TWithoutNegator
Definition: BigMatrix.h:256
Definition: BigMatrix.h:306
void writeUnformatted(std::ostream &o, const Matrix_< E > &v)
Raw serialization of Vector_<E>; same as VectorBase<E>.
Definition: BigMatrix.h:3319
MatrixBase< EReal > TReal
Definition: BigMatrix.h:257
void elementwiseMultiply(const Row< 1, E1, S1 > &r1, const Row< 1, E2, S2 > &r2, Row< 1, typename CNT< E1 >::template Result< E2 >::Mul > &result)
Definition: Row.h:75
RowVector_()
Definition: BigMatrix.h:2216
bool isResizeable() const
Return true if either dimension of this Matrix is resizable.
Definition: BigMatrix.h:302
Here we declare the classes needed for managing the properties of matrices, which we call Matrix Char...
This file is the user-includeable header to be included in user programs to provide fixed-length Vec ...
EltResult< EE >::Mul rowScale(const VectorBase< EE > &r) const
Definition: BigMatrix.h:549
const TNeg & operator-() const
Definition: BigMatrix.h:941
const MatrixView_< ELT > & getAsMatrixView() const
Definition: BigMatrix.h:969
MatrixView_< double > dMatrixView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3578
MatrixBase & elementwiseInvertInPlace()
Set M(i,j) = M(i,j)^-1.
Definition: BigMatrix.h:2441
MatrixBase & rowScaleInPlace(const VectorBase< EE > &)
M = diag(r) * M; r must have nrow() elements.
int size() const
Definition: BigMatrix.h:1690
Vector_(const Vector_ &src)
Definition: BigMatrix.h:2072
MatrixHelper & writableViewAssign(MatrixHelper &source)
const Vector_< ELT > & getAsVector() const
Definition: BigMatrix.h:978
CNT< ScalarNormSq >::TSqrt weightedNormRMS(const VectorBase< EE > &w, int *worstOne=0) const
Return the weighted root-mean-square (WRMS) norm of a Vector of scalars, with optional return of the ...
Definition: BigMatrix.h:1231
RowVectorView_< Complex > ComplexRowVectorView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3565
This is the Matrix class intended to appear in user code.
Definition: BigMatrix.h:181
MatrixView_(const MatrixHelper< S > &h)
Definition: BigMatrix.h:1804
int getPackedSizeofElement() const
This is like sizeof(ELT), but returning the number of bytes we use to store the element which may be ...
Definition: BigMatrix.h:1012
Matrix_< Complex > ComplexMatrix
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3572
ELT & operator()(int i)
Definition: BigMatrix.h:1427
CNT< E >::TSqTHerm ESqTHerm
Definition: BigMatrix.h:240
RowVectorBase & operator=(const RowVectorBase &b)
Copy assignment is deep copy but behavior depends on type of lhs: if view, rhs must match...
Definition: BigMatrix.h:1589
MatrixView_< EHerm > operator~() const
Definition: BigMatrix.h:762
Definition: BigMatrix.h:190
DeadMatrixView_(MatrixHelperRep< S > *hrep)
Definition: BigMatrix.h:1846
MatrixBase & elementwiseAddScalarInPlace(const S &s)
Set M(i,j)+=s for every element of M and some value s.
RowVectorView_< ELT > index(const Array_< int > &indices) const
Definition: BigMatrix.h:1715
MatrixBase & operator+=(const MatrixBase &r)
Definition: BigMatrix.h:443
Definition: BigMatrix.h:187
K::StdNumber StdNumber
Definition: CompositeNumericalTypes.h:163
ELT getAnyElt(int i, int j) const
Definition: BigMatrix.h:844
DeadMatrixView_ & operator-=(const ELT &r)
Definition: BigMatrix.h:1869
Matrix_(const BaseNeg &v)
Definition: BigMatrix.h:1929
const S * getElt(int i, int j) const
RowVectorBase< typename CNT< ELEM >::TAbs > abs(const RowVectorBase< ELEM > &v)
Definition: VectorMath.h:120
void colScale(const VectorBase< EE > &c, typename EltResult< EE >::Mul &out) const
MatrixBase & operator=(const MatrixBase< EE > &b)
Definition: BigMatrix.h:449
void elementwiseMultiply(const MatrixBase< EE > &, typename EltResult< EE >::Mul &) const
MatrixBase & setToZero()
Definition: BigMatrix.h:736
MatrixBase< S >::template EltResult< E >::Sub elementwiseSubtractFromScalar(const S &s) const
Definition: BigMatrix.h:667
MatrixBase(const MatrixCommitment &commitment, int m, int n, const ELT &initialValue)
Initializing constructor with all of the initially-allocated elements initialized to the same value...
Definition: BigMatrix.h:376
bool hasContiguousData() const
Matrix_(int m, int n)
Definition: BigMatrix.h:1935
THerm updTranspose()
Definition: BigMatrix.h:1729
void elementwiseAddScalar(const S &s, typename EltResult< S >::Add &) const
Specialized information about Composite Numerical Types which allows us to define appropriate templat...
Definition: CompositeNumericalTypes.h:136
const RowVectorView_< ELT > & getAsRowVectorView() const
Definition: BigMatrix.h:987
RowVectorBase(int n, int stride, Scalar *s)
Construct a writable view into existing data.
Definition: BigMatrix.h:1555
Matrix_(int m, int n, const ELT *cppInitialValuesByRow)
Definition: BigMatrix.h:1937
ptrdiff_t operator-(VectorIterator iter) const
Definition: BigMatrix.h:3647
MatrixBase & operator-=(const MatrixBase< EE > &b)
Definition: BigMatrix.h:453
bool operator>=(VectorIterator iter) const
Definition: BigMatrix.h:3644
int ncol() const
void invertInPlace()
Definition: BigMatrix.h:792
TAbs abs() const
Definition: BigMatrix.h:1421
Generic Row.
Definition: Row.h:118
void addIn(const MatrixHelper &)
DeadMatrixView_ & operator-=(const MatrixBase< EE > &m)
Definition: BigMatrix.h:1863
VectorIterator(const VectorIterator &iter)
Definition: BigMatrix.h:3598
Matrix_ & operator+=(const MatrixBase< EE > &m)
Definition: BigMatrix.h:1961
MatrixView_ & operator*=(const StdNumber &t)
Definition: BigMatrix.h:1817
MatrixBase(const MatrixCommitment &commitment)
This constructor takes a handle commitment and allocates the default matrix for that kind of commitme...
Definition: BigMatrix.h:324
static std::istream & fillVectorViewFromStream(std::istream &in, VectorView_< T > &out)
Read in a fixed number of elements from a stream into an VectorView.
Definition: BigMatrix.h:3509
MatrixView_< Complex > ComplexMatrixView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3579
CNT< E >::ScalarNormSq EScalarNormSq
Definition: BigMatrix.h:246
K T
Definition: CompositeNumericalTypes.h:138
Matrix_ & operator/=(const StdNumber &t)
Definition: BigMatrix.h:1967
EAbs normInf(int *worstOne=0) const
Return the infinity norm (max absolute value) of a Vector of scalars, with optional return of the ind...
Definition: BigMatrix.h:1266
bool operator>(VectorIterator iter) const
Definition: BigMatrix.h:3638
VectorView_< fComplex > fComplexVectorView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3552
RowVectorView_ & operator*=(const StdNumber &t)
Definition: BigMatrix.h:2189
VectorBase(MatrixHelperRep< Scalar > *hrep)
Definition: BigMatrix.h:1482
MatrixBase & scalarDivideInPlace(const S &)
Set M(i,j) = M(i,j)/S for some "scalar" S.
MatrixBase< EE >::template EltResult< EE >::Dvd elementwiseDivideFromLeft(const MatrixBase< EE > &m) const
Definition: BigMatrix.h:727
VectorView_< ELT > diag() const
Select main diagonal (of largest leading square if rectangular) and return it as a read-only view of ...
Definition: BigMatrix.h:2318
RowVectorView_(MatrixHelperRep< S > *hrep)
Definition: BigMatrix.h:2161
VectorView_< ELT > operator()(int i, int m)
Definition: BigMatrix.h:1431
MatrixBase & operator=(const MatrixBase &b)
Definition: BigMatrix.h:352
VectorView_ & operator+=(const ELT &b)
Definition: BigMatrix.h:2045
MatrixBase(int m, int n)
This constructor allocates the default matrix a completely uncommitted matrix commitment, given particular initial dimensions.
Definition: BigMatrix.h:316
const MatrixHelper< Scalar > & getHelper() const
Definition: BigMatrix.h:1041
ptrdiff_t getContiguousDataLength() const
RowVector_(const Row< M, ELT > &v)
Convert a Row to a RowVector_.
Definition: BigMatrix.h:2252
void rowScale(const VectorBase< EE > &v, typename EltResult< EE >::Mul &out) const
Definition: BigMatrix.h:1185
RowVector_ & operator+=(const RowVectorBase< EE > &b)
Definition: BigMatrix.h:2261
VectorBase & operator=(const ELT &t)
Fill current allocation with copies of element.
Definition: BigMatrix.h:1177
void clear()
Definition: BigMatrix.h:1470
RowVectorView_ & operator+=(const ELT &b)
Definition: BigMatrix.h:2191
CNT< E >::TAbs EAbs
Definition: BigMatrix.h:235
ELT & operator[](int j)
Definition: BigMatrix.h:1706
ELT & operator[](ptrdiff_t i)
Definition: BigMatrix.h:3609
void getAnyElt(int i, int j, S *value) const
void writeUnformatted(std::ostream &o, const RowVectorBase< E > &v)
Specialize for RowVectorBase<E> to delegate to element type E, with spaces separating the elements; r...
Definition: BigMatrix.h:3284
VectorView_ & operator/=(const StdNumber &t)
Definition: BigMatrix.h:2044
K::TNeg TNeg
Definition: CompositeNumericalTypes.h:139
Matrix_< float > fMatrix
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3570
This is a dataless rehash of the MatrixBase class to specialize it for Vectors.
Definition: BigMatrix.h:176
void elementwiseMultiply(const RowVectorBase< EE > &v, typename EltResult< EE >::Mul &out) const
Definition: BigMatrix.h:1629
MatrixBase & scalarDivideFromLeftInPlace(const S &)
Set M(i,j) = S/M(i,j) for some "scalar" S.
VectorBase(int m, int stride, const Scalar *s)
Construct a read-only view of existing data.
Definition: BigMatrix.h:1116
RowVector_(int n, const S *cppData, bool)
Construct a Vector which uses borrowed space with assumed element-to-element stride equal to the C++ ...
Definition: BigMatrix.h:2241
K::TStandard TStandard
Definition: CompositeNumericalTypes.h:156
#define SimTK_INDEXCHECK(ix, ub, where)
Definition: ExceptionMacros.h:145
MatrixView_(MatrixHelper< S > &h)
Definition: BigMatrix.h:1805
Definition: BigMatrix.h:305
K::TWithoutNegator TWithoutNegator
Definition: CompositeNumericalTypes.h:140
CNT< E >::Scalar EScalar
Definition: BigMatrix.h:242
const Scalar * getContiguousScalarData() const
Definition: BigMatrix.h:1018
RowVectorView_< ELT > operator[](int i) const
Definition: BigMatrix.h:744
RowVector_(int n, int stride, S *data, bool)
Definition: BigMatrix.h:2248
VectorIterator< ELT, VectorBase< ELT > > end()
Definition: BigMatrix.h:1476
RowVectorBase(int n, int stride, const Scalar *s)
Construct a read-only view of existing data.
Definition: BigMatrix.h:1552
RowVectors are much less common than Vectors.
Definition: BigMatrix.h:191
VectorBase< EE >::template EltResult< ELT >::Dvd elementwiseDivideFromLeft(const VectorBase< EE > &v) const
Definition: BigMatrix.h:1391
DeadMatrixView_ & operator+=(const ELT &r)
Definition: BigMatrix.h:1868
const TNeg & operator-() const
Definition: BigMatrix.h:1974
const ELT & operator[](int i) const
Definition: BigMatrix.h:1424
Vector_()
Definition: BigMatrix.h:2068
TNeg & updNegate()
Definition: BigMatrix.h:1972
MatrixView_ & operator=(const MatrixBase< EE > &m)
Definition: BigMatrix.h:1810
void commitTo(const MatrixCommitment &)
std::string toString() const
toString() returns a string representation of the Vector_.
Definition: BigMatrix.h:2125
static std::istream & readVectorFromStreamHelper(std::istream &in, bool isFixedSize, Vector_< T > &out)
Definition: BigMatrix.h:3058
A MatrixCommitment provides a set of acceptable matrix characteristics.
Definition: MatrixCharacteristics.h:837
ELT & operator*()
Definition: BigMatrix.h:3605
MatrixBase & matmul(const StdNumber &beta, const StdNumber &alpha, const MatrixBase< ELT_A > &A, const MatrixBase< ELT_B > &B)
Definition: BigMatrix.h:817
VectorView_< ELT > & updAsVectorView()
Definition: BigMatrix.h:976
void swapOwnedContiguousScalarData(Scalar *newData, ptrdiff_t length, Scalar *&oldData)
Definition: BigMatrix.h:1030
VectorView_ & operator=(const VectorBase< EE > &m)
Definition: BigMatrix.h:2036
const ELT & operator()(int i) const
Definition: BigMatrix.h:1426
MatrixBase< EE >::template EltResult< E >::Mul elementwiseMultiplyFromLeft(const MatrixBase< EE > &m) const
Definition: BigMatrix.h:697
RowVectorBase(const MatrixHelper< Scalar > &h, const typename MatrixHelper< Scalar >::DeepCopy &d)
Construct a new owner vector initialized with the data from the source.
Definition: BigMatrix.h:1574
ELT sum() const
Definition: BigMatrix.h:1750
const ELT & operator()(int j) const
Definition: BigMatrix.h:1707
THerm operator~() const
Definition: BigMatrix.h:1453
void elementwiseDivideFromLeft(const RowVectorBase< EE > &v, typename RowVectorBase< EE >::template EltResult< ELT >::Dvd &out) const
Definition: BigMatrix.h:1665
bool operator==(VectorIterator iter) const
Definition: BigMatrix.h:3656
RowVectorView_< ELT > operator()(const Array_< int > &indices) const
Definition: BigMatrix.h:1724
void replaceContiguousData(S *newData, ptrdiff_t length, bool takeOwnership)
Vector_(const BaseNeg &src)
Definition: BigMatrix.h:2076
static std::istream & readVectorFromStream(std::istream &in, Vector_< T > &out)
Read in a Vector_<T> from a stream, as a sequence of space-separated or comma-separated values option...
Definition: BigMatrix.h:3471
MatrixBase(MatrixHelperRep< Scalar > *hrep)
Helper rep-stealing constructor.
Definition: BigMatrix.h:1038
Vector_ & operator/=(const StdNumber &t)
Definition: BigMatrix.h:2119
const VectorBase & operator+() const
Definition: BigMatrix.h:1456
ptrdiff_t nelt() const
Return the number of elements in the logical shape of this matrix.
Definition: BigMatrix.h:299
void elementwiseSubtractFromScalar(const S &, typename MatrixBase< S >::template EltResult< E >::Sub &) const
Definition: BigMatrix.h:2515
TAbs abs() const
abs() with the result as a function return.
Definition: BigMatrix.h:875
CoordinateDirection operator+(const CoordinateAxis &axis)
Create the positive direction along the given axis.
Definition: CoordinateAxis.h:390
VectorBase & resize(int m)
Definition: BigMatrix.h:1466
bool isResizeable() const
Definition: MatrixCharacteristics.h:930
bool operator<=(VectorIterator iter) const
Definition: BigMatrix.h:3641
MatrixView_ & operator+=(const ELT &r)
Definition: BigMatrix.h:1819
MatrixBase & elementwiseDivideInPlace(const MatrixBase< EE > &)
M(i,j) /= R(i,j); R must have same dimensions as this.
MatrixBase(const MatrixCommitment &commitment, const MatrixHelper< Scalar > &source, const typename MatrixHelper< Scalar >::ShallowCopy &shallow)
Definition: BigMatrix.h:427
This class represents a small matrix whose size is known at compile time, containing elements of any ...
Definition: Mat.h:51
RowVector_< float > fRowVector
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3556
K::TComplex TComplex
Definition: CompositeNumericalTypes.h:143
std::string toString() const
toString() returns a string representation of the Matrix_.
Definition: BigMatrix.h:1979
Variable-size 2d matrix of Composite Numerical Type (ELT) elements.
Definition: BigMatrix.h:175
ELT value_type
Definition: BigMatrix.h:3591
MatrixView_< float > fMatrixView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3577
K::Number Number
Definition: CompositeNumericalTypes.h:162
THerm transpose() const
Definition: BigMatrix.h:1450
MatrixBase & operator=(const ELT &t)
Matrix assignment to an element sets only the *diagonal* elements to the indicated value; everything ...
Definition: BigMatrix.h:466
static TAbs abs(const K &t)
Definition: CompositeNumericalTypes.h:240
VectorIterator operator++()
Definition: BigMatrix.h:3613
int ncol() const
Definition: BigMatrix.h:1696
MatrixBase & scalarAssign(const S &s)
Set M's diagonal elements to a "scalar" value S, and all off-diagonal elements to zero...
Definition: BigMatrix.h:477
VectorView_< dComplex > dComplexVectorView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3553
void writeUnformatted(std::ostream &o, const MatrixBase< E > &v)
Specialize for MatrixBase<E> delegating to RowVectorBase<E> with newlines separating the rows...
Definition: BigMatrix.h:3303
void dump(const char *msg=0) const
MatrixBase(const MatrixBase< EE > &b)
Definition: BigMatrix.h:446
RowVector_< Complex > ComplexRowVector
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3558
RowVector_ & operator=(const RowVectorBase< EE > &b)
Definition: BigMatrix.h:2259
ptrdiff_t nelt() const
Definition: BigMatrix.h:1697
MatrixBase & elementwiseMultiplyInPlace(const MatrixBase< EE > &)
M(i,j) *= R(i,j); R must have same dimensions as this.
MatrixBase & colScaleInPlace(const VectorBase< EE > &)
M = M * diag(c); c must have ncol() elements.
EltResult< EE >::Dvd elementwiseDivide(const VectorBase< EE > &v) const
Definition: BigMatrix.h:1377
VectorBase & elementwiseInvertInPlace()
Set this[i] = this[i]^-1.
Definition: BigMatrix.h:1329
void clear()
This restores the MatrixBase to the state it would be in had it been constructed specifying only its ...
Definition: BigMatrix.h:439
MatrixView_ & operator-=(const MatrixBase< EE > &m)
Definition: BigMatrix.h:1814
MatrixBase< ENeg > TNeg
Definition: BigMatrix.h:255
Vector_(int m, const ELT *cppInitialValues)
Definition: BigMatrix.h:2086
VectorBase< typename CNT< ELT >::TInvert > elementwiseInvert() const
Return out[i]=this[i]^-1 as function return.
Definition: BigMatrix.h:1340
VectorBase< typename CNT< ELT >::template Result< P >::Sub > Sub
Definition: BigMatrix.h:1148
RowVector_(const Base &src)
Definition: BigMatrix.h:2223
int ncol() const
Return the number of columns n in the logical shape of this matrix.
Definition: BigMatrix.h:290
MatrixBase & setTo(const ELT &t)
Fill every element in current allocation with given element (or NaN or 0).
Definition: BigMatrix.h:734
Matrix_< double > dMatrix
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3571
RowVectorBase & operator*=(const StdNumber &t)
Definition: BigMatrix.h:1595
Matrix_(const Mat< M, N, ELT, CS, RS > &mat)
Convert a Mat to a Matrix_.
Definition: BigMatrix.h:1951
static std::istream & fillVectorFromStream(std::istream &in, Vector_< T > &out)
Read in a fixed number of elements from a stream into a Vector.
Definition: BigMatrix.h:3501
RowVectorBase & resizeKeep(int n)
Definition: BigMatrix.h:1745
VectorBase(int m=0)
Default constructor makes a 0x1 matrix locked at 1 column; you can provide an initial allocation if y...
Definition: BigMatrix.h:1082
RowVectorView_< ELT > & updAsRowVectorView()
Definition: BigMatrix.h:989
MatrixBase & operator+=(const MatrixBase< EE > &b)
Definition: BigMatrix.h:451
const CoordinateDirection::NegXDirection & operator-(const CoordinateAxis::XCoordinateAxis &)
Create the NegXAxis direction by negating XAxis.
Definition: CoordinateAxis.h:371
K::TSqHermT TSqHermT
Definition: CompositeNumericalTypes.h:146
EltResult< EE >::Dvd elementwiseDivide(const MatrixBase< EE > &m) const
Definition: BigMatrix.h:711
Vector_(int m, int stride, S *data, bool)
Definition: BigMatrix.h:2100
RowVectorView_< double > dRowVectorView
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3564
MatrixBase & resize(int m, int n)
Change the size of this matrix.
Definition: BigMatrix.h:950
VectorBase & elementwiseMultiplyInPlace(const VectorBase< EE > &r)
Definition: BigMatrix.h:1347
bool hasContiguousData() const
Definition: BigMatrix.h:1014
MatrixBase & scalarMultiplyFromLeftInPlace(const S &)
Set M(i,j) = S * M(i,j) for some "scalar" S.
RowVector_< Real > RowVector
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3555
void rowAndColScale(const VectorBase< ER > &r, const VectorBase< EC > &c, typename EltResult< typename VectorBase< ER >::template EltResult< EC >::Mul >::Mul &out) const
Definition: BigMatrix.h:2417
RowVector_(const BaseNeg &src)
Definition: BigMatrix.h:2224
ptrdiff_t difference_type
Definition: BigMatrix.h:3592
RowVectorBase(int n, const ELT *cppInitialValues)
Construct an owner vector of length n, with the elements initialized sequentially from a C++ array of...
Definition: BigMatrix.h:1537
ELT & operator[](int i)
Definition: BigMatrix.h:1425
Vector_(int m, const ELT &initialValue)
Definition: BigMatrix.h:2087
MatrixBase< typename CNT< E >::template Result< P >::Mul > Mul
Definition: BigMatrix.h:281
MatrixBase & negateInPlace()
Definition: BigMatrix.h:944
MatrixBase(const MatrixCommitment &commitment, const MatrixHelper< Scalar > &source, const typename MatrixHelper< Scalar >::DeepCopy &deep)
Definition: BigMatrix.h:431
This class is identical to a Vector_; it is used only to manage the C++ rules for when copy construct...
Definition: BigMatrix.h:184
const TNeg & negate() const
Definition: BigMatrix.h:1738
const S * getContiguousData() const
ELT & operator()(int i, int j)
Definition: BigMatrix.h:836
RowVectorView_ & operator=(const ELT &v)
Definition: BigMatrix.h:2180
MatrixBase & elementwiseSubtractFromScalarInPlace(const S &s)
Set M(i,j) = s - M(i,j) for every element of M and some value s.
VectorBase & operator*=(const StdNumber &t)
Definition: BigMatrix.h:1160
K::THerm THerm
Definition: CompositeNumericalTypes.h:144
MatrixView_< ELT > & updAsMatrixView()
Definition: BigMatrix.h:970
ELT & reference
Definition: BigMatrix.h:3593
const MatrixCommitment & getCharacterCommitment() const
void sum(S *eltp) const
RowVector_< dComplex > dComplexRowVector
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3560
MatrixView_< ELT > operator()(int i, int j, int m, int n) const
Definition: BigMatrix.h:753
Definition: BigMatrix.h:1144
void elementwiseInvert(VectorBase< typename CNT< ELT >::TInvert > &out) const
Set supplied out[i] = this[i]^-1.
Definition: BigMatrix.h:1335
This is the default commitment for a column vector.
Definition: MatrixCharacteristics.h:986
void fillWithScalar(const StdNumber &)
const ELT & operator()(int i, int j) const
Definition: BigMatrix.h:835
ptrdiff_t nelt() const
Definition: BigMatrix.h:1418
VectorBase(int m, const ELT &initialValue)
Construct an owner vector of length m, with each element initialized to the given value...
Definition: BigMatrix.h:1094
CNT< ScalarNormSq >::TSqrt normRMS(int *worstOne=0) const
Return the root-mean-square (RMS) norm of a Vector of scalars, with optional return of the index of t...
Definition: BigMatrix.h:1195
RowVectorView_< ELT > operator()(const Array_< int > &indices)
Definition: BigMatrix.h:1725
RowVectorBase< typename CNT< ELT >::template Result< P >::Add > Add
Definition: BigMatrix.h:1583
MatrixBase & scalarSubtractInPlace(const S &s)
Subtract a scalar from M's diagonal.
Definition: BigMatrix.h:495
MatrixBase(const MatrixBase &b)
Copy constructor is a deep copy (not appropriate for views!).
Definition: BigMatrix.h:336
EPrecision Precision
Definition: BigMatrix.h:251
const TNeg & negate() const
Definition: BigMatrix.h:1460
TNeg & updNegate()
Definition: BigMatrix.h:1739
EScalar Scalar
Definition: BigMatrix.h:248
RowVectorView_ & operator/=(const StdNumber &t)
Definition: BigMatrix.h:2190
TAbs abs() const
Definition: BigMatrix.h:1700
VectorBase(int m, const ELT *cppInitialValues)
Construct an owner vector of length m, with the elements initialized sequentially from a C++ array of...
Definition: BigMatrix.h:1101
ELT * pointer
Definition: BigMatrix.h:3594
DeadMatrixView_(const MatrixHelper< S > &h)
Definition: BigMatrix.h:1853
Vector_(int m, int stride, const S *data, bool)
Borrowed-space construction with explicit stride supplied as "number of scalars between elements"...
Definition: BigMatrix.h:2099
MatrixBase< ESqHermT > TSqHermT
Definition: BigMatrix.h:267
K::TAbs TAbs
Definition: CompositeNumericalTypes.h:155
int nrow() const
Matrix_(int m, int n, int leadingDim, S *data)
Definition: BigMatrix.h:1945
Matrix_ & operator-=(const MatrixBase< EE > &m)
Definition: BigMatrix.h:1963
VectorView_< ELT > col(int j) const
Definition: BigMatrix.h:2332
const MatrixBase & operator+() const
Definition: BigMatrix.h:937