-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECM_graphical_interpretation.py
224 lines (192 loc) · 8.89 KB
/
ECM_graphical_interpretation.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
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
### Declaring functions for ECM
import numpy as np
from matplotlib import pyplot as plt
from empirical_cubature_method import EmpiricalCubatureMethod
### using LateX fonts
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
###
def generate_G(rows=6, cols=2):
np.random.seed(9) #setting a seed to ensure consistent and nicely looking results
a = np.random.rand(rows,cols)
U,_,_ = np.linalg.svd(a, full_matrices=False)
G = U
return G
def generate_illposed_G(rows=6, cols=2):
np.random.seed(9) #setting a seed to ensure consistent and nicely looking results
a = np.random.rand(rows,cols)
b = np.sum(a, axis=0)/6
a = a - b #making the sum of the rows to be zero
U,_,_ = np.linalg.svd(a, full_matrices=False)
G = U.T
return G
def plot_ECM_Steps(G, W, Z):
colours = ['yellow','cyan','green','cyan','black','magenta']
r = []
G_w = []
r.append(np.sum(G, axis=1))
green_lines = None
magenta_lines = None
for j in range(np.size(Z)):
Gz = G[:,Z[j]]
G_w.append(Gz*W[j])
Gz = Gz.reshape(-1,1)
r.append(r[j] - G_w[j])
P = ([email protected])/(Gz.T@Gz)
if green_lines is None:
p = P@r[j]
projection = p
green_lines = np.array([G_w[j][0], G_w[j][1]]).reshape(-1,1)
green_lines = np.c_[green_lines, np.array([r[0][0], r[0][1]]).reshape(-1,1)]
magenta_lines = projection.reshape(-1,1)
magenta_lines = np.c_[magenta_lines, np.array([r[0][0], r[0][1]]).reshape(-1,1)]
rr = r[j]-p
else:
p = rr
projection = np.c_[projection, rr]
p = P@r[j]
projection = np.c_[projection, p]
green_lines = np.c_[green_lines, np.array([G_w[j][0], G_w[j][1]]).reshape(-1,1)]
green_lines = np.c_[green_lines, np.array([r[0][0], r[0][1]]).reshape(-1,1)]
magenta_lines = np.c_[magenta_lines, rr]
magenta_lines = np.c_[magenta_lines, p]
rr = r[j]-p
#Step 1 plot the arrows
ax = plt.axes()
if True:
#plotting the 2D vectors (2 modes were retrieved from the SVD of the residual matrix...
# the number of arrows equals the number of elements)
for i in range(G.shape[1]):
ax.arrow(0.0, 0.0, G[0,i],G[1,i], head_width=np.max(G[:]*0.025))
ax.text(G[0,i]+0.05,G[1,i]+0.05, str(i+1), ha='center', va='center')
ax.axis('equal')
ax.set_title('Step 1: get the candidate vectors', fontweight='bold')
#plt.legend()
plt.ylim([-1, 1.5])
plt.xlim([-0.5, 2.5])
plt.tight_layout()
plt.savefig('Step1.pdf', bbox_inches='tight')
plt.show()
#Step 2 plot the original residual
ax = plt.axes()
if True:
#plotting the 2D vectors (2 modes were retrieved from the SVD of the residual matrix...
# the number of arrows equals the number of elements)
for i in range(G.shape[1]):
ax.arrow(0.0, 0.0, G[0,i],G[1,i], head_width=np.max(G[:]*0.025))
ax.arrow(0.0, 0.0, r[0][0],r[0][1], color='red', head_width=np.max(G[:]*0.025), label=r'$||r_0||$ = '+ "{:.2f}".format(np.linalg.norm(r[0]) ) )
#ax.axis('equal')
ax.set_title('Step 2: sum all vectors to obtain the original residual',fontweight='bold')
plt.legend()
plt.ylim([-1, 1.5])
plt.xlim([-0.5, 2.5])
plt.tight_layout()
plt.savefig('Step2.pdf', bbox_inches='tight')
plt.show()
#Step 3 find the projection onto the most positvely parallel vector
ax = plt.axes()
if True:
#plotting the 2D vectors (2 modes were retrieved from the SVD of the residual matrix...
# the number of arrows equals the number of elements)
for i in range(G.shape[1]):
ax.arrow(0.0, 0.0, G[0,i],G[1,i], head_width=np.max(G[:]*0.025))
ax.arrow(0.0, 0.0, r[0][0],r[0][1], color='red', head_width=np.max(G[:]*0.025), label=r'$||r_0||$ = '+ "{:.2f}".format(np.linalg.norm(r[0]) ) )
ax.arrow(0.0, 0.0, projection[0][0],projection[1][0], color='magenta', head_width=np.max(G[:]*0.025))
ax.plot(magenta_lines[0,0:2], magenta_lines[1,0:2], 'm--')
ax.set_title('Step 3: find the projection onto the most positvely parallel vector',fontweight='bold')
#ax.axis('equal')
#plt.legend()
plt.ylim([-1, 1.5])
plt.xlim([-0.5, 2.5])
plt.tight_layout()
plt.savefig('Step3.pdf', bbox_inches='tight')
plt.show()
#Step 4 update the residual
ax = plt.axes()
if True:
#plotting the 2D vectors (2 modes were retrieved from the SVD of the residual matrix...
# the number of arrows equals the number of elements)
for i in range(G.shape[1]):
ax.arrow(0.0, 0.0, G[0,i],G[1,i], head_width=np.max(G[:]*0.025))
ax.arrow(0.0, 0.0, r[0][0],r[0][1], color='red', head_width=np.max(G[:]*0.025), label=r'$||r_0||$ = '+ "{:.2f}".format(np.linalg.norm(r[0]) , alpha=0.3))
ax.arrow(0.0, 0.0, projection[0][0],projection[1][0], color='magenta', head_width=np.max(G[:]*0.025), alpha=0.3)
ax.arrow(0.0, 0.0, projection[0][1],projection[1][1], color='red', head_width=np.max(G[:]*0.025))
ax.plot(magenta_lines[0,0:2], magenta_lines[1,0:2], 'm--')
ax.axis('equal')
ax.set_title('Step 4: update the residual',fontweight='bold')
#plt.legend()
plt.ylim([-1, 1.5])
plt.xlim([-0.5, 2.5])
plt.tight_layout()
plt.savefig('Step4.pdf', bbox_inches='tight')
plt.show()
#Step 5 find vector most positely parallel to the updated residual
ax = plt.axes()
if True:
#plotting the 2D vectors (2 modes were retrieved from the SVD of the residual matrix...
# the number of arrows equals the number of elements)
for i in range(G.shape[1]):
ax.arrow(0.0, 0.0, G[0,i],G[1,i], head_width=np.max(G[:]*0.025))
ax.arrow(0.0, 0.0, r[0][0],r[0][1], color='red', head_width=np.max(G[:]*0.025), label=r'$||r_0||$ = '+ "{:.2f}".format(np.linalg.norm(r[0]) , alpha=0.3))
ax.arrow(0.0, 0.0, projection[0][0],projection[1][0], color='magenta', head_width=np.max(G[:]*0.025), alpha=0.3)
ax.arrow(0.0, 0.0, projection[0][1],projection[1][1], color='red', head_width=np.max(G[:]*0.025))
for i in range(0, int(np.size(Z)*2), 2):
ax.plot(magenta_lines[0,i:i+2], magenta_lines[1,i:i+2], 'm--')
#ax.axis('equal')
ax.set_title('Step 5: find vector most positely parallel to the updated residual',fontweight='bold')
#plt.legend()
plt.ylim([-1, 1.5])
plt.xlim([-0.5, 2.5])
plt.tight_layout()
plt.savefig('Step5.pdf', bbox_inches='tight')
plt.show()
#Step 6 find weights for the two vectors found that minimize the original residual
ax = plt.axes()
if True:
#plotting the 2D vectors (2 modes were retrieved from the SVD of the residual matrix...
# the number of arrows equals the number of elements)
for i in range(G.shape[1]):
ax.arrow(0.0, 0.0, G[0,i],G[1,i], head_width=np.max(G[:]*0.025))
ax.arrow(0.0, 0.0, r[0][0],r[0][1], color='red', head_width=np.max(G[:]*0.025), label=r'$||r_0||$ = '+ "{:.2f}".format(np.linalg.norm(r[0]) , alpha=0.3))
ax.arrow(0.0, 0.0, r[-1][0],r[-1][1], color='red', head_width=np.max(G[:]*0.025), label=r'$||r_{END}||$ = '+ "{:.2e}".format(np.linalg.norm(r[-1]) ))
for i in range(len(r) -1):
ax.arrow(0.0, 0.0, G_w[i][0],G_w[i][1], color=colours[i], head_width=np.max(G[:]*0.025), label='G[: , z_'+str(i)+'] @ w_'+str(i))
for i in range(0, int(np.size(Z)*2), 2):
ax.plot(green_lines[0,i:i+2], green_lines[1,i:i+2], 'g--')
#ax.axis('equal')
ax.set_title('Step 6: find weights for the vectors found that minimize the original residual',fontweight='bold')
#plt.legend()
ax.legend()
plt.ylim([-1, 1.5])
plt.xlim([-0.5, 2.5])
plt.tight_layout()
plt.savefig('Step6.pdf', bbox_inches='tight')
plt.show()
def PlotIllPosedG(G):
r = []
r.append(np.sum(G, axis=1))
#Step 2 plot the original residual
ax = plt.axes()
if True:
#plotting the 2D vectors (2 modes were retrieved from the SVD of the residual matrix...
# the number of arrows equals the number of elements)
for i in range(G.shape[1]):
ax.arrow(0.0, 0.0, G[0,i],G[1,i], head_width=np.max(G[:]*0.025))
ax.arrow(0.0, 0.0, r[0][0],r[0][1], color='red', head_width=np.max(G[:]*0.025), label='r0 norm = '+str(np.linalg.norm(r[0])))
#ax.axis('equal')
ax.set_title('Step 2: sum all vectors to obtain the original residual',fontweight='bold')
plt.legend()
plt.ylim([-1, 1])
plt.xlim([-1, 1])
plt.show()
def launch_ecm_on_2D_matrix():
G = generate_G()
ECM = EmpiricalCubatureMethod() # Setting up Empirical Cubature Method problem
Weights = np.ones(np.shape(G)[0]) # we arbitrary set W to the vector of ones
ECM.SetUp(G.T, Weights = Weights, constrain_sum_of_weights=False)
ECM.Run()
W = np.squeeze(ECM.w)
Z = ECM.z
plot_ECM_Steps(G.T, W, Z)
if __name__=='__main__':
launch_ecm_on_2D_matrix()