OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
ABinary.h
Go to the documentation of this file.
1#ifndef OPAL_ABinary_HH
2#define OPAL_ABinary_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: ABinary.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.2 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Template class: ABinary<T,U>
13//
14// ------------------------------------------------------------------------
15//
16// $Date: 2002/01/17 22:18:36 $
17// $Author: jsberg $
18//
19// ------------------------------------------------------------------------
20
21#include "Expressions/AList.h"
26#include <cerrno>
27#include <iosfwd>
28#include <vector>
29
30
31namespace Expressions {
32
33 // Template class ABinary
34 // ----------------------------------------------------------------------
36 // The expression first evaluates the two array operands, and then
37 // loops in parallel over the results and applies the given function
38 // to each pair of components.
39
40 template <class T, class U>
41 class ABinary: public AList<T> {
42
43 public:
44
46 // Use scalar function with two operands and two array expressions.
48 PtrToArray<U> left, PtrToArray<U> right);
49
50 ABinary(const ABinary<T, U> &);
51 virtual ~ABinary();
52
54 virtual OArray<T> *clone() const;
55
57 virtual std::vector<T> evaluate() const;
58
60 virtual void print(std::ostream &str, int precedence = 99) const;
61
62 private:
63
64 // Not implemented.
66 void operator=(const ABinary &);
67
68 // The operation object.
70
71 // The two operands.
74 };
75
76
77 // Implementation
78 // ----------------------------------------------------------------------
79
80 template <class T, class U> inline
82 AList<T>(), fun(rhs.fun), lft(rhs.lft->clone()), rgt(rhs.rgt->clone())
83 {}
84
85
86 template <class T, class U> inline
88 PtrToArray<U> left, PtrToArray<U> right):
89 AList<T>(), fun(function), lft(left), rgt(right)
90 {}
91
92
93 template <class T, class U> inline
95 {}
96
97
98 template <class T, class U> inline
100 return new ABinary<T, U>(*this);
101 }
102
103
104 template <class T, class U> inline
105 std::vector<T> ABinary<T, U>::evaluate() const {
106 errno = 0;
107 std::vector<U> op1 = lft->evaluate();
108 std::vector<U> op2 = rgt->evaluate();
109 std::vector<T> result;
110
111 if(op1.size() == op2.size()) {
112 for(typename std::vector<U>::size_type i = 0; i < op1.size(); ++i) {
113 result.push_back((*fun.function)(op1[i], op2[i]));
114 }
115 } else if(op1.size() == 1) {
116 for(typename std::vector<U>::size_type i = 0; i < op2.size(); ++i) {
117 result.push_back((*fun.function)(op1[0], op2[i]));
118 }
119 } else if(op2.size() == 1) {
120 for(typename std::vector<U>::size_type i = 0; i < op1.size(); ++i) {
121 result.push_back((*fun.function)(op1[i], op2[0]));
122 }
123 } else {
124 throw OpalException("ABinary::evaluate()",
125 "Inconsistent array dimensions.");
126 }
127
128 // Test for run-time evaluation errors.
129 switch(errno) {
130
131 case EDOM:
132 throw DomainError("ABinary::evaluate()");
133
134 case ERANGE:
135 throw OverflowError("ABinary::evaluate()");
136
137 default:
138 return result;
139 }
140 }
141
142
143 template <class T, class U> inline
144 void ABinary<T, U>::print(std::ostream &os, int precedence) const {
145 if(fun.precedence >= 0) {
146 // Binary operation.
147 if(fun.precedence <= precedence) os << "(";
148 lft->print(os, fun.precedence - 1);
149 os << fun.name;
150 rgt->print(os, fun.precedence);
151 if(fun.precedence <= precedence) os << ")";
152 } else {
153 // Function.
154 os << fun.name << '(';
155 lft->print(os, 0);
156 os << ',';
157 rgt->print(os, 0);
158 os << ')';
159 }
160 }
161
162}
163
164#endif // OPAL_ABinary_HH
py::list function(PolynomialPatch *patch, py::list point)
Representation objects and parsers for attribute expressions.
Definition: Expressions.h:64
An array expression.
Definition: Expressions.h:142
Domain error exception.
Definition: DomainError.h:32
Overflow exception.
Definition: OverflowError.h:32
An array expression with two array operands.
Definition: ABinary.h:41
virtual OArray< T > * clone() const
Make clone.
Definition: ABinary.h:99
PtrToArray< U > rgt
Definition: ABinary.h:73
void operator=(const ABinary &)
virtual void print(std::ostream &str, int precedence=99) const
Print expression.
Definition: ABinary.h:144
PtrToArray< U > lft
Definition: ABinary.h:72
const TFunction2< T, U > & fun
Definition: ABinary.h:69
virtual ~ABinary()
Definition: ABinary.h:94
virtual std::vector< T > evaluate() const
Evaluate.
Definition: ABinary.h:105
An array expression defined by a list of scalar expressions.
Definition: AList.h:36
A function of two U's returning a T.
Definition: TFunction2.h:31
The base class for all OPAL exceptions.
Definition: OpalException.h:28