source: XIOS/trunk/src/parse_expr/operator_expr.hpp @ 1019

Last change on this file since 1019 was 1019, checked in by mhnguyen, 7 years ago

Adding new operator for expression: Not Equal, NE, /=

Test
+) On Curie
+) Okie

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 11.4 KB
Line 
1#ifndef __XIOS_OPERATOR_EXPR_HPP__
2#define __XIOS_OPERATOR_EXPR_HPP__
3
4#include <map>
5#include <string>
6#include <cmath>
7#include "exception.hpp"
8#include "array_new.hpp"
9
10using namespace std;
11
12namespace xios
13{
14  class COperatorExpr
15  {
16    public:
17    typedef double (*functionScalar)(double);
18    typedef double (*functionScalarScalar)(double, double);
19    typedef CArray<double,1> (*functionField)(const CArray<double,1>&);
20    typedef CArray<double,1> (*functionFieldField)(const CArray<double,1>&, const CArray<double,1>&);
21    typedef CArray<double,1> (*functionFieldScalar)(const CArray<double,1>&, double);
22    typedef CArray<double,1> (*functionScalarField)(double, const CArray<double,1>&);
23
24    COperatorExpr(void)
25    {
26      opScalar[string("neg")] = neg_s;
27      opScalar[string("cos")] = cos_s;
28      opScalar[string("sin")] = sin_s;
29      opScalar[string("tan")] = tan_s;
30      opScalar[string("exp")] = exp_s;
31      opScalar[string("log")] = log_s;
32      opScalar[string("log10")] = log10_s;
33      opScalar[string("sqrt")] = sqrt_s;
34
35      opScalarScalar[string("add")] = add_ss;
36      opScalarScalar[string("minus")] = minus_ss;
37      opScalarScalar[string("mult")] = mult_ss;
38      opScalarScalar[string("div")] = div_ss;
39      opScalarScalar[string("pow")] = pow_ss;
40      opScalarScalar[string("eq")] = eq_ss;
41      opScalarScalar[string("lt")] = lt_ss;
42      opScalarScalar[string("gt")] = gt_ss;
43      opScalarScalar[string("le")] = le_ss;
44      opScalarScalar[string("ge")] = ge_ss;
45      opScalarScalar[string("ne")] = ne_ss;
46
47      opField[string("neg")] = neg_f;
48      opField[string("cos")] = cos_f;
49      opField[string("sin")] = sin_f;
50      opField[string("tan")] = tan_f;
51      opField[string("exp")] = exp_f;
52      opField[string("log")] = log_f;
53      opField[string("log10")] = log10_f;
54      opField[string("sqrt")] = sqrt_f;
55
56      opFieldField[string("add")] = add_ff;
57      opFieldField[string("minus")] = minus_ff;
58      opFieldField[string("mult")] = mult_ff;
59      opFieldField[string("div")] = div_ff;
60      opFieldField[string("pow")] = pow_ff;
61      opFieldField[string("eq")] = eq_ff;
62      opFieldField[string("lt")] = lt_ff;
63      opFieldField[string("gt")] = gt_ff;
64      opFieldField[string("le")] = le_ff;
65      opFieldField[string("ge")] = ge_ff;
66      opFieldField[string("ne")] = ne_ff;
67
68      opFieldScalar[string("add")] = add_fs;
69      opFieldScalar[string("minus")] = minus_fs;
70      opFieldScalar[string("mult")] = mult_fs;
71      opFieldScalar[string("div")] = div_fs;
72      opFieldScalar[string("pow")] = pow_fs;
73      opFieldScalar[string("eq")] = eq_fs;
74      opFieldScalar[string("lt")] = lt_fs;
75      opFieldScalar[string("gt")] = gt_fs;
76      opFieldScalar[string("le")] = le_fs;
77      opFieldScalar[string("ge")] = ge_fs;
78      opFieldScalar[string("ne")] = ne_fs;
79
80      opScalarField[string("add")] = add_sf;
81      opScalarField[string("minus")] = minus_sf;
82      opScalarField[string("mult")] = mult_sf;
83      opScalarField[string("div")] = div_sf;
84      opScalarField[string("eq")] = eq_sf;
85      opScalarField[string("lt")] = lt_sf;
86      opScalarField[string("gt")] = gt_sf;
87      opScalarField[string("le")] = le_sf;
88      opScalarField[string("ge")] = ge_sf;
89      opScalarField[string("ne")] = ne_sf;
90    }
91
92    functionScalar getOpScalar(const string& id)
93    {
94      map<string,double (*)(double)>::iterator it;
95      it = opScalar.find(id);
96      if (it == opScalar.end()) ERROR("functionScalar COperatorExpr::getOpScalar(const string& id)", << "unknown operator : " << id)
97      return it->second;
98    }
99
100    functionScalarScalar getOpScalarScalar(const string& id)
101    {
102      map<string,double (*)(double,double)>::iterator it;
103      it = opScalarScalar.find(id);
104      if (it == opScalarScalar.end()) ERROR("functionScalarScalar COperatorExpr::getOpScalarScalar(const string& id)", << "unknown operator : " << id)
105      return it->second;
106    }
107
108    functionField getOpField(const string& id)
109    {
110      map<string,functionField>::iterator it;
111      it = opField.find(id);
112      if (it == opField.end()) ERROR("functionField COperatorExpr::getOpField(const string& id)", << "unknown operator : " << id)
113      return it->second;
114    }
115
116    functionFieldField getOpFieldField(const string& id)
117    {
118      map<string,functionFieldField>::iterator it;
119      it = opFieldField.find(id);
120      if (it == opFieldField.end()) ERROR("dfunctionFieldField COperatorExpr::getOpFieldField(const string& id)", << "unknown operator : " << id)
121      return it->second;
122    }
123
124    functionFieldScalar getOpFieldScalar(const string& id)
125    {
126      map<string,functionFieldScalar>::iterator it;
127      it = opFieldScalar.find(id);
128      if (it == opFieldScalar.end()) ERROR("functionFieldScalar COperatorExpr::getOpFieldScalar(const string& id)", << "unknown operator : " << id)
129      return it->second;
130    }
131
132    functionScalarField getOpScalarField(const string& id)
133    {
134      map<string,functionScalarField>::iterator it;
135      it = opScalarField.find(id);
136      if (it == opScalarField.end()) ERROR("functionScalarField COperatorExpr::getOpFieldField(const string& id)", << "unknown operator : " << id)
137      return it->second;
138    }
139
140    map<string,functionScalar> opScalar;
141    map<string,functionScalarScalar> opScalarScalar;
142    map<string,functionField> opField;
143    map<string,functionFieldField> opFieldField;
144    map<string,functionFieldScalar> opFieldScalar;
145    map<string,functionScalarField> opScalarField;
146
147    static inline double neg_s(double x)   { return -x; }
148    static inline double cos_s(double x)   { return std::cos(x); }
149    static inline double sin_s(double x)   { return std::sin(x); }
150    static inline double tan_s(double x)   { return std::tan(x); }
151    static inline double exp_s(double x)   { return std::exp(x); }
152    static inline double log_s(double x)   { return std::log(x); }
153    static inline double log10_s(double x) { return std::log10(x); }
154    static inline double sqrt_s(double x)  { return std::sqrt(x); }
155
156    static inline double add_ss(double x, double y)   { return x + y; }
157    static inline double minus_ss(double x, double y) { return x - y; }
158    static inline double mult_ss(double x, double y)  { return x * y; }
159    static inline double div_ss(double x, double y)   { return x / y; }
160    static inline double pow_ss(double x, double y)   { return std::pow(x,y); }
161    static inline double eq_ss(double x, double y)    { return x == y; }
162    static inline double lt_ss(double x, double y)    { return x < y; }
163    static inline double gt_ss(double x, double y)    { return x > y; }
164    static inline double le_ss(double x, double y)    { return x <= y; }
165    static inline double ge_ss(double x, double y)    { return x >= y; }
166    static inline double ne_ss(double x, double y)    { return x != y; }
167
168    static inline CArray<double,1> neg_f(const CArray<double,1>& x)   { return Array<double,1>(-x); }
169    static inline CArray<double,1> cos_f(const CArray<double,1>& x)   { return Array<double,1>(cos(x)); }
170    static inline CArray<double,1> sin_f(const CArray<double,1>& x)   { return Array<double,1>(sin(x)); }
171    static inline CArray<double,1> tan_f(const CArray<double,1>& x)   { return Array<double,1>(tan(x)); }
172    static inline CArray<double,1> exp_f(const CArray<double,1>& x)   { return Array<double,1>(exp(x)); }
173    static inline CArray<double,1> log_f(const CArray<double,1>& x)   { return Array<double,1>(log(x)); }
174    static inline CArray<double,1> log10_f(const CArray<double,1>& x) { return Array<double,1>(log10(x)); }
175    static inline CArray<double,1> sqrt_f(const CArray<double,1>& x)  { return Array<double,1>(sqrt(x)); }
176
177    static inline CArray<double,1> add_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(x + y); }
178    static inline CArray<double,1> minus_ff(const CArray<double,1>& x, const CArray<double,1>& y) { return Array<double,1>(x - y); }
179    static inline CArray<double,1> mult_ff(const CArray<double,1>& x, const CArray<double,1>& y)  { return Array<double,1>(x * y); }
180    static inline CArray<double,1> div_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(x / y); }
181    static inline CArray<double,1> pow_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(pow(x,y)); }
182    static inline CArray<double,1> eq_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x == y); }
183    static inline CArray<double,1> lt_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x < y); }
184    static inline CArray<double,1> gt_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x > y); }
185    static inline CArray<double,1> le_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x <= y); }
186    static inline CArray<double,1> ge_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x >= y); }
187    static inline CArray<double,1> ne_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x != y); }
188
189    static inline CArray<double,1> add_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(x + y); }
190    static inline CArray<double,1> minus_fs(const CArray<double,1>& x, double y) { return Array<double,1>(x - y); }
191    static inline CArray<double,1> mult_fs(const CArray<double,1>& x, double y)  { return Array<double,1>(x * y); }
192    static inline CArray<double,1> div_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(x / y); }
193    static inline CArray<double,1> pow_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(pow(x,y)); }
194    static inline CArray<double,1> eq_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x == y); }
195    static inline CArray<double,1> lt_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x < y); }
196    static inline CArray<double,1> gt_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x > y); }
197    static inline CArray<double,1> le_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x <= y); }
198    static inline CArray<double,1> ge_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x >= y); }
199    static inline CArray<double,1> ne_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x != y); }
200
201    static inline CArray<double,1> add_sf(double x, const CArray<double,1>& y)   { return Array<double,1>(x + y); }
202    static inline CArray<double,1> minus_sf(double x, const CArray<double,1>& y) { return Array<double,1>(x - y); }
203    static inline CArray<double,1> mult_sf(double x, const CArray<double,1>& y)  { return Array<double,1>(x * y); }
204    static inline CArray<double,1> div_sf(double x, const CArray<double,1>& y)   { return Array<double,1>(x / y); }
205    static inline CArray<double,1> eq_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x == y); }
206    static inline CArray<double,1> lt_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x < y); }
207    static inline CArray<double,1> gt_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x > y); }
208    static inline CArray<double,1> le_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x <= y); }
209    static inline CArray<double,1> ge_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x >= y); }
210    static inline CArray<double,1> ne_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x != y); }
211  };
212
213  extern COperatorExpr operatorExpr;
214}
215
216#endif
217
Note: See TracBrowser for help on using the repository browser.