-
Notifications
You must be signed in to change notification settings - Fork 55
/
basecreps.cpp
196 lines (172 loc) · 6.55 KB
/
basecreps.cpp
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
36
37
38
39
40
41
42
43
44
45
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#define NULL 0
#include <algorithm>
#include <iostream>
#include <complex>
#include <assert.h>
#include "basecreps.h"
namespace CReps {
/**************************************************************************** \
|* PolynomialCRep *|
\****************************************************************************/
PolynomialCRep::PolynomialCRep() {
_coeffs = std::unordered_map<PolynomialVarsIndex, dcomplex>();
_max_num_vars = 0;
_vindices_per_int = 0;
}
PolynomialCRep::PolynomialCRep(std::unordered_map<PolynomialVarsIndex, dcomplex> coeffs, INT max_num_vars, INT vindices_per_int) {
_coeffs = coeffs;
_max_num_vars = max_num_vars;
_vindices_per_int = vindices_per_int;
}
PolynomialCRep::PolynomialCRep(const PolynomialCRep& other) {
_coeffs = other._coeffs;
_max_num_vars = other._max_num_vars;
}
PolynomialCRep::~PolynomialCRep() { }
PolynomialCRep PolynomialCRep::mult(const PolynomialCRep& other) {
std::unordered_map<PolynomialVarsIndex, dcomplex>::iterator it1, itk;
std::unordered_map<PolynomialVarsIndex, dcomplex>::const_iterator it2;
std::unordered_map<PolynomialVarsIndex, dcomplex> result;
dcomplex val;
PolynomialVarsIndex k;
for(it1 = _coeffs.begin(); it1 != _coeffs.end(); ++it1) {
for(it2 = other._coeffs.begin(); it2 != other._coeffs.end(); ++it2) {
k = mult_vinds_ints(it1->first, it2->first); //key to add
itk = result.find(k);
val = it1->second * it2->second;
if(itk != result.end())
itk->second = itk->second + val;
else result[k] = val;
}
}
PolynomialCRep ret(result, _max_num_vars, _vindices_per_int);
return ret; // need a copy constructor?
}
PolynomialCRep PolynomialCRep::abs_mult(const PolynomialCRep& other) {
std::unordered_map<PolynomialVarsIndex, dcomplex>::iterator it1, itk;
std::unordered_map<PolynomialVarsIndex, dcomplex>::const_iterator it2;
std::unordered_map<PolynomialVarsIndex, dcomplex> result;
dcomplex val;
PolynomialVarsIndex k;
for(it1 = _coeffs.begin(); it1 != _coeffs.end(); ++it1) {
for(it2 = other._coeffs.begin(); it2 != other._coeffs.end(); ++it2) {
k = mult_vinds_ints(it1->first, it2->first); //key to add
itk = result.find(k);
val = std::abs(it1->second * it2->second);
if(itk != result.end())
itk->second = itk->second + val;
else result[k] = val;
}
}
PolynomialCRep ret(result, _max_num_vars, _vindices_per_int);
return ret; // need a copy constructor?
}
void PolynomialCRep::add_inplace(const PolynomialCRep& other) {
std::unordered_map<PolynomialVarsIndex, dcomplex>::const_iterator it2;
std::unordered_map<PolynomialVarsIndex, dcomplex>::iterator itk;
dcomplex val, newval;
PolynomialVarsIndex k;
for(it2 = other._coeffs.begin(); it2 != other._coeffs.end(); ++it2) {
k = it2->first; // key
val = it2->second; // value
itk = _coeffs.find(k);
if(itk != _coeffs.end()) {
newval = itk->second + val;
if(std::abs(newval) > 1e-12) {
itk->second = newval; // note: += doens't work here (complex Cython?)
} else {
_coeffs.erase(itk);
}
}
else if(std::abs(val) > 1e-12) {
_coeffs[k] = val;
}
}
}
void PolynomialCRep::add_abs_inplace(const PolynomialCRep& other) {
std::unordered_map<PolynomialVarsIndex, dcomplex>::const_iterator it2;
std::unordered_map<PolynomialVarsIndex, dcomplex>::iterator itk;
double val;
PolynomialVarsIndex k;
for(it2 = other._coeffs.begin(); it2 != other._coeffs.end(); ++it2) {
k = it2->first; // key
val = std::abs(it2->second); // value
if(val > 1e-12) {
itk = _coeffs.find(k);
if(itk != _coeffs.end()) {
itk->second = itk->second + (dcomplex)val; // note: += doens't work here (complex Cython?)
}
else {
_coeffs[k] = (dcomplex)val;
}
}
}
}
PolynomialCRep PolynomialCRep::abs() {
std::unordered_map<PolynomialVarsIndex, dcomplex> result;
std::unordered_map<PolynomialVarsIndex, dcomplex>::iterator it;
for(it = _coeffs.begin(); it != _coeffs.end(); ++it) {
result[it->first] = std::abs(it->second);
}
PolynomialCRep ret(result, _max_num_vars, _vindices_per_int);
return ret; // need a copy constructor?
}
void PolynomialCRep::scale(dcomplex scale) {
std::unordered_map<PolynomialVarsIndex, dcomplex>::iterator it;
for(it = _coeffs.begin(); it != _coeffs.end(); ++it) {
it->second = it->second * scale; // note: *= doesn't work here (complex Cython?)
}
}
void PolynomialCRep::add_scalar_to_all_coeffs_inplace(dcomplex offset) {
std::unordered_map<PolynomialVarsIndex, dcomplex>::iterator it;
for(it = _coeffs.begin(); it != _coeffs.end(); ++it) {
it->second = it->second + offset; // note: += doesn't work here (complex Cython?)
}
}
PolynomialVarsIndex PolynomialCRep::vinds_to_int(std::vector<INT> vinds) {
INT ret, end, m;
INT sz = ceil(1.0 * vinds.size() / _vindices_per_int);
PolynomialVarsIndex ret_tup(sz);
for(INT k=0; k<sz-1; k++) {
ret = 0; m = 1;
for(std::size_t i=_vindices_per_int*k; i<_vindices_per_int*(k+1); i++) { // last tuple index is most significant
ret += (vinds[i]+1)*m;
m *= _max_num_vars+1;
}
ret_tup._parts[k] = ret;
}
if(sz > 0) { //final iteration has different uppper limit
ret = 0; m = 1;
for(std::size_t i=_vindices_per_int*(sz-1); i<vinds.size(); i++) { // last tuple index is most significant
ret += (vinds[i]+1)*m;
m *= _max_num_vars+1;
}
ret_tup._parts[sz-1] = ret;
}
return ret_tup;
}
std::vector<INT> PolynomialCRep::int_to_vinds(PolynomialVarsIndex indx_tup) {
std::vector<INT> ret;
INT nxt, i, indx;
for(std::size_t i=0; i < indx_tup._parts.size(); i++) {
indx = indx_tup._parts[i];
while(indx != 0) {
nxt = indx / (_max_num_vars+1);
i = indx - nxt*(_max_num_vars+1);
ret.push_back(i-1);
indx = nxt;
//assert(indx >= 0);
}
}
std::sort(ret.begin(),ret.end());
return ret;
}
PolynomialVarsIndex PolynomialCRep::mult_vinds_ints(PolynomialVarsIndex i1, PolynomialVarsIndex i2) {
// multiply vinds corresponding to i1 & i2 and return resulting integer
std::vector<INT> vinds1 = int_to_vinds(i1);
std::vector<INT> vinds2 = int_to_vinds(i2);
vinds1.insert( vinds1.end(), vinds2.begin(), vinds2.end() );
std::sort(vinds1.begin(),vinds1.end());
return vinds_to_int(vinds1);
}
}