-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaling.py
151 lines (119 loc) · 4.11 KB
/
scaling.py
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
import numpy as np
import matplotlib.pyplot as plt
import numpy.polynomial.legendre as leg
# Return the function value of the specified order
# of the legendre polynomial defined on the area [0,1]
def legendrePolynomial(order, x):
coef = []
if type(x) is float:
if x <= 0 or x > 1:
return 0
else:
for n in range(0, order + 1):
if(n == order):
coef.append(1)
else:
coef.append(0)
return (2.*order + 1.)**.5 \
* leg.Legendre(coef, domain=[-1, 1])(2.0*x - 1)
else:
y = np.zeros(x.size)
for i in range(x.size):
if x[i] <= 0 or x[i] > 1:
y[i] = 0
else:
for n in range(0, order+1):
if(n == order):
coef.append(1)
else:
coef.append(0)
y[i] = (2.*order + 1.)**.5 \
* leg.Legendre(coef, domain=[-1, 1])(2.0*x[i] - 1)
coef.clear()
return y
# Returns the function value of the scale and translated
# legendre polynomial
def scaling(order, scale, translation, x):
return 2**(scale/2)*legendrePolynomial(order, 2**scale*x - translation)
def scalingVec(nr_poly, scale, translation, x):
tmp = np.empty(nr_poly)
for i in range(nr_poly):
tmp[i] = scaling(i, scale, translation, x)
return tmp
# Returns the roots of the legendre polynomial
def roots(order):
coef = []
for n in range(0, order+1):
if n == order:
coef.append(1)
else:
coef.append(0)
return .5*(leg.legroots(coef) + 1)
# Returns the weights of the legendre polynomial defined on
def weights(order):
x = roots(order)
w = np.empty(order)
coef = []
for n in range(0, order+1):
if(n == order):
coef.append(1)
else:
coef.append(0)
derivative_coefficients = leg.legder(coef)
del coef[-1]
del coef[-1]
coef.append(1)
for i in range(0, order):
tmp = leg.Legendre(derivative_coefficients, domain=[-1, 1])(2*x[i] - 1)
tmp2 = leg.Legendre(coef, domain=[-1, 1])(2.0*x[i] - 1)
w[i] = 1/(order*tmp*tmp2)
# Possibly some bug here, get a factor 2 different using mathematica
return w
# Numerical approximation of the scaling functions
def np_scaling(order, f):
x = np.arange(0, 1, .01)
return .5*np.trapz(legendrePolynomial(order, x)*f(2**(-2)*x), dx=0.01)
# Numerical approximation to the scaling coeffisients
def approximatedScalingCoef(order, scale, translation, f):
x = np.arange(0, 1, .0001)
return 2**(-.5*scale) * \
np.trapz(
legendrePolynomial(order, x)
* f(2**(-scale)*(x + translation)), dx=0.0001)
# Returns the scaling coeffisients
def scalingCoef(order, nr_poly, scale, translation, f):
tmp = 0
w = weights(nr_poly+1)
x = roots(nr_poly+1)
for q in range(0, w.size):
tmp = tmp + w[q]*f(2**(-scale)*(x[q]+translation)) \
* legendrePolynomial(order, float(x[q]))
return 2**(-.5*scale)*tmp
def scalingCoefVec(nr_poly, scale, translation, f):
tmp = np.empty(nr_poly)
for i in range(nr_poly):
tmp[i] = scalingCoef(i, nr_poly, scale, translation, f)
return tmp
# Projects a function f onto a given set of polynomials and scale
def funcProj(scale, nr_poly, f, x):
tmp = 0
for j in range(0, nr_poly):
for l in range(0, 2**scale):
tmp = tmp+scaling_coef(j, nr_poly, scale, l, f) \
* scaling(j, scale, l, x)
return tmp
def integrate_scale(scale, nr_poly, f):
tmp = 0
x = np.arange(0, 1, 0.01)
for j in range(0, nr_poly):
for l in range(0, 2**scale):
tmp = tmp+scalingCoef(j, nr_poly, scale, l, f) \
* np.trapz(legendrePolynomial(j, x), dx=0.01)
return tmp
def funcProjVec(scale, nr_poly, f, x):
tmp = 0
for j in range(2**scale):
a = scalingCoefVec(nr_poly, scale, j, f)
b = scalingVec(nr_poly, scale, j, x)
tmp = tmp + (a*b).sum()
return tmp