00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_MATRIX_H
00051 #define LASS_GUARDIAN_OF_INCLUSION_NUM_MATRIX_H
00052
00053 #include "num_common.h"
00054 #include "num_traits.h"
00055 #include "impl/matrix_expressions.h"
00056 #include "../util/call_traits.h"
00057 #include "../util/scoped_ptr.h"
00058
00059 namespace lass
00060 {
00061 namespace num
00062 {
00063
00064 template
00065 <
00066 typename T,
00067 typename S = impl::MStorage<T>
00068 >
00069 class Matrix
00070 {
00071 public:
00072
00073 typedef Matrix<T, S> TSelf;
00074 typedef S TStorage;
00075 typedef typename TStorage::TSize TSize;
00076
00077 typedef typename util::CallTraits<T>::TValue TValue;
00078 typedef typename util::CallTraits<T>::TParam TParam;
00079 typedef typename util::CallTraits<T>::TReference TReference;
00080 typedef typename util::CallTraits<T>::TConstReference TConstReference;
00081 typedef num::NumTraits<T> TNumTraits;
00082 typedef TValue* TPointer;
00083
00084 template <typename U> struct Rebind
00085 {
00086 typedef Matrix<U, S> Type;
00087 };
00088
00089 class Row
00090 {
00091 public:
00092 T& operator[](size_t iJ) { return matrix_(row_, iJ); }
00093 const T& operator[](size_t iJ) const { return matrix_(row_, iJ); }
00094 T& at(size_t iJ) { return matrix_.at(row_, iJ); }
00095 const T& at(size_t iJ) const { return matrix_.at(row_, iJ); }
00096 const TSize size() const { return matrix_.columns(); }
00097 private:
00098 friend class Matrix<T, S>;
00099 Row(Matrix<T, S>& iMatrix, TSize iRow): matrix_(iMatrix), row_(iRow) {}
00100 Matrix<T, S>& matrix_;
00101 TSize row_;
00102 };
00103
00104 class ConstRow
00105 {
00106 public:
00107 ConstRow(const Row& iOther): matrix_(iOther.matrix_), row_(iOther.row_) {}
00108 const T& operator[](size_t iJ) const { return matrix_(row_, iJ); }
00109 const T& at(size_t iJ) const { return matrix_.at(row_, iJ); }
00110 const TSize size() const { return matrix_.columns(); }
00111 private:
00112 friend class Matrix<T, S>;
00113 ConstRow(const Matrix<T, S>& iMatrix, TSize iRow): matrix_(iMatrix), row_(iRow) {}
00114 const Matrix<T, S>& matrix_;
00115 TSize row_;
00116 };
00117
00118 class Column
00119 {
00120 public:
00121 T& operator[](size_t iI) { return matrix_(iI, column_); }
00122 const T& operator[](size_t iI) const { return matrix_(iI, column_); }
00123 T& at(size_t iI) { return matrix_.at(iI, column_); }
00124 const T& at(size_t iI) const { return matrix_.at(iI, column_); }
00125 const TSize size() const { return matrix_.rows(); }
00126 private:
00127 friend class Matrix<T, S>;
00128 Column(Matrix<T, S>& iMatrix, TSize iColumn): matrix_(iMatrix), column_(iColumn) {}
00129 Matrix<T, S>& matrix_;
00130 TSize column_;
00131 };
00132
00133 class ConstColumn
00134 {
00135 public:
00136 ConstColumn(const Column& iOther): matrix_(iOther.matrix_), column_(iOther.column_) {}
00137 const T& operator[](size_t iI) const { return matrix_(iI, column_); }
00138 const T& at(size_t iI) const { return matrix_.at(iI, column_); }
00139 const TSize size() const { return matrix_.rows(); }
00140 private:
00141 friend class Matrix<T, S>;
00142 ConstColumn(const Matrix<T, S>& iMatrix, TSize iColumn): matrix_(iMatrix), column_(iColumn) {}
00143 const Matrix<T, S>& matrix_;
00144 TSize column_;
00145 };
00146
00147 Matrix();
00148 explicit Matrix(TSize iRows, TSize iCols);
00149 explicit Matrix(const TStorage& iStorage);
00150 template <typename T2, typename S2> Matrix(const Matrix<T2, S2>& iOther);
00151
00152 template <typename T2, typename S2> Matrix<T, S>& operator=(const Matrix<T2, S2>& iOther);
00153
00154 const TSize rows() const;
00155 const TSize columns() const;
00156
00157 const TValue operator()(TSize iRow, TSize iCol) const;
00158 TReference operator()(TSize iRow, TSize iCol);
00159 const TValue at(signed iRow, signed iCol) const;
00160 TReference at(signed iRow, signed iCol);
00161
00162 ConstRow row(signed iRow) const;
00163 Row row(signed iRow);
00164 ConstColumn column(signed iColumn) const;
00165 Column column(signed iColumn);
00166
00167 const Matrix<T, S>& operator+() const;
00168 const Matrix<T, impl::MNeg<T, S> > operator-() const;
00169
00170 template <typename S2> Matrix<T, S>& operator+=(const Matrix<T, S2>& iB);
00171 template <typename S2> Matrix<T, S>& operator-=(const Matrix<T, S2>& iB);
00172 Matrix<T, S>& operator*=(TParam iB);
00173 Matrix<T, S>& operator/=(TParam iB);
00174
00175 void setZero(TSize iRows, TSize iCols);
00176 void setIdentity(TSize iSize);
00177
00178 bool isEmpty() const;
00179 bool isZero() const;
00180 bool isIdentity() const;
00181 bool isDiagonal() const;
00182 bool isSquare() const;
00183
00184 const Matrix<T, impl::MTrans<T, S> > transpose() const;
00185
00186 bool invert();
00187
00188 const TStorage& storage() const;
00189 TStorage& storage();
00190 void swap(Matrix<T, S>& iOther);
00191
00192 private:
00193
00194 TStorage storage_;
00195 };
00196
00197 template <typename T, typename S1, typename S2>
00198 bool operator==(const Matrix<T, S1>& iA, const Matrix<T, S2>& iB);
00199 template <typename T, typename S1, typename S2>
00200 inline bool operator!=(const Matrix<T, S1>& iA, const Matrix<T, S2>& iB);
00201
00202 template <typename T, typename S1, typename S2>
00203 const Matrix<T, impl::MAdd<T, S1, S2> > operator+(const Matrix<T, S1>& iA, const Matrix<T, S2>& iB);
00204 template <typename T, typename S1, typename S2>
00205 const Matrix<T, impl::MSub<T, S1, S2> > operator-(const Matrix<T, S1>& iA, const Matrix<T, S2>& iB);
00206 template <typename T, typename S1, typename S2>
00207 const Matrix<T, impl::MProd<T, S1, S2> > operator*(const Matrix<T, S1>& iA, const Matrix<T, S2>& iB);
00208
00209 template <typename T, typename S>
00210 const Matrix<T, impl::MAdd<T, impl::MScalar<T>, S> > operator+(const T& iA, const Matrix<T, S>& iB);
00211 template <typename T, typename S>
00212 const Matrix<T, impl::MSub<T, impl::MScalar<T>, S> > operator-(const T& iA, const Matrix<T, S>& iB);
00213 template <typename T, typename S>
00214 const Matrix<T, impl::MMul<T, impl::MScalar<T>, S> > operator*(const T& iA, const Matrix<T, S>& iB);
00215
00216 template <typename T, typename S>
00217 const Matrix<T, impl::MAdd<T, S, impl::MScalar<T> > > operator+(const Matrix<T, S>& iA, const T& iB);
00218 template <typename T, typename S>
00219 const Matrix<T, impl::MAdd<T, S, impl::MScalar<T> > > operator-(const Matrix<T, S>& iA, const T& iB);
00220 template <typename T, typename S>
00221 const Matrix<T, impl::MMul<T, S, impl::MScalar<T> > > operator*(const Matrix<T, S>& iA, const T& iB);
00222 template <typename T, typename S>
00223 const Matrix<T, impl::MMul<T, S, impl::MScalar<T> > > operator/(const Matrix<T, S>& iA, const T& iB);
00224
00225 template <typename T, typename S, typename S2>
00226 bool solve(const Matrix<T, S>& iA, Matrix<T, S2>& ioB);
00227
00228
00229 template <typename T, typename S, typename Char, typename Traits>
00230 std::basic_ostream<Char, Traits>&
00231 operator<<(std::basic_ostream<Char, Traits>& iS, const Matrix<T, S>& iA);
00232
00233 }
00234
00235 }
00236
00237 #include "matrix.inl"
00238
00239 #ifdef LASS_GUARDIAN_OF_INCLUSION_NUM_VECTOR_H
00240 #include "matrix_vector.h"
00241 #endif
00242
00243 #endif
00244
00245