25#include "neml2/misc/math.h"
26#include "neml2/misc/error.h"
27#include "neml2/tensors/tensors.h"
29#include <torch/autograd.h>
30#include <torch/linalg.h>
43 _full_to_mandel_factor =
46 _mandel_to_full_factor =
56 _skew_to_full_factor =
70 return get()._full_to_mandel_map;
76 return get()._mandel_to_full_map;
82 return get()._full_to_mandel_factor;
88 return get()._mandel_to_full_factor;
94 return get()._full_to_skew_map;
100 return get()._skew_to_full_map;
106 return get()._full_to_skew_factor;
112 return get()._skew_to_full_factor;
117 const torch::Tensor &
rmap,
121 using namespace torch::indexing;
145 const torch::Tensor &
rmap,
149 using namespace torch::indexing;
151 auto batch_dim =
reduced.batch_dim();
212 "The batch shape of the parameter must be the same as the batch shape "
213 "of the output. However, the batch shape of the parameter is ",
215 ", and the batch shape of the output is ",
231 v.index_put_({torch::indexing::Ellipsis,
i}, 1.0);
232 const auto dyfi_dp = torch::autograd::grad({
yf},
255 a,
offset,
d1 < 0 ?
d1 :
d1 + a.batch_dim() + 1,
d2 < 0 ?
d2 :
d2 + a.batch_dim() + 1),
266 return SR2(W *
E -
E * W);
274 return SSR4(
R4(torch::einsum(
"...ia,...jb->...ijab", {W,
I}) -
275 torch::einsum(
"...ia,...bj->...ijab", {
I, W})));
283 return SWR4(
R4(torch::einsum(
"...ia,...bj->...ijab", {
I,
E}) -
284 torch::einsum(
"...ia,...jb->...ijab", {
E,
I})));
293 return WR2(A *
B -
B * A);
301 return WSR4(
R4(torch::einsum(
"...ia,...bj->...ijab", {
I,
B}) -
302 torch::einsum(
"...ia,...jb->...ijab", {
B,
I})));
310 return WSR4(
R4(torch::einsum(
"...ia,...jb->...ijab", {A,
I}) -
311 torch::einsum(
"...ia,...bj->...ijab", {
I, A})));
320 "v in vector_norm has base dimension ",
322 " instead of 0 or 1.");
325 if (
v.base_dim() == 0)
329 v, 2, -1,
false, c10::nullopt),
345std::tuple<BatchTensor, BatchTensor>
TorchSize batch_dim() const
Return the number of batch dimensions.
Definition BatchTensorBase.cxx:128
static BatchTensor zeros_like(const BatchTensor &other)
Zero tensor like another, i.e. same batch and base shapes, same tensor options, etc.
Definition BatchTensorBase.cxx:59
Definition BatchTensor.h:32
static BatchTensor empty(const TorchShapeRef &base_shape, const torch::TensorOptions &options=default_tensor_options())
Unbatched empty tensor given base shape.
Definition BatchTensor.cxx:30
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
static R2 identity(const torch::TensorOptions &options=default_tensor_options())
Identity.
Definition R2Base.cxx:170
A basic R2.
Definition R2.h:42
The (logical) full fourth order tensor.
Definition R4.h:43
The (logical) symmetric second order tensor.
Definition SR2.h:46
The (logical) symmetric fourth order tensor, with symmetry in the first two dimensionss as well as in...
Definition SSR4.h:44
The (logical) symmetric fourth order tensor, with symmetry in the first two dimensionss and skew-symm...
Definition SWR4.h:40
A skew rank 2, represented as an axial vector.
Definition WR2.h:43
The (logical) symmetric fourth order tensor, with skew symmetry in the first two dimensionss and symm...
Definition WSR4.h:40
BatchTensor solve(const BatchTensor &A, const BatchTensor &B)
Solve the linear system A X = B.
Definition math.cxx:340
BatchTensor inv(const BatchTensor &m)
Inverse of a square matrix.
Definition math.cxx:334
std::tuple< BatchTensor, BatchTensor > lu_factor(const BatchTensor &A, bool pivot)
Definition math.cxx:346
BatchTensor vector_norm(const BatchTensor &v)
Vector norm of a vector. Falls back to math::abs is v is a Scalar.
Definition math.cxx:317
BatchTensor lu_solve(const BatchTensor &LU, const BatchTensor &pivots, const BatchTensor &B, bool left, bool adjoint)
Definition math.cxx:353
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:279
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:270
BatchTensor mandel_to_full(const BatchTensor &mandel, TorchSize dim)
Convert a BatchTensor from Mandel notation to full notation.
Definition math.cxx:179
constexpr Real invsqrt2
Definition math.h:41
BatchTensor skew_to_full(const BatchTensor &skew, TorchSize dim)
Convert a BatchTensor from skew vector notation to full notation.
Definition math.cxx:199
BatchTensor base_diag_embed(const BatchTensor &a, TorchSize offset, TorchSize d1, TorchSize d2)
Definition math.cxx:251
BatchTensor reduced_to_full(const BatchTensor &reduced, const torch::Tensor &rmap, const torch::Tensor &rfactors, TorchSize dim)
Convert a BatchTensor from reduced notation to full notation.
Definition math.cxx:144
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:297
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:260
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:288
BatchTensor full_to_reduced(const BatchTensor &full, const torch::Tensor &rmap, const torch::Tensor &rfactors, TorchSize dim)
Generic function to reduce two axes to one with some map.
Definition math.cxx:116
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:306
Derived abs(const Derived &a)
Definition BatchTensorBase.h:457
BatchTensor full_to_mandel(const BatchTensor &full, TorchSize dim)
Convert a BatchTensor from full notation to Mandel notation.
Definition math.cxx:169
BatchTensor jacrev(const BatchTensor &y, const BatchTensor &p)
Use automatic differentiation (AD) to calculate the derivatives w.r.t. to the parameter.
Definition math.cxx:209
constexpr Real sqrt2
Definition math.h:40
BatchTensor full_to_skew(const BatchTensor &full, TorchSize dim)
Convert a BatchTensor from full notation to skew vector notation.
Definition math.cxx:189
TorchSize storage_size(TorchShapeRef shape)
The flattened storage size of a tensor with given shape.
Definition utils.cxx:32
TorchShape add_shapes(S &&... shape)
Definition utils.h:294
Definition CrossRef.cxx:32
const torch::TensorOptions default_tensor_options()
Definition types.cxx:30
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:85
int64_t TorchSize
Definition types.h:35
const torch::TensorOptions default_integer_tensor_options()
We similarly want to have a default integer scalar type for some types of tensors.
Definition types.cxx:42
std::vector< at::indexing::TensorIndex > TorchSlice
Definition types.h:39
void neml_assert(bool assertion, Args &&... args)
Definition error.h:73
A helper class to hold static data of type torch::Tensor.
Definition math.h:64
static const torch::Tensor & skew_to_full_map()
Definition math.cxx:98
static const torch::Tensor & full_to_mandel_factor()
Definition math.cxx:80
static const torch::Tensor & full_to_skew_factor()
Definition math.cxx:104
static const torch::Tensor & full_to_skew_map()
Definition math.cxx:92
ConstantTensors()
Definition math.cxx:36
static ConstantTensors & get()
Definition math.cxx:61
static const torch::Tensor & mandel_to_full_factor()
Definition math.cxx:86
static const torch::Tensor & mandel_to_full_map()
Definition math.cxx:74
static const torch::Tensor & full_to_mandel_map()
Definition math.cxx:68
static const torch::Tensor & skew_to_full_factor()
Definition math.cxx:110