Crevice  0.1
 All Classes Functions Variables Pages
TermProduct.cc
1 /*
2  * File: TermProduct.cc
3  *
4  * Copyright 2012 Heinrich Schuchardt <xypron.glpk@gmx.de>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "Problem.h"
20 #include "TermConstant.h"
21 #include "TermProduct.h"
22 #include "TermSum.h"
23 #include "TermVariable.h"
24 #include <sstream>
25 #include <typeinfo>
26 
34  Term* factor1, Term* factor2) :
35 TermBinary::TermBinary(problem) {
36  if (factor1->less(factor2)) {
37  this->factor1_ = factor1;
38  this->factor2_ = factor2;
39  } else {
40  this->factor1_ = factor2;
41  this->factor2_ = factor1;
42  }
43 }
44 
49 }
50 
59  Term* factor1, Term* factor2) {
60  double value1;
61 
62  if (typeid (*factor1) == typeid (TermConstant)) {
63  value1 = (static_cast<TermConstant *> (factor1))->evaluate();
64  if (value1 == 0.) {
65  return factor1;
66  }
67  if (value1 == 1.) {
68  return factor2;
69  }
70  }
71  if (typeid (*factor2) == typeid (TermConstant)) {
72  double value2;
73  value2 = (static_cast<TermConstant *> (factor2))->evaluate();
74  if (value2 == 0.) {
75  return factor2;
76  }
77  if (value2 == 1.) {
78  return factor1;
79  }
80  if (typeid (*factor1) == typeid (TermConstant)) {
81  return TermConstant::create(problem,
82  value1 * value2);
83  }
84  }
85 
86  TermProduct* term = new TermProduct(problem, factor1, factor2);
87  term = static_cast<TermProduct*> (problem->addTerm(term));
88  return term;
89 }
90 
93  Problem* problem = this->getProblem();
94  if (problem != variable->getProblem()) {
95  ProblemMismatchException problem_mismatch_exception;
96  throw problem_mismatch_exception;
97  }
98  return TermSum::create(problem,
99  TermProduct::create(problem,
100  this->factor1_->differentiate(variable),
101  this->factor2_),
102  TermProduct::create(problem,
103  this->factor1_,
104  this->factor2_->differentiate(variable)));
105 
106 }
107 
108 double TermProduct::eval(const DoubleVector* variableValues,
109  TermValueSet* termValues) {
110  return this->factor1_->evaluate(variableValues, termValues) *
111  this->factor2_->evaluate(variableValues, termValues);
112 }
113 
114 bool TermProduct::less(const Term* term) const {
115  const TermProduct* term_product;
116  if (this->Term::less(term)) {
117  return true;
118  } else if (term->Term::less(this)) {
119  return false;
120  }
121  term_product = static_cast<const TermProduct *> (term);
122  if (this->factor1_ < term_product->factor1_) {
123  return true;
124  } else if (term_product->factor1_ < this->factor1_) {
125  return false;
126  }
127  if (this->factor2_ < term_product->factor2_) {
128  return true;
129  } else {
130  return false;
131  }
132 }
133 
134 std::string TermProduct::toString() const {
135  std::ostringstream os;
136  os << "(" << this->factor1_ << " * " << this->factor2_ << ")";
137  return os.str();
138 }