46 #ifndef CORE_DATATYPES_SPARSEROWMATRIX_H
47 #define CORE_DATATYPES_SPARSEROWMATRIX_H 1
52 #include <Core/Util/Assert.h>
53 #include <Core/Util/FancyAssert.h>
54 #include <Core/Util/MemoryUtil.h>
56 #include <boost/shared_array.hpp>
69 class SparseRowMatrixGeneric :
public Matrix<T>
72 typedef boost::shared_array<index_type>
Rows;
73 typedef boost::shared_array<index_type>
Columns;
95 data_(new T[dataSize])
100 data_(new T[columnAndDataSize])
102 Data(
const std::vector<index_type>&
rows,
const std::vector<index_type>&
columns,
const std::vector<T>&
data) :
109 rows_(rows), columns_(columns), data_(data)
112 bool allocated()
const {
return rows_ && columns_ && data_; }
140 data_.reset(
new T[dataSize]);
145 return Data(rows_, columns_, data_);
176 size_type nnz,
bool sort_columns =
false);
206 bool is_valid()
const {
return data_ && rows_ && columns_; }
214 throw "not implemented";
219 throw "not implemented";
250 virtual void print(std::string&)
const;
260 template <
typename T>
267 template <
typename T>
271 template <
typename T>
283 template <
typename T>
290 template <
typename T>
300 template <
typename T>
301 void SparseRowMatrixGeneric<T>::clear_memory()
317 return(cols_[i1] < cols_[i2]);
324 template <
typename T>
337 if (rows_[j+1]-rows_[j] > max_num_cols)
338 max_num_cols = rows_[j+1]- rows_[j];
340 std::vector<index_type> order(max_num_cols);
341 std::vector<T> databuffer(max_num_cols);
342 std::vector<index_type> indexbuffer(max_num_cols);
352 order.resize(num_cols);
353 for (
index_type p=rr; p<rows_[j+1]; p++) order[p-rr] = p;
354 std::sort(order.begin(),order.end(),sortmatrix);
358 databuffer[q] = data_[order[q]];
359 indexbuffer[q] = columns_[order[q]];
366 data_[p] = databuffer[q];
367 columns_[p] = indexbuffer[q];
373 if (columns_[p] == indexbuffer[q])
375 data_[p] += databuffer[q];
381 columns_[p] = indexbuffer[q];
382 data_[p] = databuffer[q];
392 nnz_ = rows_[this->nrows_];
395 template <
typename T>
400 Matrix<T>(nnrows, nncols),
402 columns_(data.columns()),
409 data_.reset(
new T[nnz_]);
414 template <
typename T>
419 this->nrows_ = copy.nrows();
420 this->ncols_ = copy.ncols();
430 template <
typename T>
432 Matrix<T>(copy.nrows_, copy.ncols_),
442 template <
typename T>
448 template <
typename T>
454 ASSERTMSG(rows_[0] == 0,
"Row start is nonzero.");
455 for (i = 0; i< this->nrows_; i++)
457 ASSERTMSG(rows_[i] <= rows_[i+1],
"Malformed rows, not increasing.");
458 for (j = rows_[i]; j < rows_[i+1]; j++)
460 ASSERTMSG(columns_[j] >= 0 && columns_[j] < this->ncols_,
"Column out of range.");
463 ASSERTMSG(columns_[j-1] != columns_[j],
"Column doubled.");
464 ASSERTMSG(columns_[j-1] < columns_[j],
"Column out of order.");
468 ASSERTMSG(rows_[this->nrows_] == nnz_,
"Row end is incorrect.");
472 template <
typename T>
479 template <
typename T>
486 template <
typename T>
490 Data transposeData(this->ncols_+1, nnz_);
497 std::vector<index_type> at(t_nnrows+1);
511 for (; c<rows_[r+1]; c++)
514 t_columns[at[mcol]] = r;
515 t_a[at[mcol]] = data_[c];
522 template <
typename T>
541 else if (j>columns_[m])
552 template <
typename T>
571 else if (j>columns_[m])
582 template <
typename T>
594 ASSERTFAIL(
"SparseRowMatrix::put into invalid(dataless) location.");
602 else if (j>columns_[m])
614 template <
typename T>
626 ASSERTFAIL(
"SparseRowMatrix::add into invalid(dataless) location.");
634 else if (j>columns_[m])
646 template <
typename T>
652 if (data_[k] < min) min = data_[k];
656 template <
typename T>
662 if (data_[k] > max) max = data_[k];
666 template <
typename T>
672 size = rows_[r+1] - rows_[r];
674 cols = columns_.get() + rows_[r];
675 vals = data_.get() + rows_[r];
678 template <
typename T>
682 std::fill(data_.get(), data_.get() + nnz_, 0);
685 template <
typename T>
689 std::ostringstream oss;
690 oss.flags(std::ios::showpoint);
692 oss <<
"nrows=" << this->nrows_ <<
" ncols=" << this->ncols_ <<
" nnz=" << nnz_ << std::endl;
695 while(index < rows_[r+1])
697 oss << r <<
" " << columns_[index] <<
" " << data_[index] << std::endl;
705 #define SPARSEROWMATRIX_VERSION 2
707 template <
typename T>
717 int r =
static_cast<int>(this->nrows_);
718 int c =
static_cast<int>(this->ncols_);
719 int n =
static_cast<int>(nnz_);
723 this->nrows_ =
static_cast<size_type>(r);
724 this->ncols_ =
static_cast<size_type>(c);
736 data_.reset(
new T[nnz_]);
742 Pio_index(stream, rows_.get(), this->nrows_+1);
750 Pio(stream, data_.get(), nnz_);
756 template <
typename T>
767 std::vector<index_type> rs(r2-r1+2);
768 std::vector<index_type> csv;
769 std::vector<T> valsv;
772 for (i = r1; i <= r2; i++)
774 rs[i-r1+1] = rs[i-r1];
775 for (j = rows_[i]; j < rows_[i+1]; j++)
777 if (columns_[j] >= c1 && columns_[j] <= c2)
779 csv.push_back(columns_[j] - c1);
780 valsv.push_back(data_[j]);
789 static_cast<size_type>(valsv.size()));
792 template <
typename T>
802 for (i=0; i<
size; i++)
813 template <
typename T>
boost::shared_array< T > Storage
Definition: SparseRowMatrix.h:74
bool reading() const
Definition: Persistent.h:164
Definition: DenseMatrix.h:68
index_type * get_cols()
Definition: SparseRowMatrix.h:200
LockingHandle< Matrix< double > > MatrixHandle
Definition: MatrixFwd.h:55
Definition: SparseRowMatrix.h:89
virtual MatrixHandle submatrix(index_type r1, index_type c1, index_type r2, index_type c2)
Definition: SparseRowMatrix.h:758
#define ASSERTMSG(condition, message)
Definition: Exception.h:113
bool validate()
Definition: SparseRowMatrix.h:450
Definition: DenseColMajMatrix.h:73
virtual void put(index_type row, index_type col, T val)
Definition: SparseRowMatrix.h:584
virtual void io(bool &)
Definition: Persistent.cc:193
virtual void scalar_multiply(T s)
Definition: SparseRowMatrix.h:815
virtual T min()
Definition: SparseRowMatrix.h:648
Definition: Persistent.h:89
const Rows & allocate_rows(size_type rowSize)
Definition: SparseRowMatrix.h:128
virtual T max()
Definition: SparseRowMatrix.h:658
T get_value(index_type i) const
Definition: SparseRowMatrix.h:205
int compute_checksum(T *data, std::size_t length)
Definition: CheckSum.h:38
virtual SparseRowMatrixGeneric * clone() const
Make a duplicate, needed to support detach from LockingHandle.
Definition: SparseRowMatrix.h:285
T * get_vals()
Definition: SparseRowMatrix.h:197
Definition: Persistent.h:187
matrix_sort_type(const index_type *cols)
Definition: SparseRowMatrix.h:311
virtual T get(index_type, index_type) const
Definition: SparseRowMatrix.h:554
virtual void mult(const ColumnMatrix &x, ColumnMatrix &b, index_type beg=-1, index_type end=-1, int spVec=0) const
Definition: SparseRowMatrixMultiplication.h:53
index_type get_row(index_type i) const
Definition: SparseRowMatrix.h:203
virtual void begin_cheap_delim()
Definition: Persistent.cc:183
bool allocated() const
Definition: SparseRowMatrix.h:112
index_type get_col(index_type i) const
Definition: SparseRowMatrix.h:204
bool operator()(index_type i1, index_type i2) const
Definition: SparseRowMatrix.h:315
virtual int begin_class(const std::string &name, int current_version)
Definition: Persistent.cc:143
#define ASSERTRANGE(c, l, h)
Definition: Assert.h:99
const index_type * get_rows() const
Definition: SparseRowMatrix.h:201
T & operator()(index_type r, index_type c)
Definition: SparseRowMatrix.h:212
virtual void io(Piostream &)
Definition: SparseRowMatrix.h:709
Data(const Rows &rows, const Columns &columns, const Storage &data)
Definition: SparseRowMatrix.h:108
virtual void add(index_type row, index_type col, T val)
Definition: SparseRowMatrix.h:616
index_type * get_rows()
Definition: SparseRowMatrix.h:199
Definition: ParallelLinearAlgebraTests.cc:358
virtual DenseMatrix * dense()
Convert this matrix to the specified type.
Definition: MatrixTypeConverter.h:167
Definition: SparseRowMatrix.h:124
void Pio_index(Piostream &stream, index_type *data, size_type size)
Definition: Persistent.cc:606
long long size_type
Definition: Types.h:40
T * end() const
Definition: Matrix.h:141
static SparseRowMatrixGeneric * identity(size_type size)
Definition: SparseRowMatrix.h:794
SparseRowMatrixGeneric & operator=(const SparseRowMatrixGeneric &)
Definition: SparseRowMatrix.h:415
virtual T * get_data_pointer() const
Definition: SparseRowMatrix.h:474
dictionary data
Definition: eabLatVolData.py:11
Definition: ColumnMatrix.h:55
virtual void print(std::string &) const
Definition: SparseRowMatrix.h:686
virtual SparseRowMatrixGeneric * make_transpose() const
Definition: SparseRowMatrix.h:488
std::string type
Definition: Persistent.h:72
virtual size_type get_data_size() const
Definition: SparseRowMatrix.h:481
const Rows & rows() const
Definition: SparseRowMatrix.h:114
Definition: SparseRowMatrix.h:308
virtual void end_cheap_delim()
Definition: Persistent.cc:188
virtual std::string dynamic_type_name() const
Persistent representation...
Definition: SparseRowMatrix.h:253
virtual void zero()
Definition: SparseRowMatrix.h:680
Builder()
Definition: SparseRowMatrix.h:127
void order_columns()
Definition: SparseRowMatrix.h:326
const T & operator()(index_type r, index_type c) const
Definition: SparseRowMatrix.h:217
virtual SparseRowMatrixGeneric * sparse()
Definition: MatrixTypeConverter.h:159
virtual ColumnMatrix * column()
Definition: MatrixTypeConverter.h:210
size_type get_nnz() const
Definition: SparseRowMatrix.h:195
index_type getIdx(index_type, index_type)
Definition: SparseRowMatrix.h:524
#define ASSERTFAIL(string)
Definition: Assert.h:52
virtual void mult_transpose(const ColumnMatrix &x, ColumnMatrix &b, index_type beg=-1, index_type end=-1, int spVec=0) const
Definition: SparseRowMatrixMultiplication.h:82
virtual DenseColMajMatrix * dense_col_maj()
Definition: MatrixTypeConverter.h:189
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
const Columns & columns() const
Definition: SparseRowMatrix.h:115
boost::shared_array< index_type > Rows
Definition: SparseRowMatrix.h:72
virtual void end_class()
Definition: Persistent.cc:178
long long index_type
Definition: Types.h:39
#define SPARSEROWMATRIX_VERSION
Definition: SparseRowMatrix.h:705
virtual bool is_zero() const
Definition: SparseRowMatrix.h:207
virtual int compute_checksum()
Definition: SparseRowMatrix.h:273
virtual void io(Piostream &)
Definition: Matrix.cc:58
boost::shared_array< T > make_deep_copy(const boost::shared_array< T > &arr, size_t length)
Definition: MemoryUtil.h:55
Data(size_type rowSize, size_type columnSize, size_type dataSize)
Definition: SparseRowMatrix.h:92
Data build()
Definition: SparseRowMatrix.h:143
Definition: Persistent.h:64
Data(size_type rowSize, size_type columnAndDataSize)
Definition: SparseRowMatrix.h:97
virtual void getRowNonzerosNoCopy(index_type r, size_type &size, size_type &stride, index_type *&cols, T *&vals)
Definition: SparseRowMatrix.h:668
int n
Definition: eab.py:9
const Storage & data() const
Definition: SparseRowMatrix.h:116
boost::shared_array< index_type > Columns
Definition: SparseRowMatrix.h:73
static Persistent * SparseRowMatrixGenericMaker()
Definition: SparseRowMatrix.h:262
const Storage & allocate_data(size_type dataSize)
Definition: SparseRowMatrix.h:138
Data(const std::vector< index_type > &rows, const std::vector< index_type > &columns, const std::vector< T > &data)
Definition: SparseRowMatrix.h:102
MatrixBase< double > Matrix
Definition: MatrixFwd.h:40
#define DEBUG_CONSTRUCTOR(type)
Definition: Debug.h:64
const T * get_vals() const
Definition: SparseRowMatrix.h:198
void Pio_size(Piostream &stream, Size &size)
Definition: Persistent.h:273
const Columns & allocate_columns(size_type colSize)
Definition: SparseRowMatrix.h:133
virtual ~SparseRowMatrixGeneric()
Destructor.
Definition: SparseRowMatrix.h:443
Definition: MatrixFwd.h:40
int size
Definition: eabLatVolData.py:2
static PersistentTypeID type_id
Definition: SparseRowMatrix.h:255
const index_type * get_cols() const
Definition: SparseRowMatrix.h:202
#define DEBUG_DESTRUCTOR(type)
Definition: Debug.h:65
bool is_valid() const
Definition: SparseRowMatrix.h:206