Crevice  0.1
 All Classes Functions Variables Pages
TermSign.cc
1 /*
2  * File: TermSign.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 "TermSign.h"
21 #include "TermConstant.h"
22 #include <sstream>
23 
29 TermSign::TermSign(Problem* problem, Term *term) :
30 Term::Term(problem) {
31  term_ = term;
32 }
33 
38 }
39 
46 TermSign* TermSign::create(Problem* problem, Term *term) throw
48  TermSign* ret = new TermSign(problem, term);
49  ret = static_cast<TermSign*> (problem->addTerm(ret));
50  return ret;
51 }
52 
55  return TermConstant::create(this->getProblem(), 0);
56 }
57 
59  const DoubleVector* variableValues,
60  TermValueSet* termValues) {
61  double value = this->term_->evaluate(variableValues, termValues);
62  if (value > 0) {
63  return 1.;
64  } else if (value < 0) {
65  return -1.;
66  } else {
67  return 0.;
68  }
69 }
70 
71 bool TermSign::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 TermSign* term_sign;
78  term_sign = static_cast<const TermSign *> (term);
79  return this->term_->less(term_sign->term_);
80  }
81 }
82 
83 std::string TermSign::toString() const {
84  std::ostringstream os;
85  os << "sgn(" << term_->toString() << ")";
86  return os.str();
87 };