OSDN Git Service

first
[psychlops/cpp.git] / psychlops / core / math / psychlops_m_matrix.h
1 /*
2  *  psychlops_m_matrix.h
3  *  Psychlops Standard Library (Universal)
4  *
5  *  Last Modified 2006/03/30 by Kenchi HOSOKAWA
6  *  (C) 2005 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
7  */
8
9
10 #ifndef HEADER_PSYCHLOPS_MATH_MATRIX
11 #define HEADER_PSYCHLOPS_MATH_MATRIX
12
13
14 #include <iostream>
15 #include "../ApplicationInterfaces/psychlops_code.h"
16 #include "psychlops_m_interval.h"
17 #include "psychlops_m_function.h"
18 #include "psychlops_m_matrix.h"
19
20 #ifdef max
21 #undef max
22 #endif
23 #ifdef min
24 #undef min
25 #endif
26 \r
27 namespace cv{ class Mat; }
28
29 namespace Psychlops {
30
31         class Matrix;
32         class MatrixExpression;
33         std::ostream& operator<<(std::ostream& ost, const Matrix &mtx);
34         MatrixExpression operator +(const Matrix &lhs, double rhs);
35         MatrixExpression operator -(const Matrix &lhs, double rhs);
36         MatrixExpression operator *(const Matrix &lhs, double rhs);
37         MatrixExpression operator /(const Matrix &lhs, double rhs);
38         MatrixExpression operator ^(const Matrix &lhs, double rhs);
39         MatrixExpression operator +(const Matrix &lhs, const Matrix &rhs);
40         MatrixExpression operator -(const Matrix &lhs, const Matrix &rhs);
41         MatrixExpression operator *(const Matrix &lhs, const Matrix &rhs);
42         //MatrixExpression operator /(const Matrix &lhs, const Matrix &rhs);
43         MatrixExpression operator ~(const Matrix &lhs);
44
45         //      these functions are alpha code
46 /*      Matrix & sin(Matrix &mtx);
47         Matrix & cos(Matrix &mtx);
48         Matrix & tan(Matrix &mtx);
49         Matrix & exp(Matrix &mtx);
50 */
51         class Matrix {
52                 friend std::ostream& operator<<(std::ostream& ost, const Matrix &mtx);
53                 friend MatrixExpression operator +(const Matrix &lhs, double rhs);
54                 friend MatrixExpression operator -(const Matrix &lhs, double rhs);
55                 friend MatrixExpression operator *(const Matrix &lhs, double rhs);
56                 friend MatrixExpression operator /(const Matrix &lhs, double rhs);
57                 friend MatrixExpression operator ^(const Matrix &lhs, double rhs);
58                 friend MatrixExpression operator +(const Matrix &lhs, const Matrix &rhs);
59                 friend MatrixExpression operator -(const Matrix &lhs, const Matrix &rhs);
60                 friend MatrixExpression operator *(const Matrix &lhs, const Matrix &rhs);
61         //      friend MatrixExpression operator /(const Matrix &lhs, const Matrix &rhs);
62                 friend MatrixExpression operator ~(const Matrix &lhs);
63
64                 public:
65                 enum OPERAND { NIL, INSTANCE, SLICE, ADD, SUB, MUL, DIV, ADD_SC, SUB_SC, MUL_SC, DIV_SC, MUL_MASK, POW, POW_MASK, MESH, MASK };
66
67                 protected:
68                 int rows_, cols_;
69                 int elementSize_;
70                 double *element_;
71                 bool has_instance_;
72                 OPERAND op_;
73
74                 protected:
75                 enum SIZE_DIFINITON { SIZE_UNDEFINED = -1 };
76                 struct elem_ptr;
77                 friend class elem_ptr;
78                 friend class MatrixExpression;
79
80                 Matrix & set_roughly(int rows, int cols);
81
82                 public:
83                 Matrix();
84                 Matrix(int rows, int cols);
85                 Matrix(const Matrix &mtx);
86                 Matrix & set(int rows, int cols);
87                 Matrix & set(Range row, double interval_rows, Range col, double interval_cols);
88                 Matrix & from(double *array);
89                 Matrix & from(double *array, int rows, int cols);
90                 virtual ~Matrix();
91
92                 void release();
93                         void destroy(); // obsolete
94                 virtual void track(int n) const;
95                 virtual void swap(Matrix &rhs);
96
97                 virtual bool has_instance() const;
98                 virtual Matrix* instance_ptr() const;
99                 virtual void get_element_ptr(double *&ptr_origin, double *&ptr_start, int &row_step) const;
100                 virtual bool track_self(const Matrix* self, int &cnt_in_add, int &cnt_in_mul, bool in_mul);
101                 virtual void track_and_calculate(Matrix& target, bool add_posi, bool mul_posi);
102                 virtual Matrix& operator =(double rhs);
103                 virtual Matrix& operator =(const Matrix &rhs);
104                 virtual Matrix& operator =(const MatrixExpression &rhs);
105                 virtual void substitute(double rhs);
106                 virtual void substitute(const Matrix &rhs);
107                 bool check_size_equal(const Matrix& rhs) const;
108                 bool check_size_mul(const Matrix& rhs) const;
109
110                 virtual double& xy(int x, int y) const;
111                 virtual double& operator ()(int row, int col) const;
112                 virtual MatrixExpression operator ()(int row, Range col);
113                 virtual MatrixExpression operator ()(Range row, int col);
114                 virtual MatrixExpression operator ()(Range row, Range col);
115
116                 void add(double rhs, bool add_posi);
117                 void add(const Matrix &rhs, bool add_posi);
118                 void mul(const Matrix &lhs, double rhs, bool add_posi);
119                 void mul(const Matrix &lhs, const Matrix &rhs, bool add_posi);
120                 void mul_mask(const Matrix &lhs, const Matrix &rhs, bool add_posi);
121                 void div(const Matrix &lhs, double rhs, bool add_posi);
122                 void pow(const Matrix &lhs, double rhs, bool add_posi);
123                 void pow_mask(const Matrix &lhs, double rhs, bool add_posi);
124
125                 Matrix & each(double (*f)(double));     // this method only for test. Don't use for your code.
126                 Matrix & each(double (*f)(double , double), double);    // this method only for test. Don't use for your code.
127
128                 virtual Matrix & slide(int drow, int dcol);
129                 virtual Matrix & transpose();
130                 virtual Matrix & rot90(int times);
131                 virtual Matrix & reshape(int rows, int cols);
132                 virtual Matrix & catRow(Matrix &mtx);
133
134                 virtual double max();
135                 virtual double min();
136
137                 virtual double trace();
138
139                 int getRows() const;
140                 int getCols() const;
141                         double* getElementPtr();
142
143                 void copy(double *target, Interval init_col, int row);
144
145                 static void convertFromCvMat(Matrix &r, Matrix &g, Matrix &b, Matrix &a, const cv::Mat &target);\r
146                 static void convertFromCvMat(Matrix &r, Matrix &g, Matrix &b, const cv::Mat &target);\r
147                 static void convertFromCvMat(Matrix &gray, const cv::Mat &target);\r
148                 static void convertToCvMat(const Matrix &r, const Matrix &g, const Matrix &b, const Matrix &a, cv::Mat &target);\r
149                 static void convertToCvMat(const Matrix &r, const Matrix &g, const Matrix &b, cv::Mat &target);\r
150                 static void convertToCvMat(const Matrix &gray, cv::Mat &target);\r
151                 void from(const cv::Mat &target);\r
152                 void to(cv::Mat &target) const;
153                 void load(std::string filename); // this function is so limited, should be replaced by major CSV reader
154                 void save(std::string filename);
155
156
157                 //      Mesh
158                 public:
159 //              static MatrixExpression mesh(Wave &rows, Wave &cols);
160 //              static Matrix mesh(const Wave &roww, int rows, const Wave &colw, int cols);
161 //              static Matrix mesh(const Wave &roww, const Range rowrng, const Wave &colw, const Range colrng);
162                 static void   mesh(const Range rowrng, Matrix &rowmtx, const Range colrng, Matrix &colmtx);
163                 static Matrix mesh(int rows, const Range colrng);
164                 static Matrix mesh(const Range rowrng, int cols);
165         };
166
167         class MatrixExpression : public Matrix {
168                 public:
169                 static char OPERAND_SYMBOL[10];
170                 Matrix *lhs_;
171                 Matrix *rhs_;
172                 Wave *meshrow_, *meshcol_;
173                 Range rowrng_, colrng_;
174                 double scholar_;
175
176
177                 MatrixExpression();
178                 MatrixExpression(int rows, int cols, const Matrix &f, const Matrix &f2, const OPERAND op);
179                 MatrixExpression(int rows, int cols, const Matrix &f, double f2, OPERAND op);
180                 MatrixExpression(int rows, int cols, const Matrix &f, Range& rowrng, Range& colrng, OPERAND op);
181                 MatrixExpression(int rows, int cols, Wave &roww, Wave &colw, OPERAND op);
182                 MatrixExpression(const MatrixExpression &mtxc);
183
184                 virtual void track(int n) const;
185                 virtual ~MatrixExpression();
186                 virtual void swap(Matrix &rhs);
187
188                 virtual bool has_instance() const;
189                 virtual Matrix* instance_ptr() const;
190                 virtual void get_element_ptr(double *&ptr_origin, double *&ptr_start, int &row_step) const;
191                 void evaluate(Matrix& target);
192                 void evaluate(MatrixExpression& target);
193                 virtual bool track_self(const Matrix* self, int &cnt_in_add, int &cnt_in_mul, bool in_mul);
194                 virtual void track_and_calculate(Matrix& target, bool add_posi, bool mul_posi);
195                 //virtual void substitute(double rhs);
196                 //virtual void substitute(const Matrix &mtx);
197
198                 virtual double& xy(int x, int y) const;
199                 virtual double& operator ()(int row, int col) const;
200                 virtual MatrixExpression operator ()(int row, Range col);
201                 virtual MatrixExpression operator ()(Range row, int col);
202                 virtual MatrixExpression operator ()(Range row, Range col);
203                 virtual Matrix& operator =(double rhs);
204                 virtual Matrix& operator =(const Matrix &rhs);
205                 virtual Matrix& operator =(const MatrixExpression &rhs);
206                 //virtual double& operator ()(int row, int col);
207
208                 private:
209                 virtual Matrix & slide(int drow, int dcol);
210                 virtual Matrix & transpose();
211
212         };
213
214 namespace MatrixOverload {
215 Matrix max(const Matrix& a, const Matrix b);
216 Matrix min(const Matrix& a, const Matrix b);
217
218 Matrix sin(const Matrix &mtx);
219 Matrix cos(const Matrix &mtx);
220 Matrix tan(const Matrix &mtx);
221 Matrix asin(const Matrix &mtx);
222 Matrix acos(const Matrix &mtx);
223 Matrix atan(const Matrix &mtx);\r
224
225 Matrix exp(const Matrix &mtx);
226 Matrix log(const Matrix &mtx);
227 Matrix log10(const Matrix &mtx);
228 Matrix pow(const Matrix &mtx, double ex);
229 Matrix sqrt(const Matrix &mtx);
230 Matrix abs(const Matrix &mtx);
231 }
232
233 }       /*      <- namespace Psycholops         */
234
235
236 #endif