// ******************** FlopCpp ********************************************** // File: MP_constant.cpp // $Id$ // Author: Tim Helge Hultberg (thh@mat.ua.pt) // Copyright (C) 2003 Tim Helge Hultberg // All Rights Reserved. //**************************************************************************** #include #include #include #include "MP_constant.hpp" #include "MP_data.hpp" #include "MP_domain.hpp" #include "MP_index.hpp" namespace flopc { class Constant_index : public Constant_base { friend class Constant; public: std::string toString()const { std::stringstream ss; ss<<"(index="<toString()<<')'; return ss.str(); } private: Constant_index(const MP_index_exp& i) : I(i) {}; double evaluate() const { return I->evaluate(); } const MP_index_exp I; }; class Constant_double : public Constant_base { friend class Constant; public: std::string toString()const { std::stringstream ss; ss<evaluate()); } Constant C; }; Constant abs(const Constant& c) { return new Constant_abs(c); } class Constant_pos : public Constant_base { friend Constant pos(const Constant& c); std::string toString()const { std::stringstream ss; ss<<" (+)("<evaluate(); if (temp>0) { return temp; } else { return 0.0; } } Constant C; }; Constant pos(const Constant& c) { return new Constant_pos(c); } class Constant_ceil : public Constant_base { friend Constant ceil(const Constant& c); public: std::string toString()const { std::stringstream ss; ss<<" ceil("<evaluate()); } Constant C; }; Constant ceil(const Constant& c) { return new Constant_ceil(c); } class Constant_floor : public Constant_base { friend Constant floor(const Constant& c); public: std::string toString()const { std::stringstream ss; ss<<" floor("<evaluate()); } Constant C; }; Constant floor(const Constant& c) { return new Constant_floor(c); } class Constant_exp : public Constant_base { public: std::string toString()const { std::stringstream ss; ss<<"("<evaluate(),right->evaluate()); } }; Constant minimum(const Constant& a, const Constant& b) { return new Constant_min_2(a,b); } class Constant_max_2 : public Constant_exp { public: std::string toString()const { std::stringstream ss; ss<<"max("<evaluate(),right->evaluate()); } }; Constant maximum(const Constant& a, const Constant& b) { return new Constant_max_2(a,b); } class Constant_plus : public Constant_exp { friend Constant operator+(const Constant& a, const Constant& b); friend Constant operator+(MP_index& a, MP_index& b); public: std::string toString()const { std::stringstream ss; ss<<"("<evaluate()+right->evaluate(); } }; Constant operator+(const Constant& a, const Constant& b) { return new Constant_plus(a,b); } Constant operator+(MP_index& a, MP_index& b) { return new Constant_plus(Constant(a),Constant(b)); } class Constant_minus : public Constant_exp { friend Constant operator-(const Constant& a, const Constant& b); friend Constant operator-(MP_index& a, MP_index& b); public: std::string toString()const { std::stringstream ss; ss<<"("<evaluate()-right->evaluate(); } }; Constant operator-(const Constant& a, const Constant& b) { return new Constant_minus(a,b); } Constant operator-(MP_index& a, MP_index& b) { return new Constant_minus(Constant(a),Constant(b)); } class Constant_mult : public Constant_exp { friend Constant operator*(const Constant& a, const Constant& b); public: std::string toString()const { std::stringstream ss; ss<<"("<evaluate()*right->evaluate(); } }; Constant operator*(const Constant& a, const Constant& b) { return new Constant_mult(a,b); } class Constant_div : public Constant_exp { friend Constant operator/(const Constant& a, const Constant& b); public: std::string toString()const { std::stringstream ss; ss<<"("<evaluate()/right->evaluate(); } }; Constant operator/(const Constant& a, const Constant& b) { return new Constant_div(a,b); } class Constant_max : public Constant_base, public Functor { friend Constant maximum(const MP_domain& i, const Constant& e); public: std::string toString()const { std::stringstream ss; ss<<"(max of "<evaluate(); if (temp > the_max) { the_max = temp; } } double evaluate() const { the_max = DBL_MIN; d.Forall(this); return the_max; } MP_domain d; Constant exp; mutable double the_max; }; class Constant_min : public Constant_base, public Functor { friend Constant minimum(const MP_domain& i, const Constant& e); public: std::string toString()const { std::stringstream ss; ss<<"(min of "<evaluate(); if (temp < the_min) { the_min = temp; } } double evaluate() const { the_min = DBL_MAX; d.Forall(this); return the_min; } MP_domain d; Constant exp; mutable double the_min; }; class Constant_sum : public Constant_base, public Functor { friend Constant sum(const MP_domain& i, const Constant& e); public: std::string toString()const { std::stringstream ss; ss<<"(sum of "<evaluate(); } double evaluate() const { the_sum = 0; d.Forall(this); return the_sum; } MP_domain d; Constant exp; mutable double the_sum; }; class Constant_product : public Constant_base, public Functor { friend Constant product(const MP_domain& i, const Constant& e); public: std::string toString()const { std::stringstream ss; ss<<"(product of "<evaluate(); } double evaluate() const { the_product = 1; d.Forall(this); return the_product; } MP_domain d; Constant exp; mutable double the_product; }; Constant maximum(const MP_domain& i, const Constant& e) { return new Constant_max(i,e); } Constant minimum(const MP_domain& i, const Constant& e) { return new Constant_min(i,e); } Constant sum(const MP_domain& i, const Constant& e) { return new Constant_sum(i,e); } Constant product(const MP_domain& i, const Constant& e) { return new Constant_product(i,e); } Constant::Constant(const DataRef& d) : Handle(const_cast(&d)) {} Constant::Constant(const MP_index_exp& i) : Handle(new Constant_index(i)){} Constant::Constant(double d) : Handle(new Constant_double(d)) {} Constant::Constant(int d) : Handle(new Constant_double(d)) {} std::string Constant::toString()const { return (operator ->())->toString(); } } // End of namespace flopc