-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwoscale.h
101 lines (95 loc) · 2.78 KB
/
twoscale.h
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
#include <memory>
#include <string>
#include <fstream>
#include "Matrix.h"
using Vector = real_vector;
//Return the twoscale coefficients for the multiwavelets of order k.
//
//Note that a cached value is returned ... if you want to modify it
//take a copy first
class TwoScaleCoeffs {
private:
std::vector<real_matrix> coeffs;
// static std::shared_ptr<TwoScaleCoeffs> _instance;
static TwoScaleCoeffs* _instance;
TwoScaleCoeffs() {
std::ifstream fil("tscoeffs");
if (fil.is_open()) {
int maxk = -1;
fil >> maxk;
for (auto k = 1; k <= maxk; k++) {
real_matrix matk;
matk.create(2*k,2*k);
int readk; double val = 0.0;
fil >> readk;
assert(k == readk);
for (auto j = 0; j < 4*k*k; j++) {
fil >> val;
matk[j] = val;
}
coeffs.push_back(matk);
}
}
}
public:
// static std::shared_ptr<TwoScaleCoeffs> instance() {
// if (!_instance) _instance = std::make_shared<TwoScaleCoeffs>();
// return _instance;
// }
static TwoScaleCoeffs* instance() {
if (!_instance) _instance = new TwoScaleCoeffs();
return _instance;
}
real_matrix hg(int k) {return coeffs[k-1];}
~TwoScaleCoeffs() {}
};
// std::shared_ptr<TwoScaleCoeffs> TwoScaleCoeffs::_instance = 0;
TwoScaleCoeffs* TwoScaleCoeffs::_instance = 0;
// Evaluate the Legendre polynomials up to the given order at x
// defined on [-1,1].
Vector legendre(double x,int order) {
Vector p(order+1);
p[0] = 1.0;
if (order == 0) return p;
p[1] = x;
for (int j = 1; j < order; j++) {
p[j+1] = j*(x*p[j] - p[j-1])/(j+1) + x*p[j];
}
return p;
}
class ScalingFunction {
private:
double norms[100];
// static std::shared_ptr<ScalingFunction> _instance;
static ScalingFunction* _instance;
ScalingFunction() {
for (int i = 0; i < 100; i++) {
norms[i] = std::sqrt(2*i+1);
}
}
public:
// static std::shared_ptr<ScalingFunction> instance() {
// if (!_instance) _instance = std::make_shared<ScalingFunction>();
// return _instance;
// }
static ScalingFunction* instance() {
if (!_instance) _instance = new ScalingFunction();
return _instance;
}
// Evaluate the shifted normalized Legendre polynomials up to the
// given order at x defined on [0,1].
// These are also our scaling functions, phi_i(x) , i=0..k-1
// In addition to forming an orthonormal basis on [0,1] we have
// phi_j(1/2-x) = (-1)^j phi_j(1/2+x)
// (the wavelets are similar with phase (-1)^(j+k)).
Vector phi(double x,int k) {
auto order = k-1;
auto p = legendre(2.*x-1, order);
for (auto n = 0; n < k; n++)
p[n] = p[n]*norms[n];
return p;
}
~ScalingFunction() {}
};
// std::shared_ptr<ScalingFunction> ScalingFunction::_instance = 0;
ScalingFunction* ScalingFunction::_instance = 0;