My Project
Matrix.h
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"
24 #include "./Interpolation/polilinear.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:
43  T *matrix_array;
44 
45  bool initialized;
46  int size_x;
47  string name;
48 
49  // constructors and destructors
50  Matrix1D() { initialized = false; };
51  Matrix1D( int size_x );
52  Matrix1D( int x_size , string name);
53  Matrix1D( const Matrix1D<T> &M );
54  ~Matrix1D();
55 
56  void AllocateMemory( int size_x );
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 implimented
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  int size_x, size_y;
140  string name;
141 
142  // Constructors and destructors
143  Matrix2D() { initialized = false; };
144  Matrix2D( const Matrix2D<T> &M );
145  Matrix2D( int size_x, int size_y );
146  ~Matrix2D();
147 
148  void AllocateMemory(int size_x, int size_y);
149 
150  // Operators
151 
152  inline T* operator[](int i) { return matrix_array[i]; }
153  inline T* operator[](int i) const { return matrix_array[i]; }
154  inline T& operator()(int x, int y) { return matrix_array[0][x*size_y + y]; }
155  inline T& Value (int x, int y) { return operator()(x, y); }
156  inline Matrix2D<T>& MatrixArray () { return *this; }
157 
158  // unary
159  inline const Matrix2D& operator+() const { return *this; }
160  inline const Matrix2D operator-() const { return ((*this)*(-1)); }
161 
162  // The following operators modify the matrix they applied to
163  inline Matrix2D& operator= (const Matrix2D<T> &M);
164  inline Matrix2D& operator= (const T val);
165 
166  //inline Matrix2D& operator*= (const Matrix2D<T> &M); // reserved for something good
167  //inline Matrix2D& operator/= (const Matrix2D<T> &M); // reserved for something good
168  //inline Matrix2D& operator+= (const Matrix2D<T> &M);
169  //inline Matrix2D& operator-= (const Matrix2D<T> &M);
170  //inline Matrix2D& operator*= (const T Val);
171  //inline Matrix2D& operator/= (const T Val);
172  //inline Matrix2D& operator+= (const T Val); ///< Add the Val to each matrix element, stores result in the matrix it's applied to
173  //inline Matrix2D& operator-= (const T Val); ///< Substract the Val from each matrix element, stores result in the matrix it's applied to
174 
175  //inline Matrix2D& times_equal (const Matrix2D<T> &M); ///< Arraywise multiplication (A.*B), stores result in the matrix it's applied to
176  //inline Matrix2D& divide_equal (const Matrix2D<T> &M); ///< Arraywise division (A./B), stores result in the matrix it's applied to
177 
178  // The following operators save the result to a new matrix
179  //inline Matrix2D operator* (const Matrix2D<T> &M) const; // reserved for something good
180  //inline Matrix2D operator/ (const Matrix2D<T> &M) const; // reserved for something good
181  //inline Matrix2D operator+ (const Matrix2D<T> &M) const;
182  //inline Matrix2D operator- (const Matrix2D<T> &M) const;
183  inline Matrix2D operator* (const T Val) const;
184  inline Matrix2D operator/ (const T Val) const;
185  //inline Matrix2D operator+ (const T Val) const; ///< Add the Val to each matrix element, stores result in a new matrix
186  //inline Matrix2D operator- (const T Val) const; ///< Substract the Val from each matrix element, stores result in a new matrix
187 
188  inline Matrix2D times (const Matrix2D<T> &M) const;
189  inline Matrix2D divide (const Matrix2D<T> &M) const;
190 
191 
192  // It returns maximum between values from class psd2DSlice and argument (VC::zero_f in that case)
193  Matrix2D max_of(T val);
194 
195  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);
196 
197  // Return corresponding index of 1d array
198  inline int index1d(int x, int y) const;
199 
200  // writeToFileting
201  void writeToFile(string filename);
202  void writeToFile(string filename, Matrix2D<T> &grid_x, Matrix2D<T> &grid_y);
203  void readFromFile(string filename);
204  void readFromFile(string filename, Matrix2D<T> &grid_x, Matrix2D<T> &grid_y);
205 };
206 
212 template <typename T> class Matrix3D {
213 private:
215  T *plane_array;
217  T ***matrix_array;
218 public:
219  bool initialized;
220  int size_x, size_y, size_z;
221  string name;
222 
223  // constructors and destructors
225  Matrix3D() { initialized = false; };
226  Matrix3D( const Matrix3D<T> &M );
227  Matrix3D( int size_x, int size_y, int size_z );
228  ~Matrix3D();
229 
230  void AllocateMemory(int size_x, int size_y, int size_z);
231 
232  // Operators
233  inline T** operator[] (int i);
234  inline T** operator[] (int i) const { return matrix_array[i]; }
235  inline T& operator() (int x, int y, int z);
236  inline T& Value (int x, int y, int z) { return operator()(x, y, z); }
237  inline Matrix3D<T>& MatrixArray () { return *this; }
238 
239  // The following operators modify the matrix they applied to
240  inline Matrix3D& operator= (const Matrix3D<T> &M);
241 // inline Matrix3D& operator= (const Matrix2D<T> &M); // confusing function
242  inline Matrix3D& operator= (const T Val);
243 
244  // unary
245  inline const Matrix3D& operator+() const { return *this; }
246  inline const Matrix3D operator-() const { return ((*this)*(-1)); }
247 
248  //inline Matrix3D& operator*= (const Matrix3D<T> &M); // reserved for something good
249  //inline Matrix3D& operator/= (const Matrix3D<T> &M); // reserved for something good
250  inline Matrix3D& operator+= (const Matrix3D<T> &M);
251  inline Matrix3D& operator-= (const Matrix3D<T> &M);
252  inline Matrix3D& operator*= (const T Val);
253  inline Matrix3D& operator/= (const T Val);
254  inline Matrix3D& operator+= (const T Val);
255  inline Matrix3D& operator-= (const T Val);
256 
257  inline Matrix3D& times_equal (const Matrix3D<T> &M);
258  inline Matrix3D& divide_equal (const Matrix3D<T> &M);
259 
260  // The following operators save the result to a new matrix
261  //inline Matrix3D operator* (const Matrix3D<T> &M) const; // reserved for something good
262  //inline Matrix3D operator/ (const Matrix3D<T> &M) const; // reserved for something good
263  inline Matrix3D operator+ (const Matrix3D<T> &M) const;
264  inline Matrix3D operator- (const Matrix3D<T> &M) const;
265  inline Matrix3D operator* (const T Val) const;
266  inline Matrix3D operator/ (const T Val) const;
267  //inline Matrix3D operator+ (const T Val) const; ///< Add the Val to each matrix element, stores result in a new matrix
268  //inline Matrix3D operator- (const T Val) const; ///< Substract the Val from each matrix element, stores result in a new matrix
269 
270  inline Matrix3D times (const Matrix3D<T> &M) const;
271  inline Matrix3D divide (const Matrix3D<T> &M) const;
272 
273  // Saving (loading) of a matrix into (from) file
274  void writeToFile(string filename);
275  void writeToFile(string filename,
276  Matrix3D<T> &grid_x, Matrix3D<T> &grid_y, Matrix3D<T> &grid_z);
277  void readFromFile(string filename);
278  void readFromFile(string filename,
279  Matrix3D<T> &grid_x, Matrix3D<T> &grid_y, Matrix3D<T> &grid_z);
280 
281  // Some other stuff
282  string change_ind;
283 
284  inline int index1d(int x, int y, int z);
285 
286  T max();
287  T maxabs();
288  Matrix3D<T> abs();
289 
290  // slices - get 2D slice from 3D array
291  Matrix2D<T> xSlice(int p_x) const;
292  Matrix2D<T> ySlice(int p_y) const;
293  Matrix2D<T> zSlice(int p_z) const;
294 
295 };
296 
297 
303 typedef map <int , Matrix1D<double> > DiagMatrix;
304 
308 class CalculationMatrix : public DiagMatrix {
309 public:
310 
311  bool initialized;
312  int x_size, y_size, total_size;
313  // flag, if needs to be recalculated
314  string change_ind;
315 
316  // Constructors
317  CalculationMatrix() { this->initialized = false; }
318  // !!! CalculationMatrix(int x_size, int y_size = 0, int z_size = 0, int n_of_diags = 1);
319  CalculationMatrix(int x_size, int y_size = 1, int z_size = 1, int n_of_diags = 1);
320 
321  // Initialization
322  // !!! void Initialize(int x_size, int y_size = 0, int z_size = 0, int n_of_diags = 1);
323  void Initialize(int x_size, int y_size = 1, int z_size = 1, int n_of_diags = 1);
324 
325  // Returns 1d index for multiple dimension array (2D or 3D)
326  int index1d(int x, int y = 0, int z = 0);
327 
328  // Save to a file
329  void writeToFile(string filename);
330 
331  // Operators
332  Matrix1D<double> operator* (Matrix1D<double> &V) const;
333 
334 };
335 
336 
337 #endif
Matrix3D< T > & MatrixArray()
Return pointer to the instance of the class.
Definition: Matrix.h:237
Matrix3D()
Default constructor. Do nothing.
Definition: Matrix.h:225
Matrix2D< T > & MatrixArray()
Return pointer to the instance of the class.
Definition: Matrix.h:156
General namespace.
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:152
This matrix calculates the diagonal values and index given parameters for x, y, and z...
Definition: Matrix.h:310
one dimensional matrix class
Definition: Matrix.h:41
Matrix1D< T > & MatrixArray()
Return pointer to the instance of the class.
Definition: Matrix.h:63
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:236
double T(double alpha)
Function for bounce time.
Definition: variousFunctions.cpp:32
three dimensional matrix class
Definition: Matrix.h:212
two dimensional matrix class
Definition: Matrix.h:132
T & Value(int x, int y)
Return the (x,y) value of matrix.
Definition: Matrix.h:155
T & operator()(int x, int y)
Return the (x,y)-th value of matrix.
Definition: Matrix.h:154