NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
operators.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 <ATen/TensorOperators.h>
28
29#include "neml2/misc/types.h"
30#include "neml2/tensors/macros.h"
31#include "neml2/tensors/tensors_fwd.h"
32
33namespace neml2
34{
35// Forward declaration
36#define FORWARD_DECLARATION(T) class T
37FOR_ALL_TENSORBASE(FORWARD_DECLARATION);
38
39// Define macros (let's be responsible and undefine them afterwards)
40#define DECLARE_BINARY_OP(op, T1, T2, TR) TR op(const T1 & a, const T2 & b)
41#define DECLARE_BINARY_OP_SELF(op, T) DECLARE_BINARY_OP(op, T, T, T)
42#define DECLARE_BINARY_OP_SYM(op, T1, T2, TR) \
43 DECLARE_BINARY_OP(op, T1, T2, TR); \
44 DECLARE_BINARY_OP(op, T2, T1, TR)
45#define DECLARE_BINARY_OP_NONCONST(op, T1, T2, TR) TR op(T1 & a, const T2 & b)
46
48// Addition
50// operator+ | non-scalar-prim tensor scalar real
51// ----------------------------------------------------------
52// non-scalar-prim | yes yes yes
53// tensor | yes yes yes
54// scalar | yes yes yes yes
55// real | yes yes yes
56#define DECLARE_ADD_SELF(T) DECLARE_BINARY_OP_SELF(operator+, T)
57#define DECLARE_ADD_SYM_SCALAR(T) DECLARE_BINARY_OP_SYM(operator+, T, Scalar, T)
58#define DECLARE_ADD_SYM_REAL(T) DECLARE_BINARY_OP_SYM(operator+, T, Real, T)
60FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_ADD_SYM_SCALAR);
62DECLARE_ADD_SELF(Tensor);
63DECLARE_ADD_SYM_SCALAR(Tensor);
64DECLARE_ADD_SYM_REAL(Tensor);
65DECLARE_ADD_SELF(Scalar);
66DECLARE_ADD_SYM_REAL(Scalar);
67#undef DECLARE_ADD_SELF
68#undef DECLARE_ADD_SYM_SCALAR
69#undef DECLARE_ADD_SYM_REAL
70
72// Subtraction
74// operator- | non-scalar-prim tensor scalar real
75// ----------------------------------------------------------
76// non-scalar-prim | yes yes yes
77// tensor | yes yes yes
78// scalar | yes yes yes yes
79// real | yes yes yes
80#define DECLARE_SUB_SELF(T) DECLARE_BINARY_OP_SELF(operator-, T)
81#define DECLARE_SUB_SYM_SCALAR(T) DECLARE_BINARY_OP_SYM(operator-, T, Scalar, T)
82#define DECLARE_SUB_SYM_REAL(T) DECLARE_BINARY_OP_SYM(operator-, T, Real, T)
84FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_SUB_SYM_SCALAR);
86DECLARE_SUB_SELF(Tensor);
87DECLARE_SUB_SYM_SCALAR(Tensor);
88DECLARE_SUB_SYM_REAL(Tensor);
89DECLARE_SUB_SELF(Scalar);
90DECLARE_SUB_SYM_REAL(Scalar);
91#undef DECLARE_SUB_SELF
92#undef DECLARE_SUB_SYM_SCALAR
93#undef DECLARE_SUB_SYM_REAL
94
96// Multiplication
98// operator* | non-scalar-prim tensor scalar real
99// ----------------------------------------------------------
100// non-scalar-prim | yes yes
101// tensor | yes yes yes
102// scalar | yes yes yes yes
103// real | yes yes yes
104#define DECLARE_MUL_SELF(T) DECLARE_BINARY_OP_SELF(operator*, T)
105#define DECLARE_MUL_SYM_SCALAR(T) DECLARE_BINARY_OP_SYM(operator*, T, Scalar, T)
106#define DECLARE_MUL_SYM_REAL(T) DECLARE_BINARY_OP_SYM(operator*, T, Real, T)
107FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_MUL_SYM_SCALAR);
109DECLARE_MUL_SELF(Tensor);
110DECLARE_MUL_SYM_SCALAR(Tensor);
111DECLARE_MUL_SYM_REAL(Tensor);
112DECLARE_MUL_SELF(Scalar);
113DECLARE_MUL_SYM_REAL(Scalar);
114#undef DECLARE_MUL_SELF
115#undef DECLARE_MUL_SYM_SCALAR
116#undef DECLARE_MUL_SYM_REAL
117
119// Division
121// operator/ | non-scalar-prim tensor scalar real
122// ----------------------------------------------------------
123// non-scalar-prim | yes yes
124// tensor | yes yes yes
125// scalar | yes yes yes yes
126// real | yes yes yes
127#define DECLARE_DIV_SELF(T) DECLARE_BINARY_OP_SELF(operator/, T)
128#define DECLARE_DIV_SYM_SCALAR(T) DECLARE_BINARY_OP_SYM(operator/, T, Scalar, T)
129#define DECLARE_DIV_SYM_REAL(T) DECLARE_BINARY_OP_SYM(operator/, T, Real, T)
130FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_DIV_SYM_SCALAR);
132DECLARE_DIV_SELF(Tensor);
133DECLARE_DIV_SYM_SCALAR(Tensor);
134DECLARE_DIV_SYM_REAL(Tensor);
135DECLARE_DIV_SELF(Scalar);
136DECLARE_DIV_SYM_REAL(Scalar);
137#undef DECLARE_DIV_SELF
138#undef DECLARE_DIV_SYM_SCALAR
139#undef DECLARE_DIV_SYM_REAL
140
142// In-place addition
144// operator+= | non-scalar-prim tensor scalar real
145// ----------------------------------------------------------
146// non-scalar-prim | yes
147// tensor | yes
148// scalar | yes
149// real |
150#define DECLARE_ADD_EQ(T) DECLARE_BINARY_OP_NONCONST(operator+=, T, Real, T &)
152DECLARE_ADD_EQ(Tensor);
153DECLARE_ADD_EQ(Scalar);
154#undef DECLARE_ADD_EQ
155
157// In-place subtraction
159// operator-= | non-scalar-prim tensor scalar real
160// ----------------------------------------------------------
161// non-scalar-prim | yes
162// tensor | yes
163// scalar | yes
164// real |
165#define DECLARE_SUB_EQ(T) DECLARE_BINARY_OP_NONCONST(operator-=, T, Real, T &)
167DECLARE_SUB_EQ(Tensor);
168DECLARE_SUB_EQ(Scalar);
169#undef DECLARE_SUB_EQ
170
172// In-place multiplication
174// operator*= | non-scalar-prim tensor scalar real
175// ----------------------------------------------------------
176// non-scalar-prim | yes
177// tensor | yes
178// scalar | yes
179// real |
180#define DECLARE_MUL_EQ(T) DECLARE_BINARY_OP_NONCONST(operator*=, T, Real, T &)
182DECLARE_MUL_EQ(Tensor);
183DECLARE_MUL_EQ(Scalar);
184#undef DECLARE_MUL_EQ
185
187// In-place division
189// operator/= | non-scalar-prim tensor scalar real
190// ----------------------------------------------------------
191// non-scalar-prim | yes
192// tensor | yes
193// scalar | yes
194// real |
195#define DECLARE_DIV_EQ(T) DECLARE_BINARY_OP_NONCONST(operator/=, T, Real, T &)
197DECLARE_DIV_EQ(Tensor);
198DECLARE_DIV_EQ(Scalar);
199#undef DECLARE_DIV_EQ
200
201// Undefine macros
202#undef DECLARE_BINARY_OP
203#undef DECLARE_BINARY_OP_SELF
204#undef DECLARE_BINARY_OP_SYM
205} // namespace neml2
Scalar.
Definition Scalar.h:38
Definition Tensor.h:46
Definition DiagnosticsInterface.cxx:30
FOR_ALL_TENSORBASE(INSTANTIATE_TENSORNAME)
FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DEFINE_ADD_SELF)