forked from kpchamp/SindyAutoencoders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsindy_utils.py
150 lines (119 loc) · 4.29 KB
/
sindy_utils.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
import numpy as np
from scipy.special import binom
from scipy.integrate import odeint
def library_size(n, poly_order, use_sine=False, include_constant=True):
l = 0
for k in range(poly_order+1):
l += int(binom(n+k-1,k))
if use_sine:
l += n
if not include_constant:
l -= 1
return l
def sindy_library(X, poly_order, include_sine=False):
m,n = X.shape
l = library_size(n, poly_order, include_sine, True)
library = np.ones((m,l))
index = 1
for i in range(n):
library[:,index] = X[:,i]
index += 1
if poly_order > 1:
for i in range(n):
for j in range(i,n):
library[:,index] = X[:,i]*X[:,j]
index += 1
if poly_order > 2:
for i in range(n):
for j in range(i,n):
for k in range(j,n):
library[:,index] = X[:,i]*X[:,j]*X[:,k]
index += 1
if poly_order > 3:
for i in range(n):
for j in range(i,n):
for k in range(j,n):
for q in range(k,n):
library[:,index] = X[:,i]*X[:,j]*X[:,k]*X[:,q]
index += 1
if poly_order > 4:
for i in range(n):
for j in range(i,n):
for k in range(j,n):
for q in range(k,n):
for r in range(q,n):
library[:,index] = X[:,i]*X[:,j]*X[:,k]*X[:,q]*X[:,r]
index += 1
if include_sine:
for i in range(n):
library[:,index] = np.sin(X[:,i])
index += 1
return library
def sindy_library_order2(X, dX, poly_order, include_sine=False):
m,n = X.shape
l = library_size(2*n, poly_order, include_sine, True)
library = np.ones((m,l))
index = 1
X_combined = np.concatenate((X, dX), axis=1)
for i in range(2*n):
library[:,index] = X_combined[:,i]
index += 1
if poly_order > 1:
for i in range(2*n):
for j in range(i,2*n):
library[:,index] = X_combined[:,i]*X_combined[:,j]
index += 1
if poly_order > 2:
for i in range(2*n):
for j in range(i,2*n):
for k in range(j,2*n):
library[:,index] = X_combined[:,i]*X_combined[:,j]*X_combined[:,k]
index += 1
if poly_order > 3:
for i in range(2*n):
for j in range(i,2*n):
for k in range(j,2*n):
for q in range(k,2*n):
library[:,index] = X_combined[:,i]*X_combined[:,j]*X_combined[:,k]*X_combined[:,q]
index += 1
if poly_order > 4:
for i in range(2*n):
for j in range(i,2*n):
for k in range(j,2*n):
for q in range(k,2*n):
for r in range(q,2*n):
library[:,index] = X_combined[:,i]*X_combined[:,j]*X_combined[:,k]*X_combined[:,q]*X_combined[:,r]
index += 1
if include_sine:
for i in range(2*n):
library[:,index] = np.sin(X_combined[:,i])
index += 1
return library
def sindy_fit(RHS, LHS, coefficient_threshold):
m,n = LHS.shape
Xi = np.linalg.lstsq(RHS,LHS, rcond=None)[0]
for k in range(10):
small_inds = (np.abs(Xi) < coefficient_threshold)
Xi[small_inds] = 0
for i in range(n):
big_inds = ~small_inds[:,i]
if np.where(big_inds)[0].size == 0:
continue
Xi[big_inds,i] = np.linalg.lstsq(RHS[:,big_inds], LHS[:,i], rcond=None)[0]
return Xi
def sindy_simulate(x0, t, Xi, poly_order, include_sine):
m = t.size
n = x0.size
f = lambda x,t : np.dot(sindy_library(np.array(x).reshape((1,n)), poly_order, include_sine), Xi).reshape((n,))
x = odeint(f, x0, t)
return x
def sindy_simulate_order2(x0, dx0, t, Xi, poly_order, include_sine):
m = t.size
n = 2*x0.size
l = Xi.shape[0]
Xi_order1 = np.zeros((l,n))
for i in range(n//2):
Xi_order1[2*(i+1),i] = 1.
Xi_order1[:,i+n//2] = Xi[:,i]
x = sindy_simulate(np.concatenate((x0,dx0)), t, Xi_order1, poly_order, include_sine)
return x