27#include "tnt_subscript.h"
28#include "tnt_vector.h"
71 void initialize(Subscript M, Subscript N)
83 assert(rowm1_ != NULL);
87 for (Subscript i=0; i<M; i++)
100 Subscript N = m_ * n_;
103#ifdef TNT_UNROLL_LOOPS
104 Subscript Nmod4 = N & 3;
105 Subscript N4 = N - Nmod4;
107 for (i=0; i<N4; i+=4)
115 for (i=N4; i< N; i++)
124 void set(
const T& val)
126 Subscript N = m_ * n_;
129#ifdef TNT_UNROLL_LOOPS
130 Subscript Nmod4 = N & 3;
131 Subscript N4 = N - Nmod4;
133 for (i=0; i<N4; i+=4)
141 for (i=N4; i< N; i++)
156 if (v_ == NULL) return ;
159 if (v_ != NULL)
delete [] (v_);
160 if (row_ != NULL)
delete [] (row_);
164 if (rowm1_ != NULL )
delete [] (rowm1_);
169 typedef Subscript size_type;
170 typedef T value_type;
171 typedef T element_type;
174 typedef T& reference;
175 typedef const T* const_iterator;
176 typedef const T& const_reference;
178 Subscript lbound()
const {
return 1;}
183 operator T**(){
return row_; }
184 operator const T**()
const {
return row_; }
190 Subscript
size()
const {
return mn_; }
194 Matrix() : m_(0), n_(0), mn_(0), v_(0), row_(0), vm1_(0), rowm1_(0) {};
196 Matrix(
const Matrix<T> &A)
198 initialize(A.m_, A.n_);
210 Matrix(Subscript M, Subscript N,
const T& value = T(0))
224 Matrix(Subscript M, Subscript N,
const T* v)
238 Matrix(Subscript M, Subscript N,
const char *s)
242 std::istringstream ins(s);
284 if (num_rows() == M && num_cols() == N)
308 if (m_ == B.m_ && n_ == B.n_)
314 initialize(B.m_, B.n_);
328 Subscript dim(Subscript d)
const
330#ifdef TNT_BOUNDS_CHECK
334 return (d==1) ? m_ : ((d==2) ? n_ : 0);
337 Subscript num_rows()
const {
return m_; }
338 Subscript num_cols()
const {
return n_; }
343 inline T* operator[](Subscript i)
345#ifdef TNT_BOUNDS_CHECK
352 inline const T* operator[](Subscript i)
const
354#ifdef TNT_BOUNDS_CHECK
361 inline reference operator()(Subscript i)
363#ifdef TNT_BOUNDS_CHECK
370 inline const_reference operator()(Subscript i)
const
372#ifdef TNT_BOUNDS_CHECK
381 inline reference operator()(Subscript i, Subscript j)
383#ifdef TNT_BOUNDS_CHECK
394 inline const_reference operator() (Subscript i, Subscript j)
const
396#ifdef TNT_BOUNDS_CHECK
407 Vector<T> diag()
const
409 Subscript N = n_ < m_ ? n_ : m_;
412 for (
int i=0; i<N; i++)
418 Matrix<T> upper_triangular()
const;
419 Matrix<T> lower_triangular()
const;
430std::ostream& operator<<(std::ostream &s,
const Matrix<T> &A)
432 Subscript M=A.num_rows();
433 Subscript N=A.num_cols();
435 s << M <<
" " << N <<
"\n";
436 for (Subscript i=0; i<M; i++)
438 for (Subscript j=0; j<N; j++)
451std::istream& operator>>(std::istream &s, Matrix<T> &A)
458 if ( !(M == A.num_rows() && N == A.num_cols() ))
464 for (Subscript i=0; i<M; i++)
465 for (Subscript j=0; j<N; j++)
495#ifdef TNT_BOUNDS_CHECK
496 assert(A.num_cols() == B.num_rows());
499 Subscript M = A.num_rows();
500 Subscript N = A.num_cols();
501 Subscript K = B.num_cols();
503#ifdef TNT_BOUNDS_CHECK
504 assert(C.num_rows() == M);
505 assert(C.num_cols() == K);
513 for (Subscript i=0; i<M; i++)
514 for (Subscript k=0; k<K; k++)
519 for (Subscript j=0; j<N; j++)
521 sum += *row_i * *col_k;
544#ifdef TNT_BOUNDS_CHECK
545 assert(A.num_cols() == B.num_rows());
548 Subscript M = A.num_rows();
549 Subscript N = A.num_cols();
550 Subscript K = B.num_cols();
598#ifdef TNT_BOUNDS_CHECK
599 assert(A.num_cols() == b.dim());
602 Subscript M = A.num_rows();
603 Subscript N = A.num_cols();
608 for (Subscript i=0; i<M; i++)
611 for (Subscript j=0; j<N; j++)
612 sum = sum + A[i][j] * b[j];
650 Subscript M = A.num_rows();
651 Subscript N = A.num_cols();
654 for (
int i=0; i<M; i++)
655 for (
int j=0; j<N; j++)
656 R[i][j] = s * A[i][j];
697 Subscript M = A.num_rows();
698 Subscript N = A.num_cols();
700 for (
int i=0; i<M; i++)
701 for (
int j=0; j<N; j++)
706inline Matrix<T>
mult_eq(Matrix<T> &A,
const T&a)
727#ifdef TNT_BOUNDS_CHECK
728 assert(A.num_rows() == B.num_rows());
731 Subscript M = A.num_cols();
732 Subscript N = A.num_rows();
733 Subscript K = B.num_cols();
738 for (Subscript i=0; i<N; i++)
739 for (Subscript k=0; k<K; k++)
742 for (Subscript j=0; j<M; j++)
743 sum = sum + A[j][i] * B[j][k];
766#ifdef TNT_BOUNDS_CHECK
767 assert(A.num_rows() == b.dim());
770 Subscript M = A.num_cols();
771 Subscript N = A.num_rows();
775 for (Subscript i=0; i<M; i++)
778 for (Subscript j=0; j<N; j++)
779 sum = sum + A[j][i] * b[j];
799 Subscript M = A.num_rows();
800 Subscript N = A.num_cols();
802 assert(M==B.num_rows());
803 assert(N==B.num_cols());
810 tmp[i][j] = A[i][j] + B[i][j];
846 Subscript M = A.num_rows();
847 Subscript N = A.num_cols();
849 assert(M==B.num_rows());
850 assert(N==B.num_cols());
857 tmp[i][j] = A[i][j] + B[i][j];
891 Subscript M = A.num_rows();
892 Subscript N = A.num_cols();
894 assert(M==B.num_rows());
895 assert(N==B.num_cols());
902 tmp[i][j] = A[i][j] - B[i][j];
940 Subscript M = A.num_rows();
941 Subscript N = A.num_cols();
943 assert(M==B.num_rows());
944 assert(N==B.num_cols());
951 tmp[i][j] = A[i][j] * B[i][j];
969 Subscript M = A.num_rows();
970 Subscript N = A.num_cols();
972 assert(M==B.num_rows());
973 assert(N==B.num_cols());
998 Subscript M = A.num_rows();
999 Subscript N = A.num_cols();
1002 for (
int i=1; i<=M; i++)
1003 for (
int j=1; j<=N; j++)
1004 sum += A(i,j) * A(i,j);
1021 Subscript M = A.num_rows();
1022 Subscript N = A.num_cols();
1054 int n = A.num_rows() < A.num_cols() ? A.num_rows() : A.num_cols();
1056 for (
int k=n; k >= 1; k--)
1059 for (
int i=1; i< k; i++ )
1060 x(i) -= x(k) * A(i,k);
1080 int n = A.num_rows() < A.num_cols() ? A.num_rows() : A.num_cols();
1082 for (
int k=1; k <= n; k++)
1085 for (
int i=k+1; i<= n; i++ )
1086 x(i) -= x(k) * A(i,k);
Definition tnt_matrix.h:57
Subscript size() const
Definition tnt_matrix.h:190
Matrix(Subscript M, Subscript N, const char *s)
Definition tnt_matrix.h:238
Matrix< T > & operator=(const Matrix< T > &B)
Definition tnt_matrix.h:303
Matrix(Subscript M, Subscript N, const T &value=T(0))
Definition tnt_matrix.h:210
Matrix< T > & newsize(Subscript M, Subscript N)
Definition tnt_matrix.h:282
Matrix(Subscript M, Subscript N, const T *v)
Definition tnt_matrix.h:224
Definition tnt_vector.h:49
Definition tnt_array1d.h:36
Matrix< T > & mult_element_eq(Matrix< T > &A, const Matrix< T > &B)
Definition tnt_matrix.h:967
Matrix< T > minus(const Matrix< T > &A, const Matrix< T > &B)
Definition tnt_matrix.h:889
Matrix< T > transpose(const Matrix< T > &A)
Definition tnt_matrix.h:1019
Matrix< T > add(const Matrix< T > &A, const Matrix< T > &B)
Definition tnt_matrix.h:797
double norm(const Matrix< T > &A)
Definition tnt_matrix.h:996
Matrix< T > mult_element(const Matrix< T > &A, const Matrix< T > &B)
Definition tnt_matrix.h:938
Vector< T > lower_triangular_solve(const Matrix< T > &A, const Vector< T > &b)
Definition tnt_matrix.h:1078
Matrix< T > & add_eq(Matrix< T > &A, const Matrix< T > &B)
Definition tnt_matrix.h:844
Matrix< T > & mult(Matrix< T > &C, const Matrix< T > &A, const Matrix< T > &B)
Definition tnt_matrix.h:492
Vector< T > upper_triangular_solve(const Matrix< T > &A, const Vector< T > &b)
Definition tnt_matrix.h:1052
Matrix< T > transpose_mult(const Matrix< T > &A, const Matrix< T > &B)
Definition tnt_matrix.h:724
Matrix< T > mult_eq(const T &s, Matrix< T > &A)
Definition tnt_matrix.h:695