Crevice  0.1
 All Classes Functions Variables Pages
TermAbs.cc
1 /*
2  * File: TermAbs.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 "TermAbs.h"
21 #include "TermProduct.h"
22 #include "TermSign.h"
23 #include <math.h>
24 #include <sstream>
25 #include <typeinfo>
26 
32 TermAbs::TermAbs(Problem* problem, Term *term) :
33 Term::Term(problem) {
34  term_ = term;
35 }
36 
41 }
42 
49 TermAbs* TermAbs::create(Problem* problem, Term *term) throw
51  TermAbs* ret = new TermAbs(problem, term);
52  ret = static_cast<TermAbs*> (problem->addTerm(ret));
53  return ret;
54 }
55 
58  Term *t1 = this->term_->differentiate(variable);
59  Term *t2 = TermSign::create(this->getProblem(), this->term_);
60  Term *ret = TermProduct::create(this->getProblem(), t1, t2);
61  return ret;
62 }
63 
65  const DoubleVector* variableValues,
66  TermValueSet* termValues) {
67  double value = fabs(this->term_->evaluate(variableValues, termValues));
68  return value;
69 }
70 
71 bool TermAbs::less(const Term* term) const {
72  if (this->Term::less(term)) {
73  return true;
74  } else if (term->Term::less(this)) {
75  return false;
76  } else {
77  const TermAbs* term_abs;
78  term_abs = static_cast<const TermAbs *> (term);
79  return this->term_->less(term_abs->term_);
80  }
81 }
82 
83 std::string TermAbs::toString() const {
84  std::ostringstream os;
85  os << "abs(" << term_->toString() << ")";
86  return os.str();
87 };