mal-math
C++20 mathematics library.
All Classes Namespaces Files Functions Variables Typedefs Concepts
rmatnxn.inl
Go to the documentation of this file.
1#pragma once
2#include "rmatnxn.hpp"
3
4namespace mal_math
5{
6 template <size_t a, size_t b, Primitive T>
7 template <size_t c, size_t d, Primitive P>
8 constexpr rmat<a, b, T>::rmat(mat<c, d, P> &p) requires(c >= a && d >= b)
9 {
10 for (size_t i = 0; i < a; i++)
11 {
12 for (size_t j = 0; j < b; j++)
13 {
14 arr[i * b + j].set_ptr(p.data[i][j]);
15 }
16 }
17 }
18 template <size_t a, size_t b, Primitive T>
19 template <size_t c, size_t d, Primitive P>
20 constexpr rmat<a, b, T>::rmat(rmat<c, d, P> &p) requires(c >= a && d >= b)
21 {
22 for (size_t i = 0; i < a; i++)
23 {
24 for (size_t j = 0; j < b; j++)
25 {
26 arr[i * b + j].set_ptr(p.arr[i * b + j]);
27 }
28 }
29 }
30
31 template <size_t a, size_t b, Primitive T>
32 template <typename U>
34 {
35 for (size_t i = 0; i < a; i++)
36 {
37 data[i] = mat.data[i];
38 }
39 return *this;
40 }
41
42 template <size_t a, size_t b, Primitive T>
43 template <typename U>
45 {
46 for (size_t i = 0; i < a; i++)
47 {
48 data[i] = mat.data[i];
49 }
50 return *this;
51 }
52
53 template <size_t a, size_t b, Primitive T>
54 constexpr void rmat<a, b, T>::reset() noexcept
55 {
56 for (size_t i = 0; i < size.x; i++)
57 {
58 data[i].reset();
59 }
60 for (size_t i = 0; i < a && i < b; i++)
61 {
62 data[i][i] = 1;
63 }
64 }
65
66 template <size_t a, size_t b, Primitive T>
67 [[nodiscard]] constexpr _detail::rvec<b, T> &rmat<a, b, T>::operator[](size_t i)
68 {
69 assert(i < size.x);
70 return data[i];
71 }
72 template <size_t a, size_t b, Primitive T>
73 [[nodiscard]] constexpr _detail::rvec<b, T> const &rmat<a, b, T>::operator[](size_t i) const
74 {
75 assert(i < size.x);
76 return data[i];
77 }
78
79 template <size_t a, size_t b, Primitive T>
80 [[nodiscard]] constexpr rmat<a, b, T> const &rmat<a, b, T>::operator+() const noexcept
81 {
82 return *this;
83 }
84
85 template <size_t a, size_t b, Primitive T>
86 [[nodiscard]] constexpr mat<a, b, T> rmat<a, b, T>::operator-() const noexcept
87 {
88 mat<a, b, T> return_value{};
89 for (size_t i = 0; i < size.x; i++)
90 {
91 return_value.data[i] = -data[i];
92 }
93 return return_value;
94 }
95
96 template <size_t a, size_t b, Primitive T>
97 template <Primitive U>
99 {
100 for (size_t i = 0; i < size.x; i++)
101 {
102 data[i] += other.data[i];
103 }
104 return *this;
105 }
106 template <size_t a, size_t b, Primitive T>
107 template <Primitive U>
109 {
110 for (size_t i = 0; i < size.x; i++)
111 {
112 data[i] -= other.data[i];
113 }
114 return *this;
115 }
116
117 template <size_t a, size_t b, Primitive T>
118 template <size_t c, Primitive U>
120 {
121 return (*this = *this * other);
122 }
123 template <size_t a, size_t b, Primitive T>
124 template <Primitive U>
126 {
127 for (size_t i = 0; i < size.x; i++)
128 {
129 data[i] += other.data[i];
130 }
131 return *this;
132 }
133 template <size_t a, size_t b, Primitive T>
134 template <Primitive U>
136 {
137 for (size_t i = 0; i < size.x; i++)
138 {
139 data[i] -= other.data[i];
140 }
141 return *this;
142 }
143
144 template <size_t a, size_t b, Primitive T>
145 template <size_t c, Primitive U>
147 {
148 return (*this = *this * other);
149 }
150
151 template <size_t a, size_t b, Primitive T>
152 template <Primitive U>
153 constexpr rmat<a, b, T> &rmat<a, b, T>::operator+=(U const value)
154 {
155 for (size_t i = 0; i < size.x; i++)
156 {
157 data[i] += value;
158 }
159 return *this;
160 }
161 template <size_t a, size_t b, Primitive T>
162 template <Primitive U>
163 constexpr rmat<a, b, T> &rmat<a, b, T>::operator-=(U const value)
164 {
165 for (size_t i = 0; i < size.x; i++)
166 {
167 data[i] -= value;
168 }
169 return *this;
170 }
171 template <size_t a, size_t b, Primitive T>
172 template <Primitive U>
173 constexpr rmat<a, b, T> &rmat<a, b, T>::operator*=(U const value)
174 {
175 for (size_t i = 0; i < size.x; i++)
176 {
177 data[i] *= value;
178 }
179 return *this;
180 }
181} // namespace mal_math
Contains mathematical utility functions and classes.
Specialized representation of matrices using reference semantics.
Internal detail definition of a reference vector with fixed size L and type T
Definition of matrix with dimensions rows x columns and type T
Definition matnxn.hpp:39
std::array< vec< size.y, T >, size.x > data
2D array (rows x columns) representation of matrix data.
Definition matnxn.hpp:249
Definition of a reference matrix with dimensions rows x columns and type T
Definition rmatnxn.hpp:45
constexpr void reset() noexcept
Resets all elements to zero.
Definition rmatnxn.inl:54
constexpr mat< a, b, T > operator-() const noexcept
Unary minus operator, returns negated matrix.
Definition rmatnxn.inl:86
std::array< _detail::rvec< size.y, T >, size.x > data
2D representation of the matrix.
Definition rmatnxn.hpp:137
constexpr rmat< a, b, T > & operator+=(rmat< a, b, U > const &other)
Adds another reference matrix of the same size to this matrix.
Definition rmatnxn.inl:98
constexpr rmat< a, c, T > & operator*=(rmat< b, c, U > const &other)
Matrix multiplication with another reference matrix.
Definition rmatnxn.inl:119
constexpr rmat< a, b, T > & operator-=(rmat< a, b, U > const &other)
Subtracts another reference matrix of the same size from this matrix.
Definition rmatnxn.inl:108
constexpr rmat()
Default constructor.
Definition rmatnxn.hpp:51
constexpr _detail::rvec< b, T > & operator[](size_t i)
Indexing operator to access rows.
Definition rmatnxn.inl:67
constexpr rmat< a, b, T > const & operator+() const noexcept
Unary plus operator, returns the matrix itself.
Definition rmatnxn.inl:80
constexpr rmat< a, b, T > & operator=(mat< a, b, U > const &mat)
Assigns values from a matrix of the same size.
Definition rmatnxn.inl:33