Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
21
22#include "shambase/assert.hpp"
23#include "shambackends/sycl.hpp"
25#include <experimental/mdspan>
26#include <array>
27
28namespace shammath {
29
36 template<class T, int m, int n>
37 class mat {
38 public:
40 std::array<T, m * n> data;
41
43 inline constexpr auto get_mdspan() {
44 return std::mdspan<T, std::extents<size_t, m, n>>(data.data());
45 }
46
48 inline constexpr auto get_mdspan() const {
49 return std::mdspan<const T, std::extents<size_t, m, n>>(data.data());
50 }
51
53 inline constexpr T &operator()(int i, int j) { return get_mdspan()(i, j); }
54
56 inline constexpr const T &operator()(int i, int j) const { return get_mdspan()(i, j); }
57
59 bool operator==(const mat<T, m, n> &other) const { return data == other.data; }
60
61 // Addition operator for matrices
62 inline mat &operator+=(const mat &other) {
63#pragma unroll
64 for (size_t i = 0; i < m * n; i++) {
65 data[i] += other.data[i];
66 }
67 return *this;
68 }
69
71 bool equal_at_precision(const mat<T, m, n> &other, const T precision) const {
72 bool res = true;
73 for (auto i = 0; i < m; i++) {
74 for (auto j = 0; j < n; j++) {
75 if (sham::abs(data[i * n + j] - other.data[i * n + j]) >= precision) {
76 res = false;
77 }
78 }
79 }
80 return res;
81 }
82 };
83
85 template<class T, int n>
86 inline constexpr mat<T, n, n> mat_identity() {
87 mat<T, n, n> res{};
89 return res;
90 }
91
97 template<class T, int n>
98 class vec {
99 public:
101 std::array<T, n> data;
102
104 inline constexpr auto get_mdspan() {
105 return std::mdspan<T, std::extents<size_t, n>>(data.data());
106 }
107
109 inline constexpr auto get_mdspan_mat_col() {
110 return std::mdspan<T, std::extents<size_t, n, 1>>(data.data());
111 }
112
114 inline constexpr auto get_mdspan_mat_row() {
115 return std::mdspan<T, std::extents<size_t, 1, n>>(data.data());
116 }
117
119 inline constexpr T &operator[](int i) { return get_mdspan()(i); }
120
122 bool operator==(const vec<T, n> &other) const { return data == other.data; }
123 };
124
129 template<class T>
130 class mat_d {
131 public:
133 std::vector<T> data;
135 int rows;
138
141
143 inline constexpr auto get_mdspan() {
144 return std::mdspan<T, std::dextents<size_t, 2>>(data.data(), rows, columns);
145 }
146
148 inline constexpr auto get_mdspan() const {
149 return std::mdspan<const T, std::dextents<size_t, 2>>(data.data(), rows, columns);
150 }
151
153 inline constexpr T &operator()(int i, int j) { return get_mdspan()(i, j); }
154
156 inline constexpr const T &operator()(int i, int j) const { return get_mdspan()(i, j); }
157
159 bool operator==(const mat_d<T> &other) const { return data == other.data; }
160
162 inline mat_d &operator+=(const mat_d &other) {
163 for (size_t i = 0; i < get_mdspan().extent(0) * get_mdspan().extent(1); i++) {
164 data[i] += other.data[i];
165 }
166 return *this;
167 }
168
170 bool equal_at_precision(const mat_d<T> &other, const T precision) const {
171 bool res = true;
172 for (auto i = 0; i < rows; i++) {
173 for (auto j = 0; j < columns; j++) {
174 if (sham::abs(data[i * columns + j] - other.data[i * columns + j])
175 >= precision) {
176 res = false;
177 }
178 }
179 }
180 return res;
181 }
182 };
183
189 template<class T>
190 class vec_d {
191 public:
193 std::vector<T> data;
195 int size;
196
199
201 inline constexpr auto get_mdspan() {
202 return std::mdspan<T, std::dextents<size_t, 1>>(data.data(), size);
203 }
204
206 inline constexpr auto get_mdspan_mat_col() {
207 return std::mdspan<T, std::dextents<size_t, 2>>(data.data(), size, 1);
208 }
209
211 inline constexpr auto get_mdspan_mat_row() {
212 return std::mdspan<T, std::dextents<size_t, 2>>(data.data(), 1, size);
213 }
214
216 inline constexpr T &operator[](int i) { return get_mdspan()(i); }
217
219 bool operator==(const vec_d<T> &other) const { return data == other.data; }
220 };
221} // namespace shammath
222
223template<class T, int m, int n>
224struct sham::VectorProperties<shammath::mat<T, m, n>> {
225 using component_type = T;
226 static constexpr u32 dimension = m * n;
227
228 static constexpr bool is_float_based
229 = std::is_same<T, f16>::value || std::is_same<T, f32>::value || std::is_same<T, f64>::value;
230 static constexpr bool is_uint_based = std::is_same<T, u8>::value || std::is_same<T, u16>::value
231 || std::is_same<T, u32>::value
232 || std::is_same<T, u64>::value;
233 static constexpr bool is_int_based = std::is_same<T, i8>::value || std::is_same<T, i16>::value
234 || std::is_same<T, i32>::value
235 || std::is_same<T, i64>::value;
236 static constexpr bool has_info = is_float_based || is_int_based || is_uint_based;
237
238 static constexpr shammath::mat<T, m, n> get_min() {
239 constexpr T min = shambase::get_min<T>();
240 return {min};
241 }
242 static constexpr shammath::mat<T, m, n> get_max() {
243 constexpr T max = shambase::get_max<T>();
244 return {max};
245 }
246 static constexpr shammath::mat<T, m, n> get_zero() {
247 constexpr T zero = 0;
248 return {zero};
249 }
250};
251
std::uint32_t u32
32 bit unsigned integer
Shamrock assertion utility.
mat_d & operator+=(const mat_d &other)
Addition operator for matrices.
Definition matrix.hpp:162
int rows
Number of rows.
Definition matrix.hpp:135
constexpr auto get_mdspan() const
const overload
Definition matrix.hpp:148
std::vector< T > data
The matrix data.
Definition matrix.hpp:133
bool operator==(const mat_d< T > &other) const
Check if this matrix is equal to another one.
Definition matrix.hpp:159
mat_d(int rows, int columns)
Constructor.
Definition matrix.hpp:140
int columns
Number of columns.
Definition matrix.hpp:137
bool equal_at_precision(const mat_d< T > &other, const T precision) const
check if this matrix is equal to another one at a given precison
Definition matrix.hpp:170
constexpr const T & operator()(int i, int j) const
const overload
Definition matrix.hpp:156
constexpr T & operator()(int i, int j)
Access the matrix entry at position (i, j).
Definition matrix.hpp:153
constexpr auto get_mdspan()
Get the matrix data as a mdspan.
Definition matrix.hpp:143
Matrix class based on std::array storage and mdspan.
Definition matrix.hpp:37
constexpr auto get_mdspan() const
const overload
Definition matrix.hpp:48
std::array< f32, m *n > data
Definition matrix.hpp:40
constexpr T & operator()(int i, int j)
Access the matrix entry at position (i, j).
Definition matrix.hpp:53
constexpr auto get_mdspan()
Get the matrix data as a mdspan.
Definition matrix.hpp:43
constexpr const T & operator()(int i, int j) const
const overload
Definition matrix.hpp:56
bool equal_at_precision(const mat< T, m, n > &other, const T precision) const
check if this matrix is equal to another one at a given precison
Definition matrix.hpp:71
bool operator==(const mat< T, m, n > &other) const
Check if this matrix is equal to another one.
Definition matrix.hpp:59
constexpr auto get_mdspan()
Get the vector data as a mdspan.
Definition matrix.hpp:201
std::vector< T > data
The vector data.
Definition matrix.hpp:193
int size
The vector size.
Definition matrix.hpp:195
constexpr auto get_mdspan_mat_row()
Get the vector data as a mdspan of a matrix with one row.
Definition matrix.hpp:211
bool operator==(const vec_d< T > &other) const
Check if this vector is equal to another one.
Definition matrix.hpp:219
constexpr auto get_mdspan_mat_col()
Get the vector data as a mdspan of a matrix with one column.
Definition matrix.hpp:206
vec_d(int size)
Constructor.
Definition matrix.hpp:198
constexpr T & operator[](int i)
Access the vector entry at position i.
Definition matrix.hpp:216
Vector class based on std::array storage and mdspan.
Definition matrix.hpp:98
constexpr auto get_mdspan()
Get the vector data as a mdspan.
Definition matrix.hpp:104
bool operator==(const vec< T, n > &other) const
Check if this vector is equal to another one.
Definition matrix.hpp:122
constexpr auto get_mdspan_mat_col()
Get the vector data as a mdspan of a matrix with one column.
Definition matrix.hpp:109
std::array< T, n > data
The vector data.
Definition matrix.hpp:101
constexpr T & operator[](int i)
Access the vector entry at position i.
Definition matrix.hpp:119
constexpr auto get_mdspan_mat_row()
Get the vector data as a mdspan of a matrix with one row.
Definition matrix.hpp:114
shammath::mat< f64, 3, 3 > f64_3x3
Alias for 3x3 double matrix.
Definition matrix.hpp:254
shammath::mat< f32, 4, 4 > f32_4x4
Alias for 4x4 float matrix.
Definition matrix.hpp:253
shammath::mat< f64, 4, 4 > f64_4x4
Alias for 4x4 double matrix.
Definition matrix.hpp:255
shammath::mat< f32, 3, 3 > f32_3x3
Alias for 3x3 float matrix.
Definition matrix.hpp:252
namespace for math utility
Definition AABB.hpp:26
constexpr mat< T, n, n > mat_identity()
Returns the identity matrix of size n.
Definition matrix.hpp:86
void mat_set_identity(const std::mdspan< T, Extents, Layout, Accessor > &input1)
Set the content of a matrix to the identity matrix.