source: XIOS/dev/branch_yushan/src/parse_expr/operator_expr.hpp @ 1037

Last change on this file since 1037 was 1037, checked in by yushan, 7 years ago

initialize the branch

  • 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: 10.8 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
46      opField[string("neg")] = neg_f;
47      opField[string("cos")] = cos_f;
48      opField[string("sin")] = sin_f;
49      opField[string("tan")] = tan_f;
50      opField[string("exp")] = exp_f;
51      opField[string("log")] = log_f;
52      opField[string("log10")] = log10_f;
53      opField[string("sqrt")] = sqrt_f;
54
55      opFieldField[string("add")] = add_ff;
56      opFieldField[string("minus")] = minus_ff;
57      opFieldField[string("mult")] = mult_ff;
58      opFieldField[string("div")] = div_ff;
59      opFieldField[string("pow")] = pow_ff;
60      opFieldField[string("eq")] = eq_ff;
61      opFieldField[string("lt")] = lt_ff;
62      opFieldField[string("gt")] = gt_ff;
63      opFieldField[string("le")] = le_ff;
64      opFieldField[string("ge")] = ge_ff;
65
66      opFieldScalar[string("add")] = add_fs;
67      opFieldScalar[string("minus")] = minus_fs;
68      opFieldScalar[string("mult")] = mult_fs;
69      opFieldScalar[string("div")] = div_fs;
70      opFieldScalar[string("pow")] = pow_fs;
71      opFieldScalar[string("eq")] = eq_fs;
72      opFieldScalar[string("lt")] = lt_fs;
73      opFieldScalar[string("gt")] = gt_fs;
74      opFieldScalar[string("le")] = le_fs;
75      opFieldScalar[string("ge")] = ge_fs;
76
77      opScalarField[string("add")] = add_sf;
78      opScalarField[string("minus")] = minus_sf;
79      opScalarField[string("mult")] = mult_sf;
80      opScalarField[string("div")] = div_sf;
81      opScalarField[string("eq")] = eq_sf;
82      opScalarField[string("lt")] = lt_sf;
83      opScalarField[string("gt")] = gt_sf;
84      opScalarField[string("le")] = le_sf;
85      opScalarField[string("ge")] = ge_sf;
86    }
87
88    functionScalar getOpScalar(const string& id)
89    {
90      map<string,double (*)(double)>::iterator it;
91      it = opScalar.find(id);
92      if (it == opScalar.end()) ERROR("functionScalar COperatorExpr::getOpScalar(const string& id)", << "unknown operator : " << id)
93      return it->second;
94    }
95
96    functionScalarScalar getOpScalarScalar(const string& id)
97    {
98      map<string,double (*)(double,double)>::iterator it;
99      it = opScalarScalar.find(id);
100      if (it == opScalarScalar.end()) ERROR("functionScalarScalar COperatorExpr::getOpScalarScalar(const string& id)", << "unknown operator : " << id)
101      return it->second;
102    }
103
104    functionField getOpField(const string& id)
105    {
106      map<string,functionField>::iterator it;
107      it = opField.find(id);
108      if (it == opField.end()) ERROR("functionField COperatorExpr::getOpField(const string& id)", << "unknown operator : " << id)
109      return it->second;
110    }
111
112    functionFieldField getOpFieldField(const string& id)
113    {
114      map<string,functionFieldField>::iterator it;
115      it = opFieldField.find(id);
116      if (it == opFieldField.end()) ERROR("dfunctionFieldField COperatorExpr::getOpFieldField(const string& id)", << "unknown operator : " << id)
117      return it->second;
118    }
119
120    functionFieldScalar getOpFieldScalar(const string& id)
121    {
122      map<string,functionFieldScalar>::iterator it;
123      it = opFieldScalar.find(id);
124      if (it == opFieldScalar.end()) ERROR("functionFieldScalar COperatorExpr::getOpFieldScalar(const string& id)", << "unknown operator : " << id)
125      return it->second;
126    }
127
128    functionScalarField getOpScalarField(const string& id)
129    {
130      map<string,functionScalarField>::iterator it;
131      it = opScalarField.find(id);
132      if (it == opScalarField.end()) ERROR("functionScalarField COperatorExpr::getOpFieldField(const string& id)", << "unknown operator : " << id)
133      return it->second;
134    }
135
136    map<string,functionScalar> opScalar;
137    map<string,functionScalarScalar> opScalarScalar;
138    map<string,functionField> opField;
139    map<string,functionFieldField> opFieldField;
140    map<string,functionFieldScalar> opFieldScalar;
141    map<string,functionScalarField> opScalarField;
142
143    static inline double neg_s(double x)   { return -x; }
144    static inline double cos_s(double x)   { return std::cos(x); }
145    static inline double sin_s(double x)   { return std::sin(x); }
146    static inline double tan_s(double x)   { return std::tan(x); }
147    static inline double exp_s(double x)   { return std::exp(x); }
148    static inline double log_s(double x)   { return std::log(x); }
149    static inline double log10_s(double x) { return std::log10(x); }
150    static inline double sqrt_s(double x)  { return std::sqrt(x); }
151
152    static inline double add_ss(double x, double y)   { return x + y; }
153    static inline double minus_ss(double x, double y) { return x - y; }
154    static inline double mult_ss(double x, double y)  { return x * y; }
155    static inline double div_ss(double x, double y)   { return x / y; }
156    static inline double pow_ss(double x, double y)   { return std::pow(x,y); }
157    static inline double eq_ss(double x, double y)    { return x == y; }
158    static inline double lt_ss(double x, double y)    { return x < y; }
159    static inline double gt_ss(double x, double y)    { return x > y; }
160    static inline double le_ss(double x, double y)    { return x <= y; }
161    static inline double ge_ss(double x, double y)    { return x >= y; }
162
163    static inline CArray<double,1> neg_f(const CArray<double,1>& x)   { return Array<double,1>(-x); }
164    static inline CArray<double,1> cos_f(const CArray<double,1>& x)   { return Array<double,1>(cos(x)); }
165    static inline CArray<double,1> sin_f(const CArray<double,1>& x)   { return Array<double,1>(sin(x)); }
166    static inline CArray<double,1> tan_f(const CArray<double,1>& x)   { return Array<double,1>(tan(x)); }
167    static inline CArray<double,1> exp_f(const CArray<double,1>& x)   { return Array<double,1>(exp(x)); }
168    static inline CArray<double,1> log_f(const CArray<double,1>& x)   { return Array<double,1>(log(x)); }
169    static inline CArray<double,1> log10_f(const CArray<double,1>& x) { return Array<double,1>(log10(x)); }
170    static inline CArray<double,1> sqrt_f(const CArray<double,1>& x)  { return Array<double,1>(sqrt(x)); }
171
172    static inline CArray<double,1> add_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(x + y); }
173    static inline CArray<double,1> minus_ff(const CArray<double,1>& x, const CArray<double,1>& y) { return Array<double,1>(x - y); }
174    static inline CArray<double,1> mult_ff(const CArray<double,1>& x, const CArray<double,1>& y)  { return Array<double,1>(x * y); }
175    static inline CArray<double,1> div_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(x / y); }
176    static inline CArray<double,1> pow_ff(const CArray<double,1>& x, const CArray<double,1>& y)   { return Array<double,1>(pow(x,y)); }
177    static inline CArray<double,1> eq_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x == y); }
178    static inline CArray<double,1> lt_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x < y); }
179    static inline CArray<double,1> gt_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x > y); }
180    static inline CArray<double,1> le_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x <= y); }
181    static inline CArray<double,1> ge_ff(const CArray<double,1>& x, const CArray<double,1>& y)    { return Array<double,1>(x >= y); }
182
183    static inline CArray<double,1> add_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(x + y); }
184    static inline CArray<double,1> minus_fs(const CArray<double,1>& x, double y) { return Array<double,1>(x - y); }
185    static inline CArray<double,1> mult_fs(const CArray<double,1>& x, double y)  { return Array<double,1>(x * y); }
186    static inline CArray<double,1> div_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(x / y); }
187    static inline CArray<double,1> pow_fs(const CArray<double,1>& x, double y)   { return Array<double,1>(pow(x,y)); }
188    static inline CArray<double,1> eq_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x == y); }
189    static inline CArray<double,1> lt_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x < y); }
190    static inline CArray<double,1> gt_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x > y); }
191    static inline CArray<double,1> le_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x <= y); }
192    static inline CArray<double,1> ge_fs(const CArray<double,1>& x, double y)    { return Array<double,1>(x >= y); }
193
194    static inline CArray<double,1> add_sf(double x, const CArray<double,1>& y)   { return Array<double,1>(x + y); }
195    static inline CArray<double,1> minus_sf(double x, const CArray<double,1>& y) { return Array<double,1>(x - y); }
196    static inline CArray<double,1> mult_sf(double x, const CArray<double,1>& y)  { return Array<double,1>(x * y); }
197    static inline CArray<double,1> div_sf(double x, const CArray<double,1>& y)   { return Array<double,1>(x / y); }
198    static inline CArray<double,1> eq_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x == y); }
199    static inline CArray<double,1> lt_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x < y); }
200    static inline CArray<double,1> gt_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x > y); }
201    static inline CArray<double,1> le_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x <= y); }
202    static inline CArray<double,1> ge_sf(double x, const CArray<double,1>& y)    { return Array<double,1>(x >= y); }
203  };
204
205  extern COperatorExpr operatorExpr;
206}
207
208#endif
209
Note: See TracBrowser for help on using the repository browser.