NEML2 1.4.0
Loading...
Searching...
No Matches
Scalar.h
1// Copyright 2023, 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/FixedDimTensor.h"
28
29namespace neml2
30{
37class Scalar : public FixedDimTensor<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 BatchTensor abs and aten::abs (i.e. the
51// torch native abs) are ambiguous
52Scalar abs(const Scalar & a);
53
54template <
55 class Derived,
56 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
57 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
59operator+(const Derived & a, const Scalar & b)
60{
62 TorchSlice net{torch::indexing::Ellipsis};
63 net.insert(net.end(), a.base_dim(), torch::indexing::None);
64 return Derived(torch::operator+(a, b.index(net)), broadcast_batch_dim(a, b));
65}
66
67template <
68 class Derived,
69 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
70 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
71Derived
72operator+(const Scalar & a, const Derived & b)
73{
74 return b + a;
75}
76
77template <
78 class Derived,
79 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
80 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
81Derived
82operator-(const Derived & a, const Scalar & b)
83{
85 TorchSlice net{torch::indexing::Ellipsis};
86 net.insert(net.end(), a.base_dim(), torch::indexing::None);
87 return Derived(torch::operator-(a, b.index(net)), broadcast_batch_dim(a, b));
88}
89
90template <
91 class Derived,
92 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
93 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
94Derived
95operator-(const Scalar & a, const Derived & b)
96{
97 return -b + a;
98}
99
100template <
101 class Derived,
102 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
103 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
104Derived
105operator*(const Derived & a, const Scalar & b)
106{
108 TorchSlice net{torch::indexing::Ellipsis};
109 net.insert(net.end(), a.base_dim(), torch::indexing::None);
110 return Derived(torch::operator*(a, b.index(net)), broadcast_batch_dim(a, b));
111}
112
113template <
114 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<BatchTensorBase<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 <
126 class Derived,
127 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
128 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
129Derived
130operator/(const Derived & a, const Scalar & b)
131{
133 TorchSlice net{torch::indexing::Ellipsis};
134 net.insert(net.end(), a.base_dim(), torch::indexing::None);
135 return Derived(torch::operator/(a, b.index(net)), broadcast_batch_dim(a, b));
136}
137
138template <
139 class Derived,
140 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
141 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
142Derived
143operator/(const Scalar & a, const Derived & b)
144{
146 TorchSlice net{torch::indexing::Ellipsis};
147 net.insert(net.end(), b.base_dim(), torch::indexing::None);
148 return Derived(torch::operator/(a.index(net), b), broadcast_batch_dim(a, b));
149}
150
151namespace math
152{
153template <
154 class Derived,
155 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
156 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
157Derived
158pow(const Derived & a, const Scalar & n)
159{
161 TorchSlice net{torch::indexing::Ellipsis};
162 net.insert(net.end(), a.base_dim(), torch::indexing::None);
163 return Derived(torch::pow(a, n.index(net)), broadcast_batch_dim(a, n));
164}
165
166template <
167 class Derived,
168 typename = typename std::enable_if_t<!std::is_same_v<Derived, Scalar>>,
169 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<Derived>, Derived>>>
171pow(const Scalar & a, const Derived & n)
172{
174 TorchSlice net{torch::indexing::Ellipsis};
175 net.insert(net.end(), n.base_dim(), torch::indexing::None);
176 return Derived(torch::pow(a.index(net), n), broadcast_batch_dim(a, n));
177}
178}
179} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
FixedDimTensor inherits from BatchTensorBase and additionally templates on the base shape.
Definition FixedDimTensor.h:38
FixedDimTensor()=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
Derived pow(const Derived &a, const Real &n)
Definition BatchTensorBase.h:332
Definition CrossRef.cxx:32
Derived operator-(const Derived &a, const Real &b)
Definition BatchTensorBase.h:256
BatchTensor operator*(const BatchTensor &a, const BatchTensor &b)
Definition BatchTensor.cxx:153
const torch::TensorOptions default_tensor_options()
Definition types.cxx:30
void neml_assert_batch_broadcastable_dbg(T &&...)
A helper function to assert that (in Debug mode) all tensors are batch-broadcastable.
TorchSize broadcast_batch_dim(T &&...)
The batch dimension after broadcasting.
double Real
Definition types.h:33
Derived operator+(const Derived &a, const Real &b)
Definition BatchTensorBase.h:228
std::vector< at::indexing::TensorIndex > TorchSlice
Definition types.h:39
Derived operator/(const Derived &a, const Real &b)
Definition BatchTensorBase.h:302
Scalar abs(const Scalar &a)
Absolute value.
Definition Scalar.cxx:48