Simbody  3.6
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-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 
150 #include "SimTKcommon/Scalar.h"
151 #include "SimTKcommon/SmallMatrix.h"
152 
155 
156 #include <iostream>
157 #include <cassert>
158 #include <complex>
159 #include <cstddef>
160 #include <limits>
161 
162 namespace SimTK {
163  template <class ELT> class MatrixBase;
164  template <class ELT> class VectorBase;
165  template <class ELT> class RowVectorBase;
166 
167  template <class ELT = Real> class MatrixView_;
168  template <class ELT = Real> class Matrix_;
169 
170  template <class ELT = Real> class VectorView_;
171  template <class ELT = Real> class Vector_;
172 
173  template <class ELT = Real> class RowVectorView_;
174  template <class ELT = Real> class RowVector_;
175 
176  template <class ELT, class VECTOR_CLASS> class VectorIterator;
177 }
178 
182 
185 
188 
191 
193 
194 
195 namespace SimTK {
196 
197 // ------------------------ MatrixBase definitions ----------------------------
198 
199 template <class ELT> inline MatrixView_<ELT>
200 MatrixBase<ELT>::block(int i, int j, int m, int n) const {
201  SimTK_INDEXCHECK(i,nrow()+1,"MatrixBase::block()");
202  SimTK_INDEXCHECK(j,ncol()+1,"MatrixBase::block()");
203  SimTK_SIZECHECK(i+m,nrow(),"MatrixBase::block()");
204  SimTK_SIZECHECK(j+n,ncol(),"MatrixBase::block()");
205 
206  MatrixHelper<Scalar> h(MatrixCommitment(),helper,i,j,m,n);
207  return MatrixView_<ELT>(h.stealRep());
208 }
209 
210 template <class ELT> inline MatrixView_<ELT>
211 MatrixBase<ELT>::updBlock(int i, int j, int m, int n) {
212  SimTK_INDEXCHECK(i,nrow()+1,"MatrixBase::updBlock()");
213  SimTK_INDEXCHECK(j,ncol()+1,"MatrixBase::updBlock()");
214  SimTK_SIZECHECK(i+m,nrow(),"MatrixBase::updBlock()");
215  SimTK_SIZECHECK(j+n,ncol(),"MatrixBase::updBlock()");
216 
217  MatrixHelper<Scalar> h(MatrixCommitment(),helper,i,j,m,n);
218  return MatrixView_<ELT>(h.stealRep());
219 }
220 
221 template <class E> inline MatrixView_<typename CNT<E>::THerm>
224  h(MatrixCommitment(),
225  helper, typename MatrixHelper<typename CNT<Scalar>::THerm>::TransposeView());
227 }
228 
229 template <class E> inline MatrixView_<typename CNT<E>::THerm>
232  h(MatrixCommitment(),
233  helper, typename MatrixHelper<typename CNT<Scalar>::THerm>::TransposeView());
235 }
236 
237 template <class E> inline VectorView_<E>
240  helper, typename MatrixHelper<Scalar>::DiagonalView());
241  return VectorView_<E>(h.stealRep());
242 }
243 
244 template <class E> inline VectorView_<E>
247  helper, typename MatrixHelper<Scalar>::DiagonalView());
248  return VectorView_<E>(h.stealRep());
249 }
250 
251 template <class ELT> inline VectorView_<ELT>
252 MatrixBase<ELT>::col(int j) const {
253  SimTK_INDEXCHECK(j,ncol(),"MatrixBase::col()");
254 
256  helper,0,j,nrow(),1);
257  return VectorView_<ELT>(h.stealRep());
258 }
259 
260 template <class ELT> inline VectorView_<ELT>
262  SimTK_INDEXCHECK(j,ncol(),"MatrixBase::updCol()");
263 
265  helper,0,j,nrow(),1);
266  return VectorView_<ELT>(h.stealRep());
267 }
268 
269 template <class ELT> inline RowVectorView_<ELT>
270 MatrixBase<ELT>::row(int i) const {
271  SimTK_INDEXCHECK(i,nrow(),"MatrixBase::row()");
272 
274  helper,i,0,1,ncol());
275  return RowVectorView_<ELT>(h.stealRep());
276 }
277 
278 template <class ELT> inline RowVectorView_<ELT>
280  SimTK_INDEXCHECK(i,nrow(),"MatrixBase::updRow()");
281 
283  helper,i,0,1,ncol());
284  return RowVectorView_<ELT>(h.stealRep());
285 }
286 
287 // M = diag(v) * M; v must have nrow() elements.
288 // That is, M[i] *= v[i].
289 template <class ELT> template <class EE> inline MatrixBase<ELT>&
291  assert(v.nrow() == nrow());
292  for (int i=0; i < nrow(); ++i)
293  (*this)[i] *= v[i];
294  return *this;
295 }
296 
297 template <class ELT> template <class EE> inline void
298 MatrixBase<ELT>::rowScale(const VectorBase<EE>& v, typename MatrixBase<ELT>::template EltResult<EE>::Mul& out) const {
299  assert(v.nrow() == nrow());
300  out.resize(nrow(), ncol());
301  for (int j=0; j<ncol(); ++j)
302  for (int i=0; i<nrow(); ++i)
303  out(i,j) = (*this)(i,j) * v[i];
304 }
305 
306 // M = M * diag(v); v must have ncol() elements
307 // That is, M(i) *= v[i]
308 template <class ELT> template <class EE> inline MatrixBase<ELT>&
310  assert(v.nrow() == ncol());
311  for (int j=0; j < ncol(); ++j)
312  (*this)(j) *= v[j];
313  return *this;
314 }
315 
316 template <class ELT> template <class EE> inline void
317 MatrixBase<ELT>::colScale(const VectorBase<EE>& v, typename MatrixBase<ELT>::template EltResult<EE>::Mul& out) const {
318  assert(v.nrow() == ncol());
319  out.resize(nrow(), ncol());
320  for (int j=0; j<ncol(); ++j)
321  for (int i=0; i<nrow(); ++i)
322  out(i,j) = (*this)(i,j) * v[j];
323 }
324 
325 
326 // M(i,j) *= r[i]*c[j]; r must have nrow() elements; c must have ncol() elements
327 template <class ELT> template <class ER, class EC> inline MatrixBase<ELT>&
329  assert(r.nrow()==nrow() && c.nrow()==ncol());
330  for (int j=0; j<ncol(); ++j)
331  for (int i=0; i<nrow(); ++i)
332  (*this)(i,j) *= (r[i]*c[j]);
333  return *this;
334 }
335 
336 template <class ELT> template <class ER, class EC> inline void
338  const VectorBase<ER>& r,
339  const VectorBase<EC>& c,
340  typename EltResult<typename VectorBase<ER>::template EltResult<EC>::Mul>::Mul&
341  out) const
342 {
343  assert(r.nrow()==nrow() && c.nrow()==ncol());
344  out.resize(nrow(), ncol());
345  for (int j=0; j<ncol(); ++j)
346  for (int i=0; i<nrow(); ++i)
347  out(i,j) = (*this)(i,j) * (r[i]*c[j]);
348 }
349 
350 // M(i,j) = s
351 template <class ELT> template <class S> inline MatrixBase<ELT>&
353  for (int j=0; j<ncol(); ++j)
354  for (int i=0; i<nrow(); ++i)
355  (*this)(i,j) = s;
356  return *this;
357 }
358 
359 // Set M(i,j) = M(i,j)^-1.
360 template <class ELT> inline MatrixBase<ELT>&
362  const int nr=nrow(), nc=ncol();
363  for (int j=0; j<nc; ++j)
364  for (int i=0; i<nr; ++i) {
365  ELT& e = updElt(i,j);
366  e = CNT<ELT>::invert(e);
367  }
368  return *this;
369 }
370 
371 template <class ELT> inline void
373  const int nr=nrow(), nc=ncol();
374  out.resize(nr,nc);
375  for (int j=0; j<nc; ++j)
376  for (int i=0; i<nr; ++i)
377  out(i,j) = CNT<ELT>::invert((*this)(i,j));
378 }
379 
380 // M(i,j) += s
381 template <class ELT> template <class S> inline MatrixBase<ELT>&
383  for (int j=0; j<ncol(); ++j)
384  for (int i=0; i<nrow(); ++i)
385  (*this)(i,j) += s;
386  return *this;
387 }
388 
389 template <class ELT> template <class S> inline void
391  const S& s,
392  typename MatrixBase<ELT>::template EltResult<S>::Add& out) const
393 {
394  const int nr=nrow(), nc=ncol();
395  out.resize(nr,nc);
396  for (int j=0; j<nc; ++j)
397  for (int i=0; i<nr; ++i)
398  out(i,j) = (*this)(i,j) + s;
399 }
400 
401 // M(i,j) -= s
402 template <class ELT> template <class S> inline MatrixBase<ELT>&
404  for (int j=0; j<ncol(); ++j)
405  for (int i=0; i<nrow(); ++i)
406  (*this)(i,j) -= s;
407  return *this;
408 }
409 
410 template <class ELT> template <class S> inline void
412  const S& s,
413  typename MatrixBase<ELT>::template EltResult<S>::Sub& out) const
414 {
415  const int nr=nrow(), nc=ncol();
416  out.resize(nr,nc);
417  for (int j=0; j<nc; ++j)
418  for (int i=0; i<nr; ++i)
419  out(i,j) = (*this)(i,j) - s;
420 }
421 
422 // M(i,j) = s - M(i,j)
423 template <class ELT> template <class S> inline MatrixBase<ELT>&
425  const int nr=nrow(), nc=ncol();
426  for (int j=0; j<nc; ++j)
427  for (int i=0; i<nr; ++i) {
428  ELT& e = updElt(i,j);
429  e = s - e;
430  }
431  return *this;
432 }
433 
434 template <class ELT> template <class S> inline void
436  const S& s,
437  typename MatrixBase<S>::template EltResult<ELT>::Sub& out) const
438 {
439  const int nr=nrow(), nc=ncol();
440  out.resize(nr,nc);
441  for (int j=0; j<nc; ++j)
442  for (int i=0; i<nr; ++i)
443  out(i,j) = s - (*this)(i,j);
444 }
445 
446 // M(i,j) *= R(i,j); R must have same dimensions as this
447 template <class ELT> template <class EE> inline MatrixBase<ELT>&
449  const int nr=nrow(), nc=ncol();
450  assert(r.nrow()==nr && r.ncol()==nc);
451  for (int j=0; j<nc; ++j)
452  for (int i=0; i<nr; ++i)
453  (*this)(i,j) *= r(i,j);
454  return *this;
455 }
456 
457 template <class ELT> template <class EE> inline void
459  const MatrixBase<EE>& r,
460  typename MatrixBase<ELT>::template EltResult<EE>::Mul& out) const
461 {
462  const int nr=nrow(), nc=ncol();
463  assert(r.nrow()==nr && r.ncol()==nc);
464  out.resize(nr,nc);
465  for (int j=0; j<nc; ++j)
466  for (int i=0; i<nr; ++i)
467  out(i,j) = (*this)(i,j) * r(i,j);
468 }
469 
470 // M(i,j) = R(i,j) * M(i,j); R must have same dimensions as this
471 template <class ELT> template <class EE> inline MatrixBase<ELT>&
473  const int nr=nrow(), nc=ncol();
474  assert(r.nrow()==nr && r.ncol()==nc);
475  for (int j=0; j<nc; ++j)
476  for (int i=0; i<nr; ++i) {
477  ELT& e = updElt(i,j);
478  e = r(i,j) * e;
479  }
480  return *this;
481 }
482 
483 template <class ELT> template <class EE> inline void
485  const MatrixBase<EE>& r,
486  typename MatrixBase<EE>::template EltResult<ELT>::Mul& out) const
487 {
488  const int nr=nrow(), nc=ncol();
489  assert(r.nrow()==nr && r.ncol()==nc);
490  out.resize(nr,nc);
491  for (int j=0; j<nc; ++j)
492  for (int i=0; i<nr; ++i)
493  out(i,j) = r(i,j) * (*this)(i,j);
494 }
495 
496 // M(i,j) /= R(i,j); R must have same dimensions as this
497 template <class ELT> template <class EE> inline MatrixBase<ELT>&
499  const int nr=nrow(), nc=ncol();
500  assert(r.nrow()==nr && r.ncol()==nc);
501  for (int j=0; j<nc; ++j)
502  for (int i=0; i<nr; ++i)
503  (*this)(i,j) /= r(i,j);
504  return *this;
505 }
506 
507 template <class ELT> template <class EE> inline void
509  const MatrixBase<EE>& r,
510  typename MatrixBase<ELT>::template EltResult<EE>::Dvd& out) const
511 {
512  const int nr=nrow(), nc=ncol();
513  assert(r.nrow()==nr && r.ncol()==nc);
514  out.resize(nr,nc);
515  for (int j=0; j<nc; ++j)
516  for (int i=0; i<nr; ++i)
517  out(i,j) = (*this)(i,j) / r(i,j);
518 }
519 // M(i,j) = R(i,j) / M(i,j); R must have same dimensions as this
520 template <class ELT> template <class EE> inline MatrixBase<ELT>&
522  const int nr=nrow(), nc=ncol();
523  assert(r.nrow()==nr && r.ncol()==nc);
524  for (int j=0; j<nc; ++j)
525  for (int i=0; i<nr; ++i) {
526  ELT& e = updElt(i,j);
527  e = r(i,j) / e;
528  }
529  return *this;
530 }
531 
532 template <class ELT> template <class EE> inline void
534  const MatrixBase<EE>& r,
535  typename MatrixBase<EE>::template EltResult<ELT>::Dvd& out) const
536 {
537  const int nr=nrow(), nc=ncol();
538  assert(r.nrow()==nr && r.ncol()==nc);
539  out.resize(nr,nc);
540  for (int j=0; j<nc; ++j)
541  for (int i=0; i<nr; ++i)
542  out(i,j) = r(i,j) / (*this)(i,j);
543 }
544 
545 /*
546 template <class ELT> inline MatrixView_< typename CNT<ELT>::TReal >
547 MatrixBase<ELT>::real() const {
548  if (!CNT<ELT>::IsComplex) { // known at compile time
549  return MatrixView_< typename CNT<ELT>::TReal >( // this is just ELT
550  MatrixHelper(helper,0,0,nrow(),ncol())); // a view of the whole matrix
551  }
552  // Elements are complex -- helper uses underlying precision (real) type.
553  MatrixHelper<Precision> h(helper,typename MatrixHelper<Precision>::RealView);
554  return MatrixView_< typename CNT<ELT>::TReal >(h);
555 }
556 */
557 
558 
559 // ----------------------------------------------------------------------------
563 
564 // + and - allow mixed element types, but will fail to compile if the elements aren't
565 // compatible. At run time these will fail if the dimensions are incompatible.
566 template <class E1, class E2>
567 Matrix_<typename CNT<E1>::template Result<E2>::Add>
569  return Matrix_<typename CNT<E1>::template Result<E2>::Add>(l) += r;
570 }
571 
572 template <class E>
573 Matrix_<E> operator+(const MatrixBase<E>& l, const typename CNT<E>::T& r) {
574  return Matrix_<E>(l) += r;
575 }
576 
577 template <class E>
578 Matrix_<E> operator+(const typename CNT<E>::T& l, const MatrixBase<E>& r) {
579  return Matrix_<E>(r) += l;
580 }
581 
582 template <class E1, class E2>
583 Matrix_<typename CNT<E1>::template Result<E2>::Sub>
585  return Matrix_<typename CNT<E1>::template Result<E2>::Sub>(l) -= r;
586 }
587 
588 template <class E>
589 Matrix_<E> operator-(const MatrixBase<E>& l, const typename CNT<E>::T& r) {
590  return Matrix_<E>(l) -= r;
591 }
592 
593 template <class E>
594 Matrix_<E> operator-(const typename CNT<E>::T& l, const MatrixBase<E>& r) {
595  Matrix_<E> temp(r.nrow(), r.ncol());
596  temp = l;
597  return (temp -= r);
598 }
599 
600 // Scalar multiply and divide. You might wish the scalar could be
601 // a templatized type "E2", but that would create horrible ambiguities since
602 // E2 would match not only scalar types but everything else including
603 // matrices.
604 template <class E> Matrix_<E>
605 operator*(const MatrixBase<E>& l, const typename CNT<E>::StdNumber& r)
606  { return Matrix_<E>(l)*=r; }
607 
608 template <class E> Matrix_<E>
609 operator*(const typename CNT<E>::StdNumber& l, const MatrixBase<E>& r)
610  { return Matrix_<E>(r)*=l; }
611 
612 template <class E> Matrix_<E>
613 operator/(const MatrixBase<E>& l, const typename CNT<E>::StdNumber& r)
614  { return Matrix_<E>(l)/=r; }
615 
616 // Handle ints explicitly.
617 template <class E> Matrix_<E>
618 operator*(const MatrixBase<E>& l, int r)
619  { return Matrix_<E>(l)*= typename CNT<E>::StdNumber(r); }
620 
621 template <class E> Matrix_<E>
622 operator*(int l, const MatrixBase<E>& r)
623  { return Matrix_<E>(r)*= typename CNT<E>::StdNumber(l); }
624 
625 template <class E> Matrix_<E>
626 operator/(const MatrixBase<E>& l, int r)
627  { return Matrix_<E>(l)/= typename CNT<E>::StdNumber(r); }
628 
630 
634 
635 template <class E1, class E2>
636 Vector_<typename CNT<E1>::template Result<E2>::Add>
638  return Vector_<typename CNT<E1>::template Result<E2>::Add>(l) += r;
639 }
640 template <class E>
641 Vector_<E> operator+(const VectorBase<E>& l, const typename CNT<E>::T& r) {
642  return Vector_<E>(l) += r;
643 }
644 template <class E>
645 Vector_<E> operator+(const typename CNT<E>::T& l, const VectorBase<E>& r) {
646  return Vector_<E>(r) += l;
647 }
648 template <class E1, class E2>
649 Vector_<typename CNT<E1>::template Result<E2>::Sub>
651  return Vector_<typename CNT<E1>::template Result<E2>::Sub>(l) -= r;
652 }
653 template <class E>
654 Vector_<E> operator-(const VectorBase<E>& l, const typename CNT<E>::T& r) {
655  return Vector_<E>(l) -= r;
656 }
657 template <class E>
658 Vector_<E> operator-(const typename CNT<E>::T& l, const VectorBase<E>& r) {
659  Vector_<E> temp(r.size());
660  temp = l;
661  return (temp -= r);
662 }
663 
664 // Scalar multiply and divide.
665 
666 template <class E> Vector_<E>
667 operator*(const VectorBase<E>& l, const typename CNT<E>::StdNumber& r)
668  { return Vector_<E>(l)*=r; }
669 
670 template <class E> Vector_<E>
671 operator*(const typename CNT<E>::StdNumber& l, const VectorBase<E>& r)
672  { return Vector_<E>(r)*=l; }
673 
674 template <class E> Vector_<E>
675 operator/(const VectorBase<E>& l, const typename CNT<E>::StdNumber& r)
676  { return Vector_<E>(l)/=r; }
677 
678 // Handle ints explicitly
679 template <class E> Vector_<E>
680 operator*(const VectorBase<E>& l, int r)
681  { return Vector_<E>(l)*= typename CNT<E>::StdNumber(r); }
682 
683 template <class E> Vector_<E>
684 operator*(int l, const VectorBase<E>& r)
685  { return Vector_<E>(r)*= typename CNT<E>::StdNumber(l); }
686 
687 template <class E> Vector_<E>
688 operator/(const VectorBase<E>& l, int r)
689  { return Vector_<E>(l)/= typename CNT<E>::StdNumber(r); }
690 
691 // These are fancier "scalars"; whether they are allowed depends on
692 // whether the element type and the CNT are compatible.
693 
694 // Vector * Vec
695 template <class E1, int M, class E2, int S>
696 Vector_<typename CNT<E1>::template Result< Vec<M,E2,S> >::Mul>
697 operator*(const VectorBase<E1>& v, const Vec<M,E2,S>& s) {
698  Vector_<typename CNT<E1>::template Result< Vec<M,E2,S> >::Mul> res(v.nrow());
699  for (int i=0; i < v.nrow(); ++i)
700  res[i] = v[i]*s;
701  return res;
702 }
703 
704 // Vec * Vector
705 template <class E1, int M, class E2, int S>
707 operator*(const Vec<M,E2,S>& s, const VectorBase<E1>& v) {
708  Vector_<typename Vec<M,E2,S>::template Result<E1>::Mul> res(v.nrow());
709  for (int i=0; i < v.nrow(); ++i)
710  res[i] = s*v[i];
711  return res;
712 }
713 
714 // Vector * Row
715 template <class E1, int N, class E2, int S>
716 Vector_<typename CNT<E1>::template Result< Row<N,E2,S> >::Mul>
717 operator*(const VectorBase<E1>& v, const Row<N,E2,S>& s) {
718  Vector_<typename CNT<E1>::template Result< Row<N,E2,S> >::Mul> res(v.nrow());
719  for (int i=0; i < v.nrow(); ++i)
720  res[i] = v[i]*s;
721  return res;
722 }
723 
724 // Row * Vector
725 template <class E1, int N, class E2, int S>
727 operator*(const Row<N,E2,S>& s, const VectorBase<E1>& v) {
728  Vector_<typename Row<N,E2,S>::template Result<E1>::Mul> res(v.nrow());
729  for (int i=0; i < v.nrow(); ++i)
730  res[i] = s*v[i];
731  return res;
732 }
733 
734 // Vector * Mat
735 template <class E1, int M, int N, class E2, int S1, int S2>
736 Vector_<typename CNT<E1>::template Result< Mat<M,N,E2,S1,S2> >::Mul>
738  Vector_<typename CNT<E1>::template Result< Mat<M,N,E2,S1,S2> >::Mul> res(v.nrow());
739  for (int i=0; i < v.nrow(); ++i)
740  res[i] = v[i]*s;
741  return res;
742 }
743 
744 // Mat * Vector
745 template <class E1, int M, int N, class E2, int S1, int S2>
748  Vector_<typename Mat<M,N,E2,S1,S2>::template Result<E1>::Mul> res(v.nrow());
749  for (int i=0; i < v.nrow(); ++i)
750  res[i] = s*v[i];
751  return res;
752 }
753 
754 // Vector * SymMat
755 template <class E1, int M, class E2, int S>
756 Vector_<typename CNT<E1>::template Result< SymMat<M,E2,S> >::Mul>
758  Vector_<typename CNT<E1>::template Result< SymMat<M,E2,S> >::Mul> res(v.nrow());
759  for (int i=0; i < v.nrow(); ++i)
760  res[i] = v[i]*s;
761  return res;
762 }
763 
764 // SymMat * Vector
765 template <class E1, int M, class E2, int S>
768  Vector_<typename SymMat<M,E2,S>::template Result<E1>::Mul> res(v.nrow());
769  for (int i=0; i < v.nrow(); ++i)
770  res[i] = s*v[i];
771  return res;
772 }
773 
775 
779 
780 template <class E1, class E2>
783  return RowVector_<typename CNT<E1>::template Result<E2>::Add>(l) += r;
784 }
785 template <class E>
786 RowVector_<E> operator+(const RowVectorBase<E>& l, const typename CNT<E>::T& r) {
787  return RowVector_<E>(l) += r;
788 }
789 template <class E>
790 RowVector_<E> operator+(const typename CNT<E>::T& l, const RowVectorBase<E>& r) {
791  return RowVector_<E>(r) += l;
792 }
793 template <class E1, class E2>
796  return RowVector_<typename CNT<E1>::template Result<E2>::Sub>(l) -= r;
797 }
798 template <class E>
799 RowVector_<E> operator-(const RowVectorBase<E>& l, const typename CNT<E>::T& r) {
800  return RowVector_<E>(l) -= r;
801 }
802 template <class E>
803 RowVector_<E> operator-(const typename CNT<E>::T& l, const RowVectorBase<E>& r) {
804  RowVector_<E> temp(r.size());
805  temp = l;
806  return (temp -= r);
807 }
808 
809 // Scalar multiply and divide
810 
811 template <class E> RowVector_<E>
812 operator*(const RowVectorBase<E>& l, const typename CNT<E>::StdNumber& r)
813  { return RowVector_<E>(l)*=r; }
814 
815 template <class E> RowVector_<E>
816 operator*(const typename CNT<E>::StdNumber& l, const RowVectorBase<E>& r)
817  { return RowVector_<E>(r)*=l; }
818 
819 template <class E> RowVector_<E>
820 operator/(const RowVectorBase<E>& l, const typename CNT<E>::StdNumber& r)
821  { return RowVector_<E>(l)/=r; }
822 
823 // Handle ints explicitly.
824 template <class E> RowVector_<E>
825 operator*(const RowVectorBase<E>& l, int r)
826  { return RowVector_<E>(l)*= typename CNT<E>::StdNumber(r); }
827 
828 template <class E> RowVector_<E>
829 operator*(int l, const RowVectorBase<E>& r)
830  { return RowVector_<E>(r)*= typename CNT<E>::StdNumber(l); }
831 
832 template <class E> RowVector_<E>
833 operator/(const RowVectorBase<E>& l, int r)
834  { return RowVector_<E>(l)/= typename CNT<E>::StdNumber(r); }
835 
836 
837 // These are fancier "scalars"; whether they are allowed depends on
838 // whether the element type and the CNT are compatible.
839 
840 // RowVector * Vec
841 template <class E1, int M, class E2, int S>
842 RowVector_<typename CNT<E1>::template Result< Vec<M,E2,S> >::Mul>
844  RowVector_<typename CNT<E1>::template Result< Vec<M,E2,S> >::Mul> res(v.ncol());
845  for (int i=0; i < v.ncol(); ++i)
846  res[i] = v[i]*s;
847  return res;
848 }
849 
850 // Vec * RowVector
851 template <class E1, int M, class E2, int S>
854  RowVector_<typename Vec<M,E2,S>::template Result<E1>::Mul> res(v.ncol());
855  for (int i=0; i < v.ncol(); ++i)
856  res[i] = s*v[i];
857  return res;
858 }
859 
860 // RowVector * Row
861 template <class E1, int N, class E2, int S>
862 RowVector_<typename CNT<E1>::template Result< Row<N,E2,S> >::Mul>
864  RowVector_<typename CNT<E1>::template Result< Row<N,E2,S> >::Mul> res(v.ncol());
865  for (int i=0; i < v.ncol(); ++i)
866  res[i] = v[i]*s;
867  return res;
868 }
869 
870 // Row * RowVector
871 template <class E1, int N, class E2, int S>
874  RowVector_<typename Row<N,E2,S>::template Result<E1>::Mul> res(v.ncol());
875  for (int i=0; i < v.ncol(); ++i)
876  res[i] = s*v[i];
877  return res;
878 }
879 
880 // RowVector * Mat
881 template <class E1, int M, int N, class E2, int S1, int S2>
882 RowVector_<typename CNT<E1>::template Result< Mat<M,N,E2,S1,S2> >::Mul>
884  RowVector_<typename CNT<E1>::template Result< Mat<M,N,E2,S1,S2> >::Mul> res(v.ncol());
885  for (int i=0; i < v.ncol(); ++i)
886  res[i] = v[i]*s;
887  return res;
888 }
889 
890 // Mat * RowVector
891 template <class E1, int M, int N, class E2, int S1, int S2>
894  RowVector_<typename Mat<M,N,E2,S1,S2>::template Result<E1>::Mul> res(v.ncol());
895  for (int i=0; i < v.ncol(); ++i)
896  res[i] = s*v[i];
897  return res;
898 }
899 
900 // RowVector * SymMat
901 template <class E1, int M, class E2, int S>
902 RowVector_<typename CNT<E1>::template Result< SymMat<M,E2,S> >::Mul>
904  RowVector_<typename CNT<E1>::template Result< SymMat<M,E2,S> >::Mul> res(v.ncol());
905  for (int i=0; i < v.ncol(); ++i)
906  res[i] = v[i]*s;
907  return res;
908 }
909 
910 // SymMat * RowVector
911 template <class E1, int M, class E2, int S>
914  RowVector_<typename SymMat<M,E2,S>::template Result<E1>::Mul> res(v.ncol());
915  for (int i=0; i < v.ncol(); ++i)
916  res[i] = s*v[i];
917  return res;
918 }
919 
921 
922 
927 
928  // TODO: these should use LAPACK!
929 
930 // Dot product
931 template <class E1, class E2>
932 typename CNT<E1>::template Result<E2>::Mul
934  assert(r.ncol() == v.nrow());
935  typename CNT<E1>::template Result<E2>::Mul sum(0);
936  for (int j=0; j < r.ncol(); ++j)
937  sum += r(j) * v[j];
938  return sum;
939 }
940 
941 template <class E1, class E2>
942 Vector_<typename CNT<E1>::template Result<E2>::Mul>
944  assert(m.ncol() == v.nrow());
945  Vector_<typename CNT<E1>::template Result<E2>::Mul> res(m.nrow());
946  for (int i=0; i< m.nrow(); ++i)
947  res[i] = m[i]*v;
948  return res;
949 }
950 
951 template <class E1, class E2>
952 Matrix_<typename CNT<E1>::template Result<E2>::Mul>
953 operator*(const MatrixBase<E1>& m1, const MatrixBase<E2>& m2) {
954  assert(m1.ncol() == m2.nrow());
955  Matrix_<typename CNT<E1>::template Result<E2>::Mul>
956  res(m1.nrow(),m2.ncol());
957 
958  for (int j=0; j < res.ncol(); ++j)
959  for (int i=0; i < res.nrow(); ++i)
960  res(i,j) = m1[i] * m2(j);
961 
962  return res;
963 }
964 
966 
967 // This "private" static method is used to implement VectorView's
968 // fillVectorViewFromStream() and Vector's readVectorFromStream()
969 // namespace-scope static methods, which are in turn used to implement
970 // VectorView's and
971 // Vector's stream extraction operators ">>". This method has to be in the
972 // header file so that we don't need to pass streams through the API, but it
973 // is not intended for use by users and has no Doxygen presence, unlike
974 // fillArrayFromStream() and readArrayFromStream() and (more commonly)
975 // the extraction operators.
976 template <class T> static inline
977 std::istream& readVectorFromStreamHelper
978  (std::istream& in, bool isFixedSize, Vector_<T>& out)
979 {
980  // If already failed, bad, or eof, set failed bit and return without
981  // touching the Vector.
982  if (!in.good()) {in.setstate(std::ios::failbit); return in;}
983 
984  // If the passed-in Vector isn't resizeable, then we have to treat it as
985  // a fixed size VectorView regardless of the setting of the isFixedSize
986  // argument.
987  if (!out.isResizeable())
988  isFixedSize = true; // might be overriding the argument here
989 
990  // numRequired will be ignored unless isFixedSize==true.
991  const int numRequired = isFixedSize ? out.size() : 0;
992 
993  if (!isFixedSize)
994  out.clear(); // We're going to replace the entire contents of the Array.
995 
996  // Skip initial whitespace. If that results in eof this may be a successful
997  // read of a 0-length, unbracketed Vector. That is OK for either a
998  // variable-length Vector or a fixed-length VectorView of length zero.
999  std::ws(in); if (in.fail()) return in;
1000  if (in.eof()) {
1001  if (isFixedSize && numRequired != 0)
1002  in.setstate(std::ios_base::failbit); // zero elements not OK
1003  return in;
1004  }
1005 
1006  // Here the stream is good and the next character is non-white.
1007  assert(in.good());
1008 
1009  // Use this for raw i/o (peeks and gets).
1010  typename std::iostream::int_type ch;
1011 #ifndef NDEBUG
1012  const typename std::iostream::int_type EOFch =
1013  std::iostream::traits_type::eof();
1014 #endif
1015 
1016  // First we'll look for the optional "~". If found, the brackets become
1017  // required.
1018  bool tildeFound = false;
1019  ch = in.peek(); if (in.fail()) return in;
1020  assert(ch != EOFch); // we already checked above
1021  if ((char)ch == '~') {
1022  tildeFound = true;
1023  in.get(); // absorb the tilde
1024  // Eat whitespace after the tilde to see what's next.
1025  if (in.good()) std::ws(in);
1026  // If we hit eof after the tilde we don't like the formatting.
1027  if (!in.good()) {in.setstate(std::ios_base::failbit); return in;}
1028  }
1029 
1030  // Here the stream is good, the next character is non-white, and we
1031  // might have seen a tilde.
1032  assert(in.good());
1033 
1034  // Now see if the sequence is bare or surrounded by (), or [].
1035  bool lookForCloser = true;
1036  char openBracket, closeBracket;
1037  ch = in.peek(); if (in.fail()) return in;
1038  assert(ch != EOFch); // we already checked above
1039 
1040  openBracket = (char)ch;
1041  if (openBracket=='(') {in.get(); closeBracket = ')';}
1042  else if (openBracket=='[') {in.get(); closeBracket = ']';}
1043  else lookForCloser = false;
1044 
1045  // If we found a tilde, the opening bracket was mandatory. If we didn't
1046  // find one then we reject the formatting.
1047  if (tildeFound && !lookForCloser)
1048  { in.setstate(std::ios_base::failbit); return in;}
1049 
1050  // If lookForCloser is true, then closeBracket contains the terminating
1051  // delimiter, otherwise we're not going to quit until eof.
1052 
1053  // Eat whitespace after the opening bracket to see what's next.
1054  if (in.good()) std::ws(in);
1055 
1056  // If we're at eof now it must be because the open bracket was the
1057  // last non-white character in the stream, which is an error.
1058  if (!in.good()) {
1059  if (in.eof()) {
1060  assert(lookForCloser); // or we haven't read anything that could eof
1061  in.setstate(std::ios::failbit);
1062  }
1063  return in;
1064  }
1065 
1066  // istream is good and next character is non-white; ready to read first
1067  // value or terminator.
1068 
1069  // We need to figure out whether the elements are space- or comma-
1070  // separated and then insist on consistency.
1071  bool commaOK = true, commaRequired = false;
1072  bool terminatorSeen = false;
1073  int nextIndex = 0;
1074  while (true) {
1075  char c;
1076 
1077  // Here at the top of this loop, we have already successfully read
1078  // n=nextIndex values of type T. For fixed-size reads, it might be
1079  // the case that n==numRequired already, but we still may need to
1080  // look for a closing bracket before we can declare victory.
1081  // The stream is good() (not at eof) but it might be the case that
1082  // there is nothing but white space left; we don't know yet because
1083  // if we have satisfied the fixed-size count and are not expecting
1084  // a terminator then we should quit without absorbing the trailing
1085  // white space.
1086  assert(in.good());
1087 
1088  // Look for closing bracket before trying to read value.
1089  if (lookForCloser) {
1090  // Eat white space to find the closing bracket.
1091  std::ws(in); if (!in.good()) break; // eof?
1092  ch = in.peek(); assert(ch != EOFch);
1093  if (!in.good()) break;
1094  c = (char)ch;
1095  if (c == closeBracket) {
1096  in.get(); // absorb the closing bracket
1097  terminatorSeen = true;
1098  break;
1099  }
1100  // next char not a closing bracket; fall through
1101  }
1102 
1103  // We didn't look or didn't find a closing bracket. The istream is good
1104  // but we might be looking at white space.
1105 
1106  // If we already got all the elements we want, break for final checks.
1107  if (isFixedSize && (nextIndex == numRequired))
1108  break; // that's a full count.
1109 
1110  // Look for comma before value, except the first time.
1111  if (commaOK && nextIndex != 0) {
1112  // Eat white space to find the comma.
1113  std::ws(in); if (!in.good()) break; // eof?
1114  ch = in.peek(); assert(ch != EOFch);
1115  if (!in.good()) break;
1116  c = (char)ch;
1117  if (c == ',') {
1118  in.get(); // absorb comma
1119  commaRequired = true; // all commas from now on
1120  } else { // next char not a comma
1121  if (commaRequired) // bad, e.g.: v1, v2, v3 v4
1122  { in.setstate(std::ios::failbit); break; }
1123  else commaOK = false; // saw: v1 v2 (no commas now)
1124  }
1125  if (!in.good()) break; // might be eof
1126  }
1127 
1128  // No closing bracket yet; don't have enough elements; skipped comma
1129  // if any; istream is good; might be looking at white space.
1130  assert(in.good());
1131 
1132  // Now read in an element of type T.
1133  // The extractor T::operator>>() will ignore leading white space.
1134  if (!isFixedSize)
1135  out.resizeKeep(out.size()+1); // grow by one (default consructed)
1136  in >> out[nextIndex]; if (in.fail()) break;
1137  ++nextIndex;
1138 
1139  if (!in.good()) break; // might be eof
1140  }
1141 
1142  // We will get here under a number of circumstances:
1143  // - the fail bit is set in the istream, or
1144  // - we reached eof
1145  // - we saw a closing brace
1146  // - we got all the elements we wanted (for a fixed-size read)
1147  // Note that it is possible that we consumed everything except some
1148  // trailing white space (meaning we're not technically at eof), but
1149  // for consistency with built-in operator>>()'s we won't try to absorb
1150  // that trailing white space.
1151 
1152  if (!in.fail()) {
1153  if (lookForCloser && !terminatorSeen)
1154  in.setstate(std::ios::failbit); // missing terminator
1155 
1156  if (isFixedSize && nextIndex != numRequired)
1157  in.setstate(std::ios::failbit); // wrong number of values
1158  }
1159 
1160  return in;
1161 }
1162 
1163 
1164 
1165 //------------------------------------------------------------------------------
1166 // RELATED GLOBAL OPERATORS
1167 //------------------------------------------------------------------------------
1168 // These are logically part of the Matrix_<T> class but are not actually
1169 // class members; that is, they are in the SimTK namespace.
1170 
1182 template <class E> inline void
1183 writeUnformatted(std::ostream& o, const VectorBase<E>& v) {
1184  const int sz = v.size();
1185  for (int i=0; i < sz; ++i) {
1186  if (i != 0) o << " ";
1187  writeUnformatted(o, v[i]);
1188  }
1189 }
1192 template <class E> inline void
1193 writeUnformatted(std::ostream& o, const VectorView_<E>& v)
1194 { writeUnformatted(o, static_cast< const VectorBase<E> >(v)); }
1195 
1198 template <class E> inline void
1199 writeUnformatted(std::ostream& o, const Vector_<E>& v)
1200 { writeUnformatted(o, static_cast< const VectorBase<E> >(v)); }
1201 
1205 template <class E> inline void
1206 writeUnformatted(std::ostream& o, const RowVectorBase<E>& v)
1207 { writeUnformatted(o, ~v); }
1208 
1211 template <class E> inline void
1212 writeUnformatted(std::ostream& o, const RowVectorView_<E>& v)
1213 { writeUnformatted(o, static_cast< const RowVectorBase<E> >(v)); }
1214 
1217 template <class E> inline void
1218 writeUnformatted(std::ostream& o, const RowVector_<E>& v)
1219 { writeUnformatted(o, static_cast< const RowVectorBase<E> >(v)); }
1220 
1224 template <class E> inline void
1225 writeUnformatted(std::ostream& o, const MatrixBase<E>& v) {
1226  const int nr = v.nrow();
1227  for (int i=0; i < nr; ++i) {
1228  if (i != 0) o << std::endl;
1229  writeUnformatted(o, v[i]);
1230  }
1231 }
1234 template <class E> inline void
1235 writeUnformatted(std::ostream& o, const MatrixView_<E>& v)
1236 { writeUnformatted(o, static_cast< const MatrixBase<E> >(v)); }
1237 
1240 template <class E> inline void
1241 writeUnformatted(std::ostream& o, const Matrix_<E>& v)
1242 { writeUnformatted(o, static_cast< const MatrixBase<E> >(v)); }
1243 
1247 template <class E> inline bool
1248 readUnformatted(std::istream& in, VectorView_<E>& v) {
1249  for (int i=0; i < v.size(); ++i)
1250  if (!readUnformatted(in, v[i])) return false;
1251  return true;
1252 }
1253 
1256 template <class E> inline bool
1257 readUnformatted(std::istream& in, Vector_<E>& v) {
1258  if (!v.isResizeable())
1259  return readUnformatted(in, v.updAsVectorView());
1260 
1261  Array_<E,int> a;
1262  if (!readUnformatted(in,a)) return false;
1263  v.resize(a.size());
1264  for (int i=0; i<a.size(); ++i)
1265  v[i] = a[i];
1266  return true;
1267 }
1268 
1272 template <class E> inline bool
1273 readUnformatted(std::istream& in, RowVectorView_<E>& v)
1274 { VectorView_<E> vt(~v);
1275  return readUnformatted<E>(in, vt); }
1276 
1280 template <class E> inline bool
1281 readUnformatted(std::istream& in, RowVector_<E>& v)
1282 { Vector_<E> vt(~v);
1283  return readUnformatted<E>(in, vt); }
1284 
1290 template <class E> inline bool
1291 readUnformatted(std::istream& in, MatrixView_<E>& v) {
1292  for (int row=0; row < v.nrow(); ++row) {
1293  RowVectorView_<E> oneRow(v[row]);
1294  if (!readUnformatted<E>(in, oneRow)) return false;
1295  }
1296  return true;
1297 }
1298 
1304 template <class E> inline bool
1305 fillUnformatted(std::istream& in, Matrix_<E>& v) {
1306  return readUnformatted<E>(in, v.updAsMatrixView());
1307 }
1308 
1312 template <class E> inline bool
1313 readUnformatted(std::istream& in, Matrix_<E>& v) {
1314  SimTK_ASSERT_ALWAYS(!"implemented",
1315  "SimTK::readUnformatted(istream, Matrix) is not implemented; try"
1316  " SimTK::fillUnformatted(istream, Matrix) instead.");
1317  return false;
1318 }
1319 
1326 template <class T> inline std::ostream&
1327 operator<<(std::ostream& o, const VectorBase<T>& v)
1328 { o << "~[";
1329  if (v.size()) {
1330  o << v[0];
1331  for (int i=1; i < v.size(); ++i) o << " " << v[i];
1332  }
1333  return o << "]";
1334 }
1335 
1342 template <class T> inline std::ostream&
1343 operator<<(std::ostream& o, const RowVectorBase<T>& v)
1344 { o << "[";
1345  if (v.size()) {
1346  o << v[0];
1347  for (int i=1; i < v.size(); ++i) o << " " << v[i];
1348  }
1349  return o << "]";
1350 }
1351 
1359 template <class T> inline std::ostream&
1360 operator<<(std::ostream& o, const MatrixBase<T>& m) {
1361  for (int i=0;i<m.nrow();++i)
1362  o << std::endl << m[i];
1363  if (m.nrow()) o << std::endl;
1364  return o;
1365 }
1366 
1367 
1399 template <class T> static inline
1400 std::istream& readVectorFromStream(std::istream& in, Vector_<T>& out)
1401 { return readVectorFromStreamHelper<T>(in, false /*variable sizez*/, out); }
1402 
1403 
1404 
1429 template <class T> static inline
1430 std::istream& fillVectorFromStream(std::istream& in, Vector_<T>& out)
1431 { return readVectorFromStreamHelper<T>(in, true /*fixed size*/, out); }
1432 
1437 template <class T> static inline
1438 std::istream& fillVectorViewFromStream(std::istream& in, VectorView_<T>& out)
1439 { return readVectorFromStreamHelper<T>(in, true /*fixed size*/, out); }
1440 
1441 
1451 template <class T> inline
1452 std::istream& operator>>(std::istream& in, Vector_<T>& out)
1453 { return readVectorFromStream<T>(in, out); }
1454 
1462 template <class T> inline
1463 std::istream& operator>>(std::istream& in, VectorView_<T>& out)
1464 { return fillVectorViewFromStream<T>(in, out); } // End of Matrix serialization.
1466 
1467 // Friendly abbreviations for vectors and matrices with scalar elements.
1474 
1479 
1494 
1501 
1508 
1517 
1526 
1535 
1544 
1553 
1564 } //namespace SimTK
1565 
1566 #endif //SimTK_SIMMATRIX_BIGMATRIX_H_
Matrix_< E > operator/(const MatrixBase< E > &l, const typename CNT< E >::StdNumber &r)
Definition: BigMatrix.h:613
VectorView_< dComplex > dComplexVectorView
Abbreviation for VectorView_<std::complex<double>>.
Definition: BigMatrix.h:1525
MatrixView_< Real > MatrixView
Non-owner matrix sharing Real elements.
Definition: BigMatrix.h:1498
MatrixView_< fComplex > fComplexMatrixView
Abbreviation for MatrixView_<std::complex<float>>.
Definition: BigMatrix.h:1559
#define SimTK_SIZECHECK(sz, maxsz, where)
Definition: ExceptionMacros.h:146
Vector_< fComplex > fComplexVector
Abbreviation for Vector_<std::complex<float>>.
Definition: BigMatrix.h:1514
Here we declare the detailed interface to the Simmatrix classes.
size_type size() const
Return the current number of elements stored in this array.
Definition: Array.h:2075
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:1183
Vector_< float > fVector
Abbreviation for Vector_<float>.
Definition: BigMatrix.h:1510
This is a small, fixed-size symmetric or Hermitian matrix designed for no-overhead inline computation...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:621
This is the vector class intended to appear in user code for large, variable size column vectors...
Definition: BigMatrix.h:171
void elementwiseSubtractScalar(const S &s, typename EltResult< S >::Sub &) const
This is a dataless rehash of the MatrixBase class to specialize it for RowVectors.
Definition: BigMatrix.h:165
VectorView_< Complex > ComplexVectorView
Non-owner column vector sharing Complex (std::complex<Real>) elements.
Definition: BigMatrix.h:1503
void elementwiseSubtractFromScalar(const S &, typename MatrixBase< S >::template EltResult< E >::Sub &) const
Definition: BigMatrix.h:435
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:1463
bool readUnformatted(std::istream &in, T &v)
The default implementation of readUnformatted<T> reads in the next whitespace-separated token and the...
Definition: Serialize.h:176
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
MatrixBase & elementwiseSubtractScalarInPlace(const S &s)
Set M(i,j)-=s for every element of M and some value s.
VectorView_< Real > VectorView
Non-owner column vector sharing Real elements.
Definition: BigMatrix.h:1496
Defines an iterator that can be used to iterate over the elements of any kind of Simbody vector...
Define the SimTK::RowVectorBase class that is part of Simbody&#39;s BigMatrix toolset.
#define SimTK_ASSERT_ALWAYS(cond, msg)
Definition: ExceptionMacros.h:349
Matrix_< typename CNT< E1 >::template Result< E2 >::Sub > operator-(const MatrixBase< E1 > &l, const MatrixBase< E2 > &r)
Definition: BigMatrix.h:584
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.
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
MatrixBase & elementwiseMultiplyFromLeftInPlace(const MatrixBase< EE > &)
M(i,j) = R(i,j) * M(i,j); R must have same dimensions as this.
void elementwiseDivideFromLeft(const MatrixBase< EE > &, typename MatrixBase< EE >::template EltResult< E >::Dvd &) const
Definition: BigMatrix.h:533
bool readUnformatted(std::istream &in, VectorView_< E > &v)
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:1248
MatrixBase & elementwiseAssign(const S &s)
Set M(i,j)=s for every element of M and some value s.
RowVectorView_< Complex > ComplexRowVectorView
Non-owner row vector sharing Complex (std::complex<Real>) elements.
Definition: BigMatrix.h:1507
VectorView_< ELT > updCol(int j)
Definition: BigMatrix.h:261
MatrixView_< double > dMatrixView
Abbreviation for MatrixView_<double>.
Definition: BigMatrix.h:1557
void writeUnformatted(std::ostream &o, const Vector_< E > &v)
Raw serialization of Vector_<E>; same as VectorBase<E>.
Definition: BigMatrix.h:1199
RowVectorView_< ELT > updRow(int i)
Definition: BigMatrix.h:279
VectorView_< double > dVectorView
Abbreviation for VectorView_<double>.
Definition: BigMatrix.h:1521
static TInvert invert(const K &t)
Definition: CompositeNumericalTypes.h:243
VectorView_< ELT > col(int j) const
Definition: BigMatrix.h:252
MatrixView_< float > fMatrixView
Abbreviation for MatrixView_<float>.
Definition: BigMatrix.h:1555
bool readUnformatted(std::istream &in, Vector_< E > &v)
Read variable-size Vector from input stream.
Definition: BigMatrix.h:1257
Define the SimTK::VectorView_ class that is part of Simbody&#39;s BigMatrix toolset.
Vector_< dComplex > dComplexVector
Abbreviation for Vector_<std::complex<double>>.
Definition: BigMatrix.h:1516
void writeUnformatted(std::ostream &o, const VectorView_< E > &v)
Raw serialization of VectorView_<E>; same as VectorBase<E>.
Definition: BigMatrix.h:1193
VectorView_< fComplex > fComplexVectorView
Abbreviation for VectorView_<std::complex<float>>.
Definition: BigMatrix.h:1523
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:1452
(Advanced) This class is identical to RowVector_ except that it has shallow (reference) copy and assi...
Definition: BigMatrix.h:173
void writeUnformatted(std::ostream &o, const RowVector_< E > &v)
Raw serialization of RowVector_<E>; same as Vector_<E>.
Definition: BigMatrix.h:1218
bool isResizeable() const
Return true if either dimension of this Matrix is resizable.
Definition: MatrixBase.h:150
Matrix_< float > fMatrix
Abbreviation for Matrix_<float>.
Definition: BigMatrix.h:1546
ELEM sum(const VectorBase< ELEM > &v)
Definition: VectorMath.h:147
Define the SimTK::VectorBase class that is part of Simbody&#39;s BigMatrix toolset.
MatrixView_< ELT > updBlock(int i, int j, int m, int n)
Definition: BigMatrix.h:211
This is a fixed-length column vector designed for no-overhead inline computation. ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:618
MatrixBase< typename CNT< E >::TInvert > elementwiseInvert() const
Definition: MatrixBase.h:452
void writeUnformatted(std::ostream &o, const MatrixView_< E > &v)
Raw serialization of MatrixView_<E>; same as MatrixBase<E>.
Definition: BigMatrix.h:1235
This is an iterator for iterating over the elements of a Vector_ or Vec object.
Definition: BigMatrix.h:176
This is the default commitment for a row vector.
Definition: MatrixCharacteristics.h:1005
Matrix_< fComplex > fComplexMatrix
Abbreviation for Matrix_<std::complex<float>>.
Definition: BigMatrix.h:1550
Matrix_< E > operator*(const MatrixBase< E > &l, const typename CNT< E >::StdNumber &r)
Definition: BigMatrix.h:605
The Array_<T> container class is a plug-compatible replacement for the C++ standard template library ...
Definition: Array.h:53
void writeUnformatted(std::ostream &o, const RowVectorView_< E > &v)
Raw serialization of RowVectorView_<E>; same as VectorView_<E>.
Definition: BigMatrix.h:1212
Define the SimTK::Matrix_ class that is part of Simbody&#39;s BigMatrix toolset.
void colScale(const VectorBase< EE > &c, typename EltResult< EE >::Mul &out) const
(Advanced) This class is identical to Matrix_ except that it has shallow (reference) copy and assignm...
Definition: BigMatrix.h:167
Matrix_< double > dMatrix
Abbreviation for Matrix_<double>.
Definition: BigMatrix.h:1548
bool readUnformatted(std::istream &in, RowVector_< E > &v)
Read variable-size RowVector from unformatted (whitespace-separated) input stream.
Definition: BigMatrix.h:1281
K::TInvert TInvert
Definition: CompositeNumericalTypes.h:157
MatrixView_< EHerm > updTranspose()
Definition: BigMatrix.h:230
This is a user-includable header which includes everything needed to make use of SimMatrix Scalar cod...
Define the SimTK::MatrixView_ class that is part of Simbody&#39;s BigMatrix toolset.
VectorView_< float > fVectorView
Abbreviation for VectorView_<float>.
Definition: BigMatrix.h:1519
Matrix_< dComplex > dComplexMatrix
Abbreviation for Matrix_<std::complex<double>>.
Definition: BigMatrix.h:1552
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
VectorBase & resizeKeep(int m)
Definition: VectorBase.h:452
void elementwiseMultiplyFromLeft(const MatrixBase< EE > &, typename MatrixBase< EE >::template EltResult< E >::Mul &) const
Definition: BigMatrix.h:484
int ncol() const
Return the number of columns n in the logical shape of this matrix.
Definition: MatrixBase.h:138
RowVector_< float > fRowVector
Abbreviation for RowVector_<float>.
Definition: BigMatrix.h:1528
MatrixView_< Complex > ComplexMatrixView
Non-owner matrix sharing Complex (std::complex<Real>) elements.
Definition: BigMatrix.h:1505
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:238
Matrix_< Real > Matrix
Variable-size 2D matrix of Real elements; abbreviation for Matrix_<Real>.
Definition: BigMatrix.h:1478
MatrixHelperRep< S > * stealRep()
Definition: MatrixHelper.h:336
void writeUnformatted(std::ostream &o, const Matrix_< E > &v)
Raw serialization of Vector_<E>; same as VectorBase<E>.
Definition: BigMatrix.h:1241
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 ...
MatrixBase & elementwiseInvertInPlace()
Set M(i,j) = M(i,j)^-1.
Definition: BigMatrix.h:361
MatrixBase & rowScaleInPlace(const VectorBase< EE > &)
M = diag(r) * M; r must have nrow() elements.
RowVector_< double > dRowVector
Abbreviation for RowVector_<double>.
Definition: BigMatrix.h:1530
bool readUnformatted(std::istream &in, Matrix_< E > &v)
NOT IMPLEMENTED: read variable-size Matrix recognizing newlines as end of row; use fillUnformatted() ...
Definition: BigMatrix.h:1313
RowVector_< dComplex > dComplexRowVector
Abbreviation for RowVector_<std::complex<double>>.
Definition: BigMatrix.h:1534
This is the matrix class intended to appear in user code for large, variable size matrices...
Definition: BigMatrix.h:168
RowVectorView_< double > dRowVectorView
Abbreviation for RowVectorView_<double>.
Definition: BigMatrix.h:1539
MatrixBase & elementwiseAddScalarInPlace(const S &s)
Set M(i,j)+=s for every element of M and some value s.
K::StdNumber StdNumber
Definition: CompositeNumericalTypes.h:163
int nrow() const
Definition: VectorBase.h:401
Specialized information about Composite Numerical Types which allows us to define appropriate templat...
Definition: CompositeNumericalTypes.h:136
bool readUnformatted(std::istream &in, RowVectorView_< E > &v)
Read fixed-size RowVectorView from input stream.
Definition: BigMatrix.h:1273
This is a fixed-length row vector designed for no-overhead inline computation.
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:619
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:1438
K T
Definition: CompositeNumericalTypes.h:138
RowVectorView_< fComplex > fComplexRowVectorView
Abbreviation for RowVectorView_<std::complex<float>>.
Definition: BigMatrix.h:1541
MatrixView_< EHerm > transpose() const
Definition: BigMatrix.h:222
RowVector_< Complex > ComplexRowVector
Variable-size row vector of Complex (std::complex<Real>) elements.
Definition: BigMatrix.h:1493
void clear()
Definition: VectorBase.h:455
bool readUnformatted(std::istream &in, MatrixView_< E > &v)
Read fixed-size MatrixView in row order from unformatted (whitespace- separated) input stream...
Definition: BigMatrix.h:1291
Vector_< Complex > ComplexVector
Variable-size column vector of Complex (std::complex<Real>) elements.
Definition: BigMatrix.h:1489
MatrixView_< dComplex > dComplexMatrixView
Abbreviation for MatrixView_<std::complex<double>>.
Definition: BigMatrix.h:1561
void elementwiseAddScalar(const S &s, typename EltResult< S >::Add &) const
RowVector_< fComplex > fComplexRowVector
Abbreviation for RowVector_<std::complex<float>>.
Definition: BigMatrix.h:1532
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:1206
Define the SimTK::RowVector_ class that is part of Simbody&#39;s BigMatrix toolset.
int size() const
Definition: RowVectorBase.h:237
This is a dataless rehash of the MatrixBase class to specialize it for Vectors.
Definition: BigMatrix.h:164
Vector_< Real > Vector
Variable-size column vector of Real elements; abbreviation for Vector_<Real>.
Definition: BigMatrix.h:1473
#define SimTK_INDEXCHECK(ix, ub, where)
Definition: ExceptionMacros.h:145
Define the SimTK::MatrixBase class that is part of Simbody&#39;s BigMatrix toolset.
Represents a variable size row vector; much less common than the column vector type Vector_...
Definition: BigMatrix.h:174
int size() const
Definition: VectorBase.h:396
Define the SimTK::Vector_ class that is part of Simbody&#39;s BigMatrix toolset.
static std::istream & readVectorFromStreamHelper(std::istream &in, bool isFixedSize, Vector_< T > &out)
Definition: BigMatrix.h:978
Define the SimTK::RowVectorView_ class that is part of Simbody&#39;s BigMatrix toolset.
A MatrixCommitment provides a set of acceptable matrix characteristics.
Definition: MatrixCharacteristics.h:832
VectorView_< ELT > & updAsVectorView()
Definition: MatrixBase.h:799
void elementwiseDivide(const MatrixBase< EE > &, typename EltResult< EE >::Dvd &) const
RowVectorView_< Real > RowVectorView
Non-owner row vector sharing Real elements.
Definition: BigMatrix.h:1500
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 &#39;this&#39; but will have element types...
Matrix_< typename CNT< E1 >::template Result< E2 >::Add > operator+(const MatrixBase< E1 > &l, const MatrixBase< E2 > &r)
Definition: BigMatrix.h:568
Matrix_< Complex > ComplexMatrix
Variable-size 2D matrix of Complex (std::complex<Real>) elements.
Definition: BigMatrix.h:1491
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:1400
MatrixView_< ELT > block(int i, int j, int m, int n) const
Definition: BigMatrix.h:200
VectorBase & resize(int m)
Definition: VectorBase.h:451
Vector_< double > dVector
Abbreviation for Vector_<double>.
Definition: BigMatrix.h:1512
MatrixBase & elementwiseDivideInPlace(const MatrixBase< EE > &)
M(i,j) /= R(i,j); R must have same dimensions as this.
This class represents a small matrix whose size is known at compile time, containing elements of any ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:620
This is the common base class for Simbody&#39;s Vector_ and Matrix_ classes for handling large...
Definition: BigMatrix.h:163
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:1305
void elementwiseMultiply(const MatrixBase< EE > &, typename EltResult< EE >::Mul &) const
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:1225
int ncol() const
Definition: RowVectorBase.h:243
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.
RowVector_< Real > RowVector
Variable-size row vector of Real elements; abbreviation for RowVector_<Real>.
Definition: BigMatrix.h:1483
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:1430
MatrixBase & resize(int m, int n)
Change the size of this matrix.
Definition: MatrixBase.h:773
RowVectorView_< dComplex > dComplexRowVectorView
Abbreviation for RowVectorView_<std::complex<double>>.
Definition: BigMatrix.h:1543
(Advanced) This class is identical to Vector_ except that it has shallow (reference) copy and assignm...
Definition: BigMatrix.h:170
MatrixBase & elementwiseSubtractFromScalarInPlace(const S &s)
Set M(i,j) = s - M(i,j) for every element of M and some value s.
K::THerm THerm
Definition: CompositeNumericalTypes.h:144
MatrixView_< ELT > & updAsMatrixView()
Definition: MatrixBase.h:793
This is the default commitment for a column vector.
Definition: MatrixCharacteristics.h:981
int nrow() const
Return the number of rows m in the logical shape of this matrix.
Definition: MatrixBase.h:136
void writeUnformatted(std::ostream &o, const T &v)
The default implementation of writeUnformatted<T> converts the object to a String using the templatiz...
Definition: Serialize.h:76
RowVectorView_< float > fRowVectorView
Abbreviation for RowVectorView_<float>.
Definition: BigMatrix.h:1537
RowVectorView_< ELT > row(int i) const
Definition: BigMatrix.h:270