42 #ifndef CORE_DATATYPES_DENSEMATRIX_H
43 #define CORE_DATATYPES_DENSEMATRIX_H 1
46 #include <Core/Util/FancyAssert.h>
51 #if defined(HAVE_LAPACK)
158 virtual void print(std::string&)
const;
175 namespace LinearAlgebra
211 template <
typename T>
214 template <
typename T>
224 template <
typename T>
233 template <
typename T>
241 template <
typename T>
249 template <
typename T>
256 dataptr_[0] = scalar;
259 template <
typename T>
266 dataptr_[0] = vec.
x();
267 dataptr_[1] = vec.
y();
268 dataptr_[2] = vec.
z();
271 template <
typename T>
278 dataptr_[0] = pnt.
x();
279 dataptr_[1] = pnt.
y();
280 dataptr_[2] = pnt.
z();
283 template <
typename T>
290 dataptr_[0] = tens.
xx();
291 dataptr_[1] = tens.
xy();
292 dataptr_[2] = tens.
xz();
293 dataptr_[3] = tens.
yy();
294 dataptr_[4] = tens.
yz();
295 dataptr_[5] = tens.
zz();
298 template <
typename T>
301 data =
new T*[this->nrows_];
302 T* tmp =
new T[this->nrows_ * this->ncols_];
311 template <
typename T>
313 Matrix<T>(m.nrows_, m.ncols_)
317 data =
new T*[this->
nrows_];
324 for (
int j=0; j<this->
ncols_; j++)
331 template <
typename T>
337 std::fill(this->
begin(), this->
end(), value);
340 template <
typename T>
348 data =
new T*[this->
nrows_];
358 *tmp++ =
static_cast<T
>(*p++);
363 template <
typename T>
370 template <
typename T>
374 return this->nrows() * this->ncols();
377 template <
typename T>
388 template <
typename T>
397 data =
new T*[this->nrows_];
398 T* tmp =
new T[this->nrows_ * this->ncols_];
412 template <
typename T>
421 template <
typename T>
430 template <
typename T>
439 template <
typename T>
444 for (
index_type k=0; k<this->nrows_*this->ncols_; k++)
445 if (dataptr_[k] < min) min = dataptr_[k];
449 template <
typename T>
454 for (
index_type k=0; k<this->nrows_*this->ncols_; k++)
455 if (dataptr_[k] > max) max = dataptr_[k];
459 template <
typename T>
464 T *mptr = &((*m)[0][0]);
467 *mptr++ =
data[r][c];
471 template <
typename T>
474 ASSERT(this->nrows_ == this->ncols_);
487 template <
typename T>
498 template <
typename T>
502 std::fill(this->begin(), this->end(), 0);
505 template <
typename T>
508 template <
typename T>
525 template <
typename T>
535 template <
typename T>
539 std::ostringstream oss;
544 oss <<
data[i][j] <<
" ";
548 str.assign(oss.str());
551 template <
typename T>
563 memcpy(mat->data[i-r1],
data[i] + c1, (c2 - c1 + 1) *
sizeof(
double));
569 #define DENSEMATRIX_VERSION 4
571 template <
typename T>
582 int nrows =
static_cast<int>(this->nrows_);
583 int ncols =
static_cast<int>(this->ncols_);
586 this->nrows_ =
static_cast<size_type>(nrows);
587 this->ncols_ =
static_cast<size_type>(ncols);
591 long long nrows =
static_cast<long long>(this->nrows_);
592 long long ncols =
static_cast<long long>(this->ncols_);
595 this->nrows_ =
static_cast<size_type>(nrows);
596 this->ncols_ =
static_cast<size_type>(ncols);
601 data=
new double*[this->nrows_];
602 double* tmp=
new double[this->nrows_ * this->ncols_];
617 split = this->separate_raw_;
619 if (this->separate_raw_)
621 Pio(stream, this->raw_filename_);
622 FILE *f=fopen(this->raw_filename_.c_str(),
"r");
625 fread(
data[0],
sizeof(T), this->nrows_ * this->ncols_, f);
630 const std::string errmsg =
"Error reading separated file '" +
631 this->raw_filename_ +
"'";
632 std::cerr << errmsg <<
"\n";
639 this->separate_raw_ =
false;
641 split = this->separate_raw_;
645 std::string filename = this->raw_filename_;
646 split = this->separate_raw_;
653 size_t pos = stream.
file_name.rfind(
'.');
654 if (pos == std::string::npos) filename = stream.
file_name +
".raw";
655 else filename = stream.
file_name.substr(0,pos) +
".raw";
666 Pio(stream, filename);
667 FILE *f = fopen(filename.c_str(),
"w");
668 fwrite(
data[0],
sizeof(T), this->nrows_ * this->ncols_, f);
675 size_t block_size = this->nrows_ * this->ncols_;
676 if (!stream.
block_io(dataptr_,
sizeof(T), block_size))
678 for (
size_t i = 0; i < block_size; i++)
680 stream.
io(dataptr_[i]);
690 template <
typename T>
694 if (this->nrows_ != this->ncols_)
return false;
696 #if defined(HAVE_LAPACK)
699 transpose_square_in_place();
707 std::ostringstream oss;
708 oss <<
"Caught LapackError exception: " << exception.
message();
710 std::cerr << oss.str() << std::endl;
715 transpose_square_in_place();
720 T** newdata=
new T*[this->nrows_];
721 T* tmp=
new T[this->nrows_ * this->ncols_];
725 for (i=0; i<this->nrows_; i++)
737 for (i=0;i<this->nrows_;i++)
742 for (j=i+1; j<this->nrows_; j++)
757 for (j=0; j<this->ncols_; j++)
759 T ntmp=newdata[i][j];
760 newdata[i][j]=newdata[row][j];
761 newdata[row][j]=ntmp;
764 T denom=1./
data[i][i];
767 for (j=i+1; j<this->nrows_; j++)
769 T factor=
data[j][i]*denom;
781 for (i=1; i<this->nrows_; i++)
783 T denom=1./
data[i][i];
788 T factor=
data[j][i]*denom;
800 for (i=0;i<this->nrows_;i++)
802 T factor=1./
data[i][i];
806 newdata[i][j]*=factor;
812 dataptr_=newdataptr_;
818 T* old_ptr =
data[i];
819 T* new_ptr = dataptr_ + i*this->ncols_;
826 new_ptr[j] = old_ptr[j];
837 template <
typename T>
854 template <
typename T>
863 while (i<this->ncols_) sum+=rp[i++];
867 template <
typename T>
871 T a,b,c,d,e,f,g,h,i,j,k,l,m,
n,o,p;
872 ASSERTMSG(((this->nrows_ == 4) && (this->ncols_ == 4)),
873 "Number of Rows and Colums for Determinant must equal 4! (Code not completed)");
879 T q=a*f*k*p - a*f*l*o - a*j*g*p + a*j*h*o + a*n*g*l - a*n*h*k
880 - e*b*k*p + e*b*l*o + e*j*c*p - e*j*d*o - e*n*c*l + e*n*d*k
881 + i*b*g*p - i*b*h*o - i*f*c*p + i*f*d*o + i*n*c*h - i*n*d*g
882 - m*b*g*l + m*b*h*k + m*f*c*l - m*f*d*k - m*j*c*h + m*j*d*g;
void Sub(ColumnMatrixGeneric< T > &result, const ColumnMatrixGeneric< T > &a, const ColumnMatrixGeneric< T > &b)
Definition: ColumnMatrixFunctions.h:113
virtual void put(index_type r, index_type c, T val)
Definition: DenseMatrix.h:423
virtual bool block_io(void *, size_t, size_t)
Definition: Persistent.h:174
virtual T get(index_type r, index_type c) const
Definition: DenseMatrix.h:414
bool reading() const
Definition: Persistent.h:164
Definition: DenseMatrix.h:68
LockingHandle< Matrix< double > > MatrixHandle
Definition: MatrixFwd.h:55
double xx() const
Definition: Tensor.h:121
virtual DenseMatrixGeneric * make_transpose() const
Definition: DenseMatrix.h:461
double zz() const
Definition: Tensor.h:126
#define SCI_THROW(exc)
Definition: Exception.h:87
virtual ColumnMatrix * column()
Definition: MatrixTypeConverter.h:321
#define ASSERTMSG(condition, message)
Definition: Exception.h:113
static DenseMatrixGeneric * zero_matrix(size_type rows, size_type cols)
Definition: DenseMatrix.h:527
double Abs(double d)
Definition: MiscMath.h:369
Definition: DenseColMajMatrix.h:73
virtual void io(bool &)
Definition: Persistent.cc:193
DenseMatrixGeneric & operator=(const DenseMatrixGeneric &)
assignment operator
Definition: DenseMatrix.h:390
Definition: Persistent.h:89
T * operator[](index_type r)
fast accessors
Definition: DenseMatrix.h:129
double yz() const
Definition: Tensor.h:125
int compute_checksum(T *data, std::size_t length)
Definition: CheckSum.h:38
T const * operator[](index_type r) const
Definition: DenseMatrix.h:133
void y(const double)
Definition: Point.h:135
Definition: Persistent.h:187
#define SCISHARE
Definition: share.h:39
double yy() const
Definition: Tensor.h:124
virtual void mult(const ColumnMatrix &x, ColumnMatrix &b, index_type beg=-1, index_type end=-1, int spVec=0) const
Definition: DenseMatrixMultiplication.h:49
void lapackinvert(double *, int)
Definition: sci_lapack.cc:449
T sumOfRow(index_type)
Definition: DenseMatrix.h:856
static DenseMatrix * make_diagonal_from_column(const ColumnMatrix &column, size_type rows, size_type cols)
Definition: DenseMatrixMultiplication.h:215
virtual void begin_cheap_delim()
Definition: Persistent.cc:183
const T & operator()(index_type i, index_type j) const
Definition: DenseMatrix.h:144
#define ASSERT(condition)
Definition: Assert.h:110
virtual T max()
Definition: DenseMatrix.h:451
virtual int begin_class(const std::string &name, int current_version)
Definition: Persistent.cc:143
#define ASSERTRANGE(c, l, h)
Definition: Assert.h:99
T ** get_raw_2D_pointer() const
Definition: DenseMatrix.h:138
void z(const double)
Definition: Point.h:145
size_type ncols_
Definition: Matrix.h:113
Definition: ParallelLinearAlgebraTests.cc:358
double xz() const
Definition: Tensor.h:123
virtual size_type get_data_size() const
Definition: DenseMatrix.h:372
void x(const double)
Definition: Point.h:125
SCISHARE void Concat_rows(DenseMatrix &, const DenseMatrix &, const DenseMatrix &)
Definition: DenseMatrixFunctions.cc:329
Generic exception for internal errors.
long long size_type
Definition: Types.h:40
T * end() const
Definition: Matrix.h:141
T determinant()
throws an assertion if not square
Definition: DenseMatrix.h:869
virtual MatrixHandle submatrix(index_type r1, index_type c1, index_type r2, index_type c2)
Definition: DenseMatrix.h:553
dictionary data
Definition: eabLatVolData.py:11
Definition: ColumnMatrix.h:55
DenseMatrixGeneric()
Constructors.
Definition: DenseMatrix.h:234
size_type nrows_
Definition: Matrix.h:112
void transpose_square_in_place()
Definition: DenseMatrix.h:472
virtual void end_cheap_delim()
Definition: Persistent.cc:188
virtual SparseRowMatrix * sparse()
Definition: MatrixTypeConverter.h:331
SCISHARE void eigenvalues(const DenseMatrix &, ColumnMatrix &, ColumnMatrix &)
Definition: DenseMatrix.cc:461
LockingHandle< SparseRowMatrix > SparseRowMatrixHandle
Definition: MatrixFwd.h:58
void x(double)
Definition: Vector.h:175
T * begin() const
Definition: Matrix.h:140
virtual void io(Piostream &)
Definition: DenseMatrix.h:573
virtual const char * message() const
Definition: LapackError.cc:59
virtual int compute_checksum()
Definition: DenseMatrix.h:216
SCISHARE void Mult_X_trans(DenseMatrix &, const DenseMatrix &, const DenseMatrix &)
Definition: DenseMatrixFunctions.cc:302
virtual bool invert()
return false if not invertible.
Definition: DenseMatrix.h:692
double xy() const
Definition: Tensor.h:122
virtual void add(index_type r, index_type c, T val)
Definition: DenseMatrix.h:432
virtual DenseMatrixGeneric * clone() const
Public member functions.
Definition: DenseMatrix.h:226
void y(double)
Definition: Vector.h:185
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
virtual void mult_transpose(const ColumnMatrix &x, ColumnMatrix &b, index_type beg=-1, index_type end=-1, int spVec=0) const
Definition: DenseMatrixMultiplication.h:178
static DenseMatrixGeneric * identity(size_type size)
Definition: DenseMatrix.h:510
SCISHARE void svd(const DenseMatrix &, DenseMatrix &, SparseRowMatrixHandle &, DenseMatrix &)
Definition: DenseMatrix.cc:443
virtual ~DenseMatrixGeneric()
Destructor.
Definition: DenseMatrix.h:378
SCISHARE void Concat_cols(DenseMatrix &, const DenseMatrix &, const DenseMatrix &)
Definition: DenseMatrixFunctions.cc:352
virtual T * get_data_pointer() const
Definition: DenseMatrix.h:365
virtual void end_class()
Definition: Persistent.cc:178
T sumOfCol(index_type)
Definition: DenseMatrix.h:839
long long index_type
Definition: Types.h:39
virtual void zero()
slow setters/getter for polymorphic operations
Definition: DenseMatrix.h:500
virtual T min()
Definition: DenseMatrix.h:441
virtual void print(std::string &) const
Definition: DenseMatrix.h:537
virtual std::string dynamic_type_name() const
Persistent representation...
Definition: DenseMatrix.h:506
virtual DenseColMajMatrix * dense_col_maj()
Definition: MatrixTypeConverter.h:299
virtual void io(Piostream &)
Definition: Matrix.cc:58
SCISHARE void Mult_trans_X(DenseMatrix &, const DenseMatrix &, const DenseMatrix &)
Definition: DenseMatrixFunctions.cc:278
virtual void getRowNonzerosNoCopy(index_type r, size_type &size, index_type &stride, index_type *&cols, T *&vals)
Definition: DenseMatrix.h:489
Definition: Persistent.h:64
boost::error_info< struct tag_file_not_found, std::string > FileNotFound
Definition: Exception.h:54
int n
Definition: eab.py:9
SCISHARE int solve(const DenseMatrix &, ColumnMatrix &)
Definition: DenseMatrix.cc:55
std::string file_name
Definition: Persistent.h:135
#define DEBUG_CONSTRUCTOR(type)
Definition: Debug.h:64
static PersistentTypeID type_id
Definition: DenseMatrix.h:163
SCISHARE void eigenvectors(const DenseMatrix &, ColumnMatrix &, ColumnMatrix &, DenseMatrix &)
Definition: DenseMatrix.cc:467
void Add(ColumnMatrixGeneric< T > &result, const ColumnMatrixGeneric< T > &a, const ColumnMatrixGeneric< T > &b)
Definition: ColumnMatrixFunctions.h:209
void z(double)
Definition: Vector.h:195
T & operator()(index_type i, index_type j)
Definition: DenseMatrix.h:143
SCISHARE void solve_lapack(const DenseMatrix &matrix, const ColumnMatrix &rhs, ColumnMatrix &lhs)
Definition: DenseMatrix.cc:437
Definition: LapackError.h:40
DenseMatrixGeneric< double > DenseMatrix
Definition: MatrixFwd.h:44
size_type nrows() const
Definition: Matrix.h:132
Definition: MatrixFwd.h:40
virtual DenseMatrix * dense()
Convert this matrix to the specified type.
Definition: MatrixTypeConverter.h:292
int size
Definition: eabLatVolData.py:2
void multiply(ColumnMatrix &x, ColumnMatrix &b) const
Definition: DenseMatrixMultiplication.h:130
#define DENSEMATRIX_VERSION
Definition: DenseMatrix.h:569
#define DEBUG_DESTRUCTOR(type)
Definition: Debug.h:65
SCISHARE friend void Mult(DenseMatrix &, const DenseMatrix &, const DenseMatrix &)
Friend functions.
Definition: DenseMatrixFunctions.cc:48