SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ColumnMatrixFunctions.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 ColumnMatrixFunctions.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_COLUMNMATRIXFUNCTIONS_H
42 #define CORE_DATATYPES_COLUMNMATRIXFUNCTIONS_H
43 
44 #include <Core/Datatypes/ColumnMatrix.h>
45 #include <Core/Util/Assert.h>
46 #include <Core/Datatypes/share.h>
47 
48 namespace SCIRun {
49 
50  namespace Private
51  {
52  SCISHARE double linalg_norm2(size_type n, const double* data);
53  SCISHARE void linalg_mult(size_type n, double* result, double* a, double* b);
54  SCISHARE void linalg_sub(size_type n, double* result, double* a, double* b);
55  SCISHARE void linalg_add(size_type n, double* result, double* a, double* b);
56  SCISHARE double linalg_dot(size_type n, double* a, double* b);
57  SCISHARE void linalg_smadd(size_type n, double* result, double s, double* a, double* b);
58  }
59 
60  template <typename T>
62  {
63  return Private::linalg_norm2(this->nrows_, data_);
64  }
65 
66  template <typename T>
68  {
69  ASSERTRANGE(end, 0, this->nrows_+1);
70  ASSERTRANGE(beg, 0, end);
71  return Private::linalg_norm2((end-beg), data_+beg);
72  }
73 
75  {
76  double operator()(double x) const
77  {
78  return std::fabs(x);
79  }
80  };
81 
82  template <typename T>
84  {
85  std::vector<T> absoluteValues(this->nrows());
86  std::transform(this->begin(), this->end(), absoluteValues.begin(), AbsoluteValue());
87  return *std::max_element(absoluteValues.begin(), absoluteValues.end());
88  }
89 
90  template <typename T>
91  void
93  {
94  ASSERTEQ(result.nrows(), a.nrows());
95  ASSERTEQ(result.nrows(), b.nrows());
97  }
98 
99  template <typename T>
100  void
102  index_type beg, index_type end)
103  {
104  ASSERTEQ(result.nrows(), a.nrows());
105  ASSERTEQ(result.nrows(), b.nrows());
106  ASSERTRANGE(end, 0, result.nrows()+1);
107  ASSERTRANGE(beg, 0, end);
108  Private::linalg_mult(end-beg, result.get_data_pointer()+beg, a.get_data_pointer()+beg, b.get_data_pointer()+beg);
109  }
110 
111  template <typename T>
112  void
114  {
115  ASSERTEQ(result.nrows(), a.nrows());
116  ASSERTEQ(result.nrows(), b.nrows());
118  }
119 
120  template <typename T>
121  void
123  index_type beg, index_type end)
124  {
125  ASSERTEQ(result.nrows(), a.nrows());
126  ASSERTEQ(result.nrows(), b.nrows());
127  Private::linalg_sub(end-beg, result.get_data_pointer()+beg, a.get_data_pointer()+beg, b.get_data_pointer()+beg);
128  }
129 
130  template <typename T>
131  void
132  ScMult_Add(ColumnMatrix& result, T s, const ColumnMatrix& a,
133  const ColumnMatrix& b)
134  {
135  ASSERTEQ(result.nrows(), a.nrows());
136  ASSERTEQ(result.nrows(), b.nrows());
138  }
139 
140  template <typename T>
141  void
142  ScMult_Add(ColumnMatrix& result, T s, const ColumnMatrix& a,
143  const ColumnMatrix& b,
144  index_type beg, index_type end)
145  {
146  ASSERTEQ(result.nrows(), a.nrows());
147  ASSERTEQ(result.nrows(), b.nrows());
148  ASSERTRANGE(end, 0, result.nrows()+1);
149  ASSERTRANGE(beg, 0, end);
150  Private::linalg_smadd(end-beg, result.get_data_pointer()+beg, s, a.get_data_pointer()+beg, b.get_data_pointer()+beg);
151  }
152 
153  template <typename T>
154  T
156  {
157  ASSERTEQ(a.nrows(), b.nrows());
159  }
160 
161  template <typename T>
162  T
164  index_type beg, index_type end)
165  {
166  ASSERTEQ(a.nrows(), b.nrows());
167  ASSERTRANGE(end, 0, a.nrows()+1);
168  ASSERTRANGE(beg, 0, end);
169  return Private::linalg_dot((end-beg), a.get_data_pointer()+beg, b.get_data_pointer()+beg);
170  }
171 
172  template <typename T>
173  void
175  {
176  ASSERTEQ(out.nrows(), in.nrows());
177  for(index_type i=0; i<out.nrows(); i++)
178  {
179  out[i] = in[i];
180  }
181  }
182 
183  template <typename T>
184  void
186  index_type beg, index_type end)
187  {
188  ASSERTEQ(out.nrows(), in.nrows());
189  ASSERTRANGE(end, 0, out.nrows()+1);
190  ASSERTRANGE(beg, 0, end);
191  for(index_type i=beg;i<end;i++)
192  {
193  out[i]=in[i];
194  }
195  }
196 
197  template <typename T>
198  void
200  T s, const ColumnMatrixGeneric<T>& b)
201  {
202  ASSERTEQ(result.nrows(), a.nrows());
203  ASSERTEQ(result.nrows(), b.nrows());
205  }
206 
207  template <typename T>
208  void
210  {
211  ASSERTEQ(result.nrows(), a.nrows());
212  ASSERTEQ(result.nrows(), b.nrows());
214  }
215 
216  template <typename T>
218  const ColumnMatrixGeneric<T>& c)
219  {
220  ASSERTEQ(result.nrows(), a.nrows());
221  ASSERTEQ(result.nrows(), b.nrows());
222  ASSERTEQ(result.nrows(), c.nrows());
223  for(index_type i=0;i<result.nrows();i++)
224  {
225  result[i]=a[i]+b[i]+c[i];
226  }
227  }
228 
229  template <typename T>
231  {
232  ASSERTEQ(result.nrows(), a.nrows());
233  for(index_type i=0; i<result.nrows(); i++)
234  result[i] = a[i]*s;
235  }
236 
237 
238 
239 
240 } // End namespace SCIRun
241 
242 #endif
void Sub(ColumnMatrixGeneric< T > &result, const ColumnMatrixGeneric< T > &a, const ColumnMatrixGeneric< T > &b)
Definition: ColumnMatrixFunctions.h:113
#define ASSERTEQ(c1, c2)
Definition: Assert.h:98
void linalg_add(size_type rows, double *res, double *a, double *b)
Definition: ColumnMatrix.cc:96
T Dot(const ColumnMatrixGeneric< T > &a, const ColumnMatrixGeneric< T > &b)
Definition: ColumnMatrixFunctions.h:155
void Mult(ColumnMatrixGeneric< T > &result, const ColumnMatrixGeneric< T > &a, const ColumnMatrixGeneric< T > &b)
Definition: ColumnMatrixFunctions.h:92
#define SCISHARE
Definition: share.h:39
void linalg_mult(size_type rows, double *res, double *a, double *b)
Definition: ColumnMatrix.cc:78
T infinity_norm() const
Definition: ColumnMatrixFunctions.h:83
#define ASSERTRANGE(c, l, h)
Definition: Assert.h:99
void ScMult_Add(ColumnMatrix &result, T s, const ColumnMatrix &a, const ColumnMatrix &b)
Definition: ColumnMatrixFunctions.h:132
long long size_type
Definition: Types.h:40
Definition: ColumnMatrixFunctions.h:74
dictionary data
Definition: eabLatVolData.py:11
Definition: ColumnMatrix.h:55
void AddScMult(ColumnMatrixGeneric< T > &result, const ColumnMatrixGeneric< T > &a, T s, const ColumnMatrixGeneric< T > &b)
Definition: ColumnMatrixFunctions.h:199
double linalg_norm2(size_type rows, const double *data)
Definition: ColumnMatrix.cc:49
virtual T * get_data_pointer() const
Definition: ColumnMatrix.h:200
double linalg_dot(size_type rows, double *a, double *b)
Definition: ColumnMatrix.cc:105
void linalg_sub(size_type rows, double *res, double *a, double *b)
Definition: ColumnMatrix.cc:87
void Copy(ColumnMatrixGeneric< T > &out, const ColumnMatrixGeneric< T > &in)
Definition: ColumnMatrixFunctions.h:174
long long index_type
Definition: Types.h:39
T vector_norm() const
Definition: ColumnMatrixFunctions.h:61
void linalg_smadd(size_type rows, double *res, double s, double *a, double *b)
Definition: ColumnMatrix.cc:137
int n
Definition: eab.py:9
void Add(ColumnMatrixGeneric< T > &result, const ColumnMatrixGeneric< T > &a, const ColumnMatrixGeneric< T > &b)
Definition: ColumnMatrixFunctions.h:209
double operator()(double x) const
Definition: ColumnMatrixFunctions.h:76
size_type nrows() const
Definition: Matrix.h:132