4#include "feddlib/core/FEDDCore.hpp"
26 typedef std::vector<T> Vec_T_Type;
27 typedef const std::vector<T> Vec_T_Const_Type;
28 typedef std::vector<Vec_T_Type> Vec2D_T_Type;
32 SmallMatrix(
int size);
34 SmallMatrix(
int size, T v);
36 Vec_T_Type &operator[](
int i);
38 Vec_T_Const_Type &operator[](
int i)
const;
40 SmallMatrix<T> &operator=(SmallMatrix<T> &sm);
42 SmallMatrix<T> operator*(SmallMatrix<T> &other);
44 SmallMatrix<T> operator+(SmallMatrix<T> &other);
46 SmallMatrix<T> &operator*=(SmallMatrix<T> &other);
48 SmallMatrix<T> &operator+=(SmallMatrix<T> &other);
52 int multiply(SmallMatrix<T> &bMat, SmallMatrix<T> &cMat);
54 int add(SmallMatrix<T> &bMat, SmallMatrix<T> &cMat);
56 int innerProduct(SmallMatrix<T> &bMat, T &res);
58 double innerProduct(SmallMatrix<T> &bMat);
70 double computeInverse(SmallMatrix<T> &inverse);
74 double computeScaling();
82#include "SmallMatrix.hpp"
85SmallMatrix<T>::SmallMatrix():
90SmallMatrix<T>::SmallMatrix(
int size):
91values_(size,Vec_T_Type(size)),
96SmallMatrix<T>::SmallMatrix(
int size, T v):
97values_( size, Vec_T_Type( size, v ) ),
102typename SmallMatrix<T>::Vec_T_Type &SmallMatrix<T>::operator[](
int i) {
104 return values_.at(i);
107typename SmallMatrix<T>::Vec_T_Const_Type &SmallMatrix<T>::operator[](
int i)
const{
109 return values_.at(i);
115 cMat[0][0] = values_[0][0]*bMat[0][0] + values_[0][1]*bMat[1][0];
116 cMat[0][1] = values_[0][0]*bMat[0][1] + values_[0][1]*bMat[1][1];
117 cMat[1][0] = values_[1][0]*bMat[0][0] + values_[1][1]*bMat[1][0];
118 cMat[1][1] = values_[1][0]*bMat[0][1] + values_[1][1]*bMat[1][1];
121 cMat[0][0] = values_[0][0]*bMat[0][0] + values_[0][1]*bMat[1][0] + values_[0][2]*bMat[2][0];
122 cMat[0][1] = values_[0][0]*bMat[0][1] + values_[0][1]*bMat[1][1] + values_[0][2]*bMat[2][1];
123 cMat[0][2] = values_[0][0]*bMat[0][2] + values_[0][1]*bMat[1][2] + values_[0][2]*bMat[2][2];
125 cMat[1][0] = values_[1][0]*bMat[0][0] + values_[1][1]*bMat[1][0] + values_[1][2]*bMat[2][0];
126 cMat[1][1] = values_[1][0]*bMat[0][1] + values_[1][1]*bMat[1][1] + values_[1][2]*bMat[2][1];
127 cMat[1][2] = values_[1][0]*bMat[0][2] + values_[1][1]*bMat[1][2] + values_[1][2]*bMat[2][2];
129 cMat[2][0] = values_[2][0]*bMat[0][0] + values_[2][1]*bMat[1][0] + values_[2][2]*bMat[2][0];
130 cMat[2][1] = values_[2][0]*bMat[0][1] + values_[2][1]*bMat[1][1] + values_[2][2]*bMat[2][1];
131 cMat[2][2] = values_[2][0]*bMat[0][2] + values_[2][1]*bMat[1][2] + values_[2][2]*bMat[2][2];
134 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"SmallMatrix wrong size!");
142 for(
int i=0; i< size_; i++){
143 for(
int j=0; j< size_;j++){
144 cMat[i][j] = values_[i][j]+bMat[i][j];
178 res = values_[0][0]*bMat[0][0] + values_[0][1]*bMat[0][1] +
179 values_[1][0]*bMat[1][0] + values_[1][1]*bMat[1][1];
182 res = values_[0][0]*bMat[0][0] + values_[0][1]*bMat[0][1] + values_[0][2]*bMat[0][2] +
183 values_[1][0]*bMat[1][0] + values_[1][1]*bMat[1][1] + values_[1][2]*bMat[1][2] +
184 values_[2][0]*bMat[2][0] + values_[2][1]*bMat[2][1] + values_[2][2]*bMat[2][2];
187 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"SmallMatrix wrong size!");
196 return values_[0][0]*bMat[0][0] + values_[0][1]*bMat[0][1] +
197 values_[1][0]*bMat[1][0] + values_[1][1]*bMat[1][1];
200 return values_[0][0]*bMat[0][0] + values_[0][1]*bMat[0][1] + values_[0][2]*bMat[0][2] +
201 values_[1][0]*bMat[1][0] + values_[1][1]*bMat[1][1] + values_[1][2]*bMat[1][2] +
202 values_[2][0]*bMat[2][0] + values_[2][1]*bMat[2][1] + values_[2][2]*bMat[2][2];
205 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"SmallMatrix wrong size!");
211void SmallMatrix<T>::trace(T &res)
216 res = values_[0][0] + values_[1][1];
220 res = values_[0][0] + values_[1][1] + values_[2][2];
223 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"SmallMatrix wrong size!");
229void SmallMatrix<T>::scale(T scalar){
231 for (
int i=0; i<size_; i++) {
232 for (
int j=0; j<size_; j++) {
233 values_[i][j] *= scalar;
240int SmallMatrix<T>::size()
const{
245void SmallMatrix<T>::resize(UN size){
246 for (
int i=0; i<size_; i++)
247 values_[i].resize(size);
249 values_.resize(size,Vec_T_Type(size));
254typename SmallMatrix<T>::SmallMatrix &SmallMatrix<T>::operator=(
SmallMatrix<T> &sm){
257 values_.resize(size_);
259 for (
int i=0; i<sm.size(); i++) {
260 values_.at(i) = sm[i];
267typename SmallMatrix<T>::SmallMatrix SmallMatrix<T>::operator*(
SmallMatrix<T> &other){
269 size_ = other.size();
272 this->multiply(other, result);
278typename SmallMatrix<T>::SmallMatrix SmallMatrix<T>::operator+(
SmallMatrix<T> &other){
280 size_ = other.size();
283 this->add(other, result);
289typename SmallMatrix<T>::SmallMatrix &SmallMatrix<T>::operator*=(
SmallMatrix<T> &other){
291 this->multiply(other, *
this);
297typename SmallMatrix<T>::SmallMatrix &SmallMatrix<T>::operator+=(
SmallMatrix<T> &other){
299 this->add(other, *
this);
305void SmallMatrix<T>::print(){
307 for (
int i=0; i<size_; i++) {
308 std::cout <<
"row "<< i <<
" values:";
309 for (
int j=0; j<size_; j++) {
310 std::cout <<
" "<< values_[i][j];
312 std::cout << std::endl;
319 T det = this->computeDet();
321 inverse.resize(size_);
324 inverse[0][0] = values_[1][1] / det;
325 inverse[0][1] = (-values_[0][1]) / det;
326 inverse[1][0] = (-values_[1][0]) / det;
327 inverse[1][1] = values_[0][0] / det;
330 inverse[0][0] = (values_[1][1] * values_[2][2] - values_[1][2] * values_[2][1]) / det;
331 inverse[0][1] = (values_[0][2] * values_[2][1] - values_[0][1] * values_[2][2]) / det;
332 inverse[0][2] = (values_[0][1] * values_[1][2] - values_[0][2] * values_[1][1]) / det;
334 inverse[1][0] = (values_[1][2] * values_[2][0] - values_[1][0] * values_[2][2]) / det;
335 inverse[1][1] = (values_[0][0] * values_[2][2] - values_[0][2] * values_[2][0]) / det;
336 inverse[1][2] = (values_[0][2] * values_[1][0] - values_[0][0] * values_[1][2]) / det;
338 inverse[2][0] = (values_[1][0] * values_[2][1] - values_[1][1] * values_[2][0]) / det;
339 inverse[2][1] = (values_[0][1] * values_[2][0] - values_[0][0] * values_[2][1]) / det;
340 inverse[2][2] = (values_[0][0] * values_[1][1] - values_[0][1] * values_[1][0]) / det;
344 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
"Matrix size is not 2 or 3.");
349double SmallMatrix<T>::computeDet( ){
353 det = values_[0][0] * values_[1][1] - values_[1][0] * values_[0][1];
356 det = values_[0][0] * values_[1][1] * values_[2][2] +
357 values_[0][1] * values_[1][2] * values_[2][0] +
358 values_[0][2] * values_[1][0] * values_[2][1] -
359 values_[2][0] * values_[1][1] * values_[0][2] -
360 values_[2][1] * values_[1][2] * values_[0][0] -
361 values_[2][2] * values_[1][0] * values_[0][1] ;
364 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
"Matrix size is not 2 or 3.");
371double SmallMatrix<T>::computeScaling( ){
375 scaling = std::sqrt( values_[0][0] * values_[0][0] + values_[1][0] * values_[1][0] );
378 double c1 = ( values_[1][0] * values_[2][1] - values_[2][0] * values_[1][1] );
379 double c2 = ( values_[2][0] * values_[0][1] - values_[0][0] * values_[2][1] );
380 double c3 = ( values_[0][0] * values_[1][1] - values_[1][0] * values_[0][1] );
382 scaling = std::sqrt( c1*c1 + c2*c2 + c3*c3 );
385 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
"Matrix size is not 2 or 3.");
This class represents a templated small Matrix of type T. Primarily created for 2x2 and 3x3 matrices....
Definition SmallMatrix.hpp:22
Adaptive Mesh Refinement.
Definition AdaptiveMeshRefinement.cpp:5