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

Last change on this file since 1038 was 1038, checked in by ymipsl, 7 years ago
  • Add generic ternary arithmetic operators and filters
  • Add conditional operator "(cond) ? x : y" for arithmetic filters.

YM

  • 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: 18.2 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 double (*functionScalarScalarScalar)(double, double,double);
20    typedef CArray<double,1> (*functionField)(const CArray<double,1>&);
21    typedef CArray<double,1> (*functionFieldField)(const CArray<double,1>&, const CArray<double,1>&);
22    typedef CArray<double,1> (*functionFieldScalar)(const CArray<double,1>&, double);
23    typedef CArray<double,1> (*functionScalarField)(double, const CArray<double,1>&);
24    typedef CArray<double,1> (*functionScalarScalarField)(double, double, const CArray<double,1>&);
25    typedef CArray<double,1> (*functionScalarFieldScalar)(double, const CArray<double,1>&, double);
26    typedef CArray<double,1> (*functionScalarFieldField)(double, const CArray<double,1>&, const CArray<double,1>&);
27    typedef CArray<double,1> (*functionFieldScalarScalar)(const CArray<double,1>&, double, double);
28    typedef CArray<double,1> (*functionFieldScalarField)(const CArray<double,1>&, double, const CArray<double,1>&);
29    typedef CArray<double,1> (*functionFieldFieldScalar)(const CArray<double,1>&,  const CArray<double,1>&, double);
30    typedef CArray<double,1> (*functionFieldFieldField)(const CArray<double,1>&,  const CArray<double,1>&, const CArray<double,1>&);
31   
32    COperatorExpr(void)
33    {
34      opScalar[string("neg")] = neg_s;
35      opScalar[string("cos")] = cos_s;
36      opScalar[string("sin")] = sin_s;
37      opScalar[string("tan")] = tan_s;
38      opScalar[string("exp")] = exp_s;
39      opScalar[string("log")] = log_s;
40      opScalar[string("log10")] = log10_s;
41      opScalar[string("sqrt")] = sqrt_s;
42
43      opScalarScalar[string("add")] = add_ss;
44      opScalarScalar[string("minus")] = minus_ss;
45      opScalarScalar[string("mult")] = mult_ss;
46      opScalarScalar[string("div")] = div_ss;
47      opScalarScalar[string("pow")] = pow_ss;
48      opScalarScalar[string("eq")] = eq_ss;
49      opScalarScalar[string("lt")] = lt_ss;
50      opScalarScalar[string("gt")] = gt_ss;
51      opScalarScalar[string("le")] = le_ss;
52      opScalarScalar[string("ge")] = ge_ss;
53      opScalarScalar[string("ne")] = ne_ss;
54      opScalarScalarScalar[string("cond")] = cond_sss;
55     
56      opField[string("neg")] = neg_f;
57      opField[string("cos")] = cos_f;
58      opField[string("sin")] = sin_f;
59      opField[string("tan")] = tan_f;
60      opField[string("exp")] = exp_f;
61      opField[string("log")] = log_f;
62      opField[string("log10")] = log10_f;
63      opField[string("sqrt")] = sqrt_f;
64
65      opFieldField[string("add")] = add_ff;
66      opFieldField[string("minus")] = minus_ff;
67      opFieldField[string("mult")] = mult_ff;
68      opFieldField[string("div")] = div_ff;
69      opFieldField[string("pow")] = pow_ff;
70      opFieldField[string("eq")] = eq_ff;
71      opFieldField[string("lt")] = lt_ff;
72      opFieldField[string("gt")] = gt_ff;
73      opFieldField[string("le")] = le_ff;
74      opFieldField[string("ge")] = ge_ff;
75      opFieldField[string("ne")] = ne_ff;
76
77      opFieldScalar[string("add")] = add_fs;
78      opFieldScalar[string("minus")] = minus_fs;
79      opFieldScalar[string("mult")] = mult_fs;
80      opFieldScalar[string("div")] = div_fs;
81      opFieldScalar[string("pow")] = pow_fs;
82      opFieldScalar[string("eq")] = eq_fs;
83      opFieldScalar[string("lt")] = lt_fs;
84      opFieldScalar[string("gt")] = gt_fs;
85      opFieldScalar[string("le")] = le_fs;
86      opFieldScalar[string("ge")] = ge_fs;
87      opFieldScalar[string("ne")] = ne_fs;
88
89      opScalarField[string("add")] = add_sf;
90      opScalarField[string("minus")] = minus_sf;
91      opScalarField[string("mult")] = mult_sf;
92      opScalarField[string("div")] = div_sf;
93      opScalarField[string("eq")] = eq_sf;
94      opScalarField[string("lt")] = lt_sf;
95      opScalarField[string("gt")] = gt_sf;
96      opScalarField[string("le")] = le_sf;
97      opScalarField[string("ge")] = ge_sf;
98      opScalarField[string("ne")] = ne_sf;
99
100      opScalarScalarField[string("cond")] = cond_ssf;
101      opScalarFieldScalar[string("cond")] = cond_sfs;
102      opScalarFieldField[string("cond")] = cond_sff;
103      opFieldScalarScalar[string("cond")] = cond_fss;
104      opFieldScalarField[string("cond")] = cond_fsf;
105      opFieldFieldScalar[string("cond")] = cond_ffs;
106      opFieldFieldField[string("cond")] = cond_fff;
107
108
109    }
110
111    functionScalar getOpScalar(const string& id)
112    {
113      map<string,double (*)(double)>::iterator it;
114      it = opScalar.find(id);
115      if (it == opScalar.end()) ERROR("functionScalar COperatorExpr::getOpScalar(const string& id)", << "unknown operator : " << id)
116      return it->second;
117    }
118
119    functionScalarScalar getOpScalarScalar(const string& id)
120    {
121      map<string,double (*)(double,double)>::iterator it;
122      it = opScalarScalar.find(id);
123      if (it == opScalarScalar.end()) ERROR("functionScalarScalar COperatorExpr::getOpScalarScalar(const string& id)", << "unknown operator : " << id)
124      return it->second;
125    }
126
127    functionScalarScalarScalar getOpScalarScalarScalar(const string& id)
128    {
129      map<string,double (*)(double,double,double)>::iterator it;
130      it = opScalarScalarScalar.find(id);
131      if (it == opScalarScalarScalar.end()) ERROR("functionScalarScalarScalar getOpScalarScalarScalar(const string& id)", << "unknown operator : " << id)
132      return it->second;
133    }
134
135    functionField getOpField(const string& id)
136    {
137      map<string,functionField>::iterator it;
138      it = opField.find(id);
139      if (it == opField.end()) ERROR("functionField COperatorExpr::getOpField(const string& id)", << "unknown operator : " << id)
140      return it->second;
141    }
142
143    functionFieldField getOpFieldField(const string& id)
144    {
145      map<string,functionFieldField>::iterator it;
146      it = opFieldField.find(id);
147      if (it == opFieldField.end()) ERROR("dfunctionFieldField COperatorExpr::getOpFieldField(const string& id)", << "unknown operator : " << id)
148      return it->second;
149    }
150
151    functionFieldScalar getOpFieldScalar(const string& id)
152    {
153      map<string,functionFieldScalar>::iterator it;
154      it = opFieldScalar.find(id);
155      if (it == opFieldScalar.end()) ERROR("functionFieldScalar COperatorExpr::getOpFieldScalar(const string& id)", << "unknown operator : " << id)
156      return it->second;
157    }
158
159    functionScalarField getOpScalarField(const string& id)
160    {
161      map<string,functionScalarField>::iterator it;
162      it = opScalarField.find(id);
163      if (it == opScalarField.end()) ERROR("functionScalarField COperatorExpr::getOpFieldField(const string& id)", << "unknown operator : " << id)
164      return it->second;
165    }
166
167    functionScalarScalarField getOpScalarScalarField(const string& id)
168    {
169      map<string,functionScalarScalarField>::iterator it;
170      it = opScalarScalarField.find(id);
171      if (it == opScalarScalarField.end()) ERROR("functionScalarScalarField getOpScalarScalarField(const string& id)", << "unknown operator : " << id)
172      return it->second;
173    }
174
175    functionScalarFieldScalar getOpScalarFieldScalar(const string& id)
176    {
177      map<string,functionScalarFieldScalar>::iterator it;
178      it = opScalarFieldScalar.find(id);
179      if (it == opScalarFieldScalar.end()) ERROR("functionScalarFieldScalar getOpScalarScalarField(const string& id)", << "unknown operator : " << id)
180      return it->second;
181    }
182
183    functionScalarFieldField getOpScalarFieldField(const string& id)
184    {
185      map<string,functionScalarFieldField>::iterator it;
186      it = opScalarFieldField.find(id);
187      if (it == opScalarFieldField.end()) ERROR("functionScalarFieldField getOpScalarFieldField(const string& id)", << "unknown operator : " << id)
188      return it->second;
189    }
190
191    functionFieldScalarScalar getOpFieldScalarScalar(const string& id)
192    {
193      map<string,functionFieldScalarScalar>::iterator it;
194      it = opFieldScalarScalar.find(id);
195      if (it == opFieldScalarScalar.end()) ERROR("functionFieldScalarScalar getOpFieldScalarScalar(const string& id)", << "unknown operator : " << id)
196      return it->second;
197    }
198
199    functionFieldScalarField getOpFieldScalarField(const string& id)
200    {
201      map<string,functionFieldScalarField>::iterator it;
202      it = opFieldScalarField.find(id);
203      if (it == opFieldScalarField.end()) ERROR("functionFieldScalarField getOpFieldScalarField(const string& id)", << "unknown operator : " << id)
204      return it->second;
205    }
206
207    functionFieldFieldScalar getOpFieldFieldScalar(const string& id)
208    {
209      map<string,functionFieldFieldScalar>::iterator it;
210      it = opFieldFieldScalar.find(id);
211      if (it == opFieldFieldScalar.end()) ERROR("functionFieldFieldScalar getOpFieldFieldScalar(const string& id)", << "unknown operator : " << id)
212      return it->second;
213    }
214
215    functionFieldFieldField getOpFieldFieldField(const string& id)
216    {
217      map<string,functionFieldFieldField>::iterator it;
218      it = opFieldFieldField.find(id);
219      if (it == opFieldFieldField.end()) ERROR("functionFieldFieldField getOpFieldFieldField(const string& id)", << "unknown operator : " << id)
220      return it->second;
221    }
222   
223   
224    map<string,functionScalar> opScalar;
225    map<string,functionScalarScalar> opScalarScalar;
226    map<string,functionScalarScalarScalar> opScalarScalarScalar;
227    map<string,functionField> opField;
228    map<string,functionFieldField> opFieldField;
229    map<string,functionFieldScalar> opFieldScalar;
230    map<string,functionScalarField> opScalarField;
231    map<string,functionScalarScalarField> opScalarScalarField;
232    map<string,functionScalarFieldScalar> opScalarFieldScalar;
233    map<string,functionScalarFieldField> opScalarFieldField;
234    map<string,functionFieldScalarScalar> opFieldScalarScalar;
235    map<string,functionFieldScalarField> opFieldScalarField;
236    map<string,functionFieldFieldScalar> opFieldFieldScalar;
237    map<string,functionFieldFieldField> opFieldFieldField;
238
239
240
241    static inline double neg_s(double x)   { return -x; }
242    static inline double cos_s(double x)   { return std::cos(x); }
243    static inline double sin_s(double x)   { return std::sin(x); }
244    static inline double tan_s(double x)   { return std::tan(x); }
245    static inline double exp_s(double x)   { return std::exp(x); }
246    static inline double log_s(double x)   { return std::log(x); }
247    static inline double log10_s(double x) { return std::log10(x); }
248    static inline double sqrt_s(double x)  { return std::sqrt(x); }
249
250    static inline double add_ss(double x, double y)   { return x + y; }
251    static inline double minus_ss(double x, double y) { return x - y; }
252    static inline double mult_ss(double x, double y)  { return x * y; }
253    static inline double div_ss(double x, double y)   { return x / y; }
254    static inline double pow_ss(double x, double y)   { return std::pow(x,y); }
255    static inline double eq_ss(double x, double y)    { return x == y; }
256    static inline double lt_ss(double x, double y)    { return x < y; }
257    static inline double gt_ss(double x, double y)    { return x > y; }
258    static inline double le_ss(double x, double y)    { return x <= y; }
259    static inline double ge_ss(double x, double y)    { return x >= y; }
260    static inline double ne_ss(double x, double y)    { return x != y; }
261
262    static inline CArray<double,1> neg_f(const CArray<double,1>& x)   { return Array<double,1>(-x); }
263    static inline CArray<double,1> cos_f(const CArray<double,1>& x)   { return Array<double,1>(cos(x)); }
264    static inline CArray<double,1> sin_f(const CArray<double,1>& x)   { return Array<double,1>(sin(x)); }
265    static inline CArray<double,1> tan_f(const CArray<double,1>& x)   { return Array<double,1>(tan(x)); }
266    static inline CArray<double,1> exp_f(const CArray<double,1>& x)   { return Array<double,1>(exp(x)); }
267    static inline CArray<double,1> log_f(const CArray<double,1>& x)   { return Array<double,1>(log(x)); }
268    static inline CArray<double,1> log10_f(const CArray<double,1>& x) { return Array<double,1>(log10(x)); }
269    static inline CArray<double,1> sqrt_f(const CArray<double,1>& x)  { return Array<double,1>(sqrt(x)); }
270
271    static inline CArray<double,1> add_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(x + y); }
272    static inline CArray<double,1> minus_ff(const CArray<double,1>& x, const CArray<double,1>& y) { return Array<double,1>(x - y); }
273    static inline CArray<double,1> mult_ff(const CArray<double,1>& x, const CArray<double,1>& y)  { return Array<double,1>(x * y); }
274    static inline CArray<double,1> div_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(x / y); }
275    static inline CArray<double,1> pow_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(pow(x,y)); }
276    static inline CArray<double,1> eq_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x == y); }
277    static inline CArray<double,1> lt_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x < y); }
278    static inline CArray<double,1> gt_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x > y); }
279    static inline CArray<double,1> le_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x <= y); }
280    static inline CArray<double,1> ge_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x >= y); }
281    static inline CArray<double,1> ne_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x != y); }
282
283    static inline CArray<double,1> add_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(x + y); }
284    static inline CArray<double,1> minus_fs(const CArray<double,1>& x, double y) { return Array<double,1>(x - y); }
285    static inline CArray<double,1> mult_fs(const CArray<double,1>& x, double y)  { return Array<double,1>(x * y); }
286    static inline CArray<double,1> div_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(x / y); }
287    static inline CArray<double,1> pow_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(pow(x,y)); }
288    static inline CArray<double,1> eq_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x == y); }
289    static inline CArray<double,1> lt_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x < y); }
290    static inline CArray<double,1> gt_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x > y); }
291    static inline CArray<double,1> le_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x <= y); }
292    static inline CArray<double,1> ge_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x >= y); }
293    static inline CArray<double,1> ne_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x != y); }
294
295    static inline CArray<double,1> add_sf(double x, const CArray<double,1>& y)   { return Array<double,1>(x + y); }
296    static inline CArray<double,1> minus_sf(double x, const CArray<double,1>& y) { return Array<double,1>(x - y); }
297    static inline CArray<double,1> mult_sf(double x, const CArray<double,1>& y)  { return Array<double,1>(x * y); }
298    static inline CArray<double,1> div_sf(double x, const CArray<double,1>& y)   { return Array<double,1>(x / y); }
299    static inline CArray<double,1> eq_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x == y); }
300    static inline CArray<double,1> lt_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x < y); }
301    static inline CArray<double,1> gt_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x > y); }
302    static inline CArray<double,1> le_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x <= y); }
303    static inline CArray<double,1> ge_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x >= y); }
304    static inline CArray<double,1> ne_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x != y); }
305
306
307    static inline double cond_sss(double x, double y, double z)   { return (x==0) ? z : y ; }
308
309    static inline CArray<double,1> cond_ssf(double x, double y, const CArray<double,1>& z)
310    {
311      CArray<double,1> ret(z.numElements()) ;
312      if (x==0) ret=z ;
313      else ret=y ;
314      return ret ;
315    }
316
317    static inline CArray<double,1> cond_sfs(double x, const CArray<double,1>& y, double z)
318    {
319      CArray<double,1> ret(y.numElements()) ;
320     if (x==0) ret=z ;
321      else ret=y ;
322      return ret ;
323    }
324
325    static inline CArray<double,1> cond_sff(double x, const CArray<double,1>& y, const CArray<double,1>& z)
326    {
327      CArray<double,1> ret(y.numElements()) ;
328      if (x==0) ret=z ;
329      else ret=y ;
330      return ret ;
331    }
332   
333    static inline CArray<double,1> cond_fss(const CArray<double,1>& x, double y, double z)
334    {
335      CArray<double,1> ret(x.numElements()) ;
336      Array<double,1>::const_iterator itx=x.begin(),itxe=x.end();
337      Array<double,1>::iterator itret=ret.begin() ;
338     
339      for(;itx!=itxe;++itx,++itret) *itret=( (*itx==0)?z:y) ;
340      return ret ;
341    }
342
343    static inline CArray<double,1> cond_fsf(const CArray<double,1>& x, double y, const CArray<double,1>& z)
344    {
345      CArray<double,1> ret(x.numElements()) ;
346      Array<double,1>::const_iterator itx=x.begin(), itxe=x.end(), itz=z.begin();
347      Array<double,1>::iterator itret=ret.begin() ;
348      for(;itx!=itxe;++itx,++itz,++itret) *itret=( (*itx==0)?*itz:y) ;
349      return ret ;
350    }
351
352    static inline CArray<double,1> cond_ffs(const CArray<double,1>& x, const CArray<double,1>& y, double z)
353    {
354      CArray<double,1> ret(x.numElements()) ;
355      Array<double,1>::const_iterator itx=x.begin(), itxe=x.end(), ity=y.begin() ;
356      Array<double,1>::iterator itret=ret.begin() ;
357      for(;itx!=itxe;++itx,++ity,++itret) *itret=( (*itx==0)?z:*ity) ;
358      return ret ;
359    }
360
361    static inline CArray<double,1> cond_fff(const CArray<double,1>& x, const CArray<double,1>& y, const CArray<double,1>&  z)
362    {
363      CArray<double,1> ret(x.numElements()) ;
364      Array<double,1>::const_iterator itx=x.begin(), itxe=x.end(), ity=y.begin(), itz=z.begin() ;
365      Array<double,1>::iterator itret=ret.begin() ;
366      for(;itx!=itxe;++itx,++ity,++itz,++itret) *itret=( (*itx==0)?*itz:*ity) ;
367      return ret ;
368    }
369
370  };
371
372  extern COperatorExpr operatorExpr;
373}
374
375#endif
376
Note: See TracBrowser for help on using the repository browser.