OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
SUnary.h
Go to the documentation of this file.
1#ifndef OPAL_SUnary_HH
2#define OPAL_SUnary_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: SUnary.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.1.1.1 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Template class: SUnary<T,U>
13//
14// ------------------------------------------------------------------------
15//
16// $Date: 2000/03/27 09:33:42 $
17// $Author: Andreas Adelmann $
18//
19// ------------------------------------------------------------------------
20
26#include <cerrno>
27#include <iostream>
28
29
30namespace Expressions {
31
32 // Template Class SUnary
33 // ----------------------------------------------------------------------
35 // This expression evaluates a scalar operand, and then returns the
36 // result of applying a scalar function to the result.
37
38 template <class T, class U>
39 class SUnary: public Scalar<T> {
40
41 public:
42
44 // Use scalar function of one operand and a scalar operand.
46
47 SUnary(const SUnary<T, U> &);
48 virtual ~SUnary();
49
51 virtual Scalar<T> *clone() const;
52
54 virtual T evaluate() const;
55
57 // If the operand is constant and the function is not a random
58 // generator, evaluate the expression and store the result as a
59 // constant.
62
64 virtual void print(std::ostream &, int precedence = 99) const;
65
66 private:
67
68 // Not implemented.
70 void operator=(const SUnary &);
71
72 // The operation object.
74
75 // The two operands.
77 };
78
79
80 // Implementation
81 // ------------------------------------------------------------------------
82
83 template <class T, class U> inline
85 Scalar<T>(rhs),
86 fun(rhs.fun), opr(rhs.opr->clone())
87 {}
88
89
90 template <class T, class U> inline
92 PtrToScalar<U> oper):
93 fun(function), opr(oper)
94 {}
95
96
97 template <class T, class U> inline
99 {}
100
101
102 template <class T, class U> inline
104 return new SUnary<T, U>(*this);
105 }
106
107
108 template <class T, class U> inline
110 errno = 0;
111 U arg = opr->evaluate();
112 T result = (*fun.function)(arg);
113
114 // Test for run-time evaluation errors.
115 switch(errno) {
116
117 case EDOM:
118 throw DomainError("SUnary::evaluate()");
119
120 case ERANGE:
121 // Ignore underflow.
122 if(result == T(0)) return result;
123 throw OverflowError("SUnary::evaluate()");
124
125 default:
126 ;
127 }
128
129 return result;
130 }
131
132
133 template <class T, class U> inline
136 // We must pick up the constant flag before the ownership of "operand"
137 // is transferred to "result".
138 bool isConst = operand->isConstant();
140
141 if(function.precedence != -2) {
142 if(isConst) {
143 // Constant expression.
144 double value = result->evaluate();
145 result = new SConstant<T>(value);
146 }
147 }
148
149 // Other expression.
150 return result.release();
151 }
152
153
154 template <class T, class U> inline
155 void SUnary<T, U>::print(std::ostream &os, int precedence) const {
156 if(fun.precedence >= 0) {
157 if(fun.precedence <= precedence) os << "(";
158 os << fun.name;
159 opr->print(os, fun.precedence);
160 if(fun.precedence <= precedence) os << ")";
161 } else {
162 os << fun.name << "(";
163 opr->print(os, 0);
164 os << ")";
165 }
166 }
167
168}
169
170#endif // OPAL_SUnary_HH
arg(a))
py::list function(PolynomialPatch *patch, py::list point)
Representation objects and parsers for attribute expressions.
Definition: Expressions.h:64
boost::variant< nil, bool, unsigned int, double, identifier, boost::recursive_wrapper< unary >, boost::recursive_wrapper< function_call >, boost::recursive_wrapper< expression > > operand
Definition: ast.hpp:53
A scalar expression.
Definition: Expressions.h:71
Object * release()
Release ownership.
Definition: OwnPtr.h:150
Domain error exception.
Definition: DomainError.h:32
Overflow exception.
Definition: OverflowError.h:32
A scalar constant expression.
Definition: SConstant.h:35
A scalar expression with one scalar operand.
Definition: SUnary.h:39
virtual Scalar< T > * clone() const
Make clone.
Definition: SUnary.h:103
PtrToScalar< U > opr
Definition: SUnary.h:76
virtual T evaluate() const
Evaluate.
Definition: SUnary.h:109
void operator=(const SUnary &)
const TFunction1< T, U > & fun
Definition: SUnary.h:73
virtual void print(std::ostream &, int precedence=99) const
Print expression.
Definition: SUnary.h:155
virtual ~SUnary()
Definition: SUnary.h:98
static Scalar< T > * make(const TFunction1< T, U > &function, PtrToScalar< U > operand)
Make a new expression.
Definition: SUnary.h:135
A function of one U, returning a T.
Definition: TFunction1.h:31