-
Notifications
You must be signed in to change notification settings - Fork 3
/
eigsys_invariants_3.py
207 lines (162 loc) · 5.54 KB
/
eigsys_invariants_3.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
# File: eigsys_invariants_3.py
# Author: Ryoichi Ando ([email protected])
# License: Apache v2.0
import numpy as np
import numpy.linalg as LA
from eig_py.eigsolve3x3 import sym_eigsolve_3x3
h = 1e-4
F = np.random.rand(3, 3)
np.set_printoptions(
precision=2, suppress=False, formatter={"float_kind": "{:0.2e}".format}
)
def svd3x3(F):
A = F.T @ F
lmd, eigvecs = sym_eigsolve_3x3(A)
sigma = np.sqrt(lmd)
U = F @ eigvecs
for i in range(3):
U[:, i] /= np.linalg.norm(U[:, i])
return U, sigma, eigvecs.T
def energy_I(I1, I2, I3):
return (I1 + I2 - 6) ** 2 / I1 + np.sqrt(I3 / I1 + 1) - 2
def get_invariants(F):
C = F.T @ F
I1 = np.trace(C)
I2 = 0.5 * (I1**2 - np.trace(C @ C))
I3 = LA.det(C)
return I1, I2, I3
def energy_F(F):
I1, I2, I3 = get_invariants(F)
return energy_I(I1, I2, I3)
def sym_dIda(a, b, c):
gI1 = 2 * np.array([a, b, c])
gI2 = 2 * np.array([a * b**2 + a * c**2, b * c**2 + b * a**2, c * a**2 + c * b**2])
gI3 = 2 * np.array([a * b**2 * c**2, b * c**2 * a**2, c * a**2 * b**2])
return np.array([gI1, gI2, gI3]).T
def adjugate(matrix):
result = np.zeros((3, 3))
for i in range(3):
for j in range(3):
minor = np.delete(np.delete(matrix, i, axis=0), j, axis=1)
result[i, j] = ((-1) ** (i + j)) * LA.det(minor)
return result.T
def sym_dIdF(F):
C = F.T @ F
gI1 = 2 * F
gI2 = 2 * (F * np.trace(C) - F @ C)
gI3 = 2 * F @ adjugate(C).T
return [gI1, gI2, gI3]
def sym_d2Ida2(a, b, c):
H1 = 2 * np.eye(3)
H2 = np.zeros((3, 3))
H2[:, 0] = 2 * np.array([b**2 + c**2, 2 * a * b, 2 * a * c])
H2[:, 1] = 2 * np.array([2 * a * b, c**2 + a**2, 2 * b * c])
H2[:, 2] = 2 * np.array([2 * a * c, 2 * b * c, a**2 + b**2])
H3 = np.zeros((3, 3))
H3[:, 0] = 2 * np.array([b**2 * c**2, 2 * a * b * c**2, 2 * a * b**2 * c])
H3[:, 1] = 2 * np.array([2 * a * b * c**2, c**2 * a**2, 2 * a**2 * b * c])
H3[:, 2] = 2 * np.array([2 * a * b**2 * c, 2 * a**2 * b * c, a**2 * b**2])
return H1, H2, H3
def approx_dEdI(I1, I2, I3):
return np.array(
[
(energy_I(I1 + h, I2, I3) - energy_I(I1 - h, I2, I3)) / (2 * h),
(energy_I(I1, I2 + h, I3) - energy_I(I1, I2 - h, I3)) / (2 * h),
(energy_I(I1, I2, I3 + h) - energy_I(I1, I2, I3 - h)) / (2 * h),
]
)
def approx_d2EdI2(I1, I2, I3):
H = np.zeros((3, 3))
H[:, 0] = (approx_dEdI(I1 + h, I2, I3) - approx_dEdI(I1 - h, I2, I3)) / (2 * h)
H[:, 1] = (approx_dEdI(I1, I2 + h, I3) - approx_dEdI(I1, I2 - h, I3)) / (2 * h)
H[:, 2] = (approx_dEdI(I1, I2, I3 + h) - approx_dEdI(I1, I2, I3 - h)) / (2 * h)
return H
def sym_dEda(a, b, c, dEdI):
dIda = sym_dIda(a, b, c)
return dIda @ dEdI
def sym_d2Eda2(a, b, c, dEdI, d2EdI2):
H = np.zeros((3, 3))
dIda = sym_dIda(a, b, c)
d2Ida2 = sym_d2Ida2(a, b, c)
for i in range(3):
H += dEdI[i] * d2Ida2[i]
H += dIda @ d2EdI2 @ dIda.T
return H
def approx_grad_F(F, dF):
return (energy_F(F + h * dF) - energy_F(F - h * dF)) / (2 * h)
def approx_hess_F(F, dF):
H = np.zeros((len(dF), len(dF)))
for i, dFi in enumerate(dF):
for j, dFj in enumerate(dF):
H[i][j] = (
approx_grad_F(F + h * dFi, dFj) - approx_grad_F(F - h * dFi, dFj)
) / (2 * h)
return H
def gen_dF(i, j):
dF = np.zeros((3, 3))
dF[i][j] = 1
return dF
def mat2vec(A):
x = []
for j in range(3):
for i in range(3):
x.append(A[i][j])
return np.array(x)
dF = []
for j in range(3):
for i in range(3):
dF.append(gen_dF(i, j))
print("---- numerical gradient ----")
g_F = np.zeros(9)
for i, dFi in enumerate(dF):
g_F[i] = approx_grad_F(F, dFi)
g_F = np.reshape(g_F, (3, 3)).T
print(g_F)
U, (a, b, c), Vt = svd3x3(F)
H_F = approx_hess_F(F, dF)
print("--- numerical hessian ---")
print(H_F)
####### Analytical Eigen Decomposition using Invariants #######
I1, I2, I3 = get_invariants(F)
dEdI = approx_dEdI(I1, I2, I3)
dE2dI2 = approx_d2EdI2(I1, I2, I3)
print("---- analytical gradient ----")
g_F_rebuilt = np.zeros((3, 3))
dIdF = sym_dIdF(F)
for dIk, dEk in zip(dIdF, dEdI):
g_F_rebuilt += dIk * dEk
print(g_F_rebuilt)
H_s = sym_d2Eda2(a, b, c, dEdI, dE2dI2)
S_s, U_s = sym_eigsolve_3x3(H_s)
Qs = [
np.array([[0, 1, 0], [-1, 0, 0], [0, 0, 0]]) / np.sqrt(2),
np.array([[0, 0, 1], [0, 0, 0], [-1, 0, 0]]) / np.sqrt(2),
np.array([[0, 0, 0], [0, 0, 1], [0, -1, 0]]) / np.sqrt(2),
np.array([[0, 1, 0], [1, 0, 0], [0, 0, 0]]) / np.sqrt(2),
np.array([[0, 0, 1], [0, 0, 0], [1, 0, 0]]) / np.sqrt(2),
np.array([[0, 0, 0], [0, 0, 1], [0, 1, 0]]) / np.sqrt(2),
np.diag(U_s[:, 0]),
np.diag(U_s[:, 1]),
np.diag(U_s[:, 2]),
]
abc = a * b * c
lmds = [
2 * (dEdI[0] + (a * b + c**2) * dEdI[1] + (c * abc) * dEdI[2]),
2 * (dEdI[0] + (a * c + b**2) * dEdI[1] + (b * abc) * dEdI[2]),
2 * (dEdI[0] + (b * c + a**2) * dEdI[1] + (a * abc) * dEdI[2]),
2 * (dEdI[0] + (c**2 - a * b) * dEdI[1] - (c * abc) * dEdI[2]),
2 * (dEdI[0] + (b**2 - a * c) * dEdI[1] - (b * abc) * dEdI[2]),
2 * (dEdI[0] + (a**2 - b * c) * dEdI[1] - (a * abc) * dEdI[2]),
S_s[0],
S_s[1],
S_s[2],
]
Qmat = np.zeros((9, 9))
for i, w in enumerate(Qs):
Qmat[:, i] = mat2vec(U @ w @ Vt)
H_rebuilt = Qmat @ np.diag(lmds) @ Qmat.T
print("--- analytical hessian ---")
print(H_rebuilt)
###############################################
print(f"gradient error: {LA.norm(g_F - g_F_rebuilt) / LA.norm(g_F):.3e}")
print(f"hessian error: {LA.norm(H_F - H_rebuilt) / LA.norm(H_F):.3e}")