SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DenseColMajMatrix.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) 2009 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 
29 ///
30 ///@file DenseColMajMatrix.h
31 ///@brief DenseColMaj matrices
32 ///
33 ///@author
34 /// Steven G. Parker
35 /// Department of Computer Science
36 /// University of Utah
37 ///@date October 1994
38 ///
39 
40 #ifndef CORE_DATATYPES_DENSECOLMAJMATRIX_H
41 #define CORE_DATATYPES_DENSECOLMAJMATRIX_H 1
42 
43 #include <Core/Datatypes/Matrix.h>
44 #include <Core/Geometry/Transform.h>
45 #include <Core/Math/MiscMath.h>
46 
47 #include <vector>
48 
49 #include <stdio.h>
50 
51 #include <sci_defs/lapack_defs.h>
52 #include <sci_defs/blas_defs.h>
53 
54 #include <Core/Util/Assert.h>
55 #include <Core/Util/FancyAssert.h>
56 
58 
59 #include <Core/Math/MiscMath.h>
61 
62 #include <iostream>
63 #include <sstream>
64 #include <float.h>
65 #include <string.h>
66 
67 
68 #include <Core/Datatypes/share.h>
69 
70 namespace SCIRun {
71 
72 template <typename T>
74 {
75 
76 public:
77  /// Constructors
81 
82  /// Destructor
83  virtual ~DenseColMajMatrixGeneric();
84 
85  /// Public member functions
86  virtual DenseColMajMatrixGeneric* clone() const;
88 
89  virtual DenseMatrix *dense();
90  virtual SparseRowMatrix *sparse();
91  virtual ColumnMatrix *column();
93 
94  virtual T* get_data_pointer() const;
95  virtual size_type get_data_size() const;
96 
97  /// slow setters/getter for polymorphic operations
98  virtual void zero();
99  virtual T get(index_type r, index_type c) const;
100  virtual void put(index_type r, index_type c, T val);
101  virtual void add(index_type r, index_type c, T val);
102 
103  virtual T min();
104  virtual T max();
105  virtual int compute_checksum();
106 
107  virtual void getRowNonzerosNoCopy(index_type r, size_type &size,
108  size_type &stride,
109  index_type *&cols, T *&vals);
110 
111  virtual DenseColMajMatrixGeneric* make_transpose() const;
112  virtual void mult(const ColumnMatrix& x, ColumnMatrix& b,
113  index_type beg=-1, index_type end=-1,
114  int spVec=0) const;
115  virtual void mult_transpose(const ColumnMatrix& x, ColumnMatrix& b,
116  index_type beg=-1, index_type end=-1,
117  int spVec=0) const;
119  index_type r2, index_type c2);
120 
121 
122  T sumOfCol(size_type);
123  T sumOfRow(size_type);
124 
125 
126  /// fast accessors
127  inline T &iget(index_type r, index_type c)
128  {
129  return dataptr_[c * this->nrows_ + r];
130  }
131 
132  /// fast accessors
133  inline const T &iget(index_type r, index_type c) const
134  {
135  return dataptr_[c * this->nrows_ + r];
136  }
137 
139  {
140  return iget(r, c);
141  }
142 
143  inline const T& operator()(index_type r, index_type c) const
144  {
145  return iget(r, c);
146  }
147 
148  /// Throws an assertion if not square
149  T determinant();
150 
152 
153  virtual void print(std::string&) const;
154 
155  /// Persistent representation...
156  virtual void io(Piostream&);
158 
159 private:
160  T* dataptr_;
161 };
162 
163 namespace
164 {
165  Persistent* DenseColMajMatrixMaker()
166  {
167  return new DenseColMajMatrix;
168  }
169 }
170 
171 template <typename T>
172 PersistentTypeID DenseColMajMatrixGeneric<T>::type_id("DenseColMajMatrix", "Matrix", DenseColMajMatrixMaker);
173 
174 template <typename T>
175 DenseColMajMatrixGeneric<T>*
177 {
178  return new DenseColMajMatrixGeneric(*this);
179 }
180 
181 template <typename T>
182 int
184 {
185  int sum = 0;
186  sum += SCIRun::compute_checksum(dataptr_, this->nrows_ * this->ncols_);
187  return (sum);
188 }
189 
190 /// constructors
191 template <typename T>
193 dataptr_(0)
194 {
195  DEBUG_CONSTRUCTOR("DenseColMajMatrix")
196 }
197 
198 template <typename T>
200 Matrix<T>(r, c)
201 {
202  DEBUG_CONSTRUCTOR("DenseColMajMatrix")
203 
204  dataptr_ = new double[this->nrows_ * this->ncols_];
205 }
206 
207 template <typename T>
209 Matrix<T>(m.nrows_, m.ncols_)
210 {
211  DEBUG_CONSTRUCTOR("DenseColMajMatrix")
212 
213  dataptr_ = new double[this->nrows_ * this->ncols_];
214  memcpy(dataptr_, m.dataptr_, sizeof(double) * this->nrows_ * this->ncols_);
215 }
216 
217 template <typename T>
218 T*
220 {
221  return dataptr_;
222 }
223 
224 template <typename T>
225 size_type
227 {
228  return this->nrows() * this->ncols();
229 }
230 
231 
232 /// destructor
233 template <typename T>
235 {
236  DEBUG_DESTRUCTOR("DenseColMajMatrix")
237 
238  delete[] dataptr_;
239 }
240 
241 
242 /// assignment operator
243 template <typename T>
246 {
247  delete[] dataptr_;
248 
249  this->nrows_ = m.nrows_;
250  this->ncols_ = m.ncols_;
251  dataptr_ = new double[this->nrows_ * this->ncols_];
252  memcpy(dataptr_, m.dataptr_, sizeof(double) * this->nrows_ * this->ncols_);
253 
254  return *this;
255 }
256 
257 template <typename T>
258 T
260 {
261  ASSERTRANGE(r, 0, this->nrows_);
262  ASSERTRANGE(c, 0, this->ncols_);
263  return iget(r, c);
264 }
265 
266 template <typename T>
267 void
269 {
270  ASSERTRANGE(r, 0, this->nrows_);
271  ASSERTRANGE(c, 0, this->ncols_);
272  iget(r, c) = d;
273 }
274 
275 template <typename T>
276 void
278 {
279  ASSERTRANGE(r, 0, this->nrows_);
280  ASSERTRANGE(c, 0, this->ncols_);
281  iget(r, c) += d;
282 }
283 
284 template <typename T>
285 T
287 {
288  T min = DBL_MAX;
289  for (index_type k=0; k<this->nrows_*this->ncols_; k++)
290  if (dataptr_[k] < min) min = dataptr_[k];
291  return (min);
292 }
293 
294 template <typename T>
295 T
297 {
298  T max = -DBL_MAX;
299  for (index_type k=0; k<this->nrows_*this->ncols_; k++)
300  if (dataptr_[k] > max) max = dataptr_[k];
301  return (max);
302 }
303 
304 
305 template <typename T>
308 {
309  DenseColMajMatrixGeneric<T>* m = new DenseColMajMatrixGeneric(this->ncols_, this->nrows_);
310  for (index_type c=0; c<this->ncols_; c++)
311  for (index_type r=0; r<this->nrows_; r++)
312  m->iget(c, r) = iget(r, c);
313  return m;
314 }
315 
316 template <typename T>
317 void
319  index_type &stride,
320  index_type *&cols, T *&vals)
321 {
322  size = this->ncols_;
323  stride = this->nrows_;
324  cols = 0;
325  vals = dataptr_ + r;
326 }
327 
328 template <typename T>
329 void
331 {
332  memset(dataptr_, 0, sizeof(double) * this->nrows_ * this->ncols_);
333 }
334 
335 template <typename T>
338 {
339  DenseColMajMatrixGeneric<T>* result = new DenseColMajMatrixGeneric(size, size);
340  result->zero();
341  for (index_type i = 0; i < size; i++)
342  {
343  result->iget(i, i) = 1.0;
344  }
345 
346  return result;
347 }
348 
349 template <typename T>
350 void
351 DenseColMajMatrixGeneric<T>::print(std::string& str) const
352 {
353  std::ostringstream oss;
354  for (index_type i=0; i<this->nrows_; i++)
355  {
356  for (index_type j=0; j<this->ncols_; j++)
357  {
358  oss << iget(i, j) << " ";
359  }
360  oss << std::endl;
361  }
362  str.assign(oss.str());
363 }
364 
365 template <typename T>
368  index_type r2, index_type c2)
369 {
370  ASSERTRANGE(r1, 0, r2+1);
371  ASSERTRANGE(r2, r1, this->nrows_);
372  ASSERTRANGE(c1, 0, c2+1);
373  ASSERTRANGE(c2, c1, this->ncols_);
374  DenseColMajMatrixGeneric *mat = new DenseColMajMatrixGeneric(r2 - r1 + 1, c2 - c1 + 1);
375  for (index_type i = c1; i <= c2; i++)
376  {
377  /// @todo: Test this.
378  memcpy(mat->dataptr_ + (i - c1) * (r2 - r1 + 1),
379  dataptr_ + c1 * this->nrows_ + r1,
380  (r2 - r1 + 1) * sizeof(double));
381  }
382  return mat;
383 }
384 
385 #define DENSECOLMAJMATRIX_VERSION 3
386 
387 template <typename T>
388 void
390 {
391  int version=stream.begin_class("DenseColMajMatrix", DENSECOLMAJMATRIX_VERSION);
392 
393  // Do the base class first.
394  Matrix<T>::io(stream);
395 
396  if (version < 4)
397  {
398  int nrows = static_cast<int>(this->nrows_);
399  int ncols = static_cast<int>(this->ncols_);
400  stream.io(nrows);
401  stream.io(ncols);
402  this->nrows_ = static_cast<size_type>(nrows);
403  this->ncols_ = static_cast<size_type>(ncols);
404  }
405  else
406  {
407  long long nrows = static_cast<long long>(this->nrows_);
408  long long ncols = static_cast<long long>(this->ncols_);
409  stream.io(nrows);
410  stream.io(ncols);
411  this->nrows_ = static_cast<size_type>(nrows);
412  this->ncols_ = static_cast<size_type>(ncols);
413  }
414 
415  if (stream.reading())
416  {
417  dataptr_ = new T[this->nrows_ * this->ncols_];
418  }
419  stream.begin_cheap_delim();
420 
421  int split;
422  if (stream.reading())
423  {
424  if (version > 2)
425  {
426  Pio(stream, this->separate_raw_);
427  if (this->separate_raw_)
428  {
429  Pio(stream, this->raw_filename_);
430  FILE *f = fopen(this->raw_filename_.c_str(), "r");
431  if (f)
432  {
433  fread(dataptr_, sizeof(T), this->nrows_ * this->ncols_, f);
434  fclose(f);
435  }
436  else
437  {
438  const std::string errmsg = "Error reading separated file '" +
439  this->raw_filename_ + "'";
440  std::cerr << errmsg << "\n";
441  throw FileNotFound(errmsg, __FILE__, __LINE__);
442  }
443  }
444  }
445  else
446  {
447  this->separate_raw_ = false;
448  }
449  split = this->separate_raw_;
450  }
451  else
452  { // writing
453  std::string filename = this->raw_filename_;
454  split = this->separate_raw_;
455  if (split)
456  {
457  if (filename == "")
458  {
459  if (stream.file_name.c_str())
460  {
461  size_t pos = stream.file_name.rfind('.');
462  if (pos == std::string::npos) filename = stream.file_name + ".raw";
463  else filename = stream.file_name.substr(0,pos) + ".raw";
464  }
465  else
466  {
467  split=0;
468  }
469  }
470  }
471  Pio(stream, split);
472  if (split)
473  {
474  Pio(stream, filename);
475  FILE *f = fopen(filename.c_str(), "w");
476  fwrite(dataptr_, sizeof(T), this->nrows_ * this->ncols_, f);
477  fclose(f);
478  }
479  }
480 
481  if (!split)
482  {
483  size_t block_size = this->nrows_ * this->ncols_;
484  if (!stream.block_io(dataptr_, sizeof(T), block_size))
485  {
486  for (size_t i = 0; i < block_size; i++)
487  {
488  stream.io(dataptr_[i]);
489  }
490  }
491  }
492  stream.end_cheap_delim();
493  stream.end_class();
494 }
495 
496 
497 } // End namespace SCIRun
498 
499 #endif
virtual SparseRowMatrix * sparse()
Definition: MatrixTypeConverter.h:122
virtual DenseColMajMatrixGeneric * clone() const
Public member functions.
Definition: DenseColMajMatrix.h:176
#define DENSECOLMAJMATRIX_VERSION
Definition: DenseColMajMatrix.h:385
virtual bool block_io(void *, size_t, size_t)
Definition: Persistent.h:174
bool reading() const
Definition: Persistent.h:164
Definition: DenseMatrix.h:68
LockingHandle< Matrix< double > > MatrixHandle
Definition: MatrixFwd.h:55
virtual ColumnMatrix * column()
Definition: MatrixTypeConverter.h:112
virtual MatrixHandle submatrix(index_type r1, index_type c1, index_type r2, index_type c2)
Definition: DenseColMajMatrix.h:367
const T & operator()(index_type r, index_type c) const
Definition: DenseColMajMatrix.h:143
Definition: DenseColMajMatrix.h:73
virtual void io(bool &)
Definition: Persistent.cc:193
Definition: Persistent.h:89
DenseColMajMatrixGeneric & operator=(const DenseColMajMatrixGeneric &)
assignment operator
Definition: DenseColMajMatrix.h:245
virtual void zero()
slow setters/getter for polymorphic operations
Definition: DenseColMajMatrix.h:330
const T & iget(index_type r, index_type c) const
fast accessors
Definition: DenseColMajMatrix.h:133
int compute_checksum(T *data, std::size_t length)
Definition: CheckSum.h:38
DenseColMajMatrixGeneric()
Constructors.
Definition: DenseColMajMatrix.h:192
Definition: Persistent.h:187
static DenseColMajMatrixGeneric * identity(size_type size)
Definition: DenseColMajMatrix.h:337
virtual T min()
Definition: DenseColMajMatrix.h:286
static PersistentTypeID type_id
Definition: DenseColMajMatrix.h:157
virtual void begin_cheap_delim()
Definition: Persistent.cc:183
Definition: Matrix.h:104
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 & iget(index_type r, index_type c)
fast accessors
Definition: DenseColMajMatrix.h:127
size_type ncols_
Definition: Matrix.h:113
virtual void add(index_type r, index_type c, T val)
Definition: DenseColMajMatrix.h:277
Definition: ParallelLinearAlgebraTests.cc:358
virtual void getRowNonzerosNoCopy(index_type r, size_type &size, size_type &stride, index_type *&cols, T *&vals)
Definition: DenseColMajMatrix.h:318
Generic exception for internal errors.
long long size_type
Definition: Types.h:40
T * end() const
Definition: Matrix.h:141
virtual void print(std::string &) const
Definition: DenseColMajMatrix.h:351
Definition: ColumnMatrix.h:55
virtual T * get_data_pointer() const
Definition: DenseColMajMatrix.h:219
T determinant()
Throws an assertion if not square.
size_type nrows_
Definition: Matrix.h:112
virtual size_type get_data_size() const
Definition: DenseColMajMatrix.h:226
virtual void end_cheap_delim()
Definition: Persistent.cc:188
DenseColMajMatrixGeneric< double > DenseColMajMatrix
Definition: MatrixFwd.h:52
virtual int compute_checksum()
Definition: DenseColMajMatrix.h:183
virtual DenseMatrix * dense()
Definition: MatrixTypeConverter.h:101
virtual ~DenseColMajMatrixGeneric()
Destructor.
Definition: DenseColMajMatrix.h:234
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
virtual DenseColMajMatrixGeneric * dense_col_maj()
Definition: MatrixTypeConverter.h:151
virtual void end_class()
Definition: Persistent.cc:178
virtual void io(Piostream &)
Persistent representation...
Definition: DenseColMajMatrix.h:389
virtual DenseColMajMatrixGeneric * make_transpose() const
Definition: DenseColMajMatrix.h:307
T & operator()(index_type r, index_type c)
Definition: DenseColMajMatrix.h:138
long long index_type
Definition: Types.h:39
virtual void mult(const ColumnMatrix &x, ColumnMatrix &b, index_type beg=-1, index_type end=-1, int spVec=0) const
Definition: DenseColMajMatrixMultiplication.h:50
virtual void io(Piostream &)
Definition: Matrix.cc:58
virtual void put(index_type r, index_type c, T val)
Definition: DenseColMajMatrix.h:268
Definition: Persistent.h:64
boost::error_info< struct tag_file_not_found, std::string > FileNotFound
Definition: Exception.h:54
virtual void mult_transpose(const ColumnMatrix &x, ColumnMatrix &b, index_type beg=-1, index_type end=-1, int spVec=0) const
Definition: DenseColMajMatrixMultiplication.h:90
std::string file_name
Definition: Persistent.h:135
virtual T get(index_type r, index_type c) const
Definition: DenseColMajMatrix.h:259
#define DEBUG_CONSTRUCTOR(type)
Definition: Debug.h:64
virtual T max()
Definition: DenseColMajMatrix.h:296
Definition: MatrixFwd.h:40
int size
Definition: eabLatVolData.py:2
#define DEBUG_DESTRUCTOR(type)
Definition: Debug.h:65