Simbody 3.7
Loading...
Searching...
No Matches
MatrixBase.h
Go to the documentation of this file.
1#ifndef SimTK_SIMMATRIX_MATRIXBASE_H_
2#define SimTK_SIMMATRIX_MATRIXBASE_H_
3
4/* -------------------------------------------------------------------------- *
5 * Simbody(tm): SimTKcommon *
6 * -------------------------------------------------------------------------- *
7 * This is part of the SimTK biosimulation toolkit originating from *
8 * Simbios, the NIH National Center for Physics-Based Simulation of *
9 * Biological Structures at Stanford, funded under the NIH Roadmap for *
10 * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11 * *
12 * Portions copyright (c) 2005-13 Stanford University and the Authors. *
13 * Authors: Michael Sherman *
14 * Contributors: *
15 * *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17 * not use this file except in compliance with the License. You may obtain a *
18 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19 * *
20 * Unless required by applicable law or agreed to in writing, software *
21 * distributed under the License is distributed on an "AS IS" BASIS, *
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23 * See the License for the specific language governing permissions and *
24 * limitations under the License. *
25 * -------------------------------------------------------------------------- */
26
31namespace SimTK {
32
33//==============================================================================
34// MATRIX BASE
35//==============================================================================
67// ----------------------------------------------------------------------------
68template <class ELT> class MatrixBase {
69public:
70 // These typedefs are handy, but despite appearances you cannot
71 // treat a MatrixBase as a composite numerical type. That is,
72 // CNT<MatrixBase> will not compile, or if it does it won't be
73 // meaningful.
74
75 typedef ELT E;
76 typedef typename CNT<E>::TNeg ENeg;
78 typedef typename CNT<E>::TReal EReal;
79 typedef typename CNT<E>::TImag EImag;
80 typedef typename CNT<E>::TComplex EComplex;
81 typedef typename CNT<E>::THerm EHerm;
82 typedef typename CNT<E>::TPosTrans EPosTrans;
83
84 typedef typename CNT<E>::TAbs EAbs;
85 typedef typename CNT<E>::TStandard EStandard;
86 typedef typename CNT<E>::TInvert EInvert;
88 typedef typename CNT<E>::TSqHermT ESqHermT;
89 typedef typename CNT<E>::TSqTHerm ESqTHerm;
90
91 typedef typename CNT<E>::Scalar EScalar;
92 typedef typename CNT<E>::Number ENumber;
93 typedef typename CNT<E>::StdNumber EStdNumber;
94 typedef typename CNT<E>::Precision EPrecision;
96
97 typedef EScalar Scalar; // the underlying Scalar type
98 typedef ENumber Number; // negator removed from Scalar
99 typedef EStdNumber StdNumber; // conjugate goes to complex
100 typedef EPrecision Precision; // complex removed from StdNumber
101 typedef EScalarNormSq ScalarNormSq; // type of scalar^2
102
110
115 typedef MatrixBase<ESqHermT> TSqHermT; // ~Mat*Mat
116 typedef MatrixBase<ESqTHerm> TSqTHerm; // Mat*~Mat
117
119 const MatrixCharacter& getMatrixCharacter() const {return helper.getMatrixCharacter();}
120
124 { helper.commitTo(mc); }
125
126 // This gives the resulting matrix type when (m(i,j) op P) is applied to each element.
127 // It will have element types which are the regular composite result of E op P.
128 template <class P> struct EltResult {
133 };
134
136 int nrow() const {return helper.nrow();}
138 int ncol() const {return helper.ncol();}
139
147 ptrdiff_t nelt() const {return helper.nelt();}
148
151
152 enum {
154 CppNScalarsPerElement = sizeof(E) / sizeof(Scalar)
155 };
156
161
166
172 explicit MatrixBase(const MatrixCommitment& commitment)
173 : helper(NScalarsPerElement,CppNScalarsPerElement,commitment) {}
174
175
180 MatrixBase(const MatrixCommitment& commitment, int m, int n)
181 : helper(NScalarsPerElement,CppNScalarsPerElement,commitment,m,n) {}
182
185 : helper(b.helper.getCharacterCommitment(),
186 b.helper, typename MatrixHelper<Scalar>::DeepCopy()) { }
187
190 MatrixBase(const TNeg& b)
191 : helper(b.helper.getCharacterCommitment(),
192 b.helper, typename MatrixHelper<Scalar>::DeepCopy()) { }
193
197 helper.copyAssign(b.helper);
198 return *this;
199 }
200 MatrixBase& operator=(const MatrixBase& b) { return copyAssign(b); }
201
202
212 helper.writableViewAssign(const_cast<MatrixHelper<Scalar>&>(src.helper));
213 return *this;
214 }
215
216 // default destructor
217
224 MatrixBase(const MatrixCommitment& commitment, int m, int n, const ELT& initialValue)
225 : helper(NScalarsPerElement, CppNScalarsPerElement, commitment, m, n)
226 { helper.fillWith(reinterpret_cast<const Scalar*>(&initialValue)); }
227
238 MatrixBase(const MatrixCommitment& commitment, int m, int n,
239 const ELT* cppInitialValuesByRow)
240 : helper(NScalarsPerElement, CppNScalarsPerElement, commitment, m, n)
241 { helper.copyInByRowsFromCpp(reinterpret_cast<const Scalar*>(cppInitialValuesByRow)); }
242
254
256 MatrixBase(const MatrixCommitment& commitment,
257 const MatrixCharacter& character,
258 int spacing, const Scalar* data) // read only data
260 commitment, character, spacing, data) {}
261
263 MatrixBase(const MatrixCommitment& commitment,
264 const MatrixCharacter& character,
265 int spacing, Scalar* data) // writable data
267 commitment, character, spacing, data) {}
269
270 // Create a new MatrixBase from an existing helper. Both shallow and deep copies are possible.
271 MatrixBase(const MatrixCommitment& commitment,
272 MatrixHelper<Scalar>& source,
273 const typename MatrixHelper<Scalar>::ShallowCopy& shallow)
274 : helper(commitment, source, shallow) {}
275 MatrixBase(const MatrixCommitment& commitment,
276 const MatrixHelper<Scalar>& source,
277 const typename MatrixHelper<Scalar>::ShallowCopy& shallow)
278 : helper(commitment, source, shallow) {}
279 MatrixBase(const MatrixCommitment& commitment,
280 const MatrixHelper<Scalar>& source,
281 const typename MatrixHelper<Scalar>::DeepCopy& deep)
282 : helper(commitment, source, deep) {}
283
287 void clear() {helper.clear();}
288
289 MatrixBase& operator*=(const StdNumber& t) { helper.scaleBy(t); return *this; }
290 MatrixBase& operator/=(const StdNumber& t) { helper.scaleBy(StdNumber(1)/t); return *this; }
291 MatrixBase& operator+=(const MatrixBase& r) { helper.addIn(r.helper); return *this; }
292 MatrixBase& operator-=(const MatrixBase& r) { helper.subIn(r.helper); return *this; }
293
294 template <class EE> MatrixBase(const MatrixBase<EE>& b)
295 : helper(MatrixCommitment(),b.helper, typename MatrixHelper<Scalar>::DeepCopy()) { }
296
297 template <class EE> MatrixBase& operator=(const MatrixBase<EE>& b)
298 { helper = b.helper; return *this; }
299 template <class EE> MatrixBase& operator+=(const MatrixBase<EE>& b)
300 { helper.addIn(b.helper); return *this; }
301 template <class EE> MatrixBase& operator-=(const MatrixBase<EE>& b)
302 { helper.subIn(b.helper); return *this; }
303
314 MatrixBase& operator=(const ELT& t) {
315 setToZero(); updDiag().setTo(t);
316 return *this;
317 }
318
324 template <class S> inline MatrixBase&
325 scalarAssign(const S& s) {
326 setToZero(); updDiag().setTo(s);
327 return *this;
328 }
329
333 template <class S> inline MatrixBase&
337
338
342 template <class S> inline MatrixBase&
346
349 template <class S> inline MatrixBase&
352 updDiag().elementwiseAddScalarInPlace(s); // yes, add
353 }
354
361 template <class S> inline MatrixBase&
363
367 template <class S> inline MatrixBase&
369
376 template <class S> inline MatrixBase&
378
382 template <class S> inline MatrixBase&
384
385
388 template <class EE> inline MatrixBase&
390
393 template <class EE> inline void
394 rowScale(const VectorBase<EE>& r, typename EltResult<EE>::Mul& out) const;
395
396 template <class EE> inline typename EltResult<EE>::Mul
397 rowScale(const VectorBase<EE>& r) const {
398 typename EltResult<EE>::Mul out(nrow(), ncol()); rowScale(r,out); return out;
399 }
400
403 template <class EE> inline MatrixBase&
405
406 template <class EE> inline void
407 colScale(const VectorBase<EE>& c, typename EltResult<EE>::Mul& out) const;
408
409 template <class EE> inline typename EltResult<EE>::Mul
410 colScale(const VectorBase<EE>& c) const {
411 typename EltResult<EE>::Mul out(nrow(), ncol()); colScale(c,out); return out;
412 }
413
414
419 template <class ER, class EC> inline MatrixBase&
421
422 template <class ER, class EC> inline void
423 rowAndColScale(const VectorBase<ER>& r, const VectorBase<EC>& c,
424 typename EltResult<typename VectorBase<ER>::template EltResult<EC>::Mul>::Mul& out) const;
425
426 template <class ER, class EC> inline typename EltResult<typename VectorBase<ER>::template EltResult<EC>::Mul>::Mul
427 rowAndColScale(const VectorBase<ER>& r, const VectorBase<EC>& c) const {
429 out(nrow(), ncol());
430 rowAndColScale(r,c,out); return out;
431 }
432
440 template <class S> inline MatrixBase&
441 elementwiseAssign(const S& s);
442
445 { return elementwiseAssign<Real>(Real(s)); }
446
449
450 void elementwiseInvert(MatrixBase<typename CNT<E>::TInvert>& out) const;
451
457
465 template <class S> inline MatrixBase&
467
468 template <class S> inline void
469 elementwiseAddScalar(const S& s, typename EltResult<S>::Add&) const;
470
471 template <class S> inline typename EltResult<S>::Add
472 elementwiseAddScalar(const S& s) const {
473 typename EltResult<S>::Add out(nrow(), ncol());
475 return out;
476 }
477
485 template <class S> inline MatrixBase&
487
488 template <class S> inline void
489 elementwiseSubtractScalar(const S& s, typename EltResult<S>::Sub&) const;
490
491 template <class S> inline typename EltResult<S>::Sub
492 elementwiseSubtractScalar(const S& s) const {
493 typename EltResult<S>::Sub out(nrow(), ncol());
495 return out;
496 }
497
506 template <class S> inline MatrixBase&
508
509 template <class S> inline void
511 const S&,
513
514 template <class S> inline typename MatrixBase<S>::template EltResult<E>::Sub
517 elementwiseSubtractFromScalar<S>(s,out);
518 return out;
519 }
520
522 template <class EE> inline MatrixBase&
524
525 template <class EE> inline void
527
528 template <class EE> inline typename EltResult<EE>::Mul
530 typename EltResult<EE>::Mul out(nrow(), ncol());
531 elementwiseMultiply<EE>(m,out);
532 return out;
533 }
534
536 template <class EE> inline MatrixBase&
538
539 template <class EE> inline void
541 const MatrixBase<EE>&,
543
544 template <class EE> inline typename MatrixBase<EE>::template EltResult<E>::Mul
546 typename EltResult<EE>::Mul out(nrow(), ncol());
547 elementwiseMultiplyFromLeft<EE>(m,out);
548 return out;
549 }
550
552 template <class EE> inline MatrixBase&
554
555 template <class EE> inline void
557
558 template <class EE> inline typename EltResult<EE>::Dvd
560 typename EltResult<EE>::Dvd out(nrow(), ncol());
561 elementwiseDivide<EE>(m,out);
562 return out;
563 }
564
566 template <class EE> inline MatrixBase&
568
569 template <class EE> inline void
571 const MatrixBase<EE>&,
573
574 template <class EE> inline typename MatrixBase<EE>::template EltResult<EE>::Dvd
577 elementwiseDivideFromLeft<EE>(m,out);
578 return out;
579 }
580
582 MatrixBase& setTo(const ELT& t) {helper.fillWith(reinterpret_cast<const Scalar*>(&t)); return *this;}
584 MatrixBase& setToZero() {helper.fillWithScalar(StdNumber(0)); return *this;}
585
586 // View creating operators.
587 inline RowVectorView_<ELT> row(int i) const; // select a row
588 inline RowVectorView_<ELT> updRow(int i);
589 inline VectorView_<ELT> col(int j) const; // select a column
590 inline VectorView_<ELT> updCol(int j);
591
592 RowVectorView_<ELT> operator[](int i) const {return row(i);}
594 VectorView_<ELT> operator()(int j) const {return col(j);}
596
597 // Select a block.
598 inline MatrixView_<ELT> block(int i, int j, int m, int n) const;
599 inline MatrixView_<ELT> updBlock(int i, int j, int m, int n);
600
601 MatrixView_<ELT> operator()(int i, int j, int m, int n) const
602 { return block(i,j,m,n); }
603 MatrixView_<ELT> operator()(int i, int j, int m, int n)
604 { return updBlock(i,j,m,n); }
605
606 // Hermitian transpose.
607 inline MatrixView_<EHerm> transpose() const;
609
612
615 inline VectorView_<ELT> diag() const;
618 inline VectorView_<ELT> updDiag();
622
623 // Create a view of the real or imaginary elements. TODO
624 //inline MatrixView_<EReal> real() const;
625 //inline MatrixView_<EReal> updReal();
626 //inline MatrixView_<EImag> imag() const;
627 //inline MatrixView_<EImag> updImag();
628
629 // Overload "real" and "imag" for both read and write as a nod to convention. TODO
630 //MatrixView_<EReal> real() {return updReal();}
631 //MatrixView_<EReal> imag() {return updImag();}
632
633 // TODO: this routine seems ill-advised but I need it for the IVM port at the moment
634 TInvert invert() const { // return a newly-allocated inverse; dump negator
635 TInvert m(*this);
636 m.helper.invertInPlace();
637 return m; // TODO - bad: makes an extra copy
638 }
639
640 void invertInPlace() {helper.invertInPlace();}
641
643 void dump(const char* msg=0) const {
644 helper.dump(msg);
645 }
646
655 const ELT& getElt(int i, int j) const { return *reinterpret_cast<const ELT*>(helper.getElt(i,j)); }
656 ELT& updElt(int i, int j) { return *reinterpret_cast< ELT*>(helper.updElt(i,j)); }
657
658 const ELT& operator()(int i, int j) const {return getElt(i,j);}
659 ELT& operator()(int i, int j) {return updElt(i,j);}
660
665 void getAnyElt(int i, int j, ELT& value) const
666 { helper.getAnyElt(i,j,reinterpret_cast<Scalar*>(&value)); }
667 ELT getAnyElt(int i, int j) const {ELT e; getAnyElt(i,j,e); return e;}
668
671 // TODO: very slow! Should be optimized at least for the case
672 // where ELT is a Scalar.
674 const int nr=nrow(), nc=ncol();
675 ScalarNormSq sum(0);
676 for(int j=0;j<nc;++j)
677 for (int i=0; i<nr; ++i)
678 sum += CNT<E>::scalarNormSqr((*this)(i,j));
679 return sum;
680 }
681
685 // TODO: very slow! Should be optimized at least for the case
686 // where ELT is a Scalar.
687 void abs(TAbs& mabs) const {
688 const int nr=nrow(), nc=ncol();
689 mabs.resize(nr,nc);
690 for(int j=0;j<nc;++j)
691 for (int i=0; i<nr; ++i)
692 mabs(i,j) = CNT<E>::abs((*this)(i,j));
693 }
694
698 TAbs abs() const { TAbs mabs; abs(mabs); return mabs; }
699
711 const int nr=nrow(), nc=ncol();
712 TStandard mstd(nr, nc);
713 for(int j=0;j<nc;++j)
714 for (int i=0; i<nr; ++i)
715 mstd(i,j) = CNT<E>::standardize((*this)(i,j));
716 return mstd;
717 }
718
722 ScalarNormSq normSqr() const { return scalarNormSqr(); }
723 // TODO -- not good; unnecessary overflow
726
731 normRMS() const {
733 SimTK_THROW1(Exception::Cant, "normRMS() only defined for scalar elements");
734 if (nelt() == 0)
735 return typename CNT<ScalarNormSq>::TSqrt(0);
737 }
738
741 const int cols = ncol();
742 RowVector_<ELT> row(cols);
743 for (int j = 0; j < cols; ++j)
744 helper.colSum(j, reinterpret_cast<Scalar*>(&row[j]));
745 return row;
746 }
748 RowVector_<ELT> sum() const {return colSum();}
749
752 const int rows = nrow();
753 Vector_<ELT> col(rows);
754 for (int i = 0; i < rows; ++i)
755 helper.rowSum(i, reinterpret_cast<Scalar*>(&col[i]));
756 return col;
757 }
758
759 //TODO: make unary +/- return a self-view so they won't reallocate?
760 const MatrixBase& operator+() const {return *this; }
761 const TNeg& negate() const {return *reinterpret_cast<const TNeg*>(this); }
762 TNeg& updNegate() {return *reinterpret_cast<TNeg*>(this); }
763
764 const TNeg& operator-() const {return negate();}
765 TNeg& operator-() {return updNegate();}
766
767 MatrixBase& negateInPlace() {(*this) *= EPrecision(-1); return *this;}
768
773 MatrixBase& resize(int m, int n) { helper.resize(m,n); return *this; }
779 MatrixBase& resizeKeep(int m, int n) { helper.resizeKeep(m,n); return *this; }
780
781 // This prevents shape changes in a Matrix that would otherwise allow it. No harm if is
782 // are called on a Matrix that is locked already; it always succeeds.
783 void lockShape() {helper.lockShape();}
784
785 // This allows shape changes again for a Matrix which was constructed to allow them
786 // but had them locked with the above routine. No harm if this is called on a Matrix
787 // that is already unlocked, but it is not allowed to call this on a Matrix which
788 // *never* allowed resizing. An exception will be thrown in that case.
789 void unlockShape() {helper.unlockShape();}
790
791 // An assortment of handy conversions
792 const MatrixView_<ELT>& getAsMatrixView() const { return *reinterpret_cast<const MatrixView_<ELT>*>(this); }
793 MatrixView_<ELT>& updAsMatrixView() { return *reinterpret_cast< MatrixView_<ELT>*>(this); }
794 const Matrix_<ELT>& getAsMatrix() const { return *reinterpret_cast<const Matrix_<ELT>*>(this); }
795 Matrix_<ELT>& updAsMatrix() { return *reinterpret_cast< Matrix_<ELT>*>(this); }
796
798 { assert(ncol()==1); return *reinterpret_cast<const VectorView_<ELT>*>(this); }
800 { assert(ncol()==1); return *reinterpret_cast< VectorView_<ELT>*>(this); }
801 const Vector_<ELT>& getAsVector() const
802 { assert(ncol()==1); return *reinterpret_cast<const Vector_<ELT>*>(this); }
804 { assert(ncol()==1); return *reinterpret_cast< Vector_<ELT>*>(this); }
806 { assert(ncol()==1); return *reinterpret_cast<const VectorBase<ELT>*>(this); }
808 { assert(ncol()==1); return *reinterpret_cast< VectorBase<ELT>*>(this); }
809
811 { assert(nrow()==1); return *reinterpret_cast<const RowVectorView_<ELT>*>(this); }
813 { assert(nrow()==1); return *reinterpret_cast< RowVectorView_<ELT>*>(this); }
815 { assert(nrow()==1); return *reinterpret_cast<const RowVector_<ELT>*>(this); }
817 { assert(nrow()==1); return *reinterpret_cast< RowVector_<ELT>*>(this); }
819 { assert(nrow()==1); return *reinterpret_cast<const RowVectorBase<ELT>*>(this); }
821 { assert(nrow()==1); return *reinterpret_cast< RowVectorBase<ELT>*>(this); }
822
823 // Access to raw data. We have to return the raw data
824 // pointer as pointer-to-scalar because we may pack the elements tighter
825 // than a C++ array would.
826
831
836
837 bool hasContiguousData() const {return helper.hasContiguousData();}
839 return helper.getContiguousDataLength();
840 }
842 return helper.getContiguousData();
843 }
845 return helper.updContiguousData();
846 }
847 void replaceContiguousScalarData(Scalar* newData, ptrdiff_t length, bool takeOwnership) {
848 helper.replaceContiguousData(newData,length,takeOwnership);
849 }
850 void replaceContiguousScalarData(const Scalar* newData, ptrdiff_t length) {
851 helper.replaceContiguousData(newData,length);
852 }
853 void swapOwnedContiguousScalarData(Scalar* newData, ptrdiff_t length, Scalar*& oldData) {
854 helper.swapOwnedContiguousData(newData,length,oldData);
855 }
856
861 explicit MatrixBase(MatrixHelperRep<Scalar>* hrep) : helper(hrep) {}
862
863protected:
864 const MatrixHelper<Scalar>& getHelper() const {return helper;}
865 MatrixHelper<Scalar>& updHelper() {return helper;}
866
867private:
868 MatrixHelper<Scalar> helper; // this is just one pointer
869
870 template <class EE> friend class MatrixBase;
871
872 // ============================= Unimplemented =============================
873 // This routine is useful for implementing friendlier Matrix expressions and operators.
874 // It maps closely to the Level-3 BLAS family of pxxmm() routines like sgemm(). The
875 // operation performed assumes that "this" is the result, and that "this" has
876 // already been sized correctly to receive the result. We'll compute
877 // this = beta*this + alpha*A*B
878 // If beta is 0 then "this" can be uninitialized. If alpha is 0 we promise not
879 // to look at A or B. The routine can work efficiently even if A and/or B are transposed
880 // by their views, so an expression like
881 // C += s * ~A * ~B
882 // can be performed with the single equivalent call
883 // C.matmul(1., s, Tr(A), Tr(B))
884 // where Tr(A) indicates a transposed view of the original A.
885 // The ultimate efficiency of this call depends on the data layout and views which
886 // are used for the three matrices.
887 // NOTE: neither A nor B can be the same matrix as 'this', nor views of the same data
888 // which would expose elements of 'this' that will be modified by this operation.
889 template <class ELT_A, class ELT_B>
890 MatrixBase& matmul(const StdNumber& beta, // applied to 'this'
891 const StdNumber& alpha, const MatrixBase<ELT_A>& A, const MatrixBase<ELT_B>& B)
892 {
893 helper.matmul(beta,alpha,A.helper,B.helper);
894 return *this;
895 }
896
897};
898
899} //namespace SimTK
900
901#endif // SimTK_SIMMATRIX_MATRIXBASE_H_
#define SimTK_THROW1(exc, a1)
Definition Exception.h:316
Specialized information about Composite Numerical Types which allows us to define appropriate templat...
Definition CompositeNumericalTypes.h:136
static TAbs abs(const K &t)
Definition CompositeNumericalTypes.h:240
K::ScalarNormSq ScalarNormSq
Definition CompositeNumericalTypes.h:166
K::StdNumber StdNumber
Definition CompositeNumericalTypes.h:163
static TSqrt sqrt(const K &t)
Definition CompositeNumericalTypes.h:239
K::TSqHermT TSqHermT
Definition CompositeNumericalTypes.h:146
K::TSqrt TSqrt
Definition CompositeNumericalTypes.h:154
K::TInvert TInvert
Definition CompositeNumericalTypes.h:157
K::TNormalize TNormalize
Definition CompositeNumericalTypes.h:158
K::TWithoutNegator TWithoutNegator
Definition CompositeNumericalTypes.h:140
K::TReal TReal
Definition CompositeNumericalTypes.h:141
static TStandard standardize(const K &t)
Definition CompositeNumericalTypes.h:241
K::THerm THerm
Definition CompositeNumericalTypes.h:144
K::TPosTrans TPosTrans
Definition CompositeNumericalTypes.h:145
K::TNeg TNeg
Definition CompositeNumericalTypes.h:139
K::TStandard TStandard
Definition CompositeNumericalTypes.h:156
K::TComplex TComplex
Definition CompositeNumericalTypes.h:143
K::TSqTHerm TSqTHerm
Definition CompositeNumericalTypes.h:147
K::TImag TImag
Definition CompositeNumericalTypes.h:142
K::Precision Precision
Definition CompositeNumericalTypes.h:164
K::Scalar Scalar
Definition CompositeNumericalTypes.h:160
K::TAbs TAbs
Definition CompositeNumericalTypes.h:155
K::Number Number
Definition CompositeNumericalTypes.h:162
Definition Exception.h:298
This is the common base class for Simbody's Vector_ and Matrix_ classes for handling large,...
Definition MatrixBase.h:68
const RowVectorView_< ELT > & getAsRowVectorView() const
Definition MatrixBase.h:810
Scalar * updContiguousScalarData()
Definition MatrixBase.h:844
void elementwiseDivideFromLeft(const MatrixBase< EE > &, typename MatrixBase< EE >::template EltResult< E >::Dvd &) const
Definition BigMatrix.h:533
MatrixBase & elementwiseSubtractFromScalarInPlace(const S &s)
Set M(i,j) = s - M(i,j) for every element of M and some value s.
MatrixBase(const MatrixCommitment &commitment, const MatrixCharacter &character, int spacing, Scalar *data)
Construct a writable view of pre-existing data.
Definition MatrixBase.h:263
MatrixBase & scalarDivideFromLeftInPlace(const S &)
Set M(i,j) = S/M(i,j) for some "scalar" S.
CNT< E >::TWithoutNegator EWithoutNegator
Definition MatrixBase.h:77
const MatrixHelper< Scalar > & getHelper() const
Definition MatrixBase.h:864
ScalarNormSq scalarNormSqr() const
Scalar norm square is sum( squares of all scalars ).
Definition MatrixBase.h:673
VectorBase< ELT > & updAsVectorBase()
Definition MatrixBase.h:807
RowVector_< ELT > sum() const
Alternate name for colSum(); behaves like the Matlab function sum().
Definition MatrixBase.h:748
@ NScalarsPerElement
Definition MatrixBase.h:153
@ CppNScalarsPerElement
Definition MatrixBase.h:154
MatrixBase & elementwiseAssign(const S &s)
Set M(i,j)=s for every element of M and some value s.
MatrixBase & elementwiseSubtractScalarInPlace(const S &s)
Set M(i,j)-=s for every element of M and some value s.
MatrixBase & elementwiseDivideInPlace(const MatrixBase< EE > &)
M(i,j) /= R(i,j); R must have same dimensions as this.
int getPackedSizeofElement() const
This is like sizeof(ELT), but returning the number of bytes we use to store the element which may be ...
Definition MatrixBase.h:835
MatrixBase & setToNaN()
Definition MatrixBase.h:583
EPrecision Precision
Definition MatrixBase.h:100
CNT< ScalarNormSq >::TSqrt norm() const
Definition MatrixBase.h:725
CNT< E >::Number ENumber
Definition MatrixBase.h:92
MatrixBase & rowScaleInPlace(const VectorBase< EE > &)
M = diag(r) * M; r must have nrow() elements.
EltResult< EE >::Mul elementwiseMultiply(const MatrixBase< EE > &m) const
Definition MatrixBase.h:529
const MatrixCharacter & getMatrixCharacter() const
Definition MatrixBase.h:119
MatrixBase & elementwiseInvertInPlace()
Set M(i,j) = M(i,j)^-1.
Definition BigMatrix.h:361
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 MatrixBase.h:180
MatrixBase< EHerm > THerm
Definition MatrixBase.h:108
EltResult< EE >::Mul colScale(const VectorBase< EE > &c) const
Definition MatrixBase.h:410
MatrixBase & elementwiseAssign(int s)
Overloaded to allow an integer argument, which is converted to Real.
Definition MatrixBase.h:444
MatrixBase< EInvert > TInvert
Definition MatrixBase.h:113
TStandard standardize() const
Return a Matrix of the same shape and contents as this one but with the element type converted to one...
Definition MatrixBase.h:710
MatrixBase< EComplex > TComplex
Definition MatrixBase.h:107
MatrixBase & elementwiseMultiplyFromLeftInPlace(const MatrixBase< EE > &)
M(i,j) = R(i,j) * M(i,j); R must have same dimensions as this.
MatrixBase< EReal > TReal
Definition MatrixBase.h:105
CNT< E >::TSqTHerm ESqTHerm
Definition MatrixBase.h:89
CNT< E >::TNeg ENeg
Definition MatrixBase.h:76
const MatrixCommitment & getCharacterCommitment() const
Definition MatrixBase.h:118
MatrixBase & scalarMultiplyFromLeftInPlace(const S &)
Set M(i,j) = S * M(i,j) for some "scalar" S.
MatrixBase< EE >::template EltResult< E >::Mul elementwiseMultiplyFromLeft(const MatrixBase< EE > &m) const
Definition MatrixBase.h:545
MatrixBase & colScaleInPlace(const VectorBase< EE > &)
M = M * diag(c); c must have ncol() elements.
MatrixBase< typename CNT< E >::TInvert > elementwiseInvert() const
Definition MatrixBase.h:452
MatrixView_< ELT > updBlock(int i, int j, int m, int n)
Definition BigMatrix.h:211
VectorView_< ELT > updCol(int j)
Definition BigMatrix.h:261
MatrixBase(const MatrixBase &b)
Copy constructor is a deep copy (not appropriate for views!).
Definition MatrixBase.h:184
void unlockShape()
Definition MatrixBase.h:789
RowVector_< ELT > & updAsRowVector()
Definition MatrixBase.h:816
MatrixBase & operator=(const ELT &t)
Matrix assignment to an element sets only the diagonal elements to the indicated value; everything el...
Definition MatrixBase.h:314
CNT< E >::TComplex EComplex
Definition MatrixBase.h:80
CNT< E >::ScalarNormSq EScalarNormSq
Definition MatrixBase.h:95
void abs(TAbs &mabs) const
abs() is elementwise absolute value; that is, the return value has the same dimension as this Matrix ...
Definition MatrixBase.h:687
MatrixBase & resize(int m, int n)
Change the size of this matrix.
Definition MatrixBase.h:773
MatrixBase & elementwiseMultiplyInPlace(const MatrixBase< EE > &)
M(i,j) *= R(i,j); R must have same dimensions as this.
Matrix_< ELT > & updAsMatrix()
Definition MatrixBase.h:795
const Scalar * getContiguousScalarData() const
Definition MatrixBase.h:841
MatrixBase & elementwiseAddScalarInPlace(const S &s)
Set M(i,j)+=s for every element of M and some value s.
MatrixBase & scalarSubtractFromLeftInPlace(const S &s)
Set M(i,i) = S - M(i,i), M(i,j) = -M(i,j) for i!=j.
Definition MatrixBase.h:350
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.
MatrixView_< EHerm > updTranspose()
Definition BigMatrix.h:230
TInvert invert() const
Definition MatrixBase.h:634
MatrixBase & scalarDivideInPlace(const S &)
Set M(i,j) = M(i,j)/S for some "scalar" S.
MatrixBase & operator/=(const StdNumber &t)
Definition MatrixBase.h:290
CNT< E >::Precision EPrecision
Definition MatrixBase.h:94
MatrixBase & scalarSubtractInPlace(const S &s)
Subtract a scalar from M's diagonal.
Definition MatrixBase.h:343
void colScale(const VectorBase< EE > &c, typename EltResult< EE >::Mul &out) const
MatrixBase & scalarAddInPlace(const S &s)
Add a scalar to M's diagonal.
Definition MatrixBase.h:334
VectorView_< ELT > & updAsVectorView()
Definition MatrixBase.h:799
const RowVector_< ELT > & getAsRowVector() const
Definition MatrixBase.h:814
CNT< E >::StdNumber EStdNumber
Definition MatrixBase.h:93
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 MatrixBase.h:665
CNT< E >::TPosTrans EPosTrans
Definition MatrixBase.h:82
ELT E
Definition MatrixBase.h:75
EltResult< EE >::Dvd elementwiseDivide(const MatrixBase< EE > &m) const
Definition MatrixBase.h:559
void dump(const char *msg=0) const
Matlab-compatible debug output.
Definition MatrixBase.h:643
const TNeg & negate() const
Definition MatrixBase.h:761
int nrow() const
Return the number of rows m in the logical shape of this matrix.
Definition MatrixBase.h:136
void elementwiseSubtractScalar(const S &s, typename EltResult< S >::Sub &) const
MatrixBase< ENeg > TNeg
Definition MatrixBase.h:103
TNeg & operator-()
Definition MatrixBase.h:765
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:337
ENumber Number
Definition MatrixBase.h:98
MatrixView_< ELT > operator()(int i, int j, int m, int n) const
Definition MatrixBase.h:601
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 MatrixBase.h:238
bool isResizeable() const
Return true if either dimension of this Matrix is resizable.
Definition MatrixBase.h:150
bool hasContiguousData() const
Definition MatrixBase.h:837
RowVector_< ELT > colSum() const
Form the column sums of this matrix, returned as a RowVector.
Definition MatrixBase.h:740
MatrixBase & operator=(const MatrixBase &b)
Definition MatrixBase.h:200
int ncol() const
Return the number of columns n in the logical shape of this matrix.
Definition MatrixBase.h:138
MatrixBase(const MatrixCommitment &commitment)
This constructor takes a handle commitment and allocates the default matrix for that kind of commitme...
Definition MatrixBase.h:172
const ELT & getElt(int i, int j) const
Element selection for stored elements.
Definition MatrixBase.h:655
CNT< E >::TStandard EStandard
Definition MatrixBase.h:85
RowVectorView_< ELT > operator[](int i)
Definition MatrixBase.h:593
MatrixBase< EWithoutNegator > TWithoutNegator
Definition MatrixBase.h:104
MatrixView_< ELT > block(int i, int j, int m, int n) const
Definition BigMatrix.h:200
MatrixBase & operator-=(const MatrixBase &r)
Definition MatrixBase.h:292
MatrixBase< E > TPosTrans
Definition MatrixBase.h:109
MatrixView_< EHerm > operator~()
Definition MatrixBase.h:611
MatrixBase< EImag > TImag
Definition MatrixBase.h:106
MatrixBase & negateInPlace()
Definition MatrixBase.h:767
MatrixBase< S >::template EltResult< E >::Sub elementwiseSubtractFromScalar(const S &s) const
Definition MatrixBase.h:515
void invertInPlace()
Definition MatrixBase.h:640
void elementwiseDivide(const MatrixBase< EE > &, typename EltResult< EE >::Dvd &) const
MatrixView_< ELT > & updAsMatrixView()
Definition MatrixBase.h:793
MatrixView_< ELT > operator()(int i, int j, int m, int n)
Definition MatrixBase.h:603
EltResult< S >::Sub elementwiseSubtractScalar(const S &s) const
Definition MatrixBase.h:492
void replaceContiguousScalarData(Scalar *newData, ptrdiff_t length, bool takeOwnership)
Definition MatrixBase.h:847
ELT & operator()(int i, int j)
Definition MatrixBase.h:659
void replaceContiguousScalarData(const Scalar *newData, ptrdiff_t length)
Definition MatrixBase.h:850
MatrixBase< EE >::template EltResult< EE >::Dvd elementwiseDivideFromLeft(const MatrixBase< EE > &m) const
Definition MatrixBase.h:575
CNT< ScalarNormSq >::TSqrt normRMS() const
We only allow RMS norm if the elements are scalars.
Definition MatrixBase.h:731
MatrixBase & scalarMultiplyInPlace(const S &)
Set M(i,j) = M(i,j)*S for some "scalar" S.
void elementwiseSubtractFromScalar(const S &, typename MatrixBase< S >::template EltResult< E >::Sub &) const
Definition BigMatrix.h:435
void elementwiseAddScalar(const S &s, typename EltResult< S >::Add &) const
const VectorBase< ELT > & getAsVectorBase() const
Definition MatrixBase.h:805
EltResult< EE >::Mul rowScale(const VectorBase< EE > &r) const
Definition MatrixBase.h:397
VectorView_< ELT > updDiag()
Select main diagonal (of largest leading square if rectangular) and return it as a writable view of t...
Definition BigMatrix.h:245
MatrixBase & operator+=(const MatrixBase &r)
Definition MatrixBase.h:291
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...
MatrixBase & setTo(const ELT &t)
Fill every element in current allocation with given element (or NaN or 0).
Definition MatrixBase.h:582
MatrixBase & elementwiseDivideFromLeftInPlace(const MatrixBase< EE > &)
M(i,j) = R(i,j) / M(i,j); R must have same dimensions as this.
MatrixHelper< Scalar > & updHelper()
Definition MatrixBase.h:865
MatrixBase(MatrixHelperRep< Scalar > *hrep)
Helper rep-stealing constructor.
Definition MatrixBase.h:861
void clear()
This restores the MatrixBase to the state it would be in had it been constructed specifying only its ...
Definition MatrixBase.h:287
VectorView_< ELT > operator()(int j)
Definition MatrixBase.h:595
void elementwiseMultiply(const MatrixBase< EE > &, typename EltResult< EE >::Mul &) const
MatrixBase(const MatrixCommitment &commitment, const MatrixCharacter &character, int spacing, const Scalar *data)
Construct a read-only view of pre-existing data.
Definition MatrixBase.h:256
const VectorView_< ELT > & getAsVectorView() const
Definition MatrixBase.h:797
ELT getAnyElt(int i, int j) const
Definition MatrixBase.h:667
CNT< E >::TNormalize ENormalize
Definition MatrixBase.h:87
MatrixBase & scalarAssign(const S &s)
Set M's diagonal elements to a "scalar" value S, and all off-diagonal elements to zero.
Definition MatrixBase.h:325
const Matrix_< ELT > & getAsMatrix() const
Definition MatrixBase.h:794
VectorView_< ELT > col(int j) const
Definition BigMatrix.h:252
MatrixBase(const MatrixBase< EE > &b)
Definition MatrixBase.h:294
const RowVectorBase< ELT > & getAsRowVectorBase() const
Definition MatrixBase.h:818
MatrixView_< EHerm > operator~() const
Definition MatrixBase.h:610
EScalarNormSq ScalarNormSq
Definition MatrixBase.h:101
CNT< E >::TInvert EInvert
Definition MatrixBase.h:86
MatrixBase & copyAssign(const MatrixBase &b)
Copy assignment is a deep copy but behavior depends on type of lhs: if view, rhs must match.
Definition MatrixBase.h:196
const MatrixBase & operator+() const
Definition MatrixBase.h:760
CNT< E >::Scalar EScalar
Definition MatrixBase.h:91
MatrixBase< ESqTHerm > TSqTHerm
Definition MatrixBase.h:116
const MatrixView_< ELT > & getAsMatrixView() const
Definition MatrixBase.h:792
MatrixBase< ESqHermT > TSqHermT
Definition MatrixBase.h:115
RowVectorView_< ELT > updRow(int i)
Definition BigMatrix.h:279
ELT & updElt(int i, int j)
Definition MatrixBase.h:656
VectorView_< ELT > operator()(int j) const
Definition MatrixBase.h:594
ScalarNormSq normSqr() const
This is the scalar Frobenius norm, and its square.
Definition MatrixBase.h:722
MatrixBase()
The default constructor builds a 0x0 matrix managed by a helper that understands how many scalars the...
Definition MatrixBase.h:160
CNT< E >::TReal EReal
Definition MatrixBase.h:78
const TNeg & operator-() const
Definition MatrixBase.h:764
EltResult< typenameVectorBase< ER >::templateEltResult< EC >::Mul >::Mul rowAndColScale(const VectorBase< ER > &r, const VectorBase< EC > &c) const
Definition MatrixBase.h:427
void elementwiseMultiplyFromLeft(const MatrixBase< EE > &, typename MatrixBase< EE >::template EltResult< E >::Mul &) const
Definition BigMatrix.h:484
void lockShape()
Definition MatrixBase.h:783
ptrdiff_t nelt() const
Return the number of elements in the logical shape of this matrix.
Definition MatrixBase.h:147
MatrixBase< ENormalize > TNormalize
Definition MatrixBase.h:114
CNT< E >::TSqHermT ESqHermT
Definition MatrixBase.h:88
int getNScalarsPerElement() const
This is the number of consecutive scalars used to represent one element of type ELT.
Definition MatrixBase.h:830
MatrixBase & operator*=(const StdNumber &t)
Definition MatrixBase.h:289
MatrixBase< EAbs > TAbs
Definition MatrixBase.h:111
Vector_< ELT > & updAsVector()
Definition MatrixBase.h:803
ptrdiff_t getContiguousScalarDataLength() const
Definition MatrixBase.h:838
MatrixBase(int m, int n)
This constructor allocates the default matrix a completely uncommitted matrix commitment,...
Definition MatrixBase.h:164
VectorView_< ELT > diag()
This non-const version of diag() is an alternate name for updDiag() available for historical reasons.
Definition MatrixBase.h:621
MatrixBase & operator-=(const MatrixBase< EE > &b)
Definition MatrixBase.h:301
MatrixBase & setToZero()
Definition MatrixBase.h:584
RowVectorBase< ELT > & updAsRowVectorBase()
Definition MatrixBase.h:820
MatrixBase(const TNeg &b)
Implicit conversion from matrix with negated elements (otherwise this is just like the copy construct...
Definition MatrixBase.h:190
const Vector_< ELT > & getAsVector() const
Definition MatrixBase.h:801
MatrixBase(const MatrixCommitment &commitment, const MatrixHelper< Scalar > &source, const typename MatrixHelper< Scalar >::ShallowCopy &shallow)
Definition MatrixBase.h:275
void commitTo(const MatrixCommitment &mc)
Change the handle commitment for this matrix handle; only allowed if the handle is currently clear.
Definition MatrixBase.h:123
void swapOwnedContiguousScalarData(Scalar *newData, ptrdiff_t length, Scalar *&oldData)
Definition MatrixBase.h:853
TAbs abs() const
abs() with the result as a function return.
Definition MatrixBase.h:698
MatrixBase & operator=(const MatrixBase< EE > &b)
Definition MatrixBase.h:297
RowVectorView_< ELT > operator[](int i) const
Definition MatrixBase.h:592
EScalar Scalar
Definition MatrixBase.h:97
TNeg & updNegate()
Definition MatrixBase.h:762
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 MatrixBase.h:224
EStdNumber StdNumber
Definition MatrixBase.h:99
MatrixBase & resizeKeep(int m, int n)
Change the size of this matrix, retaining as much of the old data as will fit.
Definition MatrixBase.h:779
MatrixBase< EStandard > TStandard
Definition MatrixBase.h:112
CNT< E >::TImag EImag
Definition MatrixBase.h:79
MatrixBase & viewAssign(const MatrixBase &src)
View assignment is a shallow copy, meaning that we disconnect the MatrixBase from whatever it used to...
Definition MatrixBase.h:211
RowVectorView_< ELT > row(int i) const
Definition BigMatrix.h:270
MatrixView_< EHerm > transpose() const
Definition BigMatrix.h:222
MatrixBase & operator+=(const MatrixBase< EE > &b)
Definition MatrixBase.h:299
MatrixBase(const MatrixCommitment &commitment, MatrixHelper< Scalar > &source, const typename MatrixHelper< Scalar >::ShallowCopy &shallow)
Definition MatrixBase.h:271
EltResult< S >::Add elementwiseAddScalar(const S &s) const
Definition MatrixBase.h:472
Vector_< ELT > rowSum() const
Form the row sums of this matrix, returned as a Vector.
Definition MatrixBase.h:751
const ELT & operator()(int i, int j) const
Definition MatrixBase.h:658
CNT< E >::TAbs EAbs
Definition MatrixBase.h:84
CNT< E >::THerm EHerm
Definition MatrixBase.h:81
MatrixBase(const MatrixCommitment &commitment, const MatrixHelper< Scalar > &source, const typename MatrixHelper< Scalar >::DeepCopy &deep)
Definition MatrixBase.h:279
RowVectorView_< ELT > & updAsRowVectorView()
Definition MatrixBase.h:812
A MatrixCharacter is a set containing a value for each of the matrix characteristics except element t...
Definition MatrixCharacteristics.h:597
A MatrixCommitment provides a set of acceptable matrix characteristics.
Definition MatrixCharacteristics.h:832
bool isResizeable() const
Definition MatrixCharacteristics.h:925
Definition MatrixHelper.h:48
Definition MatrixHelper.h:96
Definition MatrixHelper.h:95
Here we define class MatrixHelper, the scalar-type templatized helper class for the more general,...
Definition MatrixHelper.h:79
void rowSum(int i, S *eltp) const
void dump(const char *msg=0) const
const MatrixCommitment & getCharacterCommitment() const
const S * getElt(int i, int j) const
MatrixHelper & writableViewAssign(MatrixHelper &source)
const S * getContiguousData() const
const MatrixCharacter & getMatrixCharacter() const
bool hasContiguousData() const
void fillWith(const S *eltp)
void resizeKeep(int m, int n)
void subIn(const MatrixHelper &)
S * updElt(int i, int j)
void resize(int m, int n)
void commitTo(const MatrixCommitment &)
MatrixHelper & copyAssign(const MatrixHelper &source)
void swapOwnedContiguousData(S *newData, ptrdiff_t length, S *&oldData)
ptrdiff_t nelt() const
ptrdiff_t getContiguousDataLength() const
void getAnyElt(int i, int j, S *value) const
void scaleBy(const StdNumber &)
void copyInByRowsFromCpp(const S *elts)
void colSum(int j, S *eltp) const
void addIn(const MatrixHelper &)
void fillWithScalar(const StdNumber &)
void replaceContiguousData(S *newData, ptrdiff_t length, bool takeOwnership)
(Advanced) This class is identical to Matrix_ except that it has shallow (reference) copy and assignm...
Definition MatrixView_.h:43
This is the matrix class intended to appear in user code for large, variable size matrices.
Definition Matrix_.h:51
This is a dataless rehash of the MatrixBase class to specialize it for RowVectors.
Definition RowVectorBase.h:42
(Advanced) This class is identical to RowVector_ except that it has shallow (reference) copy and assi...
Definition RowVectorView_.h:42
Represents a variable size row vector; much less common than the column vector type Vector_.
Definition RowVector_.h:52
This is a dataless rehash of the MatrixBase class to specialize it for Vectors.
Definition VectorBase.h:42
(Advanced) This class is identical to Vector_ except that it has shallow (reference) copy and assignm...
Definition VectorView_.h:42
This is the vector class intended to appear in user code for large, variable size column vectors.
Definition Vector_.h:50
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition Assembler.h:37
SimTK_Real Real
This is the default compiled-in floating point type for SimTK, either float or double.
Definition SimTKcommon/include/SimTKcommon/internal/common.h:606
Definition MatrixBase.h:128
MatrixBase< typename CNT< E >::template Result< P >::Add > Add
Definition MatrixBase.h:131
MatrixBase< typename CNT< E >::template Result< P >::Sub > Sub
Definition MatrixBase.h:132
MatrixBase< typename CNT< E >::template Result< P >::Mul > Mul
Definition MatrixBase.h:129
MatrixBase< typename CNT< E >::template Result< P >::Dvd > Dvd
Definition MatrixBase.h:130