SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MatrixTypeConverter.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2012 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 /// @todo Documentation Core/Datatypes/Legacy/Matrix/MatrixTypeConverter.h
29 
30 #ifndef CORE_DATATYPES_MATRIX_TYPE_CONVERTER_H
31 #define CORE_DATATYPES_MATRIX_TYPE_CONVERTER_H
32 
33 #include <limits>
34 #include <functional>
35 
36 #include <Core/Datatypes/share.h>
37 
39 #include <Core/Datatypes/ColumnMatrix.h>
40 #include <Core/Datatypes/DenseColMajMatrix.h>
41 #include <Core/Datatypes/DenseColMajMatrixMultiplication.h>
43 #include <Core/Datatypes/DenseMatrixMultiplication.h>
45 #include <Core/Datatypes/SparseRowMatrixMultiplication.h>
46 
47 #include <Core/Math/MiscMath.h>
48 
49 namespace SCIRun {
50 
51 // No conversion is done.
52 // NULL is returned if the matrix is not of the appropriate type.
54 {
55 public:
56  template <class ToType>
57  static ToType* to(const MatrixHandle& matrix, typename boost::enable_if<boost::is_base_of<MatrixBase, ToType> >::type* = 0)
58  {
59  return SCI_DATATYPE_CAST<ToType*>(matrix.get_rep());
60  }
61 
62  static inline DenseMatrix* as_dense(const MatrixHandle& mh)
63  {
64  return to<DenseMatrix>(mh);
65  }
66 
67  static inline SparseRowMatrix* as_sparse(const MatrixHandle& mh)
68  {
69  return to<SparseRowMatrix>(mh);
70  }
71 
72  static inline ColumnMatrix* as_column(const MatrixHandle& mh)
73  {
74  return to<ColumnMatrix>(mh);
75  }
76 
78  {
79  return to<DenseColMajMatrix>(mh);
80  }
81 private:
82  matrix_cast();
83 };
84 
86 {
87 public:
88  // Test to see if the matrix is this subtype.
89  static inline bool dense(const MatrixHandle& mh) { return matrix_cast::as_dense(mh) != 0; }
90  static inline bool sparse(const MatrixHandle& mh) { return matrix_cast::as_sparse(mh) != 0; }
91  static inline bool column(const MatrixHandle& mh) { return matrix_cast::as_column(mh) != 0; }
92  static inline bool dense_col_maj(const MatrixHandle& mh) { return matrix_cast::as_dense_col_maj(mh) != 0; }
93 private:
94  matrix_is();
95 };
96 
97 /// @todo: these will be moved to a separate class to break compilation dependency on the subclasses of Matrix.
98 
99 template <typename T>
100 DenseMatrix *
102 {
103  DenseMatrix *m = new DenseMatrix(this->nrows_, this->ncols_);
104  for (index_type i = 0; i < this->nrows_; i++)
105  for (index_type j = 0; j < this->ncols_; j++)
106  (*m)[i][j] = iget(i, j);
107  return m;
108 }
109 
110 template <typename T>
111 ColumnMatrix *
113 {
114  ColumnMatrix *cm = new ColumnMatrix(this->nrows_);
115  for (index_type i=0; i < this->nrows_; i++)
116  (*cm)[i] = iget(i, 0);
117  return cm;
118 }
119 
120 template <typename T>
123 {
124  size_type nnz = this->number_of_nonzero_elements();
125 
126  SparseRowMatrix::Data sparseData(this->nrows_ + 1, nnz);
127 
128  const SparseRowMatrix::Rows& rows = sparseData.rows();
129  const SparseRowMatrix::Columns& columns = sparseData.columns();
130  const SparseRowMatrix::Storage& a = sparseData.data();
131 
132  index_type count = 0;
133  for (index_type r=0; r<this->nrows_; r++)
134  {
135  rows[r] = count;
136  for (index_type c=0; c<this->ncols_; c++)
137  if ( nonzero(iget(r, c)) )
138  {
139  columns[count] = c;
140  a[count] = iget(r, c);
141  count++;
142  }
143  }
144  rows[this->nrows_] = count;
145 
146  return new SparseRowMatrix(this->nrows_, this->ncols_, sparseData, nnz);
147 }
148 
149 template <typename T>
152 {
153  return this;
154 }
155 
156 
157 template <typename T>
160 {
161  return this;
162 }
163 
164 
165 template <typename T>
166 DenseMatrix *
168 {
169  DenseMatrix *dm = new DenseMatrix(this->nrows_, this->ncols_);
170  if (this->nrows_ == 0) return dm;
171 
172  dm->zero();
173  index_type count=0;
174  index_type nextRow;
175  for (index_type r=0; r<this->nrows_; r++)
176  {
177  nextRow = rows_[r+1];
178  while (count < nextRow)
179  {
180  (*dm)[r][columns_[count]]=data_[count];
181  count++;
182  }
183  }
184  return dm;
185 }
186 
187 template <typename T>
190 {
191  DenseColMajMatrix* dm = new DenseColMajMatrix(this->nrows_, this->ncols_);
192  if (this->nrows_ == 0) return dm;
193  dm->zero();
194  index_type count = 0;
195  index_type nextRow;
196  for (index_type r = 0; r<this->nrows_; r++)
197  {
198  nextRow = rows_[r+1];
199  while (count < nextRow)
200  {
201  dm->iget(r, columns_[count]) = data_[count];
202  count++;
203  }
204  }
205  return dm;
206 }
207 
208 template <typename T>
211 {
212  ColumnMatrix *cm = new ColumnMatrix(this->nrows_);
213  if (this->nrows_)
214  {
215  cm->zero();
216  for (int i=0; i<this->nrows_; i++)
217  {
218  // If the first column entry for the row is a zero.
219  if (columns_[rows_[i]] == 0)
220  {
221  (*cm)[i] = data_[rows_[i]];
222  }
223  else
224  {
225  (*cm)[i] = 0;
226  }
227  }
228  }
229  return cm;
230 }
231 
232 template <typename T>
235 {
236  return this;
237 }
238 
239 template <typename T>
240 DenseMatrix *
242 {
243  DenseMatrix *dm = new DenseMatrix(this->nrows_, 1);
244  for (index_type i = 0; i < this->nrows_; i++)
245  {
246  (*dm)[i][0] = data_[i];
247  }
248  return dm;
249 }
250 
251 template <typename T>
254 {
255  DenseColMajMatrixGeneric<T>* dm = new DenseColMajMatrixGeneric<T>(this->nrows_, 1);
256  for (index_type i = 0; i < this->nrows_; i++)
257  {
258  dm->iget(i, 0) = data_[i];
259  }
260  return dm;
261 }
262 
263 template <typename T>
266 {
267  size_type nnz = this->number_of_nonzero_elements();
268 
269  typename SparseRowMatrixGeneric<T>::Data sparseData(this->nrows_ + 1, nnz);
270 
271  index_type count = 0;
272  const SparseRowMatrix::Rows& rows = sparseData.rows();
273  const SparseRowMatrix::Columns& columns = sparseData.columns();
274  const SparseRowMatrix::Storage& a = sparseData.data();
275  for (index_type r = 0; r < this->nrows_; r++)
276  {
277  rows[r] = count;
278  if ( nonzero(data_[r]) )
279  {
280  columns[count]=0;
281  a[count]=data_[r];
282  count++;
283  }
284  }
285  rows[this->nrows_] = count;
286  return new SparseRowMatrixGeneric<T>(this->nrows_, 1, sparseData, nnz);
287 }
288 
289 
290 template <typename T>
293 {
294  return this;
295 }
296 
297 template <typename T>
300 {
301  DenseColMajMatrix* dm = new DenseColMajMatrix(this->nrows_, this->ncols_);
302  if (dm == 0) return (0);
303 
304  index_type m1 = static_cast<index_type>(this->ncols_);
305  index_type m2 = static_cast<index_type>(this->nrows_);
306 
307  T *v1 = dataptr_;
308  T *v2 = dm->get_data_pointer();
309  for (index_type i = 0; i < this->nrows_; i++)
310  {
311  for (index_type j = 0; j < this->ncols_; j++)
312  {
313  v2[i+j*m2] = v1[i*m1+j];
314  }
315  }
316  return dm;
317 }
318 
319 template <typename T>
320 ColumnMatrix *
322 {
323  ColumnMatrix *cm = new ColumnMatrix(this->nrows_);
324  for (index_type i=0; i<static_cast<index_type>(this->nrows_); i++)
325  (*cm)[i] = data[i][0];
326  return cm;
327 }
328 
329 template <typename T>
332 {
333  size_type nnz = this->number_of_nonzero_elements();
334  SparseRowMatrix::Data sparseData(this->nrows_ + 1, nnz);
335 
336  if (!sparseData.allocated())
337  {
338  std::cerr << "Could not allocate memory for rows, columns, or non-zero elements buffer "
339  << __FILE__ << ": " << __LINE__ << std::endl;
340 
341  return 0;
342  }
343 
344  index_type count = 0;
345  const SparseRowMatrix::Rows& rows = sparseData.rows();
346  const SparseRowMatrix::Columns& columns = sparseData.columns();
347  const SparseRowMatrix::Storage& a = sparseData.data();
348  for (index_type r = 0; r < this->nrows_; r++)
349  {
350  rows[r] = count;
351  for (index_type c = 0; c < this->ncols_; c++)
352  if ( nonzero(data[r][c]) )
353  {
354  columns[count]=c;
355  a[count]=data[r][c];
356  count++;
357  }
358  }
359  rows[this->nrows_] = count;
360 
361  return new SparseRowMatrix(this->nrows_, this->ncols_, sparseData, nnz);
362 }
363 
364 
365 } // End namespace SCIRun
366 
367 #endif
virtual SparseRowMatrix * sparse()
Definition: MatrixTypeConverter.h:122
virtual DenseColMajMatrixGeneric< T > * dense_col_maj()
Definition: MatrixTypeConverter.h:253
boost::shared_array< T > Storage
Definition: SparseRowMatrix.h:74
Definition: DenseMatrix.h:68
LockingHandle< Matrix< double > > MatrixHandle
Definition: MatrixFwd.h:55
virtual ColumnMatrix * column()
Definition: MatrixTypeConverter.h:112
Definition: SparseRowMatrix.h:89
static bool column(const MatrixHandle &mh)
Definition: MatrixTypeConverter.h:91
virtual ColumnMatrix * column()
Definition: MatrixTypeConverter.h:321
Definition: DenseColMajMatrix.h:73
Eigen::MatrixXd DenseMatrix
Definition: EigenDenseMatrixTests.cc:33
static DenseColMajMatrix * as_dense_col_maj(const MatrixHandle &mh)
Definition: MatrixTypeConverter.h:77
virtual void zero()
slow setters/getter for polymorphic operations
Definition: DenseColMajMatrix.h:330
static bool sparse(const MatrixHandle &mh)
Definition: MatrixTypeConverter.h:90
Definition: MatrixTypeConverter.h:85
#define SCISHARE
Definition: share.h:39
bool allocated() const
Definition: SparseRowMatrix.h:112
static DenseMatrix * as_dense(const MatrixHandle &mh)
Definition: MatrixTypeConverter.h:62
virtual SparseRowMatrixGeneric< T > * sparse()
Definition: MatrixTypeConverter.h:265
T & iget(index_type r, index_type c)
fast accessors
Definition: DenseColMajMatrix.h:127
ColumnMatrixGeneric< double > ColumnMatrix
Definition: MatrixFwd.h:48
bool nonzero(T d)
Definition: MiscMath.h:58
static SparseRowMatrix * as_sparse(const MatrixHandle &mh)
Definition: MatrixTypeConverter.h:67
virtual DenseMatrix * dense()
Convert this matrix to the specified type.
Definition: MatrixTypeConverter.h:167
long long size_type
Definition: Types.h:40
virtual ColumnMatrixGeneric< T > * column()
Definition: MatrixTypeConverter.h:234
dictionary data
Definition: eabLatVolData.py:11
Definition: ColumnMatrix.h:55
virtual T * get_data_pointer() const
Definition: DenseColMajMatrix.h:219
const Rows & rows() const
Definition: SparseRowMatrix.h:114
static bool dense(const MatrixHandle &mh)
Definition: MatrixTypeConverter.h:89
virtual SparseRowMatrix * sparse()
Definition: MatrixTypeConverter.h:331
DenseColMajMatrixGeneric< double > DenseColMajMatrix
Definition: MatrixFwd.h:52
static ColumnMatrix * as_column(const MatrixHandle &mh)
Definition: MatrixTypeConverter.h:72
virtual DenseMatrix * dense()
Definition: MatrixTypeConverter.h:101
virtual DenseMatrix * dense()
Convert this matrix to the specified type.
Definition: MatrixTypeConverter.h:241
virtual SparseRowMatrixGeneric * sparse()
Definition: MatrixTypeConverter.h:159
virtual ColumnMatrix * column()
Definition: MatrixTypeConverter.h:210
virtual DenseColMajMatrix * dense_col_maj()
Definition: MatrixTypeConverter.h:189
virtual DenseColMajMatrixGeneric * dense_col_maj()
Definition: MatrixTypeConverter.h:151
const Columns & columns() const
Definition: SparseRowMatrix.h:115
SparseRowMatrixGeneric< double > SparseRowMatrix
Definition: MatrixFwd.h:40
virtual void zero()
Definition: ColumnMatrix.h:286
boost::shared_array< index_type > Rows
Definition: SparseRowMatrix.h:72
long long index_type
Definition: Types.h:39
virtual void zero()
slow setters/getter for polymorphic operations
Definition: DenseMatrix.h:500
static ToType * to(const MatrixHandle &matrix, typename boost::enable_if< boost::is_base_of< MatrixBase, ToType > >::type *=0)
Definition: MatrixTypeConverter.h:57
virtual DenseColMajMatrix * dense_col_maj()
Definition: MatrixTypeConverter.h:299
static bool dense_col_maj(const MatrixHandle &mh)
Definition: MatrixTypeConverter.h:92
int count
Definition: readAllFields.py:25
const Storage & data() const
Definition: SparseRowMatrix.h:116
boost::shared_array< index_type > Columns
Definition: SparseRowMatrix.h:73
Definition: MatrixTypeConverter.h:53
DenseMatrixGeneric< double > DenseMatrix
Definition: MatrixFwd.h:44
Definition: MatrixFwd.h:40
virtual DenseMatrix * dense()
Convert this matrix to the specified type.
Definition: MatrixTypeConverter.h:292