Crevice  0.1
 All Classes Functions Variables Pages
TermSum.cc
1 /*
2  * File: TermSum.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 "TermSum.h"
22 #include "TermVariable.h"
23 #include <sstream>
24 #include <typeinfo>
25 
33  Term* summand1, Term* summand2) :
34 TermBinary::TermBinary(problem) {
35  if (summand1->less(summand2)) {
36  this->summand1_ = summand1;
37  this->summand2_ = summand2;
38  } else {
39  this->summand1_ = summand2;
40  this->summand2_ = summand1;
41  }
42 }
43 
48 }
49 
58  Term* summand1, Term* summand2) {
59  TermSum* term;
60 
61  if (typeid (*summand1) == typeid (TermConstant) &&
62  (static_cast<TermConstant *> (summand1))->evaluate() == 0) {
63  return summand2;
64  }
65  if (typeid (*summand2) == typeid (TermConstant) &&
66  (static_cast<TermConstant *> (summand2))->evaluate() == 0) {
67  return summand1;
68  }
69  if (typeid (*summand1) == typeid (TermConstant) &&
70  typeid (*summand2) == typeid (TermConstant)) {
71  return TermConstant::create(problem,
72  (static_cast<TermConstant *> (summand1))->evaluate() +
73  (static_cast<TermConstant *> (summand2))->evaluate());
74  }
75 
76  term = new TermSum(problem, summand1, summand2);
77  term = static_cast<TermSum *> (problem->addTerm(term));
78  return term;
79 }
80 
83  if (this->getProblem() != variable->getProblem()) {
84  ProblemMismatchException problem_mismatch_exception;
85  throw problem_mismatch_exception;
86  }
87  return TermSum::create(this->getProblem(),
88  this->summand1_->differentiate(variable),
89  this->summand2_->differentiate(variable)
90  );
91 }
92 
93 double TermSum::eval(const DoubleVector* variableValues,
94  TermValueSet* termValues) {
95  return this->summand1_->evaluate(variableValues, termValues) +
96  this->summand2_->evaluate(variableValues, termValues);
97 }
98 
99 bool TermSum::less(const Term* term) const {
100  const TermSum* term_product;
101  if (this->Term::less(term)) {
102  return true;
103  } else if (term->Term::less(this)) {
104  return false;
105  }
106  term_product = static_cast<const TermSum *> (term);
107  if (this->summand1_ < term_product->summand1_) {
108  return true;
109  } else if (term_product->summand1_ < this->summand1_) {
110  return false;
111  }
112  if (this->summand2_ < term_product->summand2_) {
113  return true;
114  } else {
115  return false;
116  }
117 }
118 
119 std::string TermSum::toString() const {
120  std::ostringstream os;
121  os << "(" << this->summand1_ << " + " << this->summand2_ << ")";
122  return os.str();
123 }