Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Matrix.C
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
29 #include "Matrix.h"
30 #include <cmath>
31 #include <cassert>
32 #include <iostream>
33 
35 
36 using namespace std;
37 
38 // For debugging purposes
39 void Matrix::info(){
40  cout << "Hi, I'm a matrix with " << rows_ << " rows and " << cols_ << " cols_" << endl;
41  cout << " DATA = ";
42  for (int i = 0; i < rows_ * cols_; i ++){
43  cout <<data_[i] << " ";
44  }
45  cout << endl;
46 }
47 
48 double &Matrix::operator() (int r, int c){
49  assert ( 0 <= r && r <= rows_ && 0 <= c && c <= cols_);
50  return data_[r*cols_ + c];
51 }
52 
53 double Matrix::operator() (int r, int c) const {
54  assert ( 0 <= r && r <= rows_ && 0 <= c && c <= cols_);
55  return data_[r*cols_ + c];
56 }
57 
58 const Matrix &Matrix::operator = (const Matrix &right){
59  if (&right != this) {
60  rows_ = right.rows_;
61  cols_ = right.cols_;
62 
63  for (int r = 0; r < rows_; r ++){
64  for (int c = 0; c < cols_; c ++ ){
65  data_[r*cols_+c] = right.data_[r*cols_+c];
66  }
67  }
68  }
69  return *this;
70 }
71 
72 // An operator used for the creation of the tensor matrix
73 // L ^= A sets L to A transpose times A
74 const Matrix &Matrix::operator ^= (const Matrix &right){
75  if (&right != this) {
76  rows_ = right.cols_;
77  cols_ = right.cols_;
78 
79  for (int r = 0; r < rows_; r ++){
80  for (int c = 0; c < rows_; c ++ ){
81  data_[r*cols_+c] = 0;
82  for (int j = 0; j < right.rows_; j ++){
83  data_[r*rows_+c] += right(j,r) * right(j,c) ;
84  }
85  }
86  }
87  }
88  return *this;
89 }
90 
91 // For debugging purposes
92 ostream &operator <<(ostream &output, const Matrix &m){
93  for (int r = 0; r <m.rows_; r++){
94  for (int c = 0; c < m.cols_; c++){
95  output << m(r,c) << " ";
96  }
97  output << endl;
98  }
99  return output;
100 }
101 
102 // The Jacobian matrix class, for 3 by 2 and 3 by 3 matrices
103 //J_Matrix::J_Matrix()
104 // :Matrix()
105 //{
106 //}
107 
108 J_Matrix::J_Matrix(Vector_3<double> pnts[],int cols)
109 {
110  //cols is the number of columns in the resulting jacobian matrix
111  //cols == 2 for triangles and quads, 3 for tets, or hexes
112  rows_ = 3;
113  cols_ = cols;
114 
115  for (int r = 0; r < cols_; r++){
116  for (int c = 0; c < 3; c++){
117  data_[c*cols_+r] = pnts[r][c];
118  }
119  }
120 }
121 
122 double J_Matrix::det (){
123 // in the case for size == 2 (jacobian matrix not square)
124 // the determinant should be twice the area, so just caclulate
125 // the area and double it.
126 //
127 // 2 2 2
128 // | y_0 z_0 1 | | x_0 x_0 1 | | x_0 y_0 1 |
129 // area = 1/2 * sqrt ( | y_1 z_1 1 | + | z_1 x_1 1 | + | x_1 y_1 1 | )
130 // | y_2 z_2 1 | | z_2 x_2 1 | | x_2 y_2 1 |
131 //
132 // see http://mathworld.wolfram.com/TriangleArea.html for more details
133  assert( (rows_ == 3) && (cols_ == 2 || cols_ ==3) );
134  if (cols_ == 2){
135  //return (*this)(0,0)*(*this)(1,1) - (*this)(0,1) * (*this)(1,0);
136  double s1 = (*this)(1,0)*(*this)(2,1) - (*this)(1,1)*(*this)(2,0);
137  double s2 = (*this)(2,0)*(*this)(0,1) - (*this)(2,1)*(*this)(0,0);
138  double s3 = (*this)(0,0)*(*this)(1,1) - (*this)(0,1)*(*this)(1,0);
139  s1 *= s1;
140  s2 *= s2;
141  s3 *= s3;
142  return sqrt ( s1 + s2 + s3 );
143  }
144  else
145  return
146  (*this)(0,0) * ((*this)(1,1)*(*this)(2,2)-(*this)(1,2)*(*this)(2,1)) -
147  (*this)(0,1) * ((*this)(1,0)*(*this)(2,2)-(*this)(1,2)*(*this)(2,0)) +
148  (*this)(0,2) * ((*this)(1,0)*(*this)(2,1)-(*this)(1,1)*(*this)(2,0));
149 }
150 
152 
153 
154 
155 
156 
157 
double sqrt(double d)
Definition: double.h:73
blockLoc i
Definition: read.cpp:79
#define MOP_END_NAMESPACE
Definition: mopbasic.h:29
Declaration of Matrix class.
double det(const Matrix3D &A)
j indices j
Definition: Indexing.h:6
#define MOP_BEGIN_NAMESPACE
Definition: mopbasic.h:28
std::ostream & operator<<(std::ostream &os, const COM_exception &ex)
Print out a given exception.
void info()
Print informations about CImg environement variables.
Definition: CImg.h:5702