NEML2 1.4.0
Loading...
Searching...
No Matches
Scalar.h
1// Copyright 2024, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#pragma once
26
27#include "neml2/tensors/PrimitiveTensor.h"
28
29namespace neml2
30{
37class Scalar : public PrimitiveTensor<Scalar>
38{
39public:
41
42 Scalar(Real init, const torch::TensorOptions & options);
43
45 [[nodiscard]] static Scalar
46 identity_map(const torch::TensorOptions & options = default_tensor_options());
47};
48
50// I don't understand why, but apparently without this the Tensor abs and aten::abs (i.e. the
51// torch native abs) are ambiguous
52Scalar abs(const Scalar & a);
53
54template <class Derived,
55 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
56 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
58operator+(const Derived & a, const Scalar & b)
59{
61 indexing::TensorIndices net{torch::indexing::Ellipsis};
62 net.insert(net.end(), a.base_dim(), torch::indexing::None);
63 return Derived(torch::operator+(a, b.index(net)), broadcast_batch_dim(a, b));
64}
65
66template <class Derived,
67 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
68 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
69Derived
70operator+(const Scalar & a, const Derived & b)
71{
72 return b + a;
73}
74
75template <class Derived,
76 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
77 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
78Derived
79operator-(const Derived & a, const Scalar & b)
80{
82 indexing::TensorIndices net{torch::indexing::Ellipsis};
83 net.insert(net.end(), a.base_dim(), torch::indexing::None);
84 return Derived(torch::operator-(a, b.index(net)), broadcast_batch_dim(a, b));
85}
86
87template <class Derived,
88 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
89 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
90Derived
91operator-(const Scalar & a, const Derived & b)
92{
93 return -b + a;
94}
95
96template <class Derived,
97 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
98 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
99Derived
100operator*(const Derived & a, const Scalar & b)
101{
103 indexing::TensorIndices net{torch::indexing::Ellipsis};
104 net.insert(net.end(), a.base_dim(), torch::indexing::None);
105 return Derived(torch::operator*(a, b.index(net)), broadcast_batch_dim(a, b));
106}
107
108template <class Derived,
109 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
110 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
111Derived
112operator*(const Scalar & a, const Derived & b)
113{
114 return b * a;
115}
116
117Scalar operator*(const Scalar & a, const Scalar & b);
118
119template <class Derived,
120 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
121 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
122Derived
123operator/(const Derived & a, const Scalar & b)
124{
126 indexing::TensorIndices net{torch::indexing::Ellipsis};
127 net.insert(net.end(), a.base_dim(), torch::indexing::None);
128 return Derived(torch::operator/(a, b.index(net)), broadcast_batch_dim(a, b));
129}
130
131template <class Derived,
132 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
133 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
134Derived
135operator/(const Scalar & a, const Derived & b)
136{
138 indexing::TensorIndices net{torch::indexing::Ellipsis};
139 net.insert(net.end(), b.base_dim(), torch::indexing::None);
140 return Derived(torch::operator/(a.index(net), b), broadcast_batch_dim(a, b));
141}
142
143namespace math
144{
145template <class Derived,
146 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
147Derived
148pow(const Derived & a, const Scalar & n)
149{
151 indexing::TensorIndices net{torch::indexing::Ellipsis};
152 net.insert(net.end(), a.base_dim(), torch::indexing::None);
153 return Derived(torch::pow(a, n.index(net)), broadcast_batch_dim(a, n));
154}
155}
156} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:56
PrimitiveTensor inherits from TensorBase and additionally templates on the base shape.
Definition PrimitiveTensor.h:38
PrimitiveTensor()=default
Default constructor.
The (logical) scalar.
Definition Scalar.h:38
Scalar(Real init, const torch::TensorOptions &options)
Definition Scalar.cxx:29
static Scalar identity_map(const torch::TensorOptions &options=default_tensor_options())
The derivative of a Scalar with respect to itself.
Definition Scalar.cxx:35
torch::SmallVector< TensorIndex > TensorIndices
Definition types.h:41
Tensor pow(const Real &a, const Tensor &n)
Definition math.cxx:309
Definition CrossRef.cxx:30
Vec operator*(const Derived1 &A, const Derived2 &b)
matrix-vector product
Definition R2Base.cxx:233
void neml_assert_batch_broadcastable_dbg(T &&...)
A helper function to assert that (in Debug mode) all tensors are batch-broadcastable.
Derived operator-(const Derived &a, const Scalar &b)
Definition Scalar.h:79
Derived operator+(const Derived &a, const Scalar &b)
Definition Scalar.h:58
Size broadcast_batch_dim(T &&...)
The batch dimension after broadcasting.
torch::TensorOptions & default_tensor_options()
Definition types.cxx:30
double Real
Definition types.h:31
Derived operator/(const Derived &a, const Scalar &b)
Definition Scalar.h:123
Scalar abs(const Scalar &a)
Absolute value.
Definition Scalar.cxx:48