NEML2 2.0.0
Loading...
Searching...
No Matches
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_UNARY_OP(op, T, TR) TR op(const T & a)
41#define DECLARE_BINARY_OP(op, T1, T2, TR) TR op(const T1 & a, const T2 & b)
42#define DECLARE_BINARY_OP_SELF(op, T) DECLARE_BINARY_OP(op, T, T, T)
43#define DECLARE_BINARY_OP_SYM(op, T1, T2, TR) \
44 DECLARE_BINARY_OP(op, T1, T2, TR); \
45 DECLARE_BINARY_OP(op, T2, T1, TR)
46#define DECLARE_BINARY_OP_NONCONST(op, T1, T2, TR) TR op(T1 & a, const T2 & b)
47
49// Addition
51// operator+ | non-scalar-prim tensor scalar cscalar
52// ----------------------------------------------------------
53// non-scalar-prim | yes yes yes
54// tensor | yes yes yes
55// scalar | yes yes yes yes
56// cscalar | yes yes yes
57#define DECLARE_ADD_SELF(T) DECLARE_BINARY_OP_SELF(operator+, T)
58#define DECLARE_ADD_SYM_SCALAR(T) DECLARE_BINARY_OP_SYM(operator+, T, Scalar, T)
59#define DECLARE_ADD_SYM_CSCALAR(T) DECLARE_BINARY_OP_SYM(operator+, T, CScalar, T)
60FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_ADD_SELF);
61FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_ADD_SYM_SCALAR);
62FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_ADD_SYM_CSCALAR);
63DECLARE_ADD_SELF(Tensor);
64DECLARE_ADD_SYM_SCALAR(Tensor);
65DECLARE_ADD_SYM_CSCALAR(Tensor);
66DECLARE_ADD_SELF(Scalar);
67DECLARE_ADD_SYM_CSCALAR(Scalar);
68#undef DECLARE_ADD_SELF
69#undef DECLARE_ADD_SYM_SCALAR
70#undef DECLARE_ADD_SYM_CSCALAR
71
73// Subtraction
75// operator- | non-scalar-prim tensor scalar cscalar
76// ----------------------------------------------------------
77// non-scalar-prim | yes yes yes
78// tensor | yes yes yes
79// scalar | yes yes yes yes
80// cscalar | yes yes yes
81#define DECLARE_SUB_SELF(T) DECLARE_BINARY_OP_SELF(operator-, T)
82#define DECLARE_SUB_SYM_SCALAR(T) DECLARE_BINARY_OP_SYM(operator-, T, Scalar, T)
83#define DECLARE_SUB_SYM_CSCALAR(T) DECLARE_BINARY_OP_SYM(operator-, T, CScalar, T)
84FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_SUB_SELF);
85FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_SUB_SYM_SCALAR);
86FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_SUB_SYM_CSCALAR);
87DECLARE_SUB_SELF(Tensor);
88DECLARE_SUB_SYM_SCALAR(Tensor);
89DECLARE_SUB_SYM_CSCALAR(Tensor);
90DECLARE_SUB_SELF(Scalar);
91DECLARE_SUB_SYM_CSCALAR(Scalar);
92#undef DECLARE_SUB_SELF
93#undef DECLARE_SUB_SYM_SCALAR
94#undef DECLARE_SUB_SYM_CSCALAR
95
97// Multiplication
99// operator* | non-scalar-prim tensor scalar cscalar
100// ----------------------------------------------------------
101// non-scalar-prim | yes yes
102// tensor | yes yes yes
103// scalar | yes yes yes yes
104// cscalar | yes yes yes
105#define DECLARE_MUL_SELF(T) DECLARE_BINARY_OP_SELF(operator*, T)
106#define DECLARE_MUL_SYM_SCALAR(T) DECLARE_BINARY_OP_SYM(operator*, T, Scalar, T)
107#define DECLARE_MUL_SYM_CSCALAR(T) DECLARE_BINARY_OP_SYM(operator*, T, CScalar, T)
108FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_MUL_SYM_SCALAR);
109FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_MUL_SYM_CSCALAR);
110DECLARE_MUL_SELF(Tensor);
111DECLARE_MUL_SYM_SCALAR(Tensor);
112DECLARE_MUL_SYM_CSCALAR(Tensor);
113DECLARE_MUL_SELF(Scalar);
114DECLARE_MUL_SYM_CSCALAR(Scalar);
115#undef DECLARE_MUL_SELF
116#undef DECLARE_MUL_SYM_SCALAR
117#undef DECLARE_MUL_SYM_CSCALAR
118
120// Division
122// operator/ | non-scalar-prim tensor scalar cscalar
123// ----------------------------------------------------------
124// non-scalar-prim | yes yes
125// tensor | yes yes yes
126// scalar | yes yes yes yes
127// cscalar | yes yes yes
128#define DECLARE_DIV_SELF(T) DECLARE_BINARY_OP_SELF(operator/, T)
129#define DECLARE_DIV_SYM_SCALAR(T) DECLARE_BINARY_OP_SYM(operator/, T, Scalar, T)
130#define DECLARE_DIV_SYM_CSCALAR(T) DECLARE_BINARY_OP_SYM(operator/, T, CScalar, T)
131FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_DIV_SYM_SCALAR);
132FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_DIV_SYM_CSCALAR);
133DECLARE_DIV_SELF(Tensor);
134DECLARE_DIV_SYM_SCALAR(Tensor);
135DECLARE_DIV_SYM_CSCALAR(Tensor);
136DECLARE_DIV_SELF(Scalar);
137DECLARE_DIV_SYM_CSCALAR(Scalar);
138#undef DECLARE_DIV_SELF
139#undef DECLARE_DIV_SYM_SCALAR
140#undef DECLARE_DIV_SYM_CSCALAR
141
143// In-place addition
145// operator+= | non-scalar-prim tensor scalar cscalar
146// ----------------------------------------------------------
147// non-scalar-prim | yes
148// tensor | yes
149// scalar | yes
150// cscalar |
151#define DECLARE_ADD_EQ(T) DECLARE_BINARY_OP_NONCONST(operator+=, T, CScalar, T &)
152FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_ADD_EQ);
153DECLARE_ADD_EQ(Tensor);
154DECLARE_ADD_EQ(Scalar);
155#undef DECLARE_ADD_EQ
156
158// In-place subtraction
160// operator-= | non-scalar-prim tensor scalar cscalar
161// ----------------------------------------------------------
162// non-scalar-prim | yes
163// tensor | yes
164// scalar | yes
165// cscalar |
166#define DECLARE_SUB_EQ(T) DECLARE_BINARY_OP_NONCONST(operator-=, T, CScalar, T &)
167FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_SUB_EQ);
168DECLARE_SUB_EQ(Tensor);
169DECLARE_SUB_EQ(Scalar);
170#undef DECLARE_SUB_EQ
171
173// In-place multiplication
175// operator*= | non-scalar-prim tensor scalar cscalar
176// ----------------------------------------------------------
177// non-scalar-prim | yes
178// tensor | yes
179// scalar | yes
180// cscalar |
181#define DECLARE_MUL_EQ(T) DECLARE_BINARY_OP_NONCONST(operator*=, T, CScalar, T &)
182FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_MUL_EQ);
183DECLARE_MUL_EQ(Tensor);
184DECLARE_MUL_EQ(Scalar);
185#undef DECLARE_MUL_EQ
186
188// In-place division
190// operator/= | non-scalar-prim tensor scalar cscalar
191// ----------------------------------------------------------
192// non-scalar-prim | yes
193// tensor | yes
194// scalar | yes
195// cscalar |
196#define DECLARE_DIV_EQ(T) DECLARE_BINARY_OP_NONCONST(operator/=, T, CScalar, T &)
197FOR_ALL_NONSCALAR_PRIMITIVETENSOR(DECLARE_DIV_EQ);
198DECLARE_DIV_EQ(Tensor);
199DECLARE_DIV_EQ(Scalar);
200#undef DECLARE_DIV_EQ
201
202// Undefine macros
203#undef DECLARE_BINARY_OP
204#undef DECLARE_BINARY_OP_SELF
205#undef DECLARE_BINARY_OP_SYM
206} // namespace neml2
Scalar.
Definition Scalar.h:38
Definition Tensor.h:47
Definition DiagnosticsInterface.cxx:30