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#include "shambase/assert.hpp"
22#include "shambackends/sycl.hpp"
24#include <experimental/mdspan>
25#include <array>
26
27namespace shammath {
28
35 template<class T, int m, int n>
36 class mat {
37 public:
39 std::array<T, m * n> data;
40
42 inline constexpr auto get_mdspan() {
43 return std::mdspan<T, std::extents<size_t, m, n>>(data.data());
44 }
45
47 inline constexpr auto get_mdspan() const {
48 return std::mdspan<const T, std::extents<size_t, m, n>>(data.data());
49 }
50
52 inline constexpr T &operator()(int i, int j) { return get_mdspan()(i, j); }
53
55 inline constexpr const T &operator()(int i, int j) const { return get_mdspan()(i, j); }
56
58 bool operator==(const mat<T, m, n> &other) const { return data == other.data; }
59
60 inline mat &operator+=(const mat &other) {
61#pragma unroll
62 for (size_t i = 0; i < m * n; i++) {
63 data[i] += other.data[i];
64 }
65 return *this;
66 }
67
69 bool equal_at_precision(const mat<T, m, n> &other, const T precision) const {
70 bool res = true;
71 for (auto i = 0; i < m; i++) {
72 for (auto j = 0; j < n; j++) {
73 if (sham::abs(data[i * n + j] - other.data[i * n + j]) >= precision) {
74 res = false;
75 }
76 }
77 }
78 return res;
79 }
80 };
81
83 template<class T, int n>
84 inline constexpr mat<T, n, n> mat_identity() {
85 mat<T, n, n> res{};
86 mat_set_identity(res.get_mdspan());
87 return res;
88 }
89
95 template<class T, int n>
96 class vec {
97 public:
99 std::array<T, n> data;
100
102 inline constexpr auto get_mdspan() {
103 return std::mdspan<T, std::extents<size_t, n>>(data.data());
104 }
105
107 inline constexpr auto get_mdspan_mat_col() {
108 return std::mdspan<T, std::extents<size_t, n, 1>>(data.data());
109 }
110
112 inline constexpr auto get_mdspan_mat_row() {
113 return std::mdspan<T, std::extents<size_t, 1, n>>(data.data());
114 }
115
117 inline constexpr T &operator[](int i) { return get_mdspan()(i); }
118
120 bool operator==(const vec<T, n> &other) { return data == other.data; }
121 };
122
123} // namespace shammath
124
125template<class T, int m, int n>
126struct sham::VectorProperties<shammath::mat<T, m, n>> {
127 using component_type = T;
128 static constexpr u32 dimension = m * n;
129
130 static constexpr bool is_float_based
131 = std::is_same<T, f16>::value || std::is_same<T, f32>::value || std::is_same<T, f64>::value;
132 static constexpr bool is_uint_based = std::is_same<T, u8>::value || std::is_same<T, u16>::value
133 || std::is_same<T, u32>::value
134 || std::is_same<T, u64>::value;
135 static constexpr bool is_int_based = std::is_same<T, i8>::value || std::is_same<T, i16>::value
136 || std::is_same<T, i32>::value
137 || std::is_same<T, i64>::value;
138 static constexpr bool has_info = is_float_based || is_int_based || is_uint_based;
139
140 static constexpr shammath::mat<T, m, n> get_min() {
141 constexpr T min = shambase::get_min<T>();
142 return {min};
143 }
144 static constexpr shammath::mat<T, m, n> get_max() {
145 constexpr T max = shambase::get_max<T>();
146 return {max};
147 }
148 static constexpr shammath::mat<T, m, n> get_zero() {
149 constexpr T zero = 0;
150 return {zero};
151 }
152};
153
std::uint32_t u32
32 bit unsigned integer
Shamrock assertion utility.
Matrix class based on std::array storage and mdspan.
Definition matrix.hpp:36
constexpr auto get_mdspan() const
const overload
Definition matrix.hpp:47
std::array< T, m *n > data
The matrix data.
Definition matrix.hpp:39
constexpr T & operator()(int i, int j)
Access the matrix entry at position (i, j)
Definition matrix.hpp:52
constexpr auto get_mdspan()
Get the matrix data as a mdspan.
Definition matrix.hpp:42
constexpr const T & operator()(int i, int j) const
const overload
Definition matrix.hpp:55
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:69
bool operator==(const mat< T, m, n > &other) const
Check if this matrix is equal to another one.
Definition matrix.hpp:58
Vector class based on std::array storage and mdspan.
Definition matrix.hpp:96
constexpr auto get_mdspan()
Get the vector data as a mdspan.
Definition matrix.hpp:102
constexpr auto get_mdspan_mat_col()
Get the vector data as a mdspan of a matrix with one column.
Definition matrix.hpp:107
bool operator==(const vec< T, n > &other)
Check if this vector is equal to another one.
Definition matrix.hpp:120
std::array< T, n > data
The vector data.
Definition matrix.hpp:99
constexpr T & operator[](int i)
Access the vector entry at position i.
Definition matrix.hpp:117
constexpr auto get_mdspan_mat_row()
Get the vector data as a mdspan of a matrix with one row.
Definition matrix.hpp:112
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
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:84
void mat_set_identity(const std::mdspan< T, Extents, Layout, Accessor > &input1)
Set the content of a matrix to the identity matrix.
Definition matrix_op.hpp:93