Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
UnaryExpressionImpl.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* UnaryExpressionImpl.h (C) 2000-2006 */
9/* */
10/* Implementation of a unary expression. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_EXPR_UNARYEXPRESSIONIMPL_H
13#define ARCANE_EXPR_UNARYEXPRESSIONIMPL_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/Convert.h"
18#include "arcane/expr/ExpressionImpl.h"
19#include "arcane/expr/Expression.h"
20#include "arcane/expr/ExpressionResult.h"
21#include "arcane/expr/BadOperandException.h"
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32class UnaryOperator;
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
40class ARCANE_EXPR_EXPORT UnaryExpressionImpl
41: public ExpressionImpl
42{
43 public:
44
45 enum eOperationType
46 {
47 UnarySubstract = 0,
48 Inverse = 1,
49 Acos = 2,
50 Asin = 3,
51 Atan = 4,
52 Ceil = 5,
53 Cos = 6,
54 Cosh = 7,
55 Exp = 8,
56 Fabs = 9,
57 Floor = 10,
58 Log = 11,
59 Log10 = 12,
60 Sin = 13,
61 Sinh = 14,
62 Sqrt = 15,
63 Tan = 16,
64 Tanh = 17,
65 NbOperationType = 18
66 };
67
68 public:
69
70 UnaryExpressionImpl(IExpressionImpl* first,
71 eOperationType operation);
72
73 public:
74
75 virtual void assign(IExpressionImpl*) {}
76 virtual void assign(IExpressionImpl*, IntegerConstArrayView) {}
77 virtual void apply(ExpressionResult* result);
78 virtual Integer vectorSize() const { return 0; }
79 String operationName() const { return operationName(m_operation); }
80 static String operationName(eOperationType type);
81
82 private:
83
84 Expression m_first;
85 eOperationType m_operation;
86};
87
88/*---------------------------------------------------------------------------*/
89/*---------------------------------------------------------------------------*/
90
95{
96 public:
97
98 virtual ~UnaryOperator() {}
99 virtual void evaluate(ExpressionResult* res, ArrayVariant* a) = 0;
100};
101
102/*---------------------------------------------------------------------------*/
103/*---------------------------------------------------------------------------*/
104
105template <class T>
107: public UnaryOperator
108{
109 public:
110
111 virtual void evaluate(ArrayView<T> res,
112 ArrayView<T> a) = 0;
113
114 virtual void evaluate(ExpressionResult* res, ArrayVariant* a)
115 {
116 // verification of the operation validity
117 Integer size = res->size();
118 if (size != a->size())
119 throw BadOperandException("DefaultUnaryOperator::evaluate");
120
121 // allocation of the result based on the type of a
122 res->allocate(a->type());
123
124 // retrieval of operand values
125 ArrayView<T> res_val;
126 res->data()->value(res_val);
127 ArrayView<T> a_val;
128 a->value(a_val);
129
130 // array evaluation
131 evaluate(res_val, a_val);
132 }
133};
134
135/*---------------------------------------------------------------------------*/
136/*---------------------------------------------------------------------------*/
137
138#define DEFAULT_UNARY_OP(classname, expression) \
139 template <class T> \
140 class classname : public DefaultUnaryOperator<T> \
141 { \
142 public: \
143\
144 virtual void evaluate(ExpressionResult* res, ArrayVariant* a) \
145 { \
146 DefaultUnaryOperator<T>::evaluate(res, a); \
147 } \
148\
149 virtual void evaluate(ArrayView<T> res, \
150 ArrayView<T> a) \
151 { \
152 Integer size = res.size(); \
153 for (Integer i = 0; i < size; ++i) \
154 expression; \
155 } \
156 };
157/*---------------------------------------------------------------------------*/
158/*---------------------------------------------------------------------------*/
159
160DEFAULT_UNARY_OP(UnarySubstractOperator, res[i] = -a[i])
161DEFAULT_UNARY_OP(InverseOperator, res[i] = 1 / a[i])
162DEFAULT_UNARY_OP(AcosOperator, res[i] = acos(Convert::toDouble(a[i])))
163DEFAULT_UNARY_OP(AsinOperator, res[i] = asin(Convert::toDouble(a[i])))
164DEFAULT_UNARY_OP(AtanOperator, res[i] = atan(Convert::toDouble(a[i])))
165DEFAULT_UNARY_OP(CeilOperator, res[i] = ceil(Convert::toDouble(a[i])))
166DEFAULT_UNARY_OP(CosOperator, res[i] = cos(Convert::toDouble(a[i])))
167DEFAULT_UNARY_OP(CoshOperator, res[i] = cosh(Convert::toDouble(a[i])))
168DEFAULT_UNARY_OP(ExpOperator, res[i] = exp(Convert::toDouble(a[i])))
169DEFAULT_UNARY_OP(FabsOperator, res[i] = fabs(Convert::toDouble(a[i])))
170DEFAULT_UNARY_OP(FloorOperator, res[i] = floor(Convert::toDouble(a[i])))
171DEFAULT_UNARY_OP(LogOperator, res[i] = log(Convert::toDouble(a[i])))
172DEFAULT_UNARY_OP(Log10Operator, res[i] = log10(Convert::toDouble(a[i])))
173DEFAULT_UNARY_OP(SinOperator, res[i] = sin(Convert::toDouble(a[i])))
174DEFAULT_UNARY_OP(SinhOperator, res[i] = sinh(Convert::toDouble(a[i])))
175DEFAULT_UNARY_OP(SqrtOperator, res[i] = sqrt(Convert::toDouble(a[i])))
176DEFAULT_UNARY_OP(TanOperator, res[i] = tan(Convert::toDouble(a[i])))
177DEFAULT_UNARY_OP(TanhOperator, res[i] = tanh(Convert::toDouble(a[i])))
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
181
182} // namespace Arcane
183
184/*---------------------------------------------------------------------------*/
185/*---------------------------------------------------------------------------*/
186
187#endif
Polymorphic base type for arrays (dimension 1).
Modifiable view of an array of type T.
constexpr const_pointer data() const noexcept
Pointer to the start of the view.
Exception for operands in expression operations.
Polymorphic base type of an expression.
Interface for the different implementations of an expression.
virtual Integer vectorSize() const
Number of elements in the vector.
Generic unary operator for expressions.
double toDouble(Real r)
Converts a Real to double.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
ConstArrayView< Integer > IntegerConstArrayView
C equivalent of a 1D array of integers.
Definition UtilsTypes.h:486