HyperspaceExplorer 0.7.1
|
00001 /* 00002 Hyperspace Explorer - vizualizing higher-dimensional geometry 00003 Copyright (C) 2010 Lene Preuss <lene.preuss@gmail.com> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License along 00016 with this program; if not, write to the Free Software Foundation, Inc., 00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00018 00019 */ 00020 00021 #ifndef MATRIX_IMPL_H 00022 #define MATRIX_IMPL_H 00023 00024 #include "Matrix.h" 00025 00026 #include "Vector.h" 00027 00028 namespace VecMath { 00029 00030 template<unsigned D, typename N> 00031 Matrix<D, N>::Matrix () { 00032 for (unsigned i = 0; i < D; i++) { // i: row 00033 for (unsigned j = 0; j < D; j++) { // j: col 00034 _M[i][j] = 0; 00035 } 00036 _M[i][i] = 1; 00037 } 00038 } 00039 00044 template<unsigned D, typename N> 00045 Matrix<D, N>::Matrix (unsigned ii, unsigned jj, N theta) { 00046 N c = cos (theta*pi/180.), s = sin (theta*pi/180.); 00047 for (unsigned i = 0; i < D; i++) { // i: row 00048 for (unsigned j = 0; j < D; j++) { // j: col 00049 _M[i][j] = 0; 00050 } 00051 _M[i][i] = 1; 00052 } 00053 _M[ii][ii] = _M[jj][jj] = c; 00054 _M[ii][jj] = -s; 00055 _M[jj][ii] = s; 00056 } 00057 00062 template<unsigned D, typename N> 00063 N &Matrix<D, N>::operator () (unsigned i, unsigned j) { 00064 return _M[i][j]; 00065 } 00066 00071 template<unsigned D, typename N> 00072 const N &Matrix<D, N>::operator () (unsigned i, unsigned j) const { 00073 return _M[i][j]; 00074 } 00075 00079 template <unsigned D, typename N> 00080 Matrix<D, N> Matrix<D, N>::operator *=(const Matrix<D, N> &B) { 00081 for (unsigned i = 0; i < D; i++) { // i: row 00082 for (unsigned j = 0; j < D; j++) { // j: col 00083 N s = 0; 00084 for (unsigned k = 0; k < D; k++) { 00085 s += _M[i][k]*B._M[k][j]; 00086 } 00087 _M[i][j] = s; 00088 } 00089 } 00090 return *this; 00091 } 00092 00095 template <unsigned D, typename N> 00096 Matrix<D, N> Matrix<D, N>::operator - () { 00097 Matrix<D, N> B (*this); 00098 for (unsigned i = 0; i < D; i++) { // i: row 00099 for (unsigned j = 0; j < D; j++) { // j: col 00100 B._M[i][j] = -_M[i][j]; 00101 } 00102 } 00103 return B; 00104 } 00105 00108 template <unsigned D, typename N> 00109 void Matrix<D, N>::scale(const Vector<D, N> &s) { 00110 for (unsigned i = 0; i < s.dimension(); ++i) { 00111 for (unsigned j = 0; j < s.dimension(); ++j) { 00112 _M[i][j] *= s[i]; 00113 } 00114 } 00115 } 00116 00117 template <unsigned D, typename N> 00118 std::string Matrix<D, N>::toString() const { 00119 std::ostringstream o; 00120 o << *this << std::ends; 00121 return o.str(); 00122 } 00123 00124 template<unsigned D, typename N> 00125 Matrix<D, N>::operator std::string() const { 00126 return toString(); 00127 } 00128 00136 template <unsigned D, typename N> 00137 Matrix<D, N> operator *(const Matrix<D, N> &A, const Matrix<D, N> &B) { 00138 Matrix<D, N> C; 00139 for (unsigned i = 0; i < D; i++) { 00140 for (unsigned j = 0; j < D; j++) { // j: col 00141 N s = 0; 00142 for (unsigned k = 0; k < D; k++) { 00143 s += A(i, k)*B(k, j); 00144 } 00145 C(i, j) = s; 00146 } 00147 } 00148 return C; 00149 } 00150 00158 template <unsigned D, typename N> 00159 Vector<D, N> operator * (const Matrix<D, N> &M, const Vector<D, N> &V) { 00160 Vector<D, N> W; 00161 for (unsigned i = 0; i < D; i++) { // i: row 00162 N s = 0; 00163 for (unsigned j = 0; j < D; j++) { // j: col 00164 s += M(i, j)*V[j]; 00165 } 00166 W[i] = s; 00167 } 00168 return W; 00169 } 00170 00178 template <unsigned D, typename N> 00179 std::ostream &operator << (std::ostream &o, const Matrix<D, N> &M) { 00180 for (unsigned i = 0; i < D; i++) { 00181 o << "|"; 00182 for (unsigned j = 0; j < D; j++) { 00183 o << std::setw (10) << std::setprecision (3) << M(i, j); 00184 } 00185 o << "|" << std::endl; 00186 } 00187 return o; 00188 } 00189 00190 } 00191 00192 #endif