NEML2 2.0.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
49namespace math
50{
52Scalar minimum(const Scalar & a, const Scalar & b);
53}
54
56// I don't understand why, but apparently without this the Tensor abs and aten::abs (i.e. the
57// torch native abs) are ambiguous
58Scalar abs(const Scalar & a);
59
60template <class Derived,
61 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
62 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
63Derived
64operator+(const Derived & a, const Scalar & b)
65{
67 indexing::TensorIndices net{torch::indexing::Ellipsis};
68 net.insert(net.end(), a.base_dim(), torch::indexing::None);
69 return Derived(torch::operator+(a, b.index(net)), broadcast_batch_dim(a, b));
70}
71
72template <class Derived,
73 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
74 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
75Derived
76operator+(const Scalar & a, const Derived & b)
77{
78 return b + a;
79}
80
81template <class Derived,
82 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
83 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
84Derived
85operator-(const Derived & a, const Scalar & b)
86{
88 indexing::TensorIndices net{torch::indexing::Ellipsis};
89 net.insert(net.end(), a.base_dim(), torch::indexing::None);
90 return Derived(torch::operator-(a, b.index(net)), broadcast_batch_dim(a, b));
91}
92
93template <class Derived,
94 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
95 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
96Derived
97operator-(const Scalar & a, const Derived & b)
98{
99 return -b + a;
100}
101
102template <class Derived,
103 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
104 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
105Derived
106operator*(const Derived & a, const Scalar & b)
107{
109 indexing::TensorIndices net{torch::indexing::Ellipsis};
110 net.insert(net.end(), a.base_dim(), torch::indexing::None);
111 return Derived(torch::operator*(a, b.index(net)), broadcast_batch_dim(a, b));
112}
113
114template <class Derived,
115 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
116 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
117Derived
118operator*(const Scalar & a, const Derived & b)
119{
120 return b * a;
121}
122
123Scalar operator*(const Scalar & a, const Scalar & b);
124
125template <class Derived,
126 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
127 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
128Derived
129operator/(const Derived & a, const Scalar & b)
130{
132 indexing::TensorIndices net{torch::indexing::Ellipsis};
133 net.insert(net.end(), a.base_dim(), torch::indexing::None);
134 return Derived(torch::operator/(a, b.index(net)), broadcast_batch_dim(a, b));
135}
136
137template <class Derived,
138 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
139 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
140Derived
141operator/(const Scalar & a, const Derived & b)
142{
144 indexing::TensorIndices net{torch::indexing::Ellipsis};
145 net.insert(net.end(), b.base_dim(), torch::indexing::None);
146 return Derived(torch::operator/(a.index(net), b), broadcast_batch_dim(a, b));
147}
148
149namespace math
150{
151template <class Derived,
152 typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<Derived>, Derived>>>
153Derived
154pow(const Derived & a, const Scalar & n)
155{
157 indexing::TensorIndices net{torch::indexing::Ellipsis};
158 net.insert(net.end(), a.base_dim(), torch::indexing::None);
159 return Derived(torch::pow(a, n.index(net)), broadcast_batch_dim(a, n));
160}
161}
162
163} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:54
PrimitiveTensor inherits from TensorBase and additionally templates on the base shape.
Definition PrimitiveTensor.h:38
PrimitiveTensor()=default
Default constructor.
Scalar.
Definition Scalar.h:38
Scalar(Real init, const torch::TensorOptions &options)
Definition Scalar.cxx:30
static Scalar identity_map(const torch::TensorOptions &options=default_tensor_options())
The derivative of a Scalar with respect to itself.
Definition Scalar.cxx:36
torch::SmallVector< TensorIndex > TensorIndices
Definition types.h:41
Scalar minimum(const Scalar &a, const Scalar &b)
Minimum between two scalars.
Definition Scalar.cxx:44
Tensor pow(const Real &a, const Tensor &n)
Definition math.cxx:336
Definition CrossRef.cxx:31
Vec operator*(const Derived1 &A, const Derived2 &b)
matrix-vector product
Definition R2Base.cxx:233
void neml_assert_batch_broadcastable_dbg(const T &...)
A helper function to assert that (in Debug mode) all tensors are batch-broadcastable.
auto operator/(const T1 &a, const T2 &b)
Definition Variable.h:367
Size broadcast_batch_dim(const T &...)
The batch dimension after broadcasting.
torch::TensorOptions & default_tensor_options()
Definition types.cxx:157
double Real
Definition types.h:31
auto operator+(const T1 &a, const T2 &b)
Definition Variable.h:364
Scalar abs(const Scalar &a)
Absolute value.
Definition Scalar.cxx:61
auto operator-(const T1 &a, const T2 &b)
Definition Variable.h:365