SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ColumnMatrix.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 ///
31 ///@file ColumnMatrix.h
32 ///@brief for RHS and LHS
33 ///
34 ///@author
35 /// Steven G. Parker
36 /// Department of Computer Science
37 /// University of Utah
38 ///@date July 1994
39 ///
40 
41 #ifndef CORE_DATATYPES_COLUMNMATRIX_H
42 #define CORE_DATATYPES_COLUMNMATRIX_H 1
43 
44 #include <Core/Util/FancyAssert.h>
45 #include <Core/Datatypes/Matrix.h>
47 
48 #include <cfloat>
49 
50 #include <Core/Datatypes/share.h>
51 
52 namespace SCIRun {
53 
54 template <typename T>
55 class ColumnMatrixGeneric : public Matrix<T> {
56 
57 public:
58  explicit ColumnMatrixGeneric(size_type rows = 0);
60  /// @todo in C++11--move ctors for all matrix types.
61  //ColumnMatrixGeneric(ColumnMatrixGeneric&& move);
63  virtual ColumnMatrixGeneric* clone() const;
64  virtual ~ColumnMatrixGeneric();
65 
66  virtual DenseMatrix* dense();
68  virtual ColumnMatrixGeneric<T>* column();
70 
71  virtual T *get_data_pointer() const;
72  virtual size_type get_data_size() const;
73 
74  inline T& operator[](int r) const
75  {
76  ASSERTRANGE(r, 0, this->nrows_)
77  return data_[r];
78  }
79  /// @todo: remove
80  void set_data(T* d) {data_ = d;}
81 
82  T get(index_type r) const
83  { ASSERTRANGE(r, 0, this->nrows_); return data_[r]; };
84  void put(index_type r, T val)
85  { ASSERTRANGE(r, 0, this->nrows_); data_[r] = val; };
86 
87  // destroys data
88  void resize(size_type);
89 
90  virtual void zero();
91  virtual T get(index_type, index_type) const;
92  virtual void put(index_type row, index_type col, T val);
93  virtual void add(index_type row, index_type col, T val);
94 
95  virtual T& operator()(index_type i, index_type j);
96  virtual const T& operator()(index_type i, index_type j) const;
97 
98  virtual T min();
99  virtual T max();
100  virtual int compute_checksum();
101 
102  virtual void getRowNonzerosNoCopy(index_type r, size_type &size,
103  index_type &stride,
104  index_type *&cols, T *&vals);
105 
106  virtual Matrix<T>* make_transpose() const;
107  virtual void mult(const ColumnMatrix& x, ColumnMatrix& b,
108  index_type beg=-1, index_type end=-1,
109  int spVec=0) const;
110  virtual void mult_transpose(const ColumnMatrix& x, ColumnMatrix& b,
111  index_type beg=-1, index_type end=-1,
112  int spVec=0) const;
113  virtual void scalar_multiply(T s);
115  index_type r2, index_type c2);
116 
117  T sumOfCol(index_type);
118 
120  T vector_norm() const;
121  T vector_norm(index_type beg, index_type end) const;
122 
123  T infinity_norm() const;
124 
125  virtual void print(std::string&) const;
126 
127  // Persistent representation...
128  virtual std::string dynamic_type_name() const { return type_id.type; }
129  virtual void io(Piostream&);
131 
132 private:
133  T* data_;
134 };
135 
136 
137 static Persistent* ColumnMatrixMaker()
138 {
139  return new ColumnMatrix(0);
140 }
141 
142 template <typename T>
143 PersistentTypeID ColumnMatrixGeneric<T>::type_id("ColumnMatrix", "Matrix", ColumnMatrixMaker);
144 
145 template <typename T>
146 int
148 {
149  int sum = 0;
150  sum += SCIRun::compute_checksum(data_,this->nrows_);
151  return (sum);
152 }
153 
154 template <typename T>
156 Matrix<double>(rows, 1)
157 {
158  DEBUG_CONSTRUCTOR("ColumnMatrix")
159 
160  if (this->nrows_)
161  data_ = new double[this->nrows_];
162  else
163  data_ = 0;
164 }
165 
166 template <typename T>
168 Matrix<double>(c.nrows_, 1)
169 {
170  DEBUG_CONSTRUCTOR("ColumnMatrix")
171 
172  if (this->nrows_)
173  {
174  data_ = new double[this->nrows_];
175  for (index_type i = 0; i < this->nrows_; i++)
176  data_[i] = c.data_[i];
177  }
178  else
179  {
180  data_ = 0;
181  }
182 }
183 
184 template <typename T>
186 {
187  ASSERT(j == 0);
188  return data_[i];
189 }
190 
191 template <typename T>
193 {
194  ASSERT(j == 0);
195  return data_[i];
196 }
197 
198 template <typename T>
199 T*
201 {
202  return data_;
203 }
204 
205 template <typename T>
206 size_type
208 {
209  return this->nrows();
210 }
211 
212 template <typename T>
213 Matrix<T>*
215 {
216  DenseMatrix *dm = new DenseMatrix(1, this->nrows_);
217  for (index_type i=0; i<this->nrows_; i++)
218  {
219  (*dm)[0][i] = data_[i];
220  }
221  return dm;
222 }
223 
224 template <typename T>
226 {
227  return new ColumnMatrixGeneric(*this);
228 }
229 
230 template <typename T>
232 {
233  if (this->nrows_ != c.nrows_) {
234  delete[] data_;
235  this->nrows_ = c.nrows_;
236  data_ = new T[this->nrows_];
237  }
238  for (index_type i = 0; i < this->nrows_; i++)
239  {
240  data_[i] = c.data_[i];
241  }
242  return *this;
243 }
244 
245 template <typename T>
246 T
248 {
249  T min = DBL_MAX;
250  for (index_type k = 0; k < this->nrows_; k++)
251  if (data_[k] < min)
252  min = data_[k];
253  return (min);
254 }
255 
256 template <typename T>
257 T
259 {
260  T max = -DBL_MAX;
261  for (index_type k = 0; k < this->nrows_; k++)
262  if (data_[k] > max)
263  max = data_[k];
264  return (max);
265 }
266 
267 template <typename T>
269 {
270  DEBUG_DESTRUCTOR("ColumnMatrix")
271  delete[] data_;
272 }
273 
274 template <typename T>
276 {
277  delete[] data_;
278  if (new_rows)
279  data_ = new T[new_rows];
280  else
281  data_ = 0;
282  this->nrows_ = new_rows;
283 }
284 
285 template <typename T>
287 {
288  std::fill(data_, data_ + this->nrows_, 0.0);
289 }
290 
291 
292 
293 template <typename T>
294 void
295 ColumnMatrixGeneric<T>::print(std::string& str) const
296 {
297  std::ostringstream oss;
298  for(index_type i=0; i<this->nrows_; i++)
299  {
300  oss << data_[i] << "\n";
301  }
302  str.assign(oss.str());
303 }
304 
305 template <typename T>
306 T
308 {
309  ASSERTRANGE(r, 0, this->nrows_);
310  ASSERTEQ(c, 0);
311  return data_[r];
312 }
313 
314 template <typename T>
315 void
317 {
318  ASSERTRANGE(r, 0, this->nrows_);
319  ASSERTEQ(c, 0);
320  data_[r] = d;
321 }
322 
323 template <typename T>
324 void
326 {
327  ASSERTRANGE(r, 0, this->nrows_);
328  ASSERTEQ(c, 0);
329  data_[r] += d;
330 }
331 
332 template <typename T>
333 T
335 {
336  ASSERTEQ(c, 0);
337  T sum = 0;
338  for (index_type i=0; i< this->nrows_; i++)
339  {
340  sum+=data_[i];
341  }
342  return sum;
343 }
344 
345 template <typename T>
346 void
348  index_type &stride,
349  index_type *&cols,
350  T *&vals)
351 {
352  size = 1;
353  stride = 1;
354  cols = 0;
355  vals = data_ + r;
356 }
357 
358 template <typename T>
359 void
361  index_type , index_type , int) const
362 {
363  ASSERTFAIL("Error - called mult on a columnmatrix.\n");
364 }
365 
366 template <typename T>
367 void
369  index_type, index_type, int) const
370 {
371  ASSERTFAIL("Error - called mult_transpose on a columnmatrix.\n");
372 }
373 
374 #define COLUMNMATRIX_VERSION 3
375 
376 template <typename T>
378 {
379  int version=stream.begin_class("ColumnMatrix", COLUMNMATRIX_VERSION);
380 
381  if (version > 1)
382  {
383  // New version inherits from Matrix
384  Matrix<T>::io(stream);
385  }
386 
387  if (version < 3)
388  {
389  int nrows = static_cast<int>(this->nrows_);
390  stream.io(nrows);
391  this->nrows_ = static_cast<size_type>(nrows);
392  }
393  else
394  {
395  long long nrows= static_cast<long long>(this->nrows_);
396  stream.io(nrows);
397  this->nrows_ = static_cast<size_type>(nrows);
398  }
399 
400  if (stream.reading())
401  {
402  data_ = new T[this->nrows_];
403  }
404 
405  if (!stream.block_io(data_, sizeof(T), this->nrows_))
406  {
407  for (index_type i=0; i<this->nrows_; i++)
408  stream.io(data_[i]);
409  }
410  stream.end_class();
411 }
412 
413 
414 /// @todo: replace with for_each
415 template <typename T>
416 void
418 {
419  for (index_type i=0; i < this->nrows_; i++)
420  {
421  data_[i] *= s;
422  }
423 }
424 
425 template <typename T>
428  index_type r2, index_type c2)
429 {
430  ASSERTRANGE(r1, 0, r2+1);
431  ASSERTRANGE(r2, r1, this->nrows_);
432  ASSERTEQ(c1, 0);
433  ASSERTEQ(c2, 0);
434 
435  ColumnMatrixGeneric<T> *mat = new ColumnMatrixGeneric<T>(r2 - r1 + 1);
436  memcpy(mat->data_, data_ + r1, (r2 - r1 + 1) * sizeof(T));
437 
438  return mat;
439 }
440 
441 template <typename T>
444 {
445  DenseMatrix ret(this->nrows_, this->nrows_);
446 
447  if (this->nrows_ != m.nrows())
448  {
449  ASSERTFAIL("Cannot compute exterior of two vectors of unequal dimensions.");
450  }
451  for (index_type i=0; i < this->nrows_; i++)
452  {
453  for (index_type j=0; j< this->nrows_; j++)
454  {
455  ret.put(i, j, get(j) * m.get(i));
456  }
457  }
458 
459  return ret;
460 }
461 
462 
463 } // End namespace SCIRun
464 
465 #endif
virtual DenseColMajMatrixGeneric< T > * dense_col_maj()
Definition: MatrixTypeConverter.h:253
virtual void put(index_type r, index_type c, T val)
Definition: DenseMatrix.h:423
virtual void getRowNonzerosNoCopy(index_type r, size_type &size, index_type &stride, index_type *&cols, T *&vals)
Definition: ColumnMatrix.h:347
virtual bool block_io(void *, size_t, size_t)
Definition: Persistent.h:174
bool reading() const
Definition: Persistent.h:164
Definition: DenseMatrix.h:68
static PersistentTypeID type_id
Definition: ColumnMatrix.h:130
LockingHandle< Matrix< double > > MatrixHandle
Definition: MatrixFwd.h:55
virtual void io(Piostream &)
Definition: ColumnMatrix.h:377
Definition: DenseColMajMatrix.h:73
virtual void io(bool &)
Definition: Persistent.cc:193
Definition: Persistent.h:89
virtual void print(std::string &) const
Definition: ColumnMatrix.h:295
virtual void mult_transpose(const ColumnMatrix &x, ColumnMatrix &b, index_type beg=-1, index_type end=-1, int spVec=0) const
Definition: ColumnMatrix.h:368
#define COLUMNMATRIX_VERSION
Definition: ColumnMatrix.h:374
virtual void scalar_multiply(T s)
Definition: ColumnMatrix.h:417
#define ASSERTEQ(c1, c2)
Definition: Assert.h:98
int compute_checksum(T *data, std::size_t length)
Definition: CheckSum.h:38
virtual Matrix< T > * make_transpose() const
Definition: ColumnMatrix.h:214
T sumOfCol(index_type)
Definition: ColumnMatrix.h:334
Definition: Persistent.h:187
T infinity_norm() const
Definition: ColumnMatrixFunctions.h:83
Definition: Matrix.h:104
#define ASSERT(condition)
Definition: Assert.h:110
virtual int begin_class(const std::string &name, int current_version)
Definition: Persistent.cc:143
#define ASSERTRANGE(c, l, h)
Definition: Assert.h:99
DenseMatrix exterior(const ColumnMatrixGeneric &) const
Definition: ColumnMatrix.h:443
virtual SparseRowMatrixGeneric< T > * sparse()
Definition: MatrixTypeConverter.h:265
ColumnMatrixGeneric< double > ColumnMatrix
Definition: MatrixFwd.h:48
virtual std::string dynamic_type_name() const
Definition: ColumnMatrix.h:128
Definition: ParallelLinearAlgebraTests.cc:358
virtual int compute_checksum()
Definition: ColumnMatrix.h:147
virtual T min()
Definition: ColumnMatrix.h:247
long long size_type
Definition: Types.h:40
T * end() const
Definition: Matrix.h:141
virtual ColumnMatrixGeneric< T > * column()
Definition: MatrixTypeConverter.h:234
Definition: ColumnMatrix.h:55
std::string type
Definition: Persistent.h:72
size_type nrows_
Definition: Matrix.h:112
virtual T * get_data_pointer() const
Definition: ColumnMatrix.h:200
ColumnMatrixGeneric & operator=(const ColumnMatrixGeneric &)
Definition: ColumnMatrix.h:231
T get(index_type r) const
Definition: ColumnMatrix.h:82
virtual DenseMatrix * dense()
Convert this matrix to the specified type.
Definition: MatrixTypeConverter.h:241
virtual ColumnMatrixGeneric * clone() const
Make a duplicate, needed to support detach from LockingHandle.
Definition: ColumnMatrix.h:225
#define ASSERTFAIL(string)
Definition: Assert.h:52
void resize(size_type)
Definition: ColumnMatrix.h:275
virtual T & operator()(index_type i, index_type j)
Definition: ColumnMatrix.h:185
virtual void zero()
Definition: ColumnMatrix.h:286
virtual void end_class()
Definition: Persistent.cc:178
virtual void add(index_type row, index_type col, T val)
Definition: ColumnMatrix.h:325
long long index_type
Definition: Types.h:39
T vector_norm() const
Definition: ColumnMatrixFunctions.h:61
virtual ~ColumnMatrixGeneric()
Definition: ColumnMatrix.h:268
virtual void io(Piostream &)
Definition: Matrix.cc:58
virtual void mult(const ColumnMatrix &x, ColumnMatrix &b, index_type beg=-1, index_type end=-1, int spVec=0) const
Definition: ColumnMatrix.h:360
virtual size_type get_data_size() const
Definition: ColumnMatrix.h:207
Definition: Persistent.h:64
void put(index_type r, T val)
Definition: ColumnMatrix.h:84
ColumnMatrixGeneric(size_type rows=0)
Definition: ColumnMatrix.h:155
virtual T max()
Definition: ColumnMatrix.h:258
T & operator[](int r) const
Definition: ColumnMatrix.h:74
#define DEBUG_CONSTRUCTOR(type)
Definition: Debug.h:64
virtual MatrixHandle submatrix(index_type r1, index_type c1, index_type r2, index_type c2)
Definition: ColumnMatrix.h:427
void set_data(T *d)
Definition: ColumnMatrix.h:80
DenseMatrixGeneric< double > DenseMatrix
Definition: MatrixFwd.h:44
size_type nrows() const
Definition: Matrix.h:132
Definition: MatrixFwd.h:40
int size
Definition: eabLatVolData.py:2
#define DEBUG_DESTRUCTOR(type)
Definition: Debug.h:65