VERB_code_2.3
Matrix.h
Go to the documentation of this file.
1 
11 #include <assert.h>
12 #include <string>
13 #include <string.h>
14 #include <fstream>
15 #include <memory.h>
16 #include <math.h>
17 #include <map>
18 
19 #ifndef matrix_array_MATRIX_H
20 #define matrix_array_MATRIX_H
21 
22 #include "./Interpolation/linear.h"
23 #include "./Interpolation/spline.h"
25 #include "./Interpolation/polint.h"
26 #include "./Interpolation/ratint.h"
27 #include "./Interpolation/akima.h"
28 
29 
30 
31 #include "../Exceptions/error.h"
32 
33 using namespace std;
34 
40 template <typename T>
41 class Matrix1D {
42 public:
44 
45  bool initialized;
46  int size_x;
47  string name;
48 
49  // constructors and destructors
50  Matrix1D() { initialized = false; };
51  Matrix1D( int x_size ); //CC changed size_x to x_size for consistency
52  Matrix1D( int x_size , string name);
53  Matrix1D( const Matrix1D<T> &M );
54  ~Matrix1D();
55 
56  void AllocateMemory( int x_size ); //CC changed size_x to x_size for consistency
57 
58  // Operators
59  inline T& operator[](int i);
60  inline T& operator[](int i) const;
61  inline T& operator()(int x);
62  inline T& Value (int x) { return operator()(x); }
63  inline Matrix1D<T>& MatrixArray () { return *this; }
64  inline T* MatrixArrayPointer () { return matrix_array; }
65 
66  // unary
67  inline const Matrix1D& operator+() const { return *this; }
68  inline const Matrix1D operator-() const { return ((*this)*(-1)); }
69 
70  // The following operators modify the matrix they applied to
71  inline Matrix1D& operator= (const Matrix1D<T> &M);
72  inline Matrix1D& operator= (const T val);
73 
74  // \todo Some of the matrix operators still need to be implemented
75  // I didn't have time yet to write these functions - these are matrix opearations
76  //inline Matrix1D& operator*= (const Matrix1D<T> &M); // reserved for something good
77  //inline Matrix1D& operator/= (const Matrix1D<T> &M); // reserved for something good
78  //inline Matrix1D& operator+= (const Matrix1D<T> &M);
79  //inline Matrix1D& operator-= (const Matrix1D<T> &M);
80  //inline Matrix1D& operator*= (const T Val);
81  //inline Matrix1D& operator/= (const T Val);
82  //inline Matrix1D& operator+= (const T Val); ///< Add the Val to each matrix element, stores result in the matrix it's applied to
83  //inline Matrix1D& operator-= (const T Val); ///< Substract the Val from each matrix element, stores result in the matrix it's applied to
84 
85  //inline Matrix1D& times_equal (const Matrix1D<T> &M); ///< Arraywise multiplication (A.*B), stores result in the matrix it's applied to
86  //inline Matrix1D& divide_equal (const Matrix1D<T> &M); ///< Arraywise division (A./B), stores result in the matrix it's applied to
87 
88  // The following operators save the result to a new matrix
89  //inline Matrix1D operator* (const Matrix1D<T> &M) const; // reserved for something good
90  //inline Matrix1D operator/ (const Matrix1D<T> &M) const; // reserved for something good
91  //inline Matrix1D operator+ (const Matrix1D<T> &M) const;
92  //inline Matrix1D operator- (const Matrix1D<T> &M) const;
93  inline Matrix1D operator* (const T Val) const;
94  inline Matrix1D operator/ (const T Val) const;
95  //inline Matrix1D operator+ (const T Val) const; ///< Add the Val to each matrix element, stores result in a new matrix
96  //inline Matrix1D operator- (const T Val) const; ///< Substract the Val from each matrix element, stores result in a new matrix
97 
98  inline Matrix1D times (const Matrix1D<T> &M) const;
99  inline Matrix1D divide (const Matrix1D<T> &M) const;
100 
101  inline T dot (const Matrix1D<T> &M) const;
102  inline T norm () const;
103 
104 
105  // interpolations
106  void Akima_interpolation(Matrix1D<T> &old_function, Matrix1D<T> &old_grid, Matrix1D<T> &new_grid, int extrapolation_type = 0, double lb = 0, double ub = 0);
107  void Akima_interpolation2(Matrix1D<T> &old_function, Matrix1D<T> &old_grid, Matrix1D<T> &new_grid, int extrapolation_type = 0, double lb = 0, double ub = 0);
108  void Interpolate(Matrix1D<T> &old_function, Matrix1D<T> &old_grid, Matrix1D<T> &new_grid);
109  void Spline(Matrix1D<T> &old_function, Matrix1D<T> &old_grid, Matrix1D<T> &new_grid, double lb, double ub, double lin_spline_coef = 0, double max_second_der = 0);
110  void Polint(Matrix1D<T> &old_function, Matrix1D<T> &old_grid, Matrix1D<T> &new_grid);
111  void Ratint(Matrix1D<T> &old_function, Matrix1D<T> &old_grid, Matrix1D<T> &new_grid);
112  void Polilinear(Matrix1D<T> &old_function, Matrix1D<T> &old_grid, Matrix1D<T> &new_grid, double lb, double ub);
113 
114  void Spline2(Matrix1D<T> &old_function, Matrix1D<T> &old_grid, Matrix1D<T> &new_grid, double lb, double ub, double lin_spline_coef = 0, double max_second_der = 0);
115 
116 
117  //T max();
118  //T maxabs();
119 
120  // writeToFileting
121  void writeToFile(string filename);
122  void writeToFile(string filename, Matrix1D<T> &grid_x);
123  void readFromFile(string filename);
124  void readFromFile(string filename, Matrix1D<T> &grid_x);
125 };
126 
132 template <typename T> class Matrix2D {
133 private:
136  T **matrix_array;
137 public:
138  bool initialized;
139 
140  int size_x, size_y;
143  string name;
144 
145  // Constructors and destructors
146  Matrix2D() { initialized = false; };
147  Matrix2D( const Matrix2D<T> &M );
148  Matrix2D( int size_x, int size_y );
149  ~Matrix2D();
150 
151  void AllocateMemory(int size_x, int size_y);
152 
153  // Operators
154 
155  inline T* operator[](int i) { return matrix_array[i]; }
156  inline T* operator[](int i) const { return matrix_array[i]; }
157  inline T& operator()(int x, int y) { return matrix_array[0][x*size_y + y]; }
158  inline T& Value (int x, int y) { return operator()(x, y); }
159  inline Matrix2D<T>& MatrixArray () { return *this; }
160 
161  // unary
162  inline const Matrix2D& operator+() const { return *this; }
163  inline const Matrix2D operator-() const { return ((*this)*(-1)); }
164 
165  // The following operators modify the matrix they applied to
166  inline Matrix2D& operator= (const Matrix2D<T> &M);
167  inline Matrix2D& operator= (const T val);
168 
169  //inline Matrix2D& operator*= (const Matrix2D<T> &M); // reserved for something good
170  //inline Matrix2D& operator/= (const Matrix2D<T> &M); // reserved for something good
171  //inline Matrix2D& operator+= (const Matrix2D<T> &M);
172  //inline Matrix2D& operator-= (const Matrix2D<T> &M);
173  //inline Matrix2D& operator*= (const T Val);
174  //inline Matrix2D& operator/= (const T Val);
175  //inline Matrix2D& operator+= (const T Val); ///< Add the Val to each matrix element, stores result in the matrix it's applied to
176  //inline Matrix2D& operator-= (const T Val); ///< Substract the Val from each matrix element, stores result in the matrix it's applied to
177 
178  //inline Matrix2D& times_equal (const Matrix2D<T> &M); ///< Arraywise multiplication (A.*B), stores result in the matrix it's applied to
179  //inline Matrix2D& divide_equal (const Matrix2D<T> &M); ///< Arraywise division (A./B), stores result in the matrix it's applied to
180 
181  // The following operators save the result to a new matrix
182  //inline Matrix2D operator* (const Matrix2D<T> &M) const; // reserved for something good
183  //inline Matrix2D operator/ (const Matrix2D<T> &M) const; // reserved for something good
184  //inline Matrix2D operator+ (const Matrix2D<T> &M) const;
185  //inline Matrix2D operator- (const Matrix2D<T> &M) const;
186  inline Matrix2D operator* (const T Val) const;
187  inline Matrix2D operator/ (const T Val) const;
188  //inline Matrix2D operator+ (const T Val) const; ///< Add the Val to each matrix element, stores result in a new matrix
189  //inline Matrix2D operator- (const T Val) const; ///< Substract the Val from each matrix element, stores result in a new matrix
190 
191  inline Matrix2D times (const Matrix2D<T> &M) const;
192  inline Matrix2D divide (const Matrix2D<T> &M) const;
193 
194 
195  // It returns maximum between values from class psd2DSlice and argument (VC::zero_f in that case)
196  Matrix2D max_of(T val);
197 
198  void Interpolate(Matrix2D<T> &old_function, Matrix2D<T> &old_grid_x, Matrix2D<T> &old_grid_y, Matrix2D<T> &new_grid_x, Matrix2D<T> &new_grid_y);
199 
200  // Return corresponding index of 1d array
201  inline int index1d(int x, int y) const;
202 
203  // writeToFileting
204  void writeToFile(string filename);
205  void writeToFile(string filename, Matrix2D<T> &grid_x, Matrix2D<T> &grid_y);
206  void readFromFile(string filename);
207  void readFromFile(string filename, Matrix2D<T> &grid_x, Matrix2D<T> &grid_y);
208 };
209 
215 template <typename T> class Matrix3D {
216 private:
218  T *plane_array;
220  T ***matrix_array;
221 public:
222  bool initialized;
223 
224  int size_x, size_y, size_z;
227  string name;
228 
229  // constructors and destructors
231  Matrix3D() { initialized = false; };
232  Matrix3D( const Matrix3D<T> &M );
234  Matrix3D( int size_x, int size_y, int size_z );
235  ~Matrix3D();
236 
237  void AllocateMemory(int size_x, int size_y, int size_z);
238 
239  // Operators
240  inline T** operator[] (int i);
241  inline T** operator[] (int i) const { return matrix_array[i]; }
242  inline T& operator() (int x, int y, int z);
243  inline T& Value (int x, int y, int z) { return operator()(x, y, z); }
244  inline Matrix3D<T>& MatrixArray () { return *this; }
245 
246  // The following operators modify the matrix they applied to
247  inline Matrix3D& operator= (const Matrix3D<T> &M);
248 // inline Matrix3D& operator= (const Matrix2D<T> &M); // confusing function
249  inline Matrix3D& operator= (const T Val);
250 
251  // unary
252  inline const Matrix3D& operator+() const { return *this; }
253  inline const Matrix3D operator-() const { return ((*this)*(-1)); }
254 
255  //inline Matrix3D& operator*= (const Matrix3D<T> &M); // reserved for something good
256  //inline Matrix3D& operator/= (const Matrix3D<T> &M); // reserved for something good
257  inline Matrix3D& operator+= (const Matrix3D<T> &M);
258  inline Matrix3D& operator-= (const Matrix3D<T> &M);
259  inline Matrix3D& operator*= (const T Val);
260  inline Matrix3D& operator/= (const T Val);
261  inline Matrix3D& operator+= (const T Val);
262  inline Matrix3D& operator-= (const T Val);
263 
264  inline Matrix3D& times_equal (const Matrix3D<T> &M);
265  inline Matrix3D& divide_equal (const Matrix3D<T> &M);
266 
267  // The following operators save the result to a new matrix
268  //inline Matrix3D operator* (const Matrix3D<T> &M) const; // reserved for something good
269  //inline Matrix3D operator/ (const Matrix3D<T> &M) const; // reserved for something good
270  inline Matrix3D operator+ (const Matrix3D<T> &M) const;
271  inline Matrix3D operator- (const Matrix3D<T> &M) const;
272  inline Matrix3D operator* (const T Val) const;
273  inline Matrix3D operator/ (const T Val) const;
274  //inline Matrix3D operator+ (const T Val) const; ///< Add the Val to each matrix element, stores result in a new matrix
275  //inline Matrix3D operator- (const T Val) const; ///< Substract the Val from each matrix element, stores result in a new matrix
276 
277  inline Matrix3D times (const Matrix3D<T> &M) const;
278  inline Matrix3D divide (const Matrix3D<T> &M) const;
279 
280  // Saving (loading) of a matrix into (from) file
281  void writeToFile(string filename);
282  void writeToFile(string filename,
283  Matrix3D<T> &grid_x, Matrix3D<T> &grid_y, Matrix3D<T> &grid_z);
284  void readFromFile(string filename);
285  void readFromFile(string filename,
286  Matrix3D<T> &grid_x, Matrix3D<T> &grid_y, Matrix3D<T> &grid_z);
287 
288  // Some other stuff
289  string change_ind;
290 
291  inline int index1d(int x, int y, int z);
292 
293  T max();
294  T maxabs();
295  Matrix3D<T> abs();
296 
297  // slices - get 2D slice from 3D array
298  Matrix2D<T> xSlice(int p_x) const;
299  Matrix2D<T> ySlice(int p_y) const;
300  Matrix2D<T> zSlice(int p_z) const;
301 
302 };
303 
304 // written this way for documentation to work
320 typedef map <int , Matrix1D<double> > DiagMatrix;
321 
328 public:
330  bool initialized;
331 
332 
333  int x_size, y_size, total_size;
335  // flag, if needs to be recalculated
336  string change_ind;
337 
338  // Constructors
339  CalculationMatrix() { this->initialized = false; }
340  // !!! CalculationMatrix(int x_size, int y_size = 0, int z_size = 0, int n_of_diags = 1);
341  CalculationMatrix(int x_size, int y_size = 1, int z_size = 1, int n_of_diags = 1);
342 
343  // Initialization
344  // !!! void Initialize(int x_size, int y_size = 0, int z_size = 0, int n_of_diags = 1);
345  void Initialize(int x_size, int y_size = 1, int z_size = 1, int n_of_diags = 1);
346 
347  // Returns 1d index for multiple dimension array (2D or 3D)
348  int index1d(int x, int y = 0, int z = 0);
349 
350  // Save to a file
351  void writeToFile(string filename);
352 
353  // Operators
354  Matrix1D<double> operator* (Matrix1D<double> &V) const;
355 
356 };
357 
358 
359 #endif
bool initialized
Flag, equal true if initialized.
Definition: Matrix.h:138
Matrix3D< T > & MatrixArray()
Return pointer to the instance of the class.
Definition: Matrix.h:244
const Matrix3D & operator+() const
Return itself as positive version of values.
Definition: Matrix.h:252
Matrix3D()
Default constructor. Do nothing.
Definition: Matrix.h:231
const Matrix3D operator-() const
Return negative version of values.
Definition: Matrix.h:253
string name
name of the Matrix
Definition: Matrix.h:47
const Matrix1D operator-() const
Return negative version of values.
Definition: Matrix.h:68
double max(double v1, double v2)
Return maximum.
bool initialized
Flag, equal true if initialized.
Definition: Matrix.h:45
Matrix2D< T > & MatrixArray()
Return pointer to the instance of the class.
Definition: Matrix.h:159
string change_ind
Variables useful for changes tracking (store here time when changed)
Definition: Matrix.h:336
T * operator[](int i)
Return the i-th pointer to 1d-array. Next [j] can be applied, so we have regular [i][j].
Definition: Matrix.h:155
bool initialized
Flag, equal true if initialized.
Definition: Matrix.h:222
map< int, Matrix1D< double > > DiagMatrix
Definition: Matrix.h:320
string name
name of the Matrix
Definition: Matrix.h:227
T * operator[](int i) const
Return the i-th pointer to 1d-array as const.
Definition: Matrix.h:156
This matrix calculates the diagonal values and index given parameters for x, y, and z...
Definition: Matrix.h:327
one dimensional matrix class
Definition: Matrix.h:41
string change_ind
Variables useful for tracking of changes (time of change can be stored here)
Definition: Matrix.h:289
const Matrix2D & operator+() const
Return itself as positive version of values.
Definition: Matrix.h:162
Matrix1D< T > & MatrixArray()
Return pointer to the instance of the class.
Definition: Matrix.h:63
This method of storage for matrices is convenient for diagonal (spread) matrices. Stored as map (diag...
const Matrix2D operator-() const
Return negative version of values.
Definition: Matrix.h:163
T & Value(int x)
Return the (x,y) value of matrix.
Definition: Matrix.h:62
T * MatrixArrayPointer()
Return pointer to the instance of the class.
Definition: Matrix.h:64
T & Value(int x, int y, int z)
Return the (x,y,z) value of matrix.
Definition: Matrix.h:243
T * matrix_array
Array to keep the values.
Definition: Matrix.h:43
double T(double alpha)
Function for bounce time.
three dimensional matrix class
Definition: Matrix.h:215
two dimensional matrix class
Definition: Matrix.h:132
string name
name of the Matrix
Definition: Matrix.h:143
T & Value(int x, int y)
Return the (x,y) value of matrix.
Definition: Matrix.h:158
T & operator()(int x, int y)
Return the (x,y)-th value of matrix.
Definition: Matrix.h:157
int size_x
size x
Definition: Matrix.h:46
const Matrix1D & operator+() const
Return itself as positive version of values.
Definition: Matrix.h:67
bool initialized
Flag, equal true if initialized.
Definition: Matrix.h:330