Simple Application Framework
1
|
00001 /* 00002 This file is part of Simple Application Framework (Saf) library. 00003 Copyright (C) 2010 - 2012 Ondrej Danek <ondrej.danek@gmail.com> 00004 00005 This library is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published 00007 by the Free Software Foundation, either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 Saf 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 00016 along with Simple Application Framework library. If not, 00017 see <http://www.gnu.org/licenses/>. 00018 */ 00019 00028 #ifndef SAF_MATH_ALGEBRA_SQUAREMATRIX_H 00029 #define SAF_MATH_ALGEBRA_SQUAREMATRIX_H 00030 00031 #include "../../NotImplementedException.h" 00032 #include "Matrix.h" 00033 00034 namespace Saf 00035 { 00036 namespace Math 00037 { 00038 namespace Algebra 00039 { 00046 template <Size N, class T> 00047 class SquareMatrix 00048 : public Matrix<N,N,T> 00049 { 00050 protected: 00051 using Matrix<N,N,T>::m_data; 00052 00053 public: 00058 SquareMatrix() 00059 : Matrix<N,N,T>() 00060 {} 00061 00066 explicit SquareMatrix(const T& val) 00067 : Matrix<N,N,T>(val) 00068 {} 00069 00071 explicit SquareMatrix(const Matrix<N,N,T>& m) 00072 : Matrix<N,N,T>(m) 00073 {} 00074 00080 explicit SquareMatrix(const Vector<N,T> &v) 00081 : Matrix<N,N,T>() 00082 { 00083 SetMainDiagonal(v); 00084 } 00085 00090 SquareMatrix<N,T>& SetMainDiagonal(const Vector<N,T> &v) 00091 { 00092 Algo::Range::Fill(Begin(), End(), 0); 00093 00094 Size j = 0; 00095 for (Size i = 0; i < N; i++, j += N+1) 00096 { 00097 m_data[j] = v[i]; 00098 } 00099 00100 return *this; 00101 } 00102 00104 Vector<N,T> MainDiagonal() const 00105 { 00106 Vector<N,T> v; 00107 Size j = 0; 00108 00109 for (Size i = 0; i < N; i++, j += N + 1) 00110 { 00111 v[i] = m_data[j]; 00112 } 00113 00114 return v; 00115 } 00116 00118 T Determinant() const 00119 { 00120 if (N == 2) 00121 { 00122 return m_data[0] * m_data[3] - m_data[1] * m_data[2]; 00123 } 00124 else if (N == 3) 00125 { 00126 return m_data[0]*m_data[4]*m_data[8] + 00127 m_data[1]*m_data[5]*m_data[6] + 00128 m_data[2]*m_data[3]*m_data[7] - 00129 m_data[2]*m_data[4]*m_data[6] - 00130 m_data[1]*m_data[3]*m_data[8] - 00131 m_data[0]*m_data[5]*m_data[7]; 00132 } 00133 else 00134 { 00135 throw NotImplementedException(SAF_SOURCE_LOCATION, 00136 "Determinant computation implemented only for 2x2 and 3x3 matrices."); 00137 } 00138 00139 return 0; 00140 } 00141 00143 SquareMatrix<N,T>& SetIdentity() 00144 { 00145 Algo::Range::Fill(Begin(), End(), 0); 00146 00147 Size k = 0; 00148 for (Size i = 0; i < N; i++) 00149 { 00150 m_data[k] = T(1); 00151 k += N + 1; 00152 } 00153 00154 return *this; 00155 } 00156 00158 static SquareMatrix<N,T> Identity() 00159 { 00160 SquareMatrix<N,T> m; 00161 m.SetIdentity(); 00162 return m; 00163 } 00164 }; 00165 } 00166 } 00167 } 00168 00169 #endif