1 // -*- C++ -*- 2 /** 3 * @brief symbolic expression (base classes and functionality) 4 * 5 * Copyright (C) 2008-2010 Leo Liberti 6 * 7 * This library is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with this library. If not, see <http://www.gnu.org/licenses/>. 19 * 20 */ 21 22 #ifndef __EV3OPERANDH__ 23 #define __EV3OPERANDH__ 24 25 #include <sstream> 26 27 #include "common.h" 28 #include "exceptions.h" 29 30 namespace Ev3 31 { 32 // algebraic expression operand 33 class Operand 34 { 35 36 private: 37 38 protected: 39 40 // one of the OperatorTypes above 41 int oplabel_; 42 43 // 0 if no dependency, 1 if constant, 2 if coefficient, 3 if exponent 44 int dependency_; 45 46 // if oplabel == CONST, the value of the constant 47 double constant_; 48 49 // dependency for constants (added for MORON - see ../PROGNOTES) 50 double* depconstant_; 51 52 // if oplabel == VAR, the index of the variable - should start from 1 53 Int varindex_; 54 55 // if oplabel == VAR, the name of the variable 56 std::string varname_; 57 58 // we allow multiplication for a constant coefficient in each Operand 59 double coefficient_; 60 61 // dependency for coefficients (added for MORON - see ../PROGNOTES) 62 double* depcoefficient_; 63 64 // we allow a real constant exponent in each Operand 65 // THIS HAS MEANING ONLY IF operand IS A LEAF!!! 66 double exponent_; 67 68 // dependency for exponents (added for MORON - see ../PROGNOTES) 69 double* depexponent_; 70 71 public: 72 73 // constructors 74 Operand(); 75 explicit Operand(const double t); 76 explicit Operand(const Int t); 77 Operand(const Int t, 78 const bool isvar); 79 // create a variable leaf and set coefficient 80 Operand(const double c, 81 const Int t, 82 const std::string & vn); 83 84 // Operand class methods: 85 86 // prints to a string 87 std::string ToString() const; 88 89 // get operator type 90 int GetOpType() const; 91 92 // get constant value - in CONSTs it multiplies by coefficient and 93 // raises to exponent 94 double GetValue() const; 95 96 // just get the value, in all cases 97 double GetSimpleValue() const; 98 99 // get variable index 100 Int GetVarIndex() const; 101 102 // get variable name 103 std::string GetVarName() const; 104 105 // get the coefficient 106 double GetCoeff() const; 107 108 // get the exponent 109 double GetExponent() const; 110 111 // set operator type 112 void SetOpType(const int t); 113 114 // set constant value 115 void SetValue(const double t); 116 117 // set variable index (start from 1 and add by steps of 1 when creating new 118 // variables) 119 void SetVarIndex(const Int t); 120 121 // set variable name 122 void SetVarName(const std::string & vn); 123 124 // set the exponent 125 void SetExponent(const double expon); 126 127 // set the coefficient 128 void SetCoeff(const double coeff); 129 130 // set constant dependencies (added for MORON - see ../PROGNOTES) 131 void SetDependencyOnOperand(const int whichconstant, 132 double** depvalue); 133 134 // is operand a constant? 135 bool IsConstant() const; 136 137 // is operand a variable? 138 bool IsVariable() const; 139 140 // is operand a leaf node? 141 bool IsLeaf() const; 142 143 // is operand a zero constant? 144 bool IsZero() const; 145 146 // is operand a constant == v? 147 bool HasValue(double v) const; 148 149 // is operand a constant <= v? 150 bool IsLessThan(double v) const; 151 152 // is operand constant >= v? 153 bool IsGreaterThan(double v) const; 154 155 // set value = coefficient * value ^ exponent 156 void ConsolidateValue(); 157 158 // enforce constant dependencies (added for MORON - see ../PROGNOTES) 159 void EnforceDependencyOnOperand(); 160 161 // is operand this == operand t? 162 bool operator == (const Operand & t); 163 164 // substitute a variable with a constant 165 void SubstituteVariableWithConstant(const int varindex, 166 const double c); 167 168 }; 169 170 std::ostream & operator<< (std::ostream & out, const Operand & operand); 171 172 } /* namespace Ev3 */ 173 174 #endif /* __EV3OPERANDHXX__ */ 175