NEML2 1.4.0
Loading...
Searching...
No Matches
neml2::math Namespace Reference

Namespaces

namespace  linalg
 

Classes

struct  ConstantTensors
 A helper class to hold static data of type torch::Tensor. More...
 

Functions

BatchTensor full_to_reduced (const BatchTensor &full, const torch::Tensor &rmap, const torch::Tensor &rfactors, TorchSize dim=0)
 Generic function to reduce two axes to one with some map.
 
BatchTensor reduced_to_full (const BatchTensor &reduced, const torch::Tensor &rmap, const torch::Tensor &rfactors, TorchSize dim=0)
 Convert a BatchTensor from reduced notation to full notation.
 
BatchTensor full_to_mandel (const BatchTensor &full, TorchSize dim=0)
 Convert a BatchTensor from full notation to Mandel notation.
 
BatchTensor mandel_to_full (const BatchTensor &mandel, TorchSize dim=0)
 Convert a BatchTensor from Mandel notation to full notation.
 
BatchTensor full_to_skew (const BatchTensor &full, TorchSize dim=0)
 Convert a BatchTensor from full notation to skew vector notation.
 
BatchTensor skew_to_full (const BatchTensor &skew, TorchSize dim=0)
 Convert a BatchTensor from skew vector notation to full notation.
 
BatchTensor jacrev (const BatchTensor &y, const BatchTensor &p)
 Use automatic differentiation (AD) to calculate the derivatives w.r.t. to the parameter.
 
BatchTensor base_diag_embed (const BatchTensor &a, TorchSize offset, TorchSize d1, TorchSize d2)
 
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.
 
SSR4 d_skew_and_sym_to_sym_d_sym (const WR2 &w)
 Derivative of w_ik e_kj - e_ik w_kj wrt. e.
 
SWR4 d_skew_and_sym_to_sym_d_skew (const SR2 &e)
 Derivative of w_ik e_kj - e_ik w_kj wrt. w.
 
WR2 multiply_and_make_skew (const SR2 &a, const SR2 &b)
 Shortcut product a_ik b_kj - b_ik a_kj with both SR2.
 
WSR4 d_multiply_and_make_skew_d_first (const SR2 &b)
 Derivative of a_ik b_kj - b_ik a_kj wrt a.
 
WSR4 d_multiply_and_make_skew_d_second (const SR2 &a)
 Derivative of a_ik b_kj - b_ik a_kj wrt b.
 
BatchTensor bmm (const BatchTensor &a, const BatchTensor &b)
 Batched matrix-matrix product.
 
BatchTensor bmv (const BatchTensor &a, const BatchTensor &v)
 Batched matrix-vector product.
 
BatchTensor bvv (const BatchTensor &a, const BatchTensor &b)
 Batched vector-vector (dot) product.
 
constexpr Real mandel_factor (TorchSize i)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived pow (const Derived &a, const Real &n)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived pow (const Real &a, const Derived &n)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived pow (const Derived &a, const Derived &n)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived sign (const Derived &a)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived cosh (const Derived &a)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived sinh (const Derived &a)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived tanh (const Derived &a)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived where (const torch::Tensor &condition, const Derived &a, const Derived &b)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived heaviside (const Derived &a)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived macaulay (const Derived &a)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived dmacaulay (const Derived &a)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived sqrt (const Derived &a)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived exp (const Derived &a)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived abs (const Derived &a)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived diff (const Derived &a, TorchSize n=1, TorchSize dim=-1)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived batch_diag_embed (const Derived &a, TorchSize offset=0, TorchSize d1=-2, TorchSize d2=-1)
 
template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived log (const Derived &a)
 
template<class Derived , typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>, typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived pow (const Derived &a, const Scalar &n)
 
template<class Derived , typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>, typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived pow (const Scalar &a, const Derived &n)
 

Variables

constexpr Real eps = std::numeric_limits<at::scalar_value_type<Real>::type>::epsilon()
 
constexpr Real sqrt2 = 1.4142135623730951
 
constexpr Real invsqrt2 = 0.7071067811865475
 
constexpr TorchSize mandel_reverse_index [3][3] = {{0, 5, 4}, {5, 1, 3}, {4, 3, 2}}
 
constexpr TorchSize mandel_index [6][2] = {{0, 0}, {1, 1}, {2, 2}, {1, 2}, {0, 2}, {0, 1}}
 
constexpr TorchSize skew_reverse_index [3][3] = {{0, 2, 1}, {2, 0, 0}, {1, 0, 0}}
 
constexpr Real skew_factor [3][3] = {{0.0, -1.0, 1.0}, {1.0, 0.0, -1.0}, {-1.0, 1.0, 0.0}}
 

Function Documentation

◆ abs()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived abs ( const Derived & a)

◆ base_diag_embed()

