1 #ifndef _matrix_h 2 #define _matrix_h 3 4 #ifndef _vector_h 5 # include "vector.h" 6 #endif 7 8 // 9 // ------------------------------------------------------------------------- 10 // class Matrix : Matrixklasse als Liste gleichdimensionierter Vektoren 11 // ------------------------------------------------------------------------- 12 // 13 14 15 class Matrix { 16 protected: 17 short dim; 18 Vector *data; 19 20 public: Matrix()21 Matrix() { dim=0; } 22 Matrix( int n, int m, ... ); 23 Matrix( const Matrix &m ); ~Matrix()24 ~Matrix() { if (dim) delete [] data; } 25 26 void show(const char *str=0) const; 27 Rows()28 int Rows() const { return dim; } 29 int Cols() const; 30 31 protected: 32 void Resize(int i); 33 void Resize(int i, int j); 34 Vector &GetRef(int i); 35 public: operator()36 const Vector &operator()(int i) const 37 { return (i>=0&&i<dim)?data[i]:VectorZero; } 38 Vector &operator[](int i) 39 { return (i>=0&&i<dim)?data[i]:GetRef(i); } 40 41 int IsZero() const; 42 43 44 const Matrix& operator=(const Matrix &v); // Copy 45 const Matrix& operator<<=(Matrix &v); // Move 46 47 Matrix Matrix::operator+(const Matrix&) const; 48 Matrix Matrix::operator-(const Matrix&) const; 49 50 Matrix Matrix::operator*(const Real&) const; 51 friend Matrix operator*(const Real&, const Matrix&); 52 Matrix Matrix::operator/(const Real&) const; 53 54 friend int operator==(const Matrix&, const Matrix&); 55 friend int operator!=(const Matrix&, const Matrix&); 56 const Matrix& operator+=(const Matrix&); 57 const Matrix& operator-=(const Matrix&); 58 const Matrix& operator*=(const Real&); 59 const Matrix& operator/=(const Real&); 60 Matrix Matrix::operator+() const; 61 Matrix Matrix::operator-() const; 62 63 friend Vector operator*(const Vector&, const Matrix&); 64 friend Vector operator*(const Matrix&, const Vector&); 65 const Matrix &operator*=(const Matrix&); 66 friend Matrix operator*(const Matrix&, const Matrix&); 67 68 friend Vector operator/(const Vector &b, const Matrix &a); 69 friend int system_calc(Matrix &a, Vector *x, Vector &b); 70 }; 71 72 inline Matrix Matrix::operator+() const 73 { 74 return *this; 75 } 76 inline Matrix Matrix::operator-() const 77 { 78 Matrix v(*this); 79 return v*=Real(-1.0); 80 } 81 82 83 // Definitions of non-member binary operator functions 84 85 inline Matrix Matrix::operator+(const Matrix& z2) const 86 { 87 if (dim>z2.dim) { 88 Matrix v(*this); 89 return v+=z2; 90 } 91 else { 92 Matrix v(z2); 93 return v+=*this; 94 } 95 } 96 inline Matrix Matrix::operator-(const Matrix& z2) const 97 { 98 Matrix v(*this); 99 return v-=z2; 100 } 101 102 inline Matrix Matrix::operator*(const Real& r) const 103 { 104 Matrix erg(*this); 105 erg *= r; 106 return erg; 107 } 108 109 inline Matrix Matrix::operator/(const Real& r) const 110 { 111 Matrix erg(*this); 112 erg /= r; 113 return erg; 114 } 115 116 117 inline Matrix operator*(const Real& val, const Matrix& z2) 118 { 119 return z2*val; // kommutativ 120 } 121 122 inline int operator!=(const Matrix& z1, const Matrix& z2) 123 { 124 return !(z1==z2); 125 } 126 127 128 extern Matrix MatrixZero; 129 130 #endif 131