25#include <experimental/mdspan>
43 template<
class T,
class Extents,
class Layout,
class Accessor,
class Func>
44 inline void mat_set_vals(
const std::mdspan<T, Extents, Layout, Accessor> &input, Func &&func) {
48 for (
int i = 0; i < input.extent(0); i++) {
49 for (
int j = 0; j < input.extent(1); j++) {
50 input(i, j) = func(i, j);
68 template<
class T,
class Extents,
class Layout,
class Accessor,
class Func>
70 const std::mdspan<T, Extents, Layout, Accessor> &input, Func &&func) {
74 for (
int i = 0; i < input.extent(0); i++) {
75 for (
int j = 0; j < input.extent(1); j++) {
76 func(input(i, j), i, j);
92 template<
class T,
class Extents,
class Layout,
class Accessor>
93 inline void mat_set_identity(
const std::mdspan<T, Extents, Layout, Accessor> &input1) {
98 return (i == j) ? 1 : 0;
111 template<
class T,
class Extents,
class Layout,
class Accessor>
113 const std::mdspan<T, Extents, Layout, Accessor> &input,
const T &scalar) {
128 template<
class T,
class Extents,
class Layout,
class Accessor>
130 const std::mdspan<T, Extents, Layout, Accessor> &input,
131 const std::mdspan<T, Extents, Layout, Accessor> &output) {
136 for (
int i = 0; i < input.extent(0); i++) {
137 for (
int j = 0; j < input.extent(1); j++) {
138 output(i, j) = input(i, j);
166 const std::mdspan<T, Extents1, Layout1, Accessor1> &input1,
167 const std::mdspan<T, Extents2, Layout2, Accessor2> &input2,
168 const std::mdspan<T, Extents3, Layout3, Accessor3> &output) {
175 for (
int i = 0; i < input1.extent(0); i++) {
176 for (
int j = 0; j < input1.extent(1); j++) {
177 output(i, j) = input1(i, j) + input2(i, j);
201 const std::mdspan<T, Extents1, Layout1, Accessor1> &inout,
202 const std::mdspan<T, Extents2, Layout2, Accessor2> &matb) {
207 for (
int i = 0; i < inout.extent(0); i++) {
208 for (
int j = 0; j < inout.extent(1); j++) {
209 inout(i, j) += matb(i, j);
237 const std::mdspan<T, Extents1, Layout1, Accessor1> &input1,
238 const std::mdspan<T, Extents2, Layout2, Accessor2> &input2,
239 const std::mdspan<T, Extents3, Layout3, Accessor3> &output) {
246 for (
int i = 0; i < input1.extent(0); i++) {
247 for (
int j = 0; j < input1.extent(1); j++) {
248 output(i, j) = input1(i, j) - input2(i, j);
273 const std::mdspan<T, Extents1, Layout1, Accessor1> &inout,
274 const std::mdspan<T, Extents2, Layout2, Accessor2> &matb) {
279 for (
int i = 0; i < inout.extent(0); i++) {
280 for (
int j = 0; j < inout.extent(1); j++) {
281 inout(i, j) -= matb(i, j);
317 const std::mdspan<Ta, Extents1, Layout1, Accessor1> &input1,
318 const std::mdspan<Ta, Extents2, Layout2, Accessor2> &input2,
319 const std::mdspan<Tb, Extents3, Layout3, Accessor3> &output) {
326 for (
int i = 0; i < input1.extent(0); i++) {
327 for (
int j = 0; j < input2.extent(1); j++) {
329 for (
int k = 0; k < input1.extent(1); k++) {
330 sum += input1(i, k) * input2(k, j);
350 template<
class T,
class SizeType,
class Layout,
class Accessor>
352 const std::mdspan<T, std::extents<SizeType, 3, 3>, Layout, Accessor> &input,
353 const std::mdspan<T, std::extents<SizeType, 3, 3>, Layout, Accessor> &output) {
355 T &a00 = input(0, 0);
356 T &a10 = input(1, 0);
357 T &a20 = input(2, 0);
359 T &a01 = input(0, 1);
360 T &a11 = input(1, 1);
361 T &a21 = input(2, 1);
363 T &a02 = input(0, 2);
364 T &a12 = input(1, 2);
365 T &a22 = input(2, 2);
368 = (-a02 * a11 * a20 + a01 * a12 * a20 + a02 * a10 * a21 - a00 * a12 * a21
369 - a01 * a10 * a22 + a00 * a11 * a22);
371 output(0, 0) = (-a12 * a21 + a11 * a22) / det;
372 output(1, 0) = (a12 * a20 - a10 * a22) / det;
373 output(2, 0) = (-a11 * a20 + a10 * a21) / det;
375 output(0, 1) = (a02 * a21 - a01 * a22) / det;
376 output(1, 1) = (-a02 * a20 + a00 * a22) / det;
377 output(2, 1) = (a01 * a20 - a00 * a21) / det;
379 output(0, 2) = (-a02 * a11 + a01 * a12) / det;
380 output(1, 2) = (a02 * a10 - a00 * a12) / det;
381 output(2, 2) = (-a01 * a10 + a00 * a11) / det;
393 template<
class T,
class U,
class Extents,
class Layout,
class Accessor>
394 inline void mat_L1_norm(
const std::mdspan<T, Extents, Layout, Accessor> &input, U &res) {
396 for (
auto i = 0; i < input.extent(0); i++) {
398 for (
auto j = 0; j < input.extent(1); j++) {
399 sum += abs(input(i, j));
401 res = sham::max(res, sum);
410 template<
class T,
class Extents,
class Layout,
class Accessor>
411 inline void mat_set_nul(
const std::mdspan<T, Extents, Layout, Accessor> &input) {
422 template<
class T,
class Extents,
class Layout,
class Accessor>
423 inline void vec_set_nul(
const std::mdspan<T, Extents, Layout, Accessor> &input) {
424 for (
auto i = 0; i < input.extent(0); i++) {
435 template<
class T,
class Extents,
class Layout,
class Accessor>
437 const std::mdspan<T, Extents, Layout, Accessor> &input,
438 const std::mdspan<T, Extents, Layout, Accessor> &output) {
441 for (
int i = 0; i < input.extent(0); i++) {
442 output(i) = input(i);
465 const std::mdspan<T, Extents1, Layout1, Accessor1> &input,
467 const std::mdspan<T, Extents2, Layout2, Accessor2> &output) {
471 for (
int i = 0; i < input.extent(0); i++) {
472 output(i) = alpha * input(i) + beta * output(i);
494 const std::mdspan<T, Extents1, Layout1, Accessor1> &input,
495 const std::mdspan<T, Extents2, Layout2, Accessor2> &output) {
519 const std::mdspan<T, Extents1, Layout1, Accessor1> &input,
521 const std::mdspan<T, Extents2, Layout2, Accessor2> &output) {
526 for (
int i = 0; i < input.extent(0); i++) {
527 for (
int j = 0; j < input.extent(1); j++) {
528 output(i, j) = alpha * input(i, j) + beta * output(i, j);
551 const std::mdspan<T, Extents1, Layout1, Accessor1> &input,
552 const std::mdspan<T, Extents2, Layout2, Accessor2> &output) {
580 const std::mdspan<T, Extents1, Layout1, Accessor1> &input1,
581 const std::mdspan<T, Extents2, Layout2, Accessor2> &input2,
583 const std::mdspan<T, Extents3, Layout3, Accessor3> &output) {
589 for (
int i = 0; i < input1.extent(0); i++) {
590 for (
int j = 0; j < input2.extent(1); j++) {
592 for (
int k = 0; k < input1.extent(1); k++) {
593 sum += input1(i, k) * input2(k, j);
595 output(i, j) = alpha * sum + beta * output(i, j);
609 template<
class T,
class U,
class Extents1,
class Layout1,
class Accessor1>
611 const std::mdspan<T, Extents1, Layout1, Accessor1> &inout,
const U beta) {
612 for (
int i = 0; i < inout.extent(0); i++) {
613 inout(i, i) = inout(i, i) + beta;
639 const std::mdspan<T, Extents1, Layout1, Accessor1> &M,
640 const std::mdspan<T, Extents2, Layout2, Accessor2> &x,
642 const std::mdspan<T, Extents3, Layout3, Accessor3> &y) {
647 for (
int i = 0; i < M.extent(0); i++) {
649 for (
int j = 0; j < M.extent(1); j++) {
650 sum += M(i, j) * x(j);
652 y(i) = alpha * sum + beta * y(i);
Shamrock assertion utility.
#define SHAM_ASSERT(x)
Shorthand for SHAM_ASSERT_NAMED without a message.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
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_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 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 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.