27#include "neml2/tensors/Tensor.h"
38constexpr Real eps = std::numeric_limits<at::scalar_value_type<Real>::type>::epsilon();
44 std::array<Size, 3>{0, 5, 4}, std::array<Size, 3>{5, 1, 3}, std::array<Size, 3>{4, 3, 2}};
45constexpr std::array<std::array<Size, 2>, 6>
mandel_index{std::array<Size, 2>{0, 0},
46 std::array<Size, 2>{1, 1},
47 std::array<Size, 2>{2, 2},
48 std::array<Size, 2>{1, 2},
49 std::array<Size, 2>{0, 2},
50 std::array<Size, 2>{0, 1}};
53 std::array<Size, 3>{0, 2, 1}, std::array<Size, 3>{2, 0, 0}, std::array<Size, 3>{1, 0, 0}};
54constexpr std::array<std::array<Real, 3>, 3>
skew_factor{std::array<Real, 3>{0.0, -1.0, 1.0},
55 std::array<Real, 3>{1.0, 0.0, -1.0},
56 std::array<Real, 3>{-1.0, 1.0, 0.0}};
61 return i < 3 ? 1.0 :
sqrt2;
89 torch::Tensor _full_to_mandel_map;
90 torch::Tensor _mandel_to_full_map;
91 torch::Tensor _full_to_mandel_factor;
92 torch::Tensor _mandel_to_full_factor;
93 torch::Tensor _full_to_skew_map;
94 torch::Tensor _skew_to_full_map;
95 torch::Tensor _full_to_skew_factor;
96 torch::Tensor _skew_to_full_factor;
121 const torch::Tensor &
rmap,
137 const torch::Tensor &
rmap,
243template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
249 auto d2 = d >= 0 ? d : d -
tensors.begin()->base_dim();
255template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
261 auto d2 = d >= 0 ? d : d -
tensors.begin()->base_dim();
267template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
271 neml_assert_dbg(a.batch_dim() > 0,
"Must have a batch dimension to sum along");
272 auto d2 = d >= 0 ? d : d - a.base_dim();
273 return T(torch::sum(a,
d2), a.batch_sizes().slice(0, -1));
278template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
282 neml_assert_dbg(a.batch_dim() > 0,
"Must have a batch dimension to take average");
283 auto d2 = d >= 0 ? d : d - a.base_dim();
284 return T(torch::mean(a,
d2), a.batch_sizes().slice(0, -1));
289template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
293 return T(torch::pow(a,
n), a.batch_sizes());
300template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
304 return T(torch::sign(a), a.batch_sizes());
307template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
311 return T(torch::cosh(a), a.batch_sizes());
314template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
318 return T(torch::sinh(a), a.batch_sizes());
321template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
325 return T(torch::tanh(a), a.batch_sizes());
328template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
332 return T(torch::arccos(a), a.batch_dim());
335template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
339 return T(torch::arcsin(a), a.batch_dim());
342template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
356template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
360 return (
sign(a) + 1.0) / 2.0;
363template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
367 return T(torch::Tensor(a) * torch::Tensor(
heaviside(a)), a.batch_dim());
370template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
377template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
381 return T(torch::sqrt(a), a.batch_sizes());
384template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
388 return T(torch::exp(a), a.batch_sizes());
391template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
395 return T(torch::abs(a), a.batch_sizes());
398template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
402 return T(torch::diff(a,
n, dim), a.batch_sizes());
405template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
409 return T(torch::diag_embed(
414template <
class T,
typename =
typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
418 return T(torch::log(a), a.batch_sizes());
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:54
The symmetric second order tensor.
Definition SR2.h:46
The symmetric fourth order tensor, with symmetry in the first two dimensionss as well as in the last ...
Definition SSR4.h:45
The symmetric fourth order tensor, with symmetry in the first two dimensionss and skew-symmetry in th...
Definition SWR4.h:40
A skew-symmetric second order tensor, represented as an axial vector.
Definition WR2.h:43
The symmetric fourth order tensor, with skew symmetry in the first two dimensionss and symmetry in th...
Definition WSR4.h:40
Tensor solve(const Tensor &A, const Tensor &B)
Solve the linear system A X = B.
Definition math.cxx:374
Tensor vector_norm(const Tensor &v)
Vector norm of a vector. Falls back to math::abs is v is a Scalar.
Definition math.cxx:351
Tensor lu_solve(const Tensor &LU, const Tensor &pivots, const Tensor &B, bool left, bool adjoint)
Definition math.cxx:387
Tensor inv(const Tensor &m)
Inverse of a square matrix.
Definition math.cxx:368
std::tuple< Tensor, Tensor > lu_factor(const Tensor &A, bool pivot)
Definition math.cxx:380
Tensor full_to_reduced(const Tensor &full, const torch::Tensor &rmap, const torch::Tensor &rfactors, Size dim)
Generic function to reduce two axes to one with some map.
Definition math.cxx:111
T dmacaulay(const T &a)
Definition math.h:372
Tensor reduced_to_full(const Tensor &reduced, const torch::Tensor &rmap, const torch::Tensor &rfactors, Size dim)
Convert a Tensor from reduced notation to full notation.
Definition math.cxx:137
T batch_sum(const T &a, Size d=0)
Definition math.h:269
Tensor jacrev(const Tensor &y, const Tensor &x, bool retain_graph, bool create_graph, bool allow_unused)
Use automatic differentiation (AD) to calculate the derivatives of a Tensor w.r.t....
Definition math.cxx:200
T batch_mean(const T &a, Size d=0)
Definition math.h:280
T cosh(const T &a)
Definition math.h:309
constexpr Real eps
Definition math.h:38
SWR4 d_skew_and_sym_to_sym_d_skew(const SR2 &e)
Derivative of w_ik e_kj - e_ik w_kj wrt. w.
Definition math.cxx:268
SSR4 d_skew_and_sym_to_sym_d_sym(const WR2 &w)
Derivative of w_ik e_kj - e_ik w_kj wrt. e.
Definition math.cxx:259
T heaviside(const T &a)
Definition math.h:358
Tensor full_to_skew(const Tensor &full, Size dim)
Convert a Tensor from full notation to skew vector notation.
Definition math.cxx:180
Tensor base_cat(const std::vector< Tensor > &tensors, Size d)
Definition math.cxx:304
constexpr Real invsqrt2
Definition math.h:41
T arccos(const T &a)
Definition math.h:330
Tensor mandel_to_full(const Tensor &mandel, Size dim)
Convert a Tensor from Mandel notation to full notation.
Definition math.cxx:170
T arcsin(const T &a)
Definition math.h:337
constexpr std::array< std::array< Real, 3 >, 3 > skew_factor
Definition math.h:54
Tensor base_diag_embed(const Tensor &a, Size offset, Size d1, Size d2)
Definition math.cxx:240
T exp(const T &a)
Definition math.h:386
T log(const T &a)
Definition math.h:416
WSR4 d_multiply_and_make_skew_d_first(const SR2 &b)
Derivative of a_ik b_kj - b_ik a_kj wrt a.
Definition math.cxx:286
SR2 skew_and_sym_to_sym(const SR2 &e, const WR2 &w)
Product w_ik e_kj - e_ik w_kj with e SR2 and w WR2.
Definition math.cxx:249
T sinh(const T &a)
Definition math.h:316
T tanh(const T &a)
Definition math.h:323
WR2 multiply_and_make_skew(const SR2 &a, const SR2 &b)
Shortcut product a_ik b_kj - b_ik a_kj with both SR2.
Definition math.cxx:277
T batch_stack(const std::vector< T > &tensors, Size d=0)
Definition math.h:257
constexpr std::array< std::array< Size, 3 >, 3 > mandel_reverse_index
Definition math.h:43
T sqrt(const T &a)
Definition math.h:379
constexpr Real mandel_factor(Size i)
Definition math.h:59
constexpr std::array< std::array< Size, 2 >, 6 > mandel_index
Definition math.h:45
T diff(const T &a, Size n=1, Size dim=-1)
Definition math.h:400
T batch_diag_embed(const T &a, Size offset=0, Size d1=-2, Size d2=-1)
Definition math.h:407
T abs(const T &a)
Definition math.h:393
T batch_cat(const std::vector< T > &tensors, Size d=0)
Definition math.h:245
Tensor base_stack(const std::vector< Tensor > &tensors, Size d)
Definition math.cxx:313
WSR4 d_multiply_and_make_skew_d_second(const SR2 &a)
Derivative of a_ik b_kj - b_ik a_kj wrt b.
Definition math.cxx:295
Tensor base_sum(const Tensor &a, Size d)
Definition math.cxx:322
Tensor base_mean(const Tensor &a, Size d)
Definition math.cxx:329
Tensor pow(const Real &a, const Tensor &n)
Definition math.cxx:336
Tensor skew_to_full(const Tensor &skew, Size dim)
Convert a Tensor from skew vector notation to full notation.
Definition math.cxx:190
T where(const torch::Tensor &condition, const T &a, const T &b)
Definition math.h:344
constexpr std::array< std::array< Size, 3 >, 3 > skew_reverse_index
Definition math.h:52
Tensor full_to_mandel(const Tensor &full, Size dim)
Convert a Tensor from full notation to Mandel notation.
Definition math.cxx:160
T macaulay(const T &a)
Definition math.h:365
T sign(const T &a)
Definition math.h:302
constexpr Real sqrt2
Definition math.h:40
Definition CrossRef.cxx:31
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:76
Size broadcast_batch_dim(const T &...)
The batch dimension after broadcasting.
double Real
Definition types.h:31
int64_t Size
Definition types.h:33
void neml_assert_broadcastable_dbg(const T &...)
A helper function to assert (in Debug mode) that all tensors are broadcastable.
A helper class to hold static data of type torch::Tensor.
Definition math.h:73
static const torch::Tensor & skew_to_full_map()
Definition math.cxx:93
static const torch::Tensor & full_to_mandel_factor()
Definition math.cxx:75
static const torch::Tensor & full_to_skew_factor()
Definition math.cxx:99
static const torch::Tensor & full_to_skew_map()
Definition math.cxx:87
ConstantTensors()
Definition math.cxx:34
static ConstantTensors & get()
Definition math.cxx:56
static const torch::Tensor & mandel_to_full_factor()
Definition math.cxx:81
static const torch::Tensor & mandel_to_full_map()
Definition math.cxx:69
static const torch::Tensor & full_to_mandel_map()
Definition math.cxx:63
static const torch::Tensor & skew_to_full_factor()
Definition math.cxx:105