My Project
gmatrix.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "../stdafx.h"
3 #include "gmatrix.h"
4 
6 {
7  for (size_t i = 0; i <= 3; i++)
8  {
9  GVector tmp;
10  this->matrix.push_back(tmp);
11  }
12 }
13 
15 {
16  for (size_t i = 0; i <= 3; i++)
17  {
18  this->matrix.push_back(other[i]);
19  }
20 }
21 
23 {
24  for (size_t i = 0; i <= 3; i++)
25  {
26  this->matrix.push_back(other[i]);
27  }
28 }
29 
31 {
32  this->matrix.clear();
33 }
34 
36 {
37  this->matrix.clear();
38  for (size_t i = 0; i <= 3; i++)
39  {
40  this->matrix.push_back(other[i]);
41  }
42 
43  return (*this);
44 }
45 
46 GVector& GMatrix::operator[](const unsigned long index)
47 {
48  return this->matrix[index];
49 }
50 
51 const GVector& GMatrix::operator[](const unsigned long index) const
52 {
53  return this->matrix[index];
54 }
55 
57 {
58  GMatrix result(*this);
59  result = result * (-1);
60  return result;;
61 }
62 
63 GMatrix GMatrix::operator*(const double value)
64 {
65  GMatrix result(*this);
66 
67  for (size_t i = 0; i <= 3; i++)
68  {
69  for (size_t j = 0; j <= 3; j++)
70  {
71  result[i][j] = result[i][j] * value;
72  }
73  }
74  return result;
75 }
76 
78 {
79  GMatrix result(*this);
80 
81  for (size_t i = 0; i <= 3; i++)
82  {
83  GVector row((*this)[i]);
84  for (size_t j = 0; j <= 3; j++)
85  {
86  GVector column(other[0][j], other[1][j], other[2][j], other[3][j]);
87  double sum = 0;
88  for (size_t k = 0; k <= 3; k++)
89  sum += row[k] * column[k];
90  result[i][j] = sum;
91  }
92  }
93  return result;
94 }
95 
97 {
98  GMatrix result;
99 
100  for (int i = 0; i <= 3; ++i)
101  {
102  for (int j = 0; j <= 3; ++j)
103  result[i][j] = 0.0;
104 
105  result[i][i] = 1.0;
106  }
107 
108 
109  GMatrix tmp(*this);
110 
111  for (int k = 0; k <= 3; ++k)
112  {
113  if (fabs(tmp[k][k]) < 1e-8)
114  {
115  bool changed = false;
116  for (int i = k + 1; i <= 3; ++i)
117  {
118  if (fabs(tmp[i][k]) > 1e-8)
119  {
120  std::swap(tmp[k], tmp[i]);
121  std::swap(result[k], result[i]);
122 
123  changed = true;
124  break;
125  }
126  }
127 
128  if (!changed)
129  {
130  return false;
131  }
132  }
133 
134 
135  double div = tmp[k][k];
136  for (int j = 0; j <= 3; ++j)
137  {
138  tmp[k][j] /= div;
139  result[k][j] /= div;
140  }
141 
142  for (int i = k + 1; i <= 3; ++i)
143  {
144  double multi = tmp[i][k];
145  for (int j = 0; j <= 3; ++j)
146  {
147  tmp[i][j] -= multi * tmp[k][j];
148  result[i][j] -= multi * result[k][j];
149  }
150  }
151  }
152 
153  for (int k = 3; k > 0; --k)
154  {
155  for (int i = k - 1; i + 1 > 0; --i)
156  {
157  double multi = tmp[i][k];
158  for (int j = 0; j <= 3; ++j)
159  {
160  tmp[i][j] -= multi * tmp[k][j];
161  result[i][j] -= multi * result[k][j];
162  }
163  }
164  }
165 
166  *this = result;
167 
168  return true;
169 }
170 
172 {
173  GMatrix result;
174  for (int i = 0; i <= 3; i++)
175  {
176  for (int j = 0; j <= 3; j++)
177  {
178  result[i][j] = this->matrix[j][i];
179  }
180  }
181  *this = result;
182  return *this;
183 }
~GMatrix()
Definition: gmatrix.cpp:30
GVector & operator[](const unsigned long index)
Definition: gmatrix.cpp:46
GMatrix operator-()
Definition: gmatrix.cpp:56
Define geometric vector.
Definition: gvector.h:26
GMatrix operator*(const double value)
Definition: gmatrix.cpp:63
Define geometric matrix.
GMatrix & operator=(GMatrix other)
Definition: gmatrix.cpp:35
GMatrix()
Definition: gmatrix.cpp:5
bool inverse()
Definition: gmatrix.cpp:96
Define geometric matrix.
Definition: gmatrix.h:24
GMatrix transposition()
Definition: gmatrix.cpp:171