Coefficient.cpp 3.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "Coefficient.h"

Coefficient::Coefficient(std::map<Monomial, double, Monomial::less> coeff) : _coeff(coeff) {}

Coefficient::Coefficient(){}
Coefficient::Coefficient(double initial_val) : _coeff({{{}, initial_val}}) {}


bool Coefficient::is_non_zero(){
  for(std::pair<const Monomial, double>& m : this->_coeff){
    if(std::abs(m.second) > 1e-100){
      return true;
    }
  }
  return false;
}

bool Coefficient::is_unity(){
  auto search_result = this->_coeff.find({});
  if(search_result != this->_coeff.end()){
    if(std::abs(search_result->second - 1) > 1e-100){
      return false;
    }else{
      auto tmp = search_result->second;
      search_result->second = 0;
      bool non_unity = this->is_non_zero();
      search_result->second = tmp;
      if(non_unity){
        return false;
      }
    }
  }
  return true;
}

jonasseidel's avatar
jonasseidel committed
36
37
38
39
40
41
42
43
44
45

double Coefficient::value(){
  double curr = 0;
  for(std::pair<Monomial, double> pair : this->_coeff){
    curr += pair.second*pair.first.value();
  }
  return curr;
}


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Coefficient operator+(double val, Coefficient coeff){
//   Coefficient tmp (coeff);
//
//   for(std::pair<Monomial, double>& m : tmp){
//     m.second += val;
//   }
//
//   return tmp;
// }
Coefficient operator+(Coefficient coeffa, Coefficient coeffb){
  Coefficient tmp (coeffa);

  for(std::pair<const Monomial, double>& m : coeffb._coeff){
    auto search_result = tmp._coeff.find(m.first);
    if(search_result != coeffb._coeff.end()){
      search_result->second += m.second;
    }else{
      tmp._coeff.insert(m);
    }
  }

  return tmp;
}
Coefficient operator*(double val, Coefficient coeff){
  Coefficient tmp (coeff);

  for(std::pair<const Monomial, double>& m : tmp._coeff){
    m.second *= val;
  }
  return tmp;
}
Coefficient operator*(Coefficient coeffa, Coefficient coeffb){
  Coefficient tmp (coeffa);

  for(std::pair<const Monomial, double>& m : coeffb._coeff){
    for(std::pair<const Monomial, double>& n : coeffa._coeff){
      tmp._coeff.insert({m.first*n.first, m.second*n.second});
    }
  }

  return tmp;
}

//Coefficient operator-(double val, Coefficient coeff);
Coefficient operator-(Coefficient coeffa, Coefficient coeffb){
  Coefficient tmp (coeffa);

  for(std::pair<const Monomial, double>& m : coeffb._coeff){
    auto search_result = tmp._coeff.find(m.first);
    if(search_result != coeffb._coeff.end()){
      search_result->second -= m.second;
    }else{
      tmp._coeff.insert(m);
    }
  }

  return tmp;
}

Coefficient operator-(Coefficient coeff){
  Coefficient tmp;
  for(std::pair<const Monomial, double> m : coeff._coeff){
    m.second = -m.second;
    tmp._coeff.insert(m);
  }

  return tmp;
}

std::ostream& operator<<(std::ostream& os, Coefficient coeff){
  if(coeff.is_non_zero()){
    bool parenthesis = coeff._coeff.size() > 1 /*|| (coeff._coeff.size() > 0 && coeff._coeff.begin()->second < 1e-100)*/;
    if(parenthesis){
      os << "(";
    }
    bool add_plus = Coefficient::always_add_sign;
    for(std::pair<const Monomial, double>& m : coeff._coeff){
      if(std::abs(m.second) > 1e-100){
        if(m.second < 1e-100){
          if(add_plus){
            os << " - ";
          }else{
            os << "-";
          }
        }else if(add_plus){
          os << " + ";
        }
        add_plus = true;

        if(std::abs(m.second-1) < 1e-100){
          os << m.first;
        }else{
          os << std::abs(m.second) << " " << m.first;
        }
      }
    }
    if(parenthesis){
      os << ")";
    }
  }

  return os;
}