-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgreen.py
More file actions
276 lines (164 loc) · 5.27 KB
/
green.py
File metadata and controls
276 lines (164 loc) · 5.27 KB
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
# Python 2.7.6
#
# TG
# 11/08/2015
#
import numpy as np
import InnerProductSpace
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
def regionPoints( corner = 0j, width = 1., condition = lambda z: 0*z + 1, N = 100 ):
'''
Points, weight = regionPoints( corner, width, condition )
corner complex number (default: 0)
width real number (default: 1)
condition function that takes in an array of complex numbers and returns 0/False or 1/True
(default: constant function 1)
N integer (default 100)
We identify K as the region inside the square S:
(corner + i*width) ------------- (corner + (1+i)*width)
| __ S |
| / \ |
| / K ) |
| | ( |
| \___/ |
(corner) ------------- (corner + width)
that fulfills condition. That is
A point z is in K if and only if z is in S and condition(z) == 1/True
Points complex 1d-array of the numbers in K (at most all N^2 points of S).
weight real, the area each point represents.
'''
minRe = corner.real
maxRe = minRe + width
minIm = corner.imag
maxIm = minIm + width
xx = np.linspace( minRe, maxRe, N + 1 )
yy = np.linspace( minIm, maxIm, N + 1 )
Re, Im = np.meshgrid( xx, yy )
Grid = Re + Im*1j
Points = Grid[ condition( Grid ).astype( bool ) ]
weight = (width / N)**2
return Points, weight
def innerProduct( u, v, n, Q, K ):
'''
r = inProd( u, v, n, Q, K )
n integer
u,v complex 1d-arrays (representing polynomials)
len(u) == len(v) == n
Q weight function
K = ( Points, weight )
Points complex array
weight real
r = <u,v>
where <u,v> is an inner product on a polynomial space defined in the following way:
We interpret K to be a region containing the numbers 'Points', each one representing the area 'weight'.
Define polynomials
p(z) = sum_j u[j] * z^j
and q(z) = sum_j v[j] * z^j
<u,v> := integral_K p * conjugate(q) * exp( -2*n*Q ) dm
'''
Points, weight = K
p = np.polynomial.polynomial.polyval( Points, u )
q = np.polynomial.polynomial.polyval( Points, v )
Int = weight * ( p * np.conjugate(q) * np.exp( -2.*n * Q( Points ) ) )
return Int.sum()
def Bergman( z, B ):
'''
S = Bergman( z, B )
z complex array (or number)
B n*n complex 2d-array
B[j] coefficients of a polynomial p_j
S = sum_j |p_j(z)|^2
'''
S = 0
for j in range( len(B) ):
p_j = np.polynomial.polynomial.polyval( z, B[j] )
S += p_j*np.conjugate(p_j)
# S has no imaginary part. Cast to real.
return S.real
def Green( z, n, Q = lambda z: 0*z, K = regionPoints() ):
'''
g = Green( z, n, Q, K )
z complex array (or number)
n integer
Q weight function (default: constant function 0)
K = ( Points, weight )
Points complex array (default: unit square [0,1] + [0,1]i )
weight real (default: 10^-4)
g The n-th approximation of the weighted Green function
G_K_Q(z) = sup{ u(z) : u in L(C), u <= Q on K }
evaluated at z.
'''
inProd = lambda u,v: innerProduct( u, v, n, Q, K )
V = InnerProductSpace.InnerProductSpace( n, inProd )
B = V.GramSchmidt()
return np.log( Bergman( z, B ) ) / (2.*n)
def drawGreen( n, Q = lambda z: 0*z, K = regionPoints(), show = True ):
'''
fig = drawGreen( n, Q, K )
n integer
Q weight function (default: constant function 0)
K = ( Points, weight )
Points complex array (default: unit square [0,1] + [0,1]i )
weight real (default: 10^-4)
show boolean (default: True)
fig Shows the n-th approximation of the weighted Green function
G_K_Q(z) = sup{ u(z) : u in L(C), u <= Q on K }
in a neighborhood of K.
If show == True, plt.show() is called.
'''
Points = K[0]
minRe = Points.real.min()
maxRe = Points.real.max()
minIm = Points.imag.min()
maxIm = Points.imag.max()
minmin = min(minRe,minIm)
maxmax = max(maxRe,maxIm)
width = maxmax - minmin
minmin = minmin - 0.5*width
maxmax = maxmax + 0.5*width
N = 100
xx = np.linspace( minmin, maxmax, N + 1 )
yy = np.linspace( minmin, maxmax, N + 1 )
Re, Im = np.meshgrid( xx, yy )
Z = Re + Im*1j
green = Green( Z, n, Q, K )
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(Re, Im, green, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
if show:
plt.show()
return fig
def GreenEll( z, a, b ):
'''
Green fall med dK = sporbaugur og Q = 0.
GreenEll(z) = max{ 0, log( sqrt( (x/a)^2 + (y/b)^2 ) ) }
'''
r = (z.real/a)**2 + (z.imag/b)**2
gr = np.log( r*(r>1) + 1.*(r<=1) ) / 2.
return gr
def main():
# Max degree of polynomials
n = 50
# Lower left corner and width of square S
corner = - 5 - 5j
width = 10.
# z is in K iff z is in S and condition(z) == 1/True
a = 1.
b = 2.
condition = lambda z: (z.real/a)**2 + (z.imag/b)**2 < 1
K = regionPoints( corner, width, condition, 100 )
Q = lambda z: 0.*z
drawGreen( n, Q, K, show = False )
# Draw exact solution for ellipse region.
xx = np.linspace(-4,4,101)
yy = np.linspace(-4,4,101)
Re, Im = np.meshgrid(xx,yy)
Z = Re + Im*1j
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(Re, Im, GreenEll(Z,a,b), rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
plt.show()
if __name__ == '__main__':
main()