SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Matrix.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 ///
32 ///@file Matrix.h
33 ///@brief Matrix definitions
34 ///
35 ///@author
36 /// Steven G. Parker
37 /// Department of Computer Science
38 /// University of Utah
39 ///@date July 1994
40 ///
41 
42 #ifndef CORE_DATATYPES_MATRIX_H
43 #define CORE_DATATYPES_MATRIX_H 1
44 
45 #include <Core/Util/CheckSum.h>
46 #include <Core/Util/Debug.h>
47 #include <Core/Util/StringUtil.h>
48 #include <Core/Containers/LockingHandle.h>
49 #include <Core/Geometry/Transform.h>
50 #include <Core/Datatypes/Types.h>
51 #include <Core/Datatypes/PropertyManager.h>
53 
54 #include <string.h>
55 #include <iosfwd>
56 
57 #include <Core/Datatypes/share.h>
58 
59 namespace SCIRun {
60 
61 template <typename T>
63 {
64 public:
65  virtual ~MatrixInterface() {}
66 
67  //Too slow to be virtual. Consider implementing on each subclass though, to enable generic algorithms.
68  //virtual T& operator()(index_type i, index_type j) = 0;
69  //virtual const T& operator()(index_type i, index_type j) const = 0;
70  virtual size_type nrows() const = 0;
71  virtual size_type ncols() const = 0;
72  virtual void zero() = 0;
73  /// @todo
74  //virtual MatrixInterface<T>* make_transpose() const = 0;
75 };
76 
78 {
79 public:
80  MatrixBase() : separate_raw_(false), raw_filename_("") {}
81  // Separate raw files.
82  void set_raw(bool v) { separate_raw_ = v; }
83  bool get_raw() const { return separate_raw_; }
84  void set_raw_filename( const std::string &f )
85  {
86  raw_filename_ = f;
87  separate_raw_ = true;
88  }
89  const std::string get_raw_filename() const { return raw_filename_; }
90 
91  virtual void print(std::string&) const {}
92 
93  // Persistent representation.
94  virtual std::string dynamic_type_name() const { return "MatrixBase"; }
95  virtual void io(Piostream&);
97 
98 protected:
100  std::string raw_filename_;
101 };
102 
103 template <typename T>
104 class Matrix : public MatrixBase, public MatrixInterface<T>
105 {
106 protected:
109  {
110  DEBUG_CONSTRUCTOR("Matrix")
111  }
114 
115 public:
116  virtual ~Matrix()
117  {
118  DEBUG_DESTRUCTOR("Matrix")
119  }
120 
121  typedef T element_type;
122 
123  /// Make a duplicate, needed to support detach from LockingHandle
124  virtual Matrix* clone() const = 0;
125 
126  /// Convert this matrix to the specified type.
127  virtual DenseMatrix* dense() = 0;
128  virtual SparseRowMatrixGeneric<T>* sparse() = 0;
129  virtual ColumnMatrixGeneric<T>* column() = 0;
131 
132  inline size_type nrows() const { return nrows_; }
133  inline size_type ncols() const { return ncols_; }
134  /// return false if not invertible.
135  virtual inline bool invert() { return false; }
136 
137  virtual T *get_data_pointer() const = 0;
138  virtual size_type get_data_size() const = 0;
139 
140  inline T* begin() const { return get_data_pointer(); }
141  inline T* end() const { return get_data_pointer() + get_data_size(); }
142 
143  virtual void zero() = 0;
144  virtual T get(index_type r, index_type c) const = 0;
145  virtual void put(index_type r, index_type c, T val) = 0;
146  virtual void add(index_type r, index_type c, T val) = 0;
147 
148  virtual T min() = 0;
149  virtual T max() = 0;
150  virtual int compute_checksum() = 0;
152  {
153  return std::count_if(this->begin(), this->end(), std::bind1st(std::not_equal_to<T>(), 0));
154  }
155  virtual bool is_zero() const
156  {
157  return std::find_if(this->begin(), this->end(), std::bind1st(std::not_equal_to<T>(), 0)) == this->end();
158  }
159 
160  // getRowNonzerosNocopy returns:
161  // vals = The values. They are not guaranteed
162  // to be nonzero, but some of the zeros may be missing.
163  // cols = The columns associated with the vals. This may be NULL, in
164  // which case the cols are 0-(size-1) (a full row).
165  // size = The number of entries in vals.
166  // stride = The matrix may not be in row order, so this is how far
167  // to walk in vals. For example vals (and cols) should be
168  // accessed as vals[3*stride] to get the fourth value. As of this
169  // time all the matrices return 1 for this value.
170  // For example usage see Dataflow/Modules/Fields/ApplyMappingMatrix.h
172  index_type &stride,
173  index_type *&cols, T *&vals) = 0;
174 
175  virtual Matrix<T>* make_transpose() const = 0;
176  virtual void mult(const ColumnMatrix& x, ColumnMatrix& b,
177  index_type beg=-1, index_type end=-1, int spVec=0) const=0;
178  virtual void mult_transpose(const ColumnMatrix& x, ColumnMatrix& b,
179  index_type beg=-1, index_type end=-1, int spVec=0) const=0;
181  index_type r2, index_type c2) = 0;
182 
183  virtual std::string dynamic_type_name() const { return type_id.type; }
184 
185  virtual void scalar_multiply(T s)
186  {
187  for (T* p = begin(); p != end(); ++p)
188  {
189  *p *= s;
190  }
191  }
192 
194 };
195 
196 template<typename T>
197 PersistentTypeID Matrix<T>::type_id("Matrix", "MatrixBase", 0);
198 
199 #ifdef _WIN32
200 template<>
201 PersistentTypeID Matrix<double>::type_id("Matrix", "MatrixBase", 0);
202 #endif
203 
204 SCISHARE void Mult(ColumnMatrix&, const Matrix<double>&, const ColumnMatrix&);
205 
206 inline std::string matrix_to_string(const MatrixBase& mat)
207 {
208  std::string str;
209  mat.print(str);
210  return (str);
211 }
212 
213 inline std::string to_string(const MatrixHandle& mat)
214 {
215  return matrix_to_string(*mat);
216 }
217 
218 } // End namespace SCIRun
219 
220 #endif
virtual std::string dynamic_type_name() const
Definition: Matrix.h:94
Definition: DenseMatrix.h:68
LockingHandle< Matrix< double > > MatrixHandle
Definition: MatrixFwd.h:55
std::string to_string(const MatrixHandle &mat)
Definition: Matrix.h:213
Definition: DenseColMajMatrix.h:73
virtual DenseMatrix * dense()=0
Convert this matrix to the specified type.
Definition: Persistent.h:89
virtual void mult(const ColumnMatrix &x, ColumnMatrix &b, index_type beg=-1, index_type end=-1, int spVec=0) const =0
void set_raw(bool v)
Definition: Matrix.h:82
void Mult(ColumnMatrixGeneric< T > &result, const ColumnMatrixGeneric< T > &a, const ColumnMatrixGeneric< T > &b)
Definition: ColumnMatrixFunctions.h:92
#define SCISHARE
Definition: share.h:39
Definition: Matrix.h:104
virtual void zero()=0
MatrixBase()
Definition: Matrix.h:80
void set_raw_filename(const std::string &f)
Definition: Matrix.h:84
virtual void scalar_multiply(T s)
Definition: Matrix.h:185
virtual void print(std::string &) const
Definition: Matrix.h:91
virtual Matrix< T > * make_transpose() const =0
virtual T min()=0
std::string raw_filename_
Definition: Matrix.h:100
size_type ncols_
Definition: Matrix.h:113
virtual std::string dynamic_type_name() const
Definition: Matrix.h:183
const std::string get_raw_filename() const
Definition: Matrix.h:89
long long size_type
Definition: Types.h:40
T * end() const
Definition: Matrix.h:141
virtual ~Matrix()
Definition: Matrix.h:116
bool get_raw() const
Definition: Matrix.h:83
Definition: ColumnMatrix.h:55
Definition: Matrix.h:62
virtual DenseColMajMatrixGeneric< T > * dense_col_maj()=0
std::string type
Definition: Persistent.h:72
size_type nrows_
Definition: Matrix.h:112
virtual size_type get_data_size() const =0
static PersistentTypeID type_id
Definition: Matrix.h:193
T * begin() const
Definition: Matrix.h:140
virtual void getRowNonzerosNoCopy(index_type r, index_type &size, index_type &stride, index_type *&cols, T *&vals)=0
virtual bool is_zero() const
Definition: Matrix.h:155
bool separate_raw_
Definition: Matrix.h:99
v
Definition: readAllFields.py:42
virtual int compute_checksum()=0
static PersistentTypeID type_id
Definition: Matrix.h:96
size_type number_of_nonzero_elements() const
Definition: Matrix.h:151
virtual bool invert()
return false if not invertible.
Definition: Matrix.h:135
long long index_type
Definition: Types.h:39
T element_type
Definition: Matrix.h:121
size_type ncols() const
Definition: Matrix.h:133
virtual ColumnMatrixGeneric< T > * column()=0
virtual ~MatrixInterface()
Definition: Matrix.h:65
Definition: Matrix.h:77
Matrix(size_type nrows=0, size_type ncols=0)
Definition: Matrix.h:107
Definition: Persistent.h:64
virtual T max()=0
virtual SparseRowMatrixGeneric< T > * sparse()=0
virtual T * get_data_pointer() const =0
#define DEBUG_CONSTRUCTOR(type)
Definition: Debug.h:64
Definition: PropertyManager.h:193
virtual Matrix * clone() const =0
Make a duplicate, needed to support detach from LockingHandle.
size_type nrows() const
Definition: Matrix.h:132
virtual void add(index_type r, index_type c, T val)=0
std::string matrix_to_string(const MatrixBase &mat)
Definition: Matrix.h:206
virtual void mult_transpose(const ColumnMatrix &x, ColumnMatrix &b, index_type beg=-1, index_type end=-1, int spVec=0) const =0
Definition: MatrixFwd.h:40
virtual MatrixHandle submatrix(index_type r1, index_type c1, index_type r2, index_type c2)=0
int size
Definition: eabLatVolData.py:2
virtual void put(index_type r, index_type c, T val)=0
#define DEBUG_DESTRUCTOR(type)
Definition: Debug.h:65