26#include <experimental/mdspan>
44 template<
class T,
class Extents,
class Layout,
class Accessor,
class Func>
45 inline void mat_set_vals(
const std::mdspan<T, Extents, Layout, Accessor> &input, Func &&func) {
49 for (
int i = 0; i < input.extent(0); i++) {
50 for (
int j = 0; j < input.extent(1); j++) {
51 input(i, j) = func(i, j);
67 template<
class T,
class Extents,
class Layout,
class Accessor,
class Func>
68 inline void vec_set_vals(
const std::mdspan<T, Extents, Layout, Accessor> &input, Func &&func) {
72 for (
int i = 0; i < input.extent(0); i++) {
90 template<
class T,
class Extents,
class Layout,
class Accessor,
class Func>
92 const std::mdspan<T, Extents, Layout, Accessor> &input, Func &&func) {
96 for (
int i = 0; i < input.extent(0); i++) {
97 for (
int j = 0; j < input.extent(1); j++) {
98 func(input(i, j), i, j);
114 template<
class T,
class Extents,
class Layout,
class Accessor>
120 return (i == j) ? 1 : 0;
133 template<
class T,
class Extents,
class Layout,
class Accessor>
135 const std::mdspan<T, Extents, Layout, Accessor> &input,
const T &scalar) {
150 template<
class T,
class Extents,
class Layout,
class Accessor>
152 const std::mdspan<T, Extents, Layout, Accessor> &input,
153 const std::mdspan<T, Extents, Layout, Accessor> &output) {
158 for (
int i = 0; i < input.extent(0); i++) {
159 for (
int j = 0; j < input.extent(1); j++) {
160 output(i, j) = input(i, j);
188 const std::mdspan<T, Extents1, Layout1, Accessor1> &input1,
189 const std::mdspan<T, Extents2, Layout2, Accessor2> &input2,
190 const std::mdspan<T, Extents3, Layout3, Accessor3> &output) {
197 for (
int i = 0; i < input1.extent(0); i++) {
198 for (
int j = 0; j < input1.extent(1); j++) {
199 output(i, j) = input1(i, j) + input2(i, j);
223 const std::mdspan<T, Extents1, Layout1, Accessor1> &inout,
224 const std::mdspan<T, Extents2, Layout2, Accessor2> &matb) {
229 for (
int i = 0; i < inout.extent(0); i++) {
230 for (
int j = 0; j < inout.extent(1); j++) {
231 inout(i, j) += matb(i, j);
259 const std::mdspan<T, Extents1, Layout1, Accessor1> &input1,
260 const std::mdspan<T, Extents2, Layout2, Accessor2> &input2,
261 const std::mdspan<T, Extents3, Layout3, Accessor3> &output) {
268 for (
int i = 0; i < input1.extent(0); i++) {
269 for (
int j = 0; j < input1.extent(1); j++) {
270 output(i, j) = input1(i, j) - input2(i, j);
295 const std::mdspan<T, Extents1, Layout1, Accessor1> &inout,
296 const std::mdspan<T, Extents2, Layout2, Accessor2> &matb) {
301 for (
int i = 0; i < inout.extent(0); i++) {
302 for (
int j = 0; j < inout.extent(1); j++) {
303 inout(i, j) -= matb(i, j);
339 const std::mdspan<Ta, Extents1, Layout1, Accessor1> &input1,
340 const std::mdspan<Ta, Extents2, Layout2, Accessor2> &input2,
341 const std::mdspan<Tb, Extents3, Layout3, Accessor3> &output) {
348 for (
int i = 0; i < input1.extent(0); i++) {
349 for (
int j = 0; j < input2.extent(1); j++) {
351 for (
int k = 0; k < input1.extent(1); k++) {
352 sum += input1(i, k) * input2(k, j);
372 template<
class T,
class SizeType,
class Layout,
class Accessor>
374 const std::mdspan<T, std::extents<SizeType, 3, 3>, Layout, Accessor> &input,
375 const std::mdspan<T, std::extents<SizeType, 3, 3>, Layout, Accessor> &output) {
377 T &a00 = input(0, 0);
378 T &a10 = input(1, 0);
379 T &a20 = input(2, 0);
381 T &a01 = input(0, 1);
382 T &a11 = input(1, 1);
383 T &a21 = input(2, 1);
385 T &a02 = input(0, 2);
386 T &a12 = input(1, 2);
387 T &a22 = input(2, 2);
390 = (-a02 * a11 * a20 + a01 * a12 * a20 + a02 * a10 * a21 - a00 * a12 * a21
391 - a01 * a10 * a22 + a00 * a11 * a22);
393 output(0, 0) = (-a12 * a21 + a11 * a22) / det;
394 output(1, 0) = (a12 * a20 - a10 * a22) / det;
395 output(2, 0) = (-a11 * a20 + a10 * a21) / det;
397 output(0, 1) = (a02 * a21 - a01 * a22) / det;
398 output(1, 1) = (-a02 * a20 + a00 * a22) / det;
399 output(2, 1) = (a01 * a20 - a00 * a21) / det;
401 output(0, 2) = (-a02 * a11 + a01 * a12) / det;
402 output(1, 2) = (a02 * a10 - a00 * a12) / det;
403 output(2, 2) = (-a01 * a10 + a00 * a11) / det;
415 template<
class T,
class U,
class Extents,
class Layout,
class Accessor>
416 inline void mat_L1_norm(
const std::mdspan<T, Extents, Layout, Accessor> &input, U &res) {
418 for (
auto i = 0; i < input.extent(0); i++) {
420 for (
auto j = 0; j < input.extent(1); j++) {
421 sum += sham::abs(input(i, j));
423 res = sham::max(res, sum);
432 template<
class T,
class Extents,
class Layout,
class Accessor>
433 inline void mat_set_nul(
const std::mdspan<T, Extents, Layout, Accessor> &input) {
444 template<
class T,
class Extents,
class Layout,
class Accessor>
445 inline void vec_set_nul(
const std::mdspan<T, Extents, Layout, Accessor> &input) {
446 for (
auto i = 0; i < input.extent(0); i++) {
457 template<
class T,
class Extents,
class Layout,
class Accessor>
459 const std::mdspan<T, Extents, Layout, Accessor> &input,
460 const std::mdspan<T, Extents, Layout, Accessor> &output) {
463 for (
int i = 0; i < input.extent(0); i++) {
464 output(i) = input(i);
487 const std::mdspan<T, Extents1, Layout1, Accessor1> &input,
489 const std::mdspan<T, Extents2, Layout2, Accessor2> &output) {
493 for (
int i = 0; i < input.extent(0); i++) {
494 output(i) = alpha * input(i) + beta * output(i);
516 const std::mdspan<T, Extents1, Layout1, Accessor1> &input,
517 const std::mdspan<T, Extents2, Layout2, Accessor2> &output) {
541 const std::mdspan<T, Extents1, Layout1, Accessor1> &input,
543 const std::mdspan<T, Extents2, Layout2, Accessor2> &output) {
548 for (
int i = 0; i < input.extent(0); i++) {
549 for (
int j = 0; j < input.extent(1); j++) {
550 output(i, j) = alpha * input(i, j) + beta * output(i, j);
573 const std::mdspan<T, Extents1, Layout1, Accessor1> &input,
574 const std::mdspan<T, Extents2, Layout2, Accessor2> &output) {
602 const std::mdspan<T, Extents1, Layout1, Accessor1> &input1,
603 const std::mdspan<T, Extents2, Layout2, Accessor2> &input2,
605 const std::mdspan<T, Extents3, Layout3, Accessor3> &output) {
611 for (
int i = 0; i < input1.extent(0); i++) {
612 for (
int j = 0; j < input2.extent(1); j++) {
614 for (
int k = 0; k < input1.extent(1); k++) {
615 sum += input1(i, k) * input2(k, j);
617 output(i, j) = alpha * sum + beta * output(i, j);
631 template<
class T,
class U,
class Extents1,
class Layout1,
class Accessor1>
633 const std::mdspan<T, Extents1, Layout1, Accessor1> &inout,
const U beta) {
634 for (
int i = 0; i < inout.extent(0); i++) {
635 inout(i, i) = inout(i, i) + beta;
661 const std::mdspan<T, Extents1, Layout1, Accessor1> &M,
662 const std::mdspan<T, Extents2, Layout2, Accessor2> &x,
664 const std::mdspan<T, Extents3, Layout3, Accessor3> &y) {
669 for (
int i = 0; i < M.extent(0); i++) {
671 for (
int j = 0; j < M.extent(1); j++) {
672 sum += M(i, j) * x(j);
674 y(i) = alpha * sum + beta * y(i);
692 const std::mdspan<T, Extents1, Layout1, Accessor1> &input,
693 const std::mdspan<T, Extents2, Layout2, Accessor2> &output) {
698 for (
int i = 0; i < output.extent(0); i++) {
699 for (
int j = 0; j < output.extent(1); j++) {
700 output(i, j) = input(j, i);
724 const std::mdspan<T, Extents1, Layout1, Accessor1> &M,
725 const std::mdspan<T, Extents2, Layout2, Accessor2> &L) {
731 for (
int i = 0; i < M.extent(0); i++) {
733 for (
int k = 0; k < i; k++) {
734 sum_ik += L(i, k) * L(i, k);
736 L(i, i) = sycl::sqrt(M(i, i) - sum_ik);
737 for (
int j = i + 1; j < M.extent(1); j++) {
739 for (
int k = 0; k < i; k++) {
740 sum_ikjk += L(i, k) * L(j, k);
742 L(j, i) = (M(i, j) - sum_ikjk) / L(i, i);
771 const std::mdspan<T, Extents1, Layout1, Accessor1> &M,
772 const std::mdspan<T, Extents2, Layout2, Accessor2> &y,
773 const std::mdspan<T, Extents3, Layout3, Accessor3> &x) {
779 std::vector<T> a(M.extent(0));
780 std::vector<T> L_storage(M.extent(0) * M.extent(1));
782 std::mdspan<T, Extents1> L{L_storage.data(), M.extent(0), M.extent(1)};
785 for (
int i = 0; i < M.extent(0); i++) {
787 for (
int k = 0; k < i; k++) {
788 sum += L(i, k) * a[k];
790 a[i] = (y(i) - sum) / L(i, i);
792 for (
int i = M.extent(0) - 1; i >= 0; i--) {
794 for (
int k = i + 1; k < M.extent(0); k++) {
795 sum += L(k, i) * x(k);
797 x(i) = (a[i] - sum) / L(i, i);
Shamrock assertion utility.
#define SHAM_ASSERT(x)
Shorthand for SHAM_ASSERT_NAMED without a message.
constexpr void check_functor_signature(Func &&func)
Check if a callable object has the correct deduced signature.
namespace for math utility
void vec_axpy(const U alpha, const std::mdspan< T, Extents1, Layout1, Accessor1 > &input, const std::mdspan< T, Extents2, Layout2, Accessor2 > &output)
This function compute y = alpha*x + y with x,y both vectors.
void mat_plus(const std::mdspan< T, Extents1, Layout1, Accessor1 > &input1, const std::mdspan< T, Extents2, Layout2, Accessor2 > &input2, const std::mdspan< T, Extents3, Layout3, Accessor3 > &output)
Add two matrices element-wise.
void mat_mul_scalar(const std::mdspan< T, Extents, Layout, Accessor > &input, const T &scalar)
Multiply a matrix by a scalar value.
void vec_set_vals(const std::mdspan< T, Extents, Layout, Accessor > &input, Func &&func)
Set the elements of a vector according to a user-provided function.
void vec_axpy_beta(const U alpha, const std::mdspan< T, Extents1, Layout1, Accessor1 > &input, const U beta, const std::mdspan< T, Extents2, Layout2, Accessor2 > &output)
This function compute y = alpha*x + beta*y with x,y both vectors.
void mat_gemm(const U alpha, const std::mdspan< T, Extents1, Layout1, Accessor1 > &input1, const std::mdspan< T, Extents2, Layout2, Accessor2 > &input2, const U beta, const std::mdspan< T, Extents3, Layout3, Accessor3 > &output)
This function compute C = alpha*A*B + beta*C with A,B,C are matrices.
void mat_transpose(const std::mdspan< T, Extents1, Layout1, Accessor1 > &input, const std::mdspan< T, Extents2, Layout2, Accessor2 > &output)
This function transposes a matrix.
void vec_copy(const std::mdspan< T, Extents, Layout, Accessor > &input, const std::mdspan< T, Extents, Layout, Accessor > &output)
Copy the content of one vector in another.
void mat_L1_norm(const std::mdspan< T, Extents, Layout, Accessor > &input, U &res)
compute the L1 norm of a given matrix
void mat_set_nul(const std::mdspan< T, Extents, Layout, Accessor > &input)
Set the content of a matrix to zero.
void mat_set_vals(const std::mdspan< T, Extents, Layout, Accessor > &input, Func &&func)
Set the elements of a matrix according to a user-provided function.
void mat_inv_33(const std::mdspan< T, std::extents< SizeType, 3, 3 >, Layout, Accessor > &input, const std::mdspan< T, std::extents< SizeType, 3, 3 >, Layout, Accessor > &output)
Compute the inverse of a 3x3 matrix.
void Cholesky_decomp(const std::mdspan< T, Extents1, Layout1, Accessor1 > &M, const std::mdspan< T, Extents2, Layout2, Accessor2 > &L)
This function performs Cholesky decomposition. From a (real) symmetric, definite-positive square matr...
void Cholesky_solve(const std::mdspan< T, Extents1, Layout1, Accessor1 > &M, const std::mdspan< T, Extents2, Layout2, Accessor2 > &y, const std::mdspan< T, Extents3, Layout3, Accessor3 > &x)
This function solves a system of linear equations with Cholesky decomposition. The system must have t...
void mat_axpy_beta(const U alpha, const std::mdspan< T, Extents1, Layout1, Accessor1 > &input, const U beta, const std::mdspan< T, Extents2, Layout2, Accessor2 > &output)
This function compute M = alpha*N + beta*M with M,N both matrices.
void mat_plus_equal_scalar_id(const std::mdspan< T, Extents1, Layout1, Accessor1 > &inout, const U beta)
This function compute addition of a matrix with mutiple of identity matrix A +=beta * I,...
void mat_plus_equal(const std::mdspan< T, Extents1, Layout1, Accessor1 > &inout, const std::mdspan< T, Extents2, Layout2, Accessor2 > &matb)
Add a matrix to another matrix element-wise and store the result in the first matrix.
void mat_sub_equal(const std::mdspan< T, Extents1, Layout1, Accessor1 > &inout, const std::mdspan< T, Extents2, Layout2, Accessor2 > &matb)
Subtract a matrix from another matrix element-wise and store the result in the first matrix.
void mat_axpy(const U alpha, const std::mdspan< T, Extents1, Layout1, Accessor1 > &input, const std::mdspan< T, Extents2, Layout2, Accessor2 > &output)
This function compute M = alpha*N + beta*M with M,N both matrices.
void mat_copy(const std::mdspan< T, Extents, Layout, Accessor > &input, const std::mdspan< T, Extents, Layout, Accessor > &output)
Copy a matrix to another matrix.
void vec_set_nul(const std::mdspan< T, Extents, Layout, Accessor > &input)
Set the content of a vector to zero.
void mat_set_identity(const std::mdspan< T, Extents, Layout, Accessor > &input1)
Set the content of a matrix to the identity matrix.
void mat_prod(const std::mdspan< Ta, Extents1, Layout1, Accessor1 > &input1, const std::mdspan< Ta, Extents2, Layout2, Accessor2 > &input2, const std::mdspan< Tb, Extents3, Layout3, Accessor3 > &output)
Compute the product of two matrices.
void mat_sub(const std::mdspan< T, Extents1, Layout1, Accessor1 > &input1, const std::mdspan< T, Extents2, Layout2, Accessor2 > &input2, const std::mdspan< T, Extents3, Layout3, Accessor3 > &output)
Subtract two matrices element-wise.
void mat_update_vals(const std::mdspan< T, Extents, Layout, Accessor > &input, Func &&func)
Update the elements of a matrix according to a user-provided function.
void mat_gemv(const U alpha, const std::mdspan< T, Extents1, Layout1, Accessor1 > &M, const std::mdspan< T, Extents2, Layout2, Accessor2 > &x, const U beta, const std::mdspan< T, Extents3, Layout3, Accessor3 > &y)
This function performs matrix-vector multiplication as y = a*Mx + b*y.