00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef EMBEDED_FUNCTIONS_HPP
00021 #define EMBEDED_FUNCTIONS_HPP
00022
00023 #include <Types.hpp>
00024
00025 #include <StreamCenter.hpp>
00026
00027 #include <cmath>
00028
00029 real_t unaryMinus(real_t x);
00030
00031 bool not_(bool a);
00032 bool and_(bool a, bool b);
00033 bool or_ (bool a, bool b);
00034 bool xor_(bool a, bool b);
00035
00036
00037
00038 real_t sum(real_t x, real_t y);
00039 real_t difference(real_t x, real_t y);
00040 real_t division(real_t x, real_t y);
00041 real_t product(real_t x, real_t y);
00042
00043 real_t modulo(real_t x, real_t y);
00044
00045
00046 bool gt(real_t x, real_t y);
00047 bool lt(real_t x, real_t y);
00048 bool ge(real_t x, real_t y);
00049 bool le(real_t x, real_t y);
00050 bool eq(real_t x, real_t y);
00051 bool ne(real_t x, real_t y);
00052
00053
00054 inline real_t function_x(real_t x, real_t y, real_t z)
00055 {
00056 return x;
00057 }
00058
00059 inline real_t function_y(real_t x, real_t y, real_t z)
00060 {
00061 return y;
00062 }
00063
00064 inline real_t function_z(real_t x, real_t y, real_t z)
00065 {
00066 return z;
00067 }
00068
00077 template <typename B,
00078 typename TypeExpression>
00079 class ExpressionBinaryOperator
00080 {
00081 public:
00082 friend std::ostream& operator << (std::ostream& os,
00083 const ExpressionBinaryOperator<B, TypeExpression>& bo)
00084 {
00085 bo.put(os);
00086 return os;
00087 }
00088
00089 B& theBinaryOperator()
00090 {
00091 return static_cast<B&>(*this);
00092 }
00093
00094 const B& theBinaryOperator() const
00095 {
00096 return static_cast<const B&>(*this);
00097 }
00098
00099 std::ostream& put(std::ostream& os) const
00100 {
00101 theBinaryOperator().put(os);
00102 return os;
00103 }
00104
00105 const TypeExpression& operator()(const TypeExpression& a, const TypeExpression& b) const
00106 {
00107 return theBinaryOperator().apply(a,b);
00108 }
00109
00110 real_t operator()(real_t a, real_t b) const
00111 {
00112 return theBinaryOperator().apply(a,b);
00113 }
00114 };
00115
00123 template <typename TypeExpression>
00124 class ExpressionPlus
00125 : public ExpressionBinaryOperator<ExpressionPlus<TypeExpression>,
00126 TypeExpression>
00127 {
00128 public:
00129 std::ostream& put(std::ostream& os) const
00130 {
00131 os << '+';
00132 return os;
00133 }
00134
00135 const TypeExpression& apply(const TypeExpression& a, const TypeExpression& b) const
00136 {
00137 return a+b;
00138 }
00139
00140 real_t apply(real_t a, real_t b) const
00141 {
00142 return a+b;
00143 }
00144 };
00145
00153 template <typename TypeExpression>
00154 class ExpressionMinus
00155 : public ExpressionBinaryOperator<ExpressionMinus<TypeExpression>,
00156 TypeExpression>
00157 {
00158 public:
00159 std::ostream& put(std::ostream& os) const
00160 {
00161 os << '-';
00162 return os;
00163 }
00164
00165 const TypeExpression& apply(const TypeExpression& a, const TypeExpression& b) const
00166 {
00167 return a-b;
00168 }
00169
00170 real_t apply(real_t a, real_t b) const
00171 {
00172 return a-b;
00173 }
00174 };
00175
00183 template <typename TypeExpression>
00184 class ExpressionMultiplies
00185 : public ExpressionBinaryOperator<ExpressionMultiplies<TypeExpression>,
00186 TypeExpression>
00187 {
00188 public:
00189 std::ostream& put(std::ostream& os) const
00190 {
00191 os << '*';
00192 return os;
00193 }
00194
00195 const TypeExpression& apply(const TypeExpression& a, const TypeExpression& b) const
00196 {
00197 return a*b;
00198 }
00199
00200 real_t apply(real_t a, real_t b) const
00201 {
00202 return a*b;
00203 }
00204 };
00205
00213 template <typename TypeExpression>
00214 class ExpressionDivides
00215 : public ExpressionBinaryOperator<ExpressionDivides<TypeExpression>,
00216 TypeExpression>
00217 {
00218 public:
00219 std::ostream& put(std::ostream& os) const
00220 {
00221 os << '/';
00222 return os;
00223 }
00224
00225 const TypeExpression& apply(const TypeExpression& a, const TypeExpression& b) const
00226 {
00227 return a/b;
00228 }
00229
00230 real_t apply(real_t a, real_t b) const
00231 {
00232 return a/b;
00233 }
00234 };
00235
00243 template <typename TypeExpression>
00244 class ExpressionPower
00245 : public ExpressionBinaryOperator<ExpressionPower<TypeExpression>,
00246 TypeExpression>
00247 {
00248 public:
00249 std::ostream& put(std::ostream& os) const
00250 {
00251 os << '^';
00252 return os;
00253 }
00254
00255 const TypeExpression& apply(const TypeExpression& a, const TypeExpression& b) const
00256 {
00257 return std::pow(a,b);
00258 }
00259
00260 real_t apply(real_t a, real_t b) const
00261 {
00262 return std::pow(a,b);
00263 }
00264 };
00265
00266
00267
00277 template <typename U, typename TypeExpression>
00278 class ExpressionUnaryOperator
00279 {
00280 public:
00281 friend std::ostream& operator << (std::ostream& os,
00282 const ExpressionUnaryOperator<U, TypeExpression>& uo)
00283 {
00284 uo.put(os);
00285 return os;
00286 }
00287
00288 U& theUnaryOperator()
00289 {
00290 return static_cast<U&>(*this);
00291 }
00292
00293 const U& theUnaryOperator() const
00294 {
00295 return static_cast<const U&>(*this);
00296 }
00297
00298 std::ostream& put(std::ostream& os) const
00299 {
00300 theUnaryOperator().put(os);
00301 return os;
00302 }
00303
00304 const TypeExpression& operator()(const TypeExpression& a) const
00305 {
00306 return theUnaryOperator().apply(a);
00307 }
00308
00309 real_t operator()(real_t a) const
00310 {
00311 return theUnaryOperator().apply(a);
00312 }
00313 };
00314
00315
00323 template <typename TypeExpression>
00324 class ExpressionUnaryMinus
00325 : public ExpressionUnaryOperator<ExpressionUnaryMinus<TypeExpression>,
00326 TypeExpression>
00327 {
00328 public:
00329 std::ostream& put(std::ostream& os) const
00330 {
00331 os << '-';
00332 return os;
00333 }
00334
00335 const TypeExpression& apply(const TypeExpression& a) const
00336 {
00337 return -a;
00338 }
00339
00340 real_t apply(real_t a) const
00341 {
00342 return -a;
00343 }
00344 };
00345
00349 template <real_t (*F)(real_t x)>
00350 std::ostream& functionName(std::ostream& os)
00351 {
00352 os << __FILE__ << ':' << __LINE__ << ": Not implemented\n";
00353 return os;
00354 }
00355
00356 template<>
00357 std::ostream& functionName<std::log>(std::ostream& os);
00358
00359 template<>
00360 std::ostream& functionName<std::sin>(std::ostream& os);
00361
00362 template<>
00363 std::ostream& functionName<std::cos>(std::ostream& os);
00364
00365 template<>
00366 std::ostream& functionName<std::tan>(std::ostream& os);
00367
00368 template<>
00369 std::ostream& functionName<std::asin>(std::ostream& os);
00370
00371 template<>
00372 std::ostream& functionName<std::acos>(std::ostream& os);
00373
00374 template<>
00375 std::ostream& functionName<std::atan>(std::ostream& os);
00376
00377 template<>
00378 std::ostream& functionName<std::exp>(std::ostream& os);
00379
00380 template<>
00381 std::ostream& functionName<std::abs>(std::ostream& os);
00382
00383 template<>
00384 std::ostream& functionName<std::sqrt>(std::ostream& os);
00385
00389 template <bool (*F)(real_t x, real_t y)>
00390 std::ostream& operatorName(std::ostream& os)
00391 {
00392 os << __FILE__ << ':' << __LINE__ << ": Not implemented\n";
00393 return os;
00394 }
00395
00396 template <>
00397 std::ostream& operatorName<gt>(std::ostream& os);
00398
00399 template <>
00400 std::ostream& operatorName<lt>(std::ostream& os);
00401
00402 template <>
00403 std::ostream& operatorName<ge>(std::ostream& os);
00404
00405 template <>
00406 std::ostream& operatorName<le>(std::ostream& os);
00407
00408 template <>
00409 std::ostream& operatorName<eq>(std::ostream& os);
00410
00411 template <>
00412 std::ostream& operatorName<ne>(std::ostream& os);
00413
00417 template <bool (*F)(bool x, bool y)>
00418 std::ostream& binaryBooleanName(std::ostream& os)
00419 {
00420 os << __FILE__ << ':' << __LINE__ << ": Not implemented\n";
00421 return os;
00422 }
00423
00424 template <>
00425 std::ostream& binaryBooleanName<and_>(std::ostream& os);
00426
00427 template <>
00428 std::ostream& binaryBooleanName<or_>(std::ostream& os);
00429
00430 template <>
00431 std::ostream& binaryBooleanName<xor_>(std::ostream& os);
00432
00436 template <bool (*F)(bool x)>
00437 std::ostream& unaryBooleanName(std::ostream& os)
00438 {
00439 os << __FILE__ << ':' << __LINE__ << ": Not implemented\n";
00440 return os;
00441 }
00442
00443 template <>
00444 std::ostream& unaryBooleanName<not_>(std::ostream& os);
00445
00449 template <typename TypeExpression,
00450 real_t (*F)(real_t x)>
00451 class ExpressionStdFunction
00452 : public ExpressionUnaryOperator<ExpressionStdFunction<TypeExpression, F>,
00453 TypeExpression>
00454 {
00455 public:
00456 std::ostream& put(std::ostream& os) const
00457 {
00458 return functionName<F>(os);
00459 }
00460
00461 const TypeExpression& apply(const TypeExpression& a) const
00462 {
00463 return F(a);
00464 }
00465
00466 real_t apply(real_t a) const
00467 {
00468 return F(a);
00469 }
00470 };
00471
00472 #endif // EMBEDED_FUNCTIONS_HPP