-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstepfunction.cpp
290 lines (219 loc) · 7.37 KB
/
stepfunction.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "stepfunction.h"
#include <limits>
#include <iostream>
StepFunction::StepFunction()
{
}
StepFunction::StepFunction(double step_size, std::vector<std::complex<double> > y)
{
this->step_size = step_size;
vals = y;
domain = step_size*vals.size();
}
std::vector<double> StepFunction::xs(){
std::vector<double> xs;
double x = -step_size*vals.size()/2.;
for (size_t i=0; i<vals.size(); i++) {
xs.push_back(x);
x += step_size;
}
return xs;
}
std::complex<double> StepFunction::operator()(double x){
if(x < 0){
x += std::ceil(std::abs(x)/domain)*domain;
if(x<0){
throw std::runtime_error("");
}
}
size_t pos = size_t(x/step_size) % vals.size();
double offset = x/step_size-pos;
if(offset < 0.01){
return vals[pos];
}
if(pos+1>vals.size()){
throw std::invalid_argument("Function argument out of range");
}
std::complex<double> val = vals[pos]*(1-offset) + vals[pos+1]*offset;
return val;
}
std::complex<double>& StepFunction::operator[](int i){
size_t pos = size_t(i) & (vals.size()-1);
return vals[pos];
}
StepFunction StepFunction::derivative(){
std::vector<std::complex<double> > new_vals;
for(int i=0; i<int(vals.size()); i++){
new_vals.push_back(((*this)[i-2] - 8.*(*this)[i-1] + 8.*(*this)[i+1] - (*this)[i+2])/(12*step_size));
}
return StepFunction(step_size, new_vals);
}
StepFunction StepFunction::second_derivative(){
//auto second = derivative().derivative();
std::vector<std::complex<double> > new_vals;
new_vals.reserve(vals.size());
double h2 = step_size*step_size;
std::array<double,7> coeficients{1./90, -3./20, 3./2, -49./18, 3./2, -3./20, 1./90};
//std::array<double,9> coeficients2{-1./560, 8./315, -1./5, 8./5, -205./72, 8./5, -1./5, 8./315, -1./560};
for(auto& c: coeficients){
c /= h2;
}
for(int i=0; i<int(vals.size()); i++){
std::complex<double> val = 0.;
for(int j=0; j<7; j++){
val += coeficients[size_t(j)]*(*this)[i-3+j];
}
new_vals.push_back(val);
//new_vals.push_back((-(*this)[i-2] + 16.*(*this)[i-1] -30.*(*this)[i] + 16.*(*this)[i+1] - (*this)[i+2])/(12.*step_size*step_size));
}
/*
auto der = StepFunction(step_size, new_vals);
second = second - der;
for (int i=0; i<int(vals.size()); i++) {
std::cout<<der[i]<<' '<<second[i]<<' '<<i<<std::endl;
}*/
return StepFunction(step_size, new_vals);
}
StepFunction StepFunction::conjugate(){
std::vector<std::complex<double> > new_vals;
for(auto&& v: vals){
new_vals.push_back(std::conj(v));
}
return StepFunction(step_size, new_vals);
}
StepFunction StepFunction::abs(){
std::vector<std::complex<double> > new_vals;
for(auto&& v: vals){
new_vals.push_back(std::abs(v));
}
return StepFunction(step_size, new_vals);
}
StepFunction StepFunction::norm_function(){
return (*this) * this->conjugate();
}
std::complex<double> StepFunction::integral(){
std::complex<double> sum=0;
for(auto&& v: vals){
sum += v;
}
return sum*step_size;
}
double StepFunction::norm(){
StepFunction norm_function = this->norm_function();
std::complex<double> norm = norm_function.integral();
if(std::imag(norm) > std::numeric_limits<double>::epsilon()){
throw std::runtime_error("Function norm has complex value");
}
return std::real(norm);
}
StepFunction StepFunction::phase(){
std::complex<double> phase(1,0);
std::vector<std::complex<double> > new_vals;
for (size_t i=0; i<vals.size(); i++) {
auto v = vals[i];
if(std::abs(v) > 1e-16){
phase = v/std::abs(v);
}
new_vals.push_back(v);
}
return StepFunction(step_size, new_vals);
}
StepFunction StepFunction::fourier_transform(){
std::vector<std::complex<double> > new_vals;
std::complex<double> i(0.,1.);
double two_pi = 2*M_PI;
for(size_t k=0; k<vals.size(); k++){
double freq = two_pi*k/vals.size();
std::complex<double> xk;
for (size_t n=0; n<vals.size();n++){
xk += vals[n]*(std::cos(freq*n) - i *std::sin(freq*n));
}
new_vals.push_back(xk);
}
return StepFunction(step_size, new_vals);
}
void ditfft2(std::complex<double>* function, std::complex<double>* target,
size_t N, size_t s){
if(N == 1){
*target = *function;
return;
}
ditfft2(function, target, N/2, 2*s);
ditfft2(function+s, target+N/2, N/2, 2*s);
double base_freq = 2.*M_PI/double(N);
for(size_t k=0; k<N/2; k++){
std::complex<double> t = *(target+k);
std::complex<double> component = *(target+k+N/2) * std::polar(1.,-base_freq*double(k));
*(target+k) = t + component;
*(target+k+N/2) = t - component;
}
}
StepFunction StepFunction::fast_fourier_transform(){
std::vector<std::complex<double> > target(vals.size());
ditfft2(&vals[0], &target[0], vals.size(), 1);
return step_size/sqrt(2*M_PI)*StepFunction(M_PI/5, target);
}
std::vector<double> StepFunction::re(){
std::vector<double > new_vals;
for(auto&& v: vals){
new_vals.push_back(std::real(v));
}
return new_vals;
}
std::vector<double> StepFunction::im(){
std::vector<double > new_vals;
for(auto&& v: vals){
new_vals.push_back(std::imag(v));
}
return new_vals;
}
StepFunction operator+(StepFunction lhs, StepFunction rhs){
if(std::abs(lhs.step_size - rhs.step_size) > std::numeric_limits<double>::epsilon()){
throw std::invalid_argument("Step sizes of added functions should be equal");
}
if(lhs.vals.size() != rhs.vals.size()){
throw std::invalid_argument("Values sizes of added functions should be equal");
}
std::vector<std::complex<double> > new_vals;
for(size_t i=0; i<lhs.vals.size(); i++){
new_vals.push_back(lhs.vals[i]+rhs.vals[i]);
}
return StepFunction(lhs.step_size, new_vals);
}
StepFunction operator*(StepFunction lhs, StepFunction rhs){
if(std::abs(lhs.step_size - rhs.step_size) > std::numeric_limits<double>::epsilon()){
throw std::invalid_argument("Step sizes of multiplied functions should be equal");
}
if(lhs.vals.size() != rhs.vals.size()){
throw std::invalid_argument("Values sizes of multiplied functions should be equal");
}
std::vector<std::complex<double> > new_vals;
for(size_t i=0; i<lhs.vals.size(); i++){
new_vals.push_back(lhs.vals[i]*rhs.vals[i]);
}
return StepFunction(lhs.step_size, new_vals);
}
StepFunction operator-(StepFunction lhs, StepFunction rhs){
return lhs + (-1.)*rhs;
}
StepFunction operator*(std::complex<double> lhs, StepFunction rhs){
std::vector<std::complex<double> > new_vals;
for(size_t i=0; i<rhs.vals.size(); i++){
new_vals.push_back(lhs*rhs.vals[i]);
}
return StepFunction(rhs.step_size, new_vals);
}
StepFunction operator/(std::complex<double> lhs, StepFunction rhs){
std::vector<std::complex<double> > new_vals;
for(size_t i=0; i<rhs.vals.size(); i++){
new_vals.push_back(lhs/rhs.vals[i]);
}
return StepFunction(rhs.step_size, new_vals);
}
StepFunction operator/(StepFunction lhs, StepFunction rhs){
return lhs * (1./rhs);
}
StepFunction operator*(double lhs, StepFunction rhs){
std::complex<double> lhs2 = lhs;
return lhs2*rhs;
}