00001
00002 #ifndef MATRIX_HH
00003 #define MATRIX_HH
00004
00005 #include <vector>
00006 #include <iostream>
00007 #include <math.h>
00008 #include "dataref"
00009 #include "mtrx_except.hh"
00010
00011 class Matrix;
00012 class SubMatrix;
00013 class MatMTimesMat;
00014 class MatTimesMat;
00015 class MatCrossMat;
00016
00017 class Matrix {
00018 #include "matrix_friend_ops.hh"
00019 friend class SubMatrix;
00020 public:
00021
00022
00023 Matrix();
00024 Matrix(const Matrix&);
00025 Matrix(int);
00026 Matrix(double);
00027 Matrix(int m, int n, double re = 0);
00028 Matrix(int m, int n, double *re, double *im = NULL);
00029 Matrix(int m, int n, double **v);
00030 Matrix(bool isc, bool isd);
00031
00032
00033 ~Matrix();
00034
00035
00036 Matrix& operator = (const Matrix&);
00037
00038
00039 operator double() const;
00040 operator int() const;
00041 operator bool() const;
00042
00043
00044 template <class T>
00045 inline SubMatrix operator () (const T&);
00046 template <class T>
00047 inline SubMatrix operator () (const T&) const;
00048 template <class T, class U>
00049 inline SubMatrix operator () (const T&, const U&);
00050 template <class T, class U>
00051 inline SubMatrix operator () (const T&, const U&) const;
00052
00053
00054 inline void Size(int *dims) const;
00055 inline bool IsEmpty() const;
00056 inline bool IsDIN() const;
00057 inline bool IsColon() const;
00058 inline bool IsVector() const;
00059 inline bool IsScalar() const;
00060 inline bool IsReal() const;
00061 inline bool IsComplex() const;
00062
00063
00064 void ExtractData(double*) const;
00065 inline double* GetPr();
00066 inline const double* GetPr() const;
00067 inline double* GetPi();
00068 inline const double* GetPi() const;
00069
00070
00071 Matrix& operator += (Matrix);
00072 inline Matrix& operator += (const MatMTimesMat&);
00073 inline Matrix& operator += (const MatTimesMat&);
00074 inline Matrix& operator += (const MatCrossMat&);
00075 Matrix& operator -= (Matrix);
00076 inline Matrix& operator *= (const Matrix&);
00077 inline Matrix& operator /= (const Matrix&);
00078
00079 static Matrix DIN;
00080
00081 protected:
00082
00083 class MatrixData {
00084 public:
00085 int m, n, l;
00086 double *re, *im;
00087 bool iscolon, isdin;
00088
00089 MatrixData();
00090 MatrixData(const MatrixData&);
00091 MatrixData(int m, int n, double re = 0);
00092 MatrixData(int m, int n, double *re, double *im = NULL);
00093 MatrixData(bool isc, bool isd);
00094 ~MatrixData();
00095
00096 MatrixData& operator = (const MatrixData&);
00097
00098 inline bool IsEmpty() const;
00099 inline bool IsReal() const;
00100 inline bool IsComplex() const;
00101
00102 inline double* GetPr();
00103 inline const double* GetPr() const;
00104 inline double* GetPi();
00105 inline const double* GetPi() const;
00106
00107 inline double index(int) const;
00108 inline double& index(int);
00109 inline double index(int, int) const;
00110 inline double& index(int, int);
00111 inline int DoubleToSingleIndex(int i, int j) const;
00112 };
00113
00114 typedef DataRef<MatrixData> MatrixRef;
00115
00116 MatrixRef ref;
00117 static Matrix COLON;
00118
00119 inline void Assign(const Matrix&);
00120 };
00121
00122 class SubMatrix : public Matrix {
00123 friend class Matrix;
00124 public:
00125 void operator = (const SubMatrix& rhs) {
00126 *base = subsasgn(*base,ind,jnd,Matrix(rhs));
00127 }
00128 void operator = (const Matrix& rhs) {
00129 *base = subsasgn(*base,ind,jnd,rhs);
00130 }
00131 ~SubMatrix() {}
00132 private:
00133 SubMatrix(Matrix *b, const Matrix& i, const Matrix& j = Matrix::DIN)
00134 : Matrix(subsref(*b,i,j)), base(b), cbase(b), ind(i), jnd(j) {}
00135 SubMatrix(const Matrix *b, const Matrix& i, const Matrix& j = Matrix::DIN)
00136 : Matrix(subsref(*b,i,j)), base(NULL), cbase(b), ind(i), jnd(j) {}
00137
00138 Matrix *base;
00139 const Matrix *cbase;
00140 const Matrix ind, jnd;
00141 };
00142
00143 class MatMTimesMat {
00144 public:
00145 MatMTimesMat(const Matrix& mm1, const Matrix& mm2) : m1(mm1), m2(mm2) {}
00146 operator Matrix() {return mtimes_eval(m1,m2);};
00147 const Matrix& m1;
00148 const Matrix& m2;
00149 };
00150
00151 class MatTimesMat {
00152 public:
00153 MatTimesMat(const Matrix& mm1, const Matrix& mm2) : m1(mm1), m2(mm2) {}
00154 operator Matrix() {return times_eval(m1,m2);};
00155 const Matrix& m1;
00156 const Matrix& m2;
00157 };
00158
00159 class MatCrossMat {
00160 public:
00161 MatCrossMat(const Matrix& mm1, const Matrix& mm2) : m1(mm1), m2(mm2) {}
00162 operator Matrix() {return cross_eval(m1,m2);};
00163 const Matrix& m1;
00164 const Matrix& m2;
00165 };
00166
00167 #include "matrix_ops.hh"
00168 #include "matrix_member_inline.hh"
00169 #include "matrix_friend_ops_inline.hh"
00170 #include "matrixdata_inline.hh"
00171
00172 #endif