40 #include "subscript.h"
51 #ifdef TNT_USE_REGIONS
52 # include "region2d.h"
55 #include <Base/Math/Utils.hh>
56 #include <Base/Debug/Error.hh>
58 #ifdef BUILD_ALLOW_NON_INTEGRAL_MATRIX_ELEMENTS
59 #include <Base/Math/Polynom.hh>
105 BIASASSERT(
v_ != NULL);
106 BIASASSERT(
row_ != NULL);
107 BIASASSERT(
rowm1_ != NULL);
125 #ifdef BUILD_ALLOW_NON_INTEGRAL_MATRIX_ELEMENTS
128 #ifdef TNT_UNROLL_LOOPS
132 for (i=0; i<N4; i+=4)
140 for (i=N4; i< N; i++)
148 memcpy((
void*)
v_, (
void*) v,
sizeof(T)*N);
157 #ifdef TNT_UNROLL_LOOPS
161 for (i=0; i<N4; i+=4)
169 for (i=N4; i< N; i++)
189 delete [] (
v_);
v_ = NULL;
251 std::istringstream ins(s);
312 #ifdef TNT_BOUNDS_CHECK
316 return (d==1) ?
m_ : ((d==2) ?
n_ : 0);
327 #ifdef TNT_BOUNDS_CHECK
336 #ifdef TNT_BOUNDS_CHECK
345 #ifdef TNT_BOUNDS_CHECK
347 BIASASSERT(i <=
mn_) ;
354 #ifdef TNT_BOUNDS_CHECK
356 BIASASSERT(i <=
mn_) ;
365 #ifdef TNT_BOUNDS_CHECK
367 BIASASSERT(i <=
m_) ;
378 #ifdef TNT_BOUNDS_CHECK
380 BIASASSERT(i <=
m_) ;
389 #ifdef TNT_USE_REGIONS
396 return Region(*
this, I,J);
400 typedef const_Region2D< Matrix<T> > const_Region;
401 const_Region
operator()(
const Index1D &I,
const Index1D &J)
const
403 return const_Region(*
this, I,J);
411 const std::string &name =
"",
413 const bool alignLeft =
true)
const;
418 Print(std::ostream &s,
421 bool scientific=
true)
const;
426 Print(std::ostream &s,
427 const bool intCastOutput=
false,
428 const bool binaryOutput=
false,
429 const bool forceFullPrecision=
false )
const;
435 Read(std::istream &s,
436 const bool intCastInput=
false,
437 const bool binaryInput=
false );
451 PrintPretty(std::ostream &s,
const std::string &name,
452 const int width,
const bool alignLeft)
const
454 const int m = this->num_rows();
455 const int n = this->num_cols();
456 const int w = width + 8;
457 std::ios_base::fmtflags oldFlags = s.flags();
458 std::string leftspace =
"";
460 s << std::endl << name <<
" =";
461 leftspace = name +
" ";
462 for (
unsigned int i = 0; i < name.size(); i++)
463 leftspace.at(i) =
' ';
468 for (
int i = 0; i < m; i++) {
472 for (
int j = 0; j < n; j++) {
474 sprintf_s(chars,
"% .8g", (
double)(*
this)[i][j]);
476 sprintf(chars,
"% .8g", (
double)(*
this)[i][j]);
478 std::string str(chars);
479 if (alignLeft) s << str;
480 for (
int k = 0; k < w - (int)str.size(); k++)
482 if (!alignLeft) s << str;
484 s <<
" ]" << std::endl;
499 bool scientific)
const
501 int M=this->num_rows();
502 int N=this->num_cols();
504 std::ios_base::fmtflags oldFlags = s.flags();
507 s<<std::setprecision(precision);
511 if(scientific) s<<std::scientific;
514 for (
int i=0; i<M; i++)
516 for (
int j=0; j<N; j++)
518 s<<std::setw( width );
519 s<<std::setprecision( precision );
536 const bool intCastOutput,
537 const bool binaryOutput,
538 const bool forceFullPrecision)
const
540 int M=this->num_rows();
541 int N=this->num_cols();
543 std::ios_base::fmtflags oldFlags = s.flags();
545 s<<std::setprecision(std::numeric_limits<int>::digits10 +1 );
549 unsigned int dig(0), w(0);
553 dig = std::numeric_limits<T>::digits10;
556 dig = BIAS::DecimalsToStore<T>();
561 BIASERR(
"not implemented");
576 for (
int i=0; i<M; i++)
578 for (
int j=0; j<N; j++)
581 s<<std::setprecision( dig );
582 #ifndef BUILD_ALLOW_NON_INTEGRAL_MATRIX_ELEMENTS
584 s<<(int)((*
this)[i][j]);
606 const bool intCastInput,
607 const bool binaryInput)
610 BIASERR(
"not implemented");
616 BIASERR(
"could not read from stream. wrong format?");
618 s.setstate(std::ios::badbit | std::ios::failbit);
621 if ( !(M==this->num_rows() && N==this->num_cols() ))
626 #ifndef BUILD_ALLOW_NON_INTEGRAL_MATRIX_ELEMENTS
631 for (
int i=0; i<M; i++){
632 for (
int j=0; j<N; j++){
635 BIASASSERT((
long)tmp>=(
long)std::numeric_limits<T>::min() );
636 BIASASSERT((
long)tmp<=(
long)std::numeric_limits<T>::max() );
638 this->row_[i][j] = (T)tmp;
647 for (
int i=0; i<M; i++){
648 for (
int j=0; j<N; j++)
663 std::ostream& operator<<(std::ostream &s, const Matrix<char> &A)
665 return A.Print(s,
true);
670 std::ostream& operator<<(std::ostream &s, const Matrix<unsigned char> &A)
672 return A.Print(s,
true );
675 #ifdef BUILD_ALLOW_NON_INTEGRAL_MATRIX_ELEMENTS
677 std::ostream& operator<<(std::ostream &s, const Matrix<BIAS::Polynom> &A)
682 s<<M<<
" "<<N <<
"\n";
683 for (
int i=0; i<M; i++) {
684 for (
int j=0; j<N; j++) {
685 s << i <<
"," << j <<
" -> " << A[i][j] <<
"\n";
695 std::ostream& operator<<(std::ostream &s, const Matrix<T> &A)
705 return A.
Read(s,
true);
713 return A.
Read(s,
true);
744 tmp[i][j] = A[i][j] + B[i][j];
766 tmp[i][j] = A[i][j] - B[i][j];
786 tmp[i][j] = A[i][j] * B[i][j];
815 #ifdef TNT_BOUNDS_CHECK
831 sum = sum + A[i][j] * B[j][k];
874 sum += *row_i * *col_k;
889 #ifdef TNT_BOUNDS_CHECK
902 const T* rowi = A[i];
904 sum = sum + rowi[j] * x[j];
Matrix< T > & operator=(const Matrix< T > &A)
Matrix< T > transpose(const Matrix< T > &A)
std::ostream & PrintPretty(std::ostream &s, const std::string &name="", const int width=8, const bool alignLeft=true) const
Subscript num_cols() const
std::istream & operator>>(std::istream &s, Matrix< T > &A)
Matrix< T > operator*(const Matrix< T > &A, const Matrix< T > &B)
bool IsConsoleStream(const std::ostream &os)
Helper returning true on console ostreams like cerr, cout Useful to distinguish between console (cout...
Matrix< T > & newsize(Subscript M, Subscript N)
Matrix< T > operator-(const Matrix< T > &A, const Matrix< T > &B)
Matrix< T > operator+(const Matrix< T > &A, const Matrix< T > &B)
Vector< T > operator*(const Matrix< T > &A, const Vector< T > &x)
TNT_SUBSCRIPT_TYPE Subscript
Matrix< T > mult_element(const Matrix< T > &A, const Matrix< T > &B)
reference operator()(Subscript i, Subscript j)
void initialize(Subscript M, Subscript N)
std::istream & Read(std::istream &s, const bool intCastInput=false, const bool binaryInput=false)
JW.
std::ostream & Print(std::ostream &s, const int width, const int precision, bool scientific=true) const
Matrix< T > matmult(const Matrix< T > &A, const Matrix< T > &B)
Matrix(const Matrix< T > &A)
std::istream & operator>>(std::istream &s, Matrix< char > &A)
reference operator()(Subscript i)
Matrix< T > & operator=(const T &scalar)
Matrix(Subscript M, Subscript N, const std::string &s)
replacement for above JW
const T * operator[](Subscript i) const
Subscript num_rows() const
T * operator[](Subscript i)
Matrix(Subscript M, Subscript N, const T *v)
const_reference operator()(Subscript i) const
Subscript dim(Subscript d) const
const T & const_reference
std::istream & operator>>(std::istream &s, Matrix< unsigned char > &A)