1 #ifndef SimTK_SIMMATRIX_BIGMATRIX_H_
2 #define SimTK_SIMMATRIX_BIGMATRIX_H_
163 template <
class ELT>
class MatrixBase;
164 template <
class ELT>
class VectorBase;
165 template <
class ELT>
class RowVectorBase;
167 template <
class ELT = Real>
class MatrixView_;
168 template <
class ELT = Real>
class Matrix_;
170 template <
class ELT = Real>
class VectorView_;
171 template <
class ELT = Real>
class Vector_;
173 template <
class ELT = Real>
class RowVectorView_;
174 template <
class ELT = Real>
class RowVector_;
176 template <
class ELT,
class VECTOR_CLASS>
class VectorIterator;
199 template <
class ELT>
inline MatrixView_<ELT>
256 helper,0,j,nrow(),1);
265 helper,0,j,nrow(),1);
274 helper,i,0,1,ncol());
283 helper,i,0,1,ncol());
291 assert(v.
nrow() == nrow());
292 for (
int i=0; i < nrow(); ++i)
297 template <
class ELT>
template <
class EE>
inline void
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];
310 assert(v.
nrow() == ncol());
311 for (
int j=0; j < ncol(); ++j)
316 template <
class ELT>
template <
class EE>
inline void
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];
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]);
336 template <
class ELT>
template <
class ER,
class EC>
inline void
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]);
353 for (
int j=0; j<ncol(); ++j)
354 for (
int i=0; i<nrow(); ++i)
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);
371 template <
class ELT>
inline void
373 const int nr=nrow(), nc=ncol();
375 for (
int j=0; j<nc; ++j)
376 for (
int i=0; i<nr; ++i)
383 for (
int j=0; j<ncol(); ++j)
384 for (
int i=0; i<nrow(); ++i)
389 template <
class ELT>
template <
class S>
inline void
394 const int nr=nrow(), nc=ncol();
396 for (
int j=0; j<nc; ++j)
397 for (
int i=0; i<nr; ++i)
398 out(i,j) = (*this)(i,j) + s;
404 for (
int j=0; j<ncol(); ++j)
405 for (
int i=0; i<nrow(); ++i)
410 template <
class ELT>
template <
class S>
inline void
415 const int nr=nrow(), nc=ncol();
417 for (
int j=0; j<nc; ++j)
418 for (
int i=0; i<nr; ++i)
419 out(i,j) = (*this)(i,j) - s;
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);
434 template <
class ELT>
template <
class S>
inline void
439 const int nr=nrow(), nc=ncol();
441 for (
int j=0; j<nc; ++j)
442 for (
int i=0; i<nr; ++i)
443 out(i,j) = s - (*this)(i,j);
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);
457 template <
class ELT>
template <
class EE>
inline void
462 const int nr=nrow(), nc=ncol();
463 assert(r.
nrow()==nr && r.
ncol()==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);
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);
483 template <
class ELT>
template <
class EE>
inline void
488 const int nr=nrow(), nc=ncol();
489 assert(r.
nrow()==nr && r.
ncol()==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);
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);
507 template <
class ELT>
template <
class EE>
inline void
512 const int nr=nrow(), nc=ncol();
513 assert(r.
nrow()==nr && r.
ncol()==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);
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);
532 template <
class ELT>
template <
class EE>
inline void
537 const int nr=nrow(), nc=ncol();
538 assert(r.
nrow()==nr && r.
ncol()==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);
566 template <
class E1,
class E2>
582 template <
class E1,
class E2>
583 Matrix_<typename CNT<E1>::template Result<E2>::Sub>
604 template <
class E> Matrix_<E>
608 template <
class E> Matrix_<E>
612 template <
class E> Matrix_<E>
617 template <
class E> Matrix_<E>
621 template <
class E> Matrix_<E>
625 template <
class E> Matrix_<E>
635 template <
class E1,
class E2>
636 Vector_<typename CNT<E1>::template Result<E2>::Add>
648 template <
class E1,
class E2>
649 Vector_<typename CNT<E1>::template Result<E2>::Sub>
666 template <
class E> Vector_<E>
670 template <
class E> Vector_<E>
674 template <
class E> Vector_<E>
679 template <
class E> Vector_<E>
683 template <
class E> Vector_<E>
687 template <
class E> Vector_<E>
695 template <
class E1,
int M,
class E2,
int S>
696 Vector_<typename CNT<E1>::template Result< Vec<M,E2,S> >::Mul>
699 for (
int i=0; i < v.
nrow(); ++i)
705 template <
class E1,
int M,
class E2,
int S>
706 Vector_<typename Vec<M,E2,S>::template Result<E1>::Mul>
709 for (
int i=0; i < v.
nrow(); ++i)
715 template <
class E1,
int N,
class E2,
int S>
716 Vector_<typename CNT<E1>::template Result< Row<N,E2,S> >::Mul>
719 for (
int i=0; i < v.
nrow(); ++i)
725 template <
class E1,
int N,
class E2,
int S>
726 Vector_<typename Row<N,E2,S>::template Result<E1>::Mul>
729 for (
int i=0; i < v.
nrow(); ++i)
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>
739 for (
int i=0; i < v.
nrow(); ++i)
745 template <
class E1,
int M,
int N,
class E2,
int S1,
int S2>
746 Vector_<typename Mat<M,N,E2,S1,S2>::template Result<E1>::Mul>
749 for (
int i=0; i < v.
nrow(); ++i)
755 template <
class E1,
int M,
class E2,
int S>
756 Vector_<typename CNT<E1>::template Result< SymMat<M,E2,S> >::Mul>
759 for (
int i=0; i < v.
nrow(); ++i)
765 template <
class E1,
int M,
class E2,
int S>
766 Vector_<typename SymMat<M,E2,S>::template Result<E1>::Mul>
769 for (
int i=0; i < v.
nrow(); ++i)
780 template <
class E1,
class E2>
781 RowVector_<typename CNT<E1>::template Result<E2>::Add>
793 template <
class E1,
class E2>
794 RowVector_<typename CNT<E1>::template Result<E2>::Sub>
811 template <
class E> RowVector_<E>
815 template <
class E> RowVector_<E>
819 template <
class E> RowVector_<E>
824 template <
class E> RowVector_<E>
828 template <
class E> RowVector_<E>
832 template <
class E> RowVector_<E>
841 template <
class E1,
int M,
class E2,
int S>
842 RowVector_<typename CNT<E1>::template Result< Vec<M,E2,S> >::Mul>
845 for (
int i=0; i < v.
ncol(); ++i)
851 template <
class E1,
int M,
class E2,
int S>
852 RowVector_<typename Vec<M,E2,S>::template Result<E1>::Mul>
855 for (
int i=0; i < v.
ncol(); ++i)
861 template <
class E1,
int N,
class E2,
int S>
862 RowVector_<typename CNT<E1>::template Result< Row<N,E2,S> >::Mul>
865 for (
int i=0; i < v.
ncol(); ++i)
871 template <
class E1,
int N,
class E2,
int S>
872 RowVector_<typename Row<N,E2,S>::template Result<E1>::Mul>
875 for (
int i=0; i < v.
ncol(); ++i)
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>
885 for (
int i=0; i < v.
ncol(); ++i)
891 template <
class E1,
int M,
int N,
class E2,
int S1,
int S2>
892 RowVector_<typename Mat<M,N,E2,S1,S2>::template Result<E1>::Mul>
895 for (
int i=0; i < v.
ncol(); ++i)
901 template <
class E1,
int M,
class E2,
int S>
902 RowVector_<typename CNT<E1>::template Result< SymMat<M,E2,S> >::Mul>
905 for (
int i=0; i < v.
ncol(); ++i)
911 template <
class E1,
int M,
class E2,
int S>
912 RowVector_<typename SymMat<M,E2,S>::template Result<E1>::Mul>
915 for (
int i=0; i < v.
ncol(); ++i)
931 template <
class E1,
class E2>
932 typename CNT<E1>::template Result<E2>::Mul
936 for (
int j=0; j < r.
ncol(); ++j)
941 template <
class E1,
class E2>
942 Vector_<typename CNT<E1>::template Result<E2>::Mul>
946 for (
int i=0; i< m.
nrow(); ++i)
951 template <
class E1,
class E2>
952 Matrix_<typename CNT<E1>::template Result<E2>::Mul>
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);
976 template <
class T>
static inline
982 if (!in.good()) {in.setstate(std::ios::failbit);
return in;}
991 const int numRequired = isFixedSize ? out.
size() : 0;
999 std::ws(in);
if (in.fail())
return in;
1001 if (isFixedSize && numRequired != 0)
1002 in.setstate(std::ios_base::failbit);
1010 typename std::iostream::int_type ch;
1011 const typename std::iostream::int_type EOFch =
1012 std::iostream::traits_type::eof();
1016 bool tildeFound =
false;
1017 ch = in.peek();
if (in.fail())
return in;
1018 assert(ch != EOFch);
1019 if ((
char)ch ==
'~') {
1023 if (in.good()) std::ws(in);
1025 if (!in.good()) {in.setstate(std::ios_base::failbit);
return in;}
1033 bool lookForCloser =
true;
1034 char openBracket, closeBracket;
1035 ch = in.peek();
if (in.fail())
return in;
1036 assert(ch != EOFch);
1038 openBracket = (char)ch;
1039 if (openBracket==
'(') {in.get(); closeBracket =
')';}
1040 else if (openBracket==
'[') {in.get(); closeBracket =
']';}
1041 else lookForCloser =
false;
1045 if (tildeFound && !lookForCloser)
1046 { in.setstate(std::ios_base::failbit);
return in;}
1052 if (in.good()) std::ws(in);
1058 assert(lookForCloser);
1059 in.setstate(std::ios::failbit);
1069 bool commaOK =
true, commaRequired =
false;
1070 bool terminatorSeen =
false;
1087 if (lookForCloser) {
1089 std::ws(in);
if (!in.good())
break;
1090 ch = in.peek(); assert(ch != EOFch);
1091 if (!in.good())
break;
1093 if (c == closeBracket) {
1095 terminatorSeen =
true;
1105 if (isFixedSize && (nextIndex == numRequired))
1109 if (commaOK && nextIndex != 0) {
1111 std::ws(in);
if (!in.good())
break;
1112 ch = in.peek(); assert(ch != EOFch);
1113 if (!in.good())
break;
1117 commaRequired =
true;
1120 { in.setstate(std::ios::failbit);
break; }
1121 else commaOK =
false;
1123 if (!in.good())
break;
1134 in >> out[nextIndex];
if (in.fail())
break;
1137 if (!in.good())
break;
1151 if (lookForCloser && !terminatorSeen)
1152 in.setstate(std::ios::failbit);
1154 if (isFixedSize && nextIndex != numRequired)
1155 in.setstate(std::ios::failbit);
1180 template <
class E>
inline void
1182 const int sz = v.
size();
1183 for (
int i=0; i < sz; ++i) {
1184 if (i != 0) o <<
" ";
1190 template <
class E>
inline void
1196 template <
class E>
inline void
1203 template <
class E>
inline void
1209 template <
class E>
inline void
1215 template <
class E>
inline void
1222 template <
class E>
inline void
1224 const int nr = v.
nrow();
1225 for (
int i=0; i < nr; ++i) {
1226 if (i != 0) o << std::endl;
1232 template <
class E>
inline void
1238 template <
class E>
inline void
1245 template <
class E>
inline bool
1247 for (
int i=0; i < v.
size(); ++i)
1254 template <
class E>
inline bool
1262 for (
int i=0; i<a.
size(); ++i)
1270 template <
class E>
inline bool
1273 return readUnformatted<E>(in, vt); }
1278 template <
class E>
inline bool
1281 return readUnformatted<E>(in, vt); }
1288 template <
class E>
inline bool
1290 for (
int row=0; row < v.
nrow(); ++row) {
1292 if (!readUnformatted<E>(in, oneRow))
return false;
1302 template <
class E>
inline bool
1310 template <
class E>
inline bool
1313 "SimTK::readUnformatted(istream, Matrix) is not implemented; try"
1314 " SimTK::fillUnformatted(istream, Matrix) instead.");
1324 template <
class T>
inline std::ostream&
1325 operator<<(std::ostream& o, const VectorBase<T>& v)
1329 for (
int i=1; i < v.size(); ++i) o <<
" " << v[i];
1340 template <
class T>
inline std::ostream&
1341 operator<<(std::ostream& o, const RowVectorBase<T>& v)
1345 for (
int i=1; i < v.size(); ++i) o <<
" " << v[i];
1357 template <
class T>
inline std::ostream&
1358 operator<<(std::ostream& o, const MatrixBase<T>& m) {
1359 for (
int i=0;i<m.nrow();++i)
1360 o << std::endl << m[i];
1361 if (m.nrow()) o << std::endl;
1397 template <
class T>
static inline
1398 std::istream& readVectorFromStream(std::istream& in,
Vector_<T>& out)
1399 {
return readVectorFromStreamHelper<T>(in,
false , out); }
1427 template <
class T>
static inline
1428 std::istream& fillVectorFromStream(std::istream& in,
Vector_<T>& out)
1429 {
return readVectorFromStreamHelper<T>(in,
true , out); }
1435 template <
class T>
static inline
1437 {
return readVectorFromStreamHelper<T>(in,
true , out); }
1449 template <
class T>
inline
1451 {
return readVectorFromStream<T>(in, out); }
1460 template <
class T>
inline
1462 {
return fillVectorViewFromStream<T>(in, out); }
1564 #endif //SimTK_SIMMATRIX_BIGMATRIX_H_