1 #include "../inc/sqmatrix3.h"
3 SqMatrix3::SqMatrix3(MatType type
)
5 m_cols
= new Vector3
[m_SIZE
];
6 if (type
!= Mat_Null
) {
7 m_cols
[0] = Vector3(Vector3::e1
);
8 m_cols
[1] = Vector3(Vector3::e2
);
9 m_cols
[2] = Vector3(Vector3::e3
);
13 SqMatrix3::SqMatrix3(const SqMatrix3
&other
)
15 m_cols
= new Vector3
[m_SIZE
];
16 _setCols(other
.m_cols
);
19 SqMatrix3::SqMatrix3 (const Vector3
&col1
, const Vector3
&col2
, const Vector3
& col3
)
21 m_cols
= new Vector3
[m_SIZE
];
27 SqMatrix3::~SqMatrix3()
34 SqMatrix3
& SqMatrix3::operator= (const SqMatrix3
&other
)
36 _setCols(other
.m_cols
);
40 // arithmetic operators
42 SqMatrix3
SqMatrix3::operator+ (const SqMatrix3
&other
) const
44 return SqMatrix3(*this) += other
;
47 SqMatrix3
& SqMatrix3::operator+= (const SqMatrix3
&other
)
49 for (unsigned int i(0) ; i
< m_SIZE
; ++i
) {
50 m_cols
[i
] += other
.m_cols
[i
];
56 SqMatrix3
SqMatrix3::operator- (const SqMatrix3
&other
) const
58 return SqMatrix3(*this) -= other
;
61 SqMatrix3
& SqMatrix3::operator-= (const SqMatrix3
&other
)
63 for (unsigned int i(0) ; i
< m_SIZE
; ++i
) {
64 m_cols
[i
] -= other
.m_cols
[i
];
70 // matrix multiplication
73 SqMatrix3
SqMatrix3::operator* (const SqMatrix3
&other
) const
77 #else // use naive algorithm
78 SqMatrix3
SqMatrix3::operator* (const SqMatrix3
&other
) const
80 SqMatrix3
result(Mat_Null
);
82 for (unsigned int i(0) ; i
< m_SIZE
; ++i
) {
83 for (unsigned int j(0) ; j
< m_SIZE
; ++j
) {
84 for (unsigned int k(0) ; k
< m_SIZE
; ++k
) {
85 result
.at(i
,j
) += this->at(i
,k
) * other
.at(k
,j
);
92 #endif // M_USE_STRASSEN
94 SqMatrix3
& SqMatrix3::operator*= (const SqMatrix3
&other
)
96 return (*this = *this * other
);
99 // scalar multiplication
100 SqMatrix3
SqMatrix3::operator* (double scalar
) const
102 return SqMatrix3(*this) *= scalar
;
105 SqMatrix3
& SqMatrix3::operator*= (double scalar
)
107 for (unsigned int i(0) ; i
< m_SIZE
; ++i
) {
114 Vector3
SqMatrix3::operator* (const Vector3
&vector
) const
116 // line vectors representing *this
117 Vector3
*m_rows
= new Vector3
[m_SIZE
];
118 for (unsigned int i(0) ; i
< m_SIZE
; ++i
) {
119 m_rows
[i
] = Vector3(this->at(i
,0), this->at(i
,1), this->at(i
,2));
122 // result is a vector3
124 for (unsigned int i(0) ; i
< m_SIZE
; ++i
) {
125 result
.at(i
) = m_rows
[i
].dotProduct(vector
, Vector3::EUCLID
); // inner product
134 // comparison operator
135 bool SqMatrix3::operator== (const SqMatrix3
&other
) const
137 for (unsigned int i(0) ; i
< m_SIZE
; ++i
) {
138 if (m_cols
[i
] != other
.m_cols
[i
]) {
145 bool SqMatrix3::operator!= (const SqMatrix3
&other
) const
147 return !(*this == other
);
151 SqMatrix3
SqMatrix3::transpose() const
153 SqMatrix3
result(Mat_Null
);
154 for (unsigned int i(0) ; i
< m_SIZE
; ++i
) {
155 result
.m_cols
[i
] = Vector3(this->at(i
,0), this->at(i
,1), this->at(i
,2));
160 SqMatrix3
& SqMatrix3::setToTransposed()
162 return ((*this) = this->transpose());
165 // determinant (alias function det points directly to determinant, could be useful if we are lazy)
166 double SqMatrix3::determinant() const
168 return (((this->at(0,0)) * ((this->at(1,1))*this->at(2,2) - (this->at(1,2))*(this->at(2,1))))
169 - ((this->at(0,1)) * ((this->at(1,0))*this->at(2,2) - (this->at(1,2))*(this->at(2,0))))
170 + ((this->at(0,2)) * ((this->at(1,0))*this->at(2,1) - (this->at(1,1))*(this->at(2,0)))));
173 double SqMatrix3::det() const
175 return this->determinant();
178 // inverse (direct formula)
179 SqMatrix3
SqMatrix3::inverse() const
181 double det(this->determinant());
183 throw std::domain_error("Tried to inverse matrix with null determinant");
186 Vector3
col1((this->at(1,1))*(this->at(2,2)) - (this->at(1,2))*(this->at(2,1)),
187 (this->at(1,2))*(this->at(2,0)) - (this->at(1,0))*(this->at(2,2)),
188 (this->at(1,0))*(this->at(2,1)) - (this->at(1,1))*(this->at(2,0)));
189 Vector3
col2((this->at(0,2))*(this->at(2,1)) - (this->at(0,1))*(this->at(2,2)),
190 (this->at(0,0))*(this->at(2,2)) - (this->at(0,2))*(this->at(2,0)),
191 (this->at(0,1))*(this->at(2,0)) - (this->at(0,0))*(this->at(2,1)));
192 Vector3
col3((this->at(0,1))*(this->at(1,2)) - (this->at(0,2))*(this->at(1,1)),
193 (this->at(0,2))*(this->at(1,0)) - (this->at(0,0))*(this->at(1,2)),
194 (this->at(0,0))*(this->at(1,1)) - (this->at(0,1))*(this->at(1,0)));
196 SqMatrix3
result(col1
, col2
, col3
);
197 return (result
* (1/det
));
200 SqMatrix3
& SqMatrix3::setToInverse()
202 return (*this = this->inverse());
205 // index operators : indexes are common math indexes, not C++ array indexes
206 // setter (math-like)
207 double& SqMatrix3::operator() (unsigned int row
, unsigned int col
)
209 if ((row
> m_SIZE
) or (col
> m_SIZE
)) {
210 throw std::out_of_range("Passed ref index > 3 on 3 by 3 matrix");
212 else if ((row
<= 0) or (col
<= 0)) {
213 throw std::out_of_range("Passed ref index 0 to SqMatrix3::operator()");
216 return m_cols
[col
-1][row
];
219 // getter (math-like)
220 double SqMatrix3::operator() (unsigned int row
, unsigned int col
) const
222 if ((row
> m_SIZE
) or (col
> m_SIZE
)) {
223 throw std::out_of_range("Passed ref index > 3 on 3 by 3 matrix");
225 else if ((row
<= 0) or (col
<= 0)) {
226 throw std::out_of_range("Passed ref index 0 to SqMatrix::operator()");
229 return m_cols
[col
-1][row
];
233 double& SqMatrix3::at (unsigned int row
, unsigned int col
)
235 if ((row
>= m_SIZE
) or (col
>= m_SIZE
)) {
236 throw std::out_of_range("Passed ref index > 3 on 3 by 3 matrix");
239 return m_cols
[col
].at(row
);
243 double SqMatrix3::at (unsigned int row
, unsigned int col
) const
245 if ((row
>= m_SIZE
) or (col
>= m_SIZE
)) {
246 throw std::out_of_range("Passed ref index > 3 on 3 by 3 matrix");
249 return m_cols
[col
].at(row
);
252 // show matrix using cout by default
253 void SqMatrix3::show(std::ostream
&out
) const
255 for (unsigned int i(0) ; i
< m_SIZE
; ++i
) {
256 for (unsigned int j(0) ; j
< m_SIZE
; ++j
) {
257 out
<< std::setfill(' ') << std::setw(6) << at(i
,j
) << " ";
264 // _setCols : copy operator for attribute m_cols
265 void SqMatrix3::_setCols (const Vector3
*cols
) // cols is intended to be a 3-dim array
267 for (unsigned int i(0) ; i
< m_SIZE
; ++i
) {
273 // external product (scalar product)
274 SqMatrix3
operator* (double scalar
, const SqMatrix3
&matrix
)
276 return matrix
* scalar
;
280 std::ostream
& operator<< (std::ostream
&out
, const SqMatrix3
&matrix
)