BatchTensor base_diag_embed ( const BatchTensor & a,
TorchSize offset,
TorchSize d1,
TorchSize d2 )

◆ batch_diag_embed()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived batch_diag_embed ( const Derived & a,
TorchSize offset = 0,
TorchSize d1 = -2,
TorchSize d2 = -1 )

◆ bmm()

Batched matrix-matrix product.

The input matrices a and b must have exactly 2 base dimensions. The batch shapes must broadcast.

◆ bmv()

Batched matrix-vector product.

The input tensor a must have exactly 2 base dimensions. The input tensor v must have exactly 1 base dimension. The batch shapes must broadcast.

◆ bvv()

Batched vector-vector (dot) product.

The input tensor a must have exactly 1 base dimension. The input tensor vbmust have exactly 1 base dimension. The batch shapes must broadcast.

◆ cosh()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived cosh ( const Derived & a)

◆ d_multiply_and_make_skew_d_first()

WSR4 d_multiply_and_make_skew_d_first ( const SR2 & b)

Derivative of a_ik b_kj - b_ik a_kj wrt a.

◆ d_multiply_and_make_skew_d_second()

WSR4 d_multiply_and_make_skew_d_second ( const SR2 & a)

Derivative of a_ik b_kj - b_ik a_kj wrt b.

◆ d_skew_and_sym_to_sym_d_skew()

SWR4 d_skew_and_sym_to_sym_d_skew ( const SR2 & e)

Derivative of w_ik e_kj - e_ik w_kj wrt. w.

◆ d_skew_and_sym_to_sym_d_sym()

SSR4 d_skew_and_sym_to_sym_d_sym ( const WR2 & w)

Derivative of w_ik e_kj - e_ik w_kj wrt. e.

◆ diff()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived diff ( const Derived & a,
TorchSize n = 1,
TorchSize dim = -1 )

◆ dmacaulay()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived dmacaulay ( const Derived & a)

◆ exp()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived exp ( const Derived & a)

◆ full_to_mandel()

BatchTensor full_to_mandel ( const BatchTensor & full,
TorchSize dim = 0 )

Convert a BatchTensor from full notation to Mandel notation.

The tensor in full notation full can have arbitrary batch shape. The optional argument dim denotes the base dimension starting from which the conversion should take place.

For example, a full tensor has shape (2, 3, 1, 5; 2, 9, 3, 3, 2, 3) where the semicolon separates batch and base shapes. The symmetric axes have base dim 2 and 3. After converting to Mandel notation, the resulting tensor will have shape (2, 3, 1, 5; 2, 9, 6, 2, 3). Note how the shape of the symmetric dimensions (3, 3) becomes (6). In this example, the base dim (the second argument to this function) should be 2.

Parameters
fullThe input tensor in full notation
dimThe base dimension where the symmetric axes start
Returns
BatchTensor The resulting tensor using Mandel notation to represent the symmetric axes.

◆ full_to_reduced()

BatchTensor full_to_reduced ( const BatchTensor & full,
const torch::Tensor & rmap,
const torch::Tensor & rfactors,
TorchSize dim = 0 )

Generic function to reduce two axes to one with some map.

The tensor in full notation full can have arbitrary batch shape. The optional argument dim denotes the base dimension starting from which the conversion should take place.

The function will reduce the two axis at the desired location down to one, using the provided maps.

For example, a full tensor has shape (2, 3, 1, 5; 2, 9, 3, 3, 2, 3) where the semicolon separates batch and base shapes. The reduction axes have base dim 2 and 3. After applying the reduction, the resulting tensor will have shape (2, 3, 1, 5; 2, 9, X, 2, 3) where X is the reduced shape. In this example, the base dim (the second argument to this function) should be 2.

Parameters
fullThe input tensor in full notation
rmapThe reduction map
rfactorsThe reduction factors
dimThe base dimension where the reduced axes start
Returns
BatchTensor The reduced tensor

◆ full_to_skew()

BatchTensor full_to_skew ( const BatchTensor & full,
TorchSize dim = 0 )

Convert a BatchTensor from full notation to skew vector notation.

The tensor in full notation full can have arbitrary batch shape. The optional argument dim denotes the base dimension starting from which the conversion should take place.

For example, a full tensor has shape (2, 3, 1, 5; 2, 9, 3, 3, 2, 3) where the semicolon separates batch and base shapes. The symmetric axes have base dim 2 and 3. After converting to skew notation, the resulting tensor will have shape (2, 3, 1, 5; 2, 9, 3, 2, 3). Note how the shape of the symmetric dimensions (3, 3) becomes (3). In this example, the base dim (the second argument to this function) should be 2.

Parameters
fullThe input tensor in full notation
dimThe base dimension where the symmetric axes start
Returns
BatchTensor The resulting tensor using skew notation to represent the skew-symmetric axes.

◆ heaviside()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived heaviside ( const Derived & a)

This is (almost) equivalent to Torch's heaviside, except that the Torch's version is not differentiable (back-propagatable). I said "almost" because torch::heaviside allows you to set the return value in the case of input == 0. Our implementation always return 0.5 when the input == 0.

◆ jacrev()

Use automatic differentiation (AD) to calculate the derivatives w.r.t. to the parameter.

Warning
Torch (and hence NEML2) AD wasn't designed to compute the full Jacobian from the very beginning. Using this method to calculate the full Jacobian is inefficient and is subjected to some restrictions on batch shapes: This method will only work when the output y and the paramter p have the same batch shape.

However, in practice, the batch shape of the output y and the batch shape of the parameter p can be different. In that case, calculating the full Jacobian is not possible, and an exception will be thrown.

One possible (inefficient) workaround is to expand and copy the parameter p batch dimensions, e.g., batch_expand_copy, before calculating the output y.

Parameters
yThe BatchTensor to to be differentiated
pThe parameter to take derivatives with respect to
Returns
BatchTensor \(\partial y/\partial p\)

◆ log()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived log ( const Derived & a)

◆ macaulay()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived macaulay ( const Derived & a)

◆ mandel_factor()

constexpr Real mandel_factor ( TorchSize i)
inlineconstexpr

◆ mandel_to_full()

BatchTensor mandel_to_full ( const BatchTensor & mandel,
TorchSize dim = 0 )

Convert a BatchTensor from Mandel notation to full notation.

See full_to_mandel for a detailed explanation.

Parameters
mandelThe input tensor in Mandel notation
dimThe base dimension where the symmetric axes start
Returns
BatchTensor The resulting tensor in full notation.

◆ multiply_and_make_skew()

WR2 multiply_and_make_skew ( const SR2 & a,
const SR2 & b )

Shortcut product a_ik b_kj - b_ik a_kj with both SR2.

◆ pow() [1/5]

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived pow ( const Derived & a,
const Derived & n )

◆ pow() [2/5]

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived pow ( const Derived & a,
const Real & n )

◆ pow() [3/5]

template<class Derived , typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>, typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived pow ( const Derived & a,
const Scalar & n )

◆ pow() [4/5]

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived pow ( const Real & a,
const Derived & n )

◆ pow() [5/5]

template<class Derived , typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>, typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived pow ( const Scalar & a,
const Derived & n )

◆ reduced_to_full()

BatchTensor reduced_to_full ( const BatchTensor & reduced,
const torch::Tensor & rmap,
const torch::Tensor & rfactors,
TorchSize dim = 0 )

Convert a BatchTensor from reduced notation to full notation.

See full_to_reduced for a detailed explanation.

Parameters
reducedThe input tensor in reduced notation
rmapThe unreduction map
rfactorsThe unreduction factors
dimThe base dimension where the reduced axes start
Returns
BatchTensor The resulting tensor in full notation.

◆ sign()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived sign ( const Derived & a)

◆ sinh()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived sinh ( const Derived & a)

◆ skew_and_sym_to_sym()

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.

◆ skew_to_full()

BatchTensor skew_to_full ( const BatchTensor & skew,
TorchSize dim = 0 )

Convert a BatchTensor from skew vector notation to full notation.

See full_to_skew for a detailed explanation.

Parameters
skewThe input tensor in skew notation
dimThe base dimension where the symmetric axes start
Returns
BatchTensor The resulting tensor in full notation.

◆ sqrt()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived sqrt ( const Derived & a)

◆ tanh()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived tanh ( const Derived & a)

◆ where()

template<class Derived , typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
Derived where ( const torch::Tensor & condition,
const Derived & a,
const Derived & b )

Variable Documentation

◆ eps

constexpr Real eps = std::numeric_limits<at::scalar_value_type<Real>::type>::epsilon()
constexpr

◆ invsqrt2

constexpr Real invsqrt2 = 0.7071067811865475
constexpr

◆ mandel_index

constexpr TorchSize mandel_index[6][2] = {{0, 0}, {1, 1}, {2, 2}, {1, 2}, {0, 2}, {0, 1}}
constexpr

◆ mandel_reverse_index

constexpr TorchSize mandel_reverse_index[3][3] = {{0, 5, 4}, {5, 1, 3}, {4, 3, 2}}
constexpr

◆ skew_factor

constexpr Real skew_factor[3][3] = {{0.0, -1.0, 1.0}, {1.0, 0.0, -1.0}, {-1.0, 1.0, 0.0}}
constexpr

◆ skew_reverse_index

constexpr TorchSize skew_reverse_index[3][3] = {{0, 2, 1}, {2, 0, 0}, {1, 0, 0}}
constexpr

◆ sqrt2

constexpr Real sqrt2 = 1.4142135623730951
constexpr