File include/jmath/matrix/base.h changed (mode: 100644) (index beed3be..d2363c0) |
17 |
17 |
*/ |
*/ |
18 |
18 |
#pragma once |
#pragma once |
19 |
19 |
#include "../vector/base.h" |
#include "../vector/base.h" |
20 |
|
#include <type_traits> |
|
21 |
|
|
|
22 |
|
#define T_NUMERIC template<typename numeric_t> constexpr |
|
23 |
|
#define T_OTHER template<typename other_t> constexpr |
|
24 |
20 |
|
|
25 |
21 |
namespace jm { |
namespace jm { |
26 |
|
template<int size, int vector_size, typename type> struct matrix; |
|
|
22 |
|
template<int size, int vector_size, typename T> struct matrix; |
27 |
23 |
} |
} |
28 |
24 |
//TODO make initialization without excess braces |
//TODO make initialization without excess braces |
29 |
|
template<int size, int vector_size, typename type> |
|
|
25 |
|
template<int size, int vector_size, typename T> |
30 |
26 |
struct jm::matrix |
struct jm::matrix |
31 |
27 |
{ |
{ |
32 |
|
using vector = vector<vector_size, type>; |
|
33 |
|
vector _[size]; |
|
|
28 |
|
vector<vector_size, T> _[size]; |
34 |
29 |
|
|
35 |
|
template<int other_size, int other_vector_size, typename other_t> |
|
36 |
|
[[nodiscard]] constexpr inline static matrix |
|
37 |
|
from(const matrix<other_size, other_vector_size, other_t> &); |
|
|
30 |
|
template<int size2, int vsize2, typename T2> [[nodiscard]] constexpr static |
|
31 |
|
matrix from(const matrix<size2, vsize2, T2> &); |
38 |
32 |
|
|
39 |
|
T_OTHER operator matrix<size, vector_size, other_t> () { |
|
40 |
|
return matrix<size, vector_size, other_t>::from(*this); |
|
|
33 |
|
template<typename T2> constexpr |
|
34 |
|
operator matrix<size, vector_size, T2> () { |
|
35 |
|
return matrix<size, vector_size, T2>::from(*this); |
41 |
36 |
} |
} |
42 |
37 |
|
|
43 |
|
[[nodiscard]] constexpr bool operator==(const matrix &other) { |
|
|
38 |
|
[[nodiscard]] constexpr |
|
39 |
|
bool operator==(const matrix &other) { |
44 |
40 |
return memcmp(this, &other, sizeof(*this)) == 0; |
return memcmp(this, &other, sizeof(*this)) == 0; |
45 |
41 |
} |
} |
46 |
|
constexpr inline bool operator!=(const matrix &m) { return *this != m; } |
|
47 |
|
|
|
48 |
|
T_NUMERIC vector& operator[](numeric_t i) { return _[i]; } |
|
49 |
|
T_NUMERIC vector operator[](numeric_t i) const { return _[i]; } |
|
|
42 |
|
constexpr |
|
43 |
|
bool operator!=(const matrix &m) { |
|
44 |
|
return *this != m; |
|
45 |
|
} |
50 |
46 |
|
|
51 |
|
template<int other_size, int other_vector_size, typename other_t> |
|
52 |
|
constexpr inline matrix& |
|
53 |
|
operator *= (const matrix<other_size, other_vector_size, other_t>&); |
|
|
47 |
|
template<typename N> [[nodiscard]] constexpr |
|
48 |
|
vector<vector_size, T>& operator[](N i) { |
|
49 |
|
return _[i]; |
|
50 |
|
} |
|
51 |
|
template<typename N> [[nodiscard]] constexpr |
|
52 |
|
vector<vector_size, T> operator[](N i) const { |
|
53 |
|
return _[i]; |
|
54 |
|
} |
54 |
55 |
|
|
55 |
|
template<int other_size, int other_vector_size, typename other_t> |
|
56 |
|
constexpr inline matrix<size, other_vector_size, type> |
|
57 |
|
operator *(const matrix<other_size, other_vector_size, other_t>&) const; |
|
|
56 |
|
template<int size2, int vsize2, typename T2> constexpr |
|
57 |
|
matrix& operator *= (const matrix<size2, vsize2, T2>&); |
|
58 |
|
template<int size2, int vsize2, typename T2> [[nodiscard]] constexpr |
|
59 |
|
matrix<size, vsize2, T> operator *(const matrix<size2, vsize2, T2>&) const; |
58 |
60 |
|
|
59 |
|
template<typename other_t> [[nodiscard]] constexpr inline |
|
60 |
|
vector operator *(const jm::vector<size, other_t>&) const; |
|
|
61 |
|
template<typename T2> [[nodiscard]] constexpr |
|
62 |
|
vector<vector_size, T> |
|
63 |
|
operator *(const jm::vector<size, T2>&) const; |
61 |
64 |
|
|
62 |
|
T_NUMERIC matrix& operator += (const numeric_t&); |
|
63 |
|
T_NUMERIC matrix& operator -= (const numeric_t&); |
|
64 |
|
T_NUMERIC matrix& operator /= (const numeric_t&); |
|
65 |
|
T_NUMERIC matrix operator + (const numeric_t&n) const {return *this += n;} |
|
66 |
|
T_NUMERIC matrix operator - (const numeric_t&n) const {return *this -= n;} |
|
67 |
|
T_NUMERIC matrix operator / (const numeric_t&n) const {return *this /= n;} |
|
|
65 |
|
template<typename N> constexpr |
|
66 |
|
matrix& |
|
67 |
|
operator += (const N&); |
|
68 |
|
template<typename N> constexpr |
|
69 |
|
matrix& |
|
70 |
|
operator -= (const N&); |
|
71 |
|
template<typename N> constexpr |
|
72 |
|
matrix& |
|
73 |
|
operator /= (const N&); |
|
74 |
|
template<typename N> [[nodiscard]] constexpr |
|
75 |
|
matrix |
|
76 |
|
operator + (const N&n) const { |
|
77 |
|
return *this += n; |
|
78 |
|
} |
|
79 |
|
template<typename N> [[nodiscard]] constexpr |
|
80 |
|
matrix |
|
81 |
|
operator - (const N&n) const { |
|
82 |
|
return *this -= n; |
|
83 |
|
} |
|
84 |
|
template<typename N> [[nodiscard]] constexpr |
|
85 |
|
matrix |
|
86 |
|
operator / (const N&n) const { |
|
87 |
|
return *this /= n; |
|
88 |
|
} |
68 |
89 |
}; |
}; |
69 |
|
|
|
70 |
|
#undef T_NUMERIC |
|
71 |
|
#undef T_OTHER |
|
File include/jmath/matrix/base.hpp changed (mode: 100644) (index 4c8a00b..170e592) |
17 |
17 |
*/ |
*/ |
18 |
18 |
#pragma once |
#pragma once |
19 |
19 |
#include "base.h" |
#include "base.h" |
20 |
|
#include <cmath> |
|
21 |
20 |
|
|
22 |
|
#define MATR_T \ |
|
23 |
|
template<int size, int vector_size, typename type> |
|
24 |
|
#define T_OTHER_OTHER_IMP \ |
|
25 |
|
template<int size, int vector_size, typename type> \ |
|
26 |
|
template<int other_size, int other_vector_size, typename other_t> constexpr |
|
27 |
|
#define T_NUMERIC_IMPL \ |
|
28 |
|
template<int size, int vector_size, typename type> \ |
|
29 |
|
template<typename numeric_t> constexpr |
|
30 |
|
#define MATRIX \ |
|
31 |
|
jm::matrix<size, vector_size, type> |
|
32 |
|
#define MATRIX_OTHER_OTHER \ |
|
33 |
|
jm::matrix<other_size, other_vector_size, other_t> |
|
34 |
|
|
|
35 |
|
T_OTHER_OTHER_IMP MATRIX MATRIX::from(const MATRIX_OTHER_OTHER &other) { |
|
36 |
|
MATRIX m{}; int i = 0, j = 0; |
|
37 |
|
for (; i < size && i < other_size; ++i) { |
|
38 |
|
for (j = 0; j < vector_size && j < other_vector_size; ++j) |
|
39 |
|
m._[i][j] = type(other._[i][j]); |
|
40 |
|
for (; j < vector_size; ++j) |
|
41 |
|
m._[i][j] = type(0); |
|
|
21 |
|
template<int S, int VS, typename T> |
|
22 |
|
template<int S2, int VS2, typename T2> constexpr |
|
23 |
|
jm::matrix<S, VS, T> |
|
24 |
|
jm::matrix<S, VS, T>::from(const jm::matrix<S2, VS2, T2> &other) { |
|
25 |
|
jm::matrix<S, VS, T> m{}; int i = 0, j = 0; |
|
26 |
|
for (; i < S && i < S2; ++i) { |
|
27 |
|
for (j = 0; j < VS && j < VS2; ++j) |
|
28 |
|
m._[i][j] = T(other._[i][j]); |
|
29 |
|
for (; j < VS; ++j) |
|
30 |
|
m._[i][j] = T(0); |
42 |
31 |
} |
} |
43 |
|
for (; i < size; ++i) |
|
44 |
|
for (; j < vector_size; ++j) |
|
45 |
|
m._[i][j] = type(0); |
|
|
32 |
|
for (; i < S; ++i) |
|
33 |
|
for (; j < VS; ++j) |
|
34 |
|
m._[i][j] = T(0); |
46 |
35 |
return m; |
return m; |
47 |
36 |
} |
} |
48 |
|
MATR_T template<int other_size, int other_vector_size, typename other_t> |
|
49 |
|
constexpr inline MATRIX& |
|
50 |
|
MATRIX::operator*=(const matrix<other_size, other_vector_size, other_t>& m) { |
|
51 |
|
static_assert(other_vector_size == vector_size); |
|
52 |
|
return *this = (*this) * m; |
|
53 |
|
} |
|
54 |
|
MATR_T template<int other_size, int other_vector_size, typename other_t> |
|
55 |
|
[[nodiscard]] constexpr jm::matrix<size, other_vector_size, type> |
|
56 |
|
MATRIX::operator*(const matrix<other_size, other_vector_size, other_t>& other) |
|
57 |
|
const { |
|
58 |
|
static_assert(other_size == vector_size); |
|
59 |
|
matrix<size, other_vector_size, type> m {}; |
|
60 |
|
for(int i = 0; i < size; i++) |
|
61 |
|
for(int k = 0; k < vector_size; k++) |
|
62 |
|
for(int j = 0; j < other_vector_size; j++) |
|
|
37 |
|
|
|
38 |
|
template<int S, int VS, typename T> |
|
39 |
|
template<int S2, int VS2, typename T2> [[nodiscard]] constexpr |
|
40 |
|
jm::matrix<S, VS2, T> |
|
41 |
|
jm::matrix<S, VS, T>::operator*(const matrix<S2, VS2, T2>& other) const { |
|
42 |
|
static_assert(S2 == VS); |
|
43 |
|
matrix<S, VS2, T> m {}; |
|
44 |
|
for(int i = 0; i < S; i++) |
|
45 |
|
for(int k = 0; k < VS; k++) |
|
46 |
|
for(int j = 0; j < VS2; j++) |
63 |
47 |
m._[i][j] += _[i][k] * other._[k][j]; |
m._[i][j] += _[i][k] * other._[k][j]; |
64 |
48 |
return m; |
return m; |
65 |
49 |
} |
} |
66 |
|
MATR_T template<typename other_t> |
|
67 |
|
[[nodiscard]] constexpr inline typename MATRIX::vector |
|
68 |
|
MATRIX::operator *(const jm::vector<size, other_t>& vec) const |
|
69 |
|
{ |
|
70 |
|
MATRIX::vector v{}; |
|
71 |
|
for(int i = 0; i < size; i++) |
|
72 |
|
for(int k = 0; k < vector_size; k++) |
|
|
50 |
|
template<int S, int VS, typename T> |
|
51 |
|
template<int S2, int VS2, typename T2> constexpr |
|
52 |
|
jm::matrix<S, VS, T>& |
|
53 |
|
jm::matrix<S, VS, T>::operator*=(const matrix<S2, VS2, T2>& m) { |
|
54 |
|
static_assert(VS2 == VS); |
|
55 |
|
return *this = (*this) * m; |
|
56 |
|
} |
|
57 |
|
|
|
58 |
|
template<int S, int VS, typename T> |
|
59 |
|
template<typename T2> [[nodiscard]] constexpr |
|
60 |
|
jm::vector<VS, T> |
|
61 |
|
jm::matrix<S, VS, T>::operator *(const jm::vector<S, T2>& vec) const { |
|
62 |
|
vector<VS, T> v{}; |
|
63 |
|
for(int i = 0; i < S; i++) |
|
64 |
|
for(int k = 0; k < VS; k++) |
73 |
65 |
v[i] += (*this)[i][k] * vec[k]; |
v[i] += (*this)[i][k] * vec[k]; |
74 |
66 |
return v; |
return v; |
75 |
67 |
} |
} |
76 |
|
T_NUMERIC_IMPL MATRIX& MATRIX::operator += (const numeric_t& value) { |
|
77 |
|
for (int i = 0; i < size; ++i) |
|
|
68 |
|
|
|
69 |
|
template<int S, int VS, typename T> |
|
70 |
|
template<typename N> constexpr |
|
71 |
|
jm::matrix<S, VS, T>& |
|
72 |
|
jm::matrix<S, VS, T>::operator += (const N& value) { |
|
73 |
|
for (int i = 0; i < S; ++i) |
78 |
74 |
(*this)[i] += value; |
(*this)[i] += value; |
79 |
75 |
return *this; |
return *this; |
80 |
76 |
} |
} |
81 |
|
T_NUMERIC_IMPL MATRIX& MATRIX::operator -= (const numeric_t& value) { |
|
82 |
|
for (int i = 0; i < size; ++i) |
|
|
77 |
|
template<int S, int VS, typename T> |
|
78 |
|
template<typename N> constexpr |
|
79 |
|
jm::matrix<S, VS, T>& |
|
80 |
|
jm::matrix<S, VS, T>::operator -= (const N& value) { |
|
81 |
|
for (int i = 0; i < S; ++i) |
83 |
82 |
(*this)[i] -= value; |
(*this)[i] -= value; |
84 |
83 |
return *this; |
return *this; |
85 |
84 |
} |
} |
86 |
|
T_NUMERIC_IMPL MATRIX& MATRIX::operator /= (const numeric_t& value) { |
|
87 |
|
for (int i = 0; i < size; ++i) |
|
|
85 |
|
template<int S, int VS, typename T> |
|
86 |
|
template<typename N> constexpr |
|
87 |
|
jm::matrix<S, VS, T>& |
|
88 |
|
jm::matrix<S, VS, T>::operator /= (const N& value) { |
|
89 |
|
for (int i = 0; i < S; ++i) |
88 |
90 |
(*this)[i] /= value; |
(*this)[i] /= value; |
89 |
91 |
return *this; |
return *this; |
90 |
92 |
} |
} |
91 |
|
|
|
92 |
|
#undef MATRIX_OTHER_OTHER |
|
93 |
|
#undef T_OTHER_OTHER_IMP |
|
94 |
|
#undef T_NUMERIC_IMPL |
|
95 |
|
#undef MATRIX |
|
96 |
|
#undef MATRIX_OTHER |
|