44 #ifndef GEN_MATRIX_CLASS_H
45 #define GEN_MATRIX_CLASS_H
47 #include "DenseLinAlgPack_DVectorClass.hpp"
48 #include "DenseLinAlgPack_DMatrixAssign.hpp"
58 namespace DenseLinAlgPack {
92 typedef DenseLinAlgPack::value_type value_type;
93 typedef DenseLinAlgPack::size_type size_type;
94 typedef ptrdiff_t difference_type;
95 typedef value_type& reference;
96 typedef const value_type& const_reference;
187 size_type
rows()
const;
189 size_type
cols()
const;
209 reference
operator()(size_type i, size_type j);
211 const_reference
operator()(size_type i, size_type j)
const;
284 , size_type j2)
const;
343 value_type*
col_ptr(size_type j);
345 const value_type*
col_ptr(size_type j)
const;
356 void validate_row_subscript(size_type i)
const;
358 void validate_col_subscript(size_type j)
const;
360 void validate_setup(size_type size)
const;
389 typedef DenseLinAlgPack::value_type value_type;
390 typedef DenseLinAlgPack::size_type size_type;
391 typedef ptrdiff_t difference_type;
392 typedef value_type& reference;
393 typedef const value_type& const_reference;
394 typedef std::valarray<value_type> valarray;
415 explicit DMatrix(value_type val, size_type rows, size_type cols);
423 explicit DMatrix(
const value_type* p, size_type rows, size_type cols);
439 void resize(size_type rows, size_type cols, value_type val = value_type());
445 size_type
rows()
const;
448 size_type
cols()
const;
468 reference
operator()(size_type i, size_type j);
471 const_reference
operator()(size_type i, size_type j)
const;
553 , size_type j2)
const;
615 value_type*
col_ptr(size_type j);
617 const value_type*
col_ptr(size_type j)
const;
622 std::valarray<value_type> v_;
626 void validate_row_subscript(size_type i)
const;
628 void validate_col_subscript(size_type j)
const;
664 #ifdef LINALGPACK_CHECK_SLICE_SETUP
666 throw std::length_error(
"Matrix must be square");
684 if(gms_lhs.rows() == rows && gms_lhs.cols() ==
cols)
return;
686 throw std::length_error(
"assert_gms_lhs(...): lhs DMatrixSlice dim does not match rhs dim");
748 gm_rhs->resize(rows,cols);
766 #ifndef LINALGPACK_CHECK_RANGE
768 void DMatrixSlice::validate_row_subscript(
size_type i)
const
772 #ifndef LINALGPACK_CHECK_RANGE
774 void DMatrixSlice::validate_col_subscript(
size_type j)
const
778 #ifndef LINALGPACK_CHECK_SLICE_SETUP
780 void DMatrixSlice::validate_setup(
size_type size)
const
787 DMatrixSlice::DMatrixSlice()
788 : ptr_(0), max_rows_(0), rows_(0), cols_(0)
792 DMatrixSlice::DMatrixSlice( value_type* ptr,
size_type size
794 : ptr_(ptr), max_rows_(max_rows), rows_(rows), cols_(cols)
796 validate_setup(size);
800 DMatrixSlice::DMatrixSlice( DMatrixSlice& gms,
const Range1D& I
802 : ptr_( gms.col_ptr(1) + (I.lbound() - 1) + (J.lbound() - 1) * gms.max_rows() )
803 , max_rows_(gms.max_rows())
807 gms.validate_row_subscript(I.ubound());
808 gms.validate_col_subscript(J.ubound());
814 max_rows_ = gms.max_rows_;
838 validate_row_subscript(i);
839 validate_col_subscript(j);
840 return ptr_[(i-1) + (j-1) * max_rows_];
846 validate_row_subscript(i);
847 validate_col_subscript(j);
848 return ptr_[(i-1) + (j-1) * max_rows_];
855 validate_row_subscript(i);
861 validate_row_subscript(i);
867 validate_col_subscript(j);
873 validate_col_subscript(j);
889 return DMatrixSlice(*
this, RangePack::full_range(I, 1,
rows()), RangePack::full_range(J,1,
cols()));
895 , RangePack::full_range(I, 1,
rows()), RangePack::full_range(J,1,
cols()) );
907 , size_type j2)
const
941 {
return max_rows_; }
946 validate_col_subscript(j);
953 validate_col_subscript(j);
962 #ifndef LINALGPACK_CHECK_RANGE
964 void DMatrix::validate_row_subscript(size_type i)
const
968 #ifndef LINALGPACK_CHECK_RANGE
970 void DMatrix::validate_col_subscript(
size_type j)
const
982 : v_(rows*cols), rows_(rows)
987 : v_(val,rows*cols), rows_(rows)
992 : v_(rows*cols), rows_(rows)
997 std::copy( p, p + rows*cols, &v_[0] );
1002 : v_(gms.rows() * gms.cols()), rows_(gms.rows())
1012 v_.resize(rows*cols,val);
1032 return rows_ > 0 ? v_.size() / rows_ : 0;
1040 validate_row_subscript(i); validate_col_subscript(j);
1041 return v_[(i-1) + (j-1) * rows_];
1047 validate_row_subscript(i); validate_col_subscript(j);
1048 return (
const_cast<std::valarray<value_type>&
>(v_))[(i-1) + (j-1) * rows_];
1056 validate_row_subscript(i);
1063 validate_row_subscript(i);
1070 validate_col_subscript(j);
1077 validate_col_subscript(j);
1096 Range1D Ix = RangePack::full_range(I,1,
rows()), Jx = RangePack::full_range(J,1,
cols());
1104 Range1D Ix = RangePack::full_range(I,1,
rows()), Jx = RangePack::full_range(J,1,
cols());
1119 , size_type j2)
const
1156 assign(
this, alpha);
1184 validate_col_subscript(j);
1196 validate_col_subscript(j);
1197 return &
const_cast<valarray&
>(v_)[ (j-1) *
max_rows() ];
1206 #endif // GEN_MATRIX_CLASS_H
const DMatrixSlice * operator&() const
value_type * col_ptr(size_type j)
size_type cols() const
Return the number of columns.
DMatrixSlice * operator&()
Allow the address to be taken of an rvalue of this object.
size_type rows(size_type rows, size_type cols, BLAS_Cpp::Transp _trans)
DMatrixSlice & operator=(value_type alpha)
DMatrixSlice operator()()
Return a DMatrixSlice that represents this entire matrix.
size_type max_rows() const
Return the number of rows in the full matrix. Equivalent to BLAS LDA argument.
DVectorSlice row(size_type i)
Return DVectorSlice object representing the ith row (1-based; 1,2,..,#this->rows()#) ...
size_type rows() const
Return the number of rows.
DVectorSlice col(size_type j)
Return DVectorSlice object representing the jth column (1-based; 1,2,..,#this->cols()#) ...
value_type * col_ptr(size_type j)
size_type max_rows() const
Return the number of rows in the full matrix. Equivalent to BLAS LDA argument.
void resize(size_type rows, size_type cols, value_type val=value_type())
Resize matrix to a (rows x cols) matrix and initializes any added elements by val.
DMatrix & operator=(value_type rhs)
void free()
frees memory and leaves a (0 x 0) matrix
DMatrix()
Construct a matrix with rows = cols = 0.
size_type rows() const
Return the number of rows.
DMatrixSlice & operator()()
Return reference of this. Included for iniformity with DMatrix.
DVectorSlice col(size_type j)
Return DVectorSlice object representing the jth column (1-based; 1,2,..,#this->cols()#, or throw std::out_of_range)
size_type cols(size_type rows, size_type cols, BLAS_Cpp::Transp _trans)
void bind(DMatrixSlice gms)
size_type cols() const
Return the number of columns.
DVectorSlice row(size_type i)
Return DVectorSlice object representing the ith row (1-based; 1,2,..,#this->rows()#, or throw std::out_of_range)