5 #include "TMM_enable_if.hpp"
10 extern char* dtostrf (
double __val,
signed char __width,
unsigned char __prec,
char * __s);
12 extern char* (*dtostrf_func)(double,
signed char,
unsigned char,
char *);
16 #ifdef USING_STANDARD_LIBRARY
25 typedef unsigned char Size;
27 template<Size n, Size m,
typename Scalar =
float>
34 for(Size i = 0; i < n; i++)
35 for(Size j = 0; j < m; j++)
39 Matrix(
const float M[n][m]){
40 for(Size i = 0; i < n; i++)
41 for(Size j = 0; j < m; j++)
46 for(Size i = 0; i < n; i++)
47 for(Size j = 0; j < m; j++)
55 for(Size i = 0; i < n; i++)
56 for(Size j = 0; j < m; j++)
57 data[i][j]=M.data[i][j];
63 operator=(
const Scalar M[n][m])
65 for(Size i = 0; i < n; i++)
66 for(Size j = 0; j < m; j++)
73 operator=(
const Scalar value)
75 for(Size i = 0; i < n; i++)
76 for(Size j = 0; j < m; j++)
86 template<
typename T = Scalar,
typename = tmm::enable_if_t<(m==1&&n==1), T>>
operator T() {
95 template<
typename Other_Scalar>
97 for(Size i = 0; i < n; i++)
98 for(Size j = 0; j < m; j++){
99 Scalar comp = data[i][j]-other.data[i][j];
100 if(comp < -tolerance || comp > tolerance)
return false;
109 template<
typename Other_Scalar>
111 return equals<Other_Scalar>(other, 0.f);
121 for(Size i = 0; i < n; i++)
122 for(Size j = 0; j < m; j++)
126 for(Size i = 0; i < n; i++)
127 for(Size j = 0; j < m; j++)
128 M[i][j+m]=other.data[i][j];
136 augmentBefore(
const Matrix<n,q,Scalar> &other)
const
138 Matrix<n,m+q,Scalar> M;
139 for(Size i = 0; i < n; i++)
140 for(Size j = 0; j < m; j++)
141 M[i][j+q]=data[i][j];
144 for(Size i = 0; i < n; i++)
145 for(Size j = 0; j < m; j++)
154 augmentAbove(
const Matrix<p,m,Scalar> &other)
const
156 Matrix<n+p,m,Scalar> M;
157 for(Size i = 0; i < n; i++)
158 for(Size j = 0; j < m; j++)
159 M[i+p][j]=data[i][j];
162 for(Size i = 0; i < n; i++)
163 for(Size j = 0; j < m; j++)
164 M[i][j]=other.data[i][j];
173 augmentBelow(
const Matrix<p,m,Scalar> &other)
const
175 Matrix<n+p,m,Scalar> M;
176 for(Size i = 0; i < n; i++)
177 for(Size j = 0; j < m; j++)
181 for(Size i = 0; i < n; i++)
182 for(Size j = 0; j < m; j++)
183 M[i+n][j]=other.data[i][j];
193 operator *(
const Matrix<m,q,Scalar> &other)
const
195 Matrix<n,q,Scalar> M;
196 for(Size i = 0; i < n; i++)
197 for(Size j = 0; j < q; j++)
198 for(Size k = 0; k < m; k++)
199 M[i][j]+=data[i][k]*other.data[k][j];
208 elementwise_times(
const Matrix<n,m,Scalar> &other)
const
210 Matrix<n,m,Scalar> M;
211 for(Size i = 0; i < n; i++)
212 for(Size j = 0; j < m; j++)
213 M[i][j]=data[i][j]*other.data[i][j];
224 Matrix<n,m,Scalar> M;
225 for(Size i = 0; i < n; i++)
226 for(Size j = 0; j < m; j++)
236 operator +(
const Matrix<n,m,Scalar> &other)
const
238 Matrix<n,m,Scalar> M;
239 for(Size i = 0; i < n; i++)
240 for(Size j = 0; j < m; j++)
241 M[i][j]=data[i][j]+other.data[i][j];
248 operator -(
const Matrix<n,m,Scalar> &other)
const
250 Matrix<n,m,Scalar> M;
251 for(Size i = 0; i < n; i++)
252 for(Size j = 0; j < m; j++)
253 M[i][j]=data[i][j]-other.data[i][j];
260 operator +(Scalar a)
const
262 Matrix<n,m,Scalar> M;
263 for(Size i = 0; i < n; i++)
264 for(Size j = 0; j < m; j++)
265 M[i][j]=data[i][j]+a;
270 operator -(Scalar a)
const
272 Matrix<n,m,Scalar> M;
273 for(Size i = 0; i < n; i++)
274 for(Size j = 0; j < m; j++)
275 M[i][j]=data[i][j]-a;
281 operator *(Scalar a)
const
283 Matrix<n,m,Scalar> M;
284 for(Size i = 0; i < n; i++)
285 for(Size j = 0; j < m; j++)
286 M[i][j]=data[i][j]*a;
290 operator /(Scalar a)
const
292 Matrix<n,m,Scalar> M;
293 for(Size i = 0; i < n; i++)
294 for(Size j = 0; j < m; j++)
295 M[i][j]=data[i][j]/a;
304 Matrix<m,n,Scalar> M;
305 for(Size i = 0; i < n; i++)
306 for(Size j = 0; j < m; j++)
319 template<Size p, Size q>
321 get(Size c, Size d)
const
323 Matrix<p,q,Scalar> M;
324 for(Size i = 0; i < p; i++)
325 for(Size j = 0; j < q; j++)
326 M[i][j]=data[i+c][j+d];
331 set(Size i, Size j, Scalar newVal)
337 template<Size p, Size q>
339 set(Size c, Size d, Matrix<p,q,Scalar> newVal)
341 for(Size i = 0; i < p; i++)
342 for(Size j = 0; j < q; j++)
343 data[i+c][j+d] = newVal[i][j];
351 return get<1, m>(i, 0);
358 return get<n,1>(0, j);
366 for(Size i = 0; i < n; i++)
367 for(Size j = 0; j < m; j++)
368 other[i][j]=data[i][j];
374 printTo(Print &serial)
const
376 for(Size i = 0; i < n; i++)
378 for(Size j = 0; j < m; j++)
382 dtostrf_func(data[i][j], 6, 3, buf);
386 serial.print(data[i][j]);
394 #ifdef USING_STANDARD_LIBRARY
396 printTo(std::ostream &out)
const
398 for(Size i = 0; i < n; i++)
400 for(Size j = 0; j < m; j++)
402 out << std::setw(6) << data[i][j];
411 #ifndef TMM_DISABLE_RECURSIVE
412 template <
typename T = Scalar>
413 tmm::enable_if_t<(m==n), T>
416 if(n == 0) {
return 1; }
417 if(n == 1) {
return Matrix<n,n,Scalar>::data[0][0]; }
418 if(n == 2) {
return Matrix<n,n,Scalar>::data[0][0] * Matrix<n,n,Scalar>::data[1][1] - Matrix<n,n,Scalar>::data[0][1] * Matrix<n,n,Scalar>::data[1][0]; }
421 for(Size i = 0; i < n; i++){
423 Matrix<(n>0?n-1:n),(n>0?n-1:n),Scalar> submatrix;
429 for(Size p = 0; p < n-1; p ++){
430 for(Size q = 0; q < i; q++){
431 submatrix[p][q]=Matrix<n,n,Scalar>::data[p+1][q];
434 for(Size q = i+1; q < n; q++){
435 submatrix[p][q]=Matrix<n,n,Scalar>::data[p+1][q-1];
439 ret = ret + sign * Matrix<n,n,Scalar>::data[0][i] * submatrix.determinant();
449 template <
typename T = Matrix<n,n,Scalar>>
450 tmm::enable_if_t<(m==n), T>
453 Matrix<n,n,Scalar> M;
454 for(Size i = 0; i < n; i ++){
455 for(Size j = 0; j < n; j++){
465 Matrix<n-1,n-1,Scalar> t;
466 for(Size p = 0; p < i; p ++){
467 for(Size q = 0; q < j; q++){
468 t[p][q]=Matrix<n,n,Scalar>::data[p][q];
470 for(Size q = j+1; q < n; q++){
471 t[p][q]=Matrix<n,n,Scalar>::data[p][q-1];
474 for(Size p = i+1; p < n; p ++){
475 for(Size q = 0; q < j; q++){
476 t[p-1][q]=Matrix<n,n,Scalar>::data[p-1][q];
478 for(Size q = j+1; q < n; q++){
479 t[p-1][q]=Matrix<n,n,Scalar>::data[p-1][q-1];
482 M[i][j] = t.determinant();
485 if (i+j%2==1) M[i][j] = -M[i][j];
499 template <
typename T = Matrix<n,n,Scalar>>
500 tmm::enable_if_t<(m==n), T>
507 return adjugate / determinant();
517 template<Size n,
typename Scalar =
float>
520 Matrix<n,n,Scalar> I;
521 for(Size i = 0; i < n; i++)
526 template<Size n, Size m,
typename Scalar =
float>
529 Matrix<n,m,Scalar> M;
Definition: TMM_matrix.hpp:28
bool operator==(const Matrix< n, m, Other_Scalar > &other) const
Returns true if all elements of this matrix are identically equal to the elements of another matrix.
Definition: TMM_matrix.hpp:110
void copyTo(Matrix< n, m, Scalar > &other) const
Copies the contents of this matrix to another matrix.
Definition: TMM_matrix.hpp:364
tmm::enable_if_t<(m==n), T > inverse() const
Attempts to invert the matrix.
Definition: TMM_matrix.hpp:501
bool equals(const Matrix< n, m, Scalar > &other, Scalar tolerance) const
Returns true if all elements of this matrix are equal to the elements of another matrix,...
Definition: TMM_matrix.hpp:96