Skip to content

Commit

Permalink
updated weak implicit_terms
Browse files Browse the repository at this point in the history
  • Loading branch information
znicolaou committed Jun 7, 2022
1 parent df0472a commit f43833a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 52 deletions.
52 changes: 31 additions & 21 deletions examples/1_feature_overview.ipynb

Large diffs are not rendered by default.

81 changes: 51 additions & 30 deletions examples/9_sindypi_with_sympy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/alankaptanoglu/pysindy/pysindy/feature_library/sindy_pi_library.py:161: UserWarning: This library is deprecated in PySINDy versions > 1.7. Please use the PDE or WeakPDE libraries instead. \n",
" \"This library is deprecated in PySINDy versions > 1.7. Please \"\n",
"/Users/alankaptanoglu/pysindy/pysindy/feature_library/sindy_pi_library.py:161: UserWarning: This library is deprecated in PySINDy versions > 1.7. Please use the PDE or WeakPDE libraries instead. \n",
" \"This library is deprecated in PySINDy versions > 1.7. Please \"\n"
"/Users/zack/pysindy/pysindy/feature_library/sindy_pi_library.py:160: UserWarning: This library is deprecated in PySINDy versions > 1.7. Please use the PDE or WeakPDE libraries instead. \n",
" warnings.warn(\n",
"/Users/zack/pysindy/pysindy/feature_library/sindy_pi_library.py:160: UserWarning: This library is deprecated in PySINDy versions > 1.7. Please use the PDE or WeakPDE libraries instead. \n",
" warnings.warn(\n"
]
}
],
Expand Down Expand Up @@ -165,6 +165,13 @@
"This reason this modified optimization is required is that linear terms in $\\dot{x}$ are no longer guaranteed, so every candidate library term is fit with a different model to find the best combination of implicit terms. Note $\\text{diag}(\\Xi) = 0$ is required because without this, the model would just fit every candidate library term with itself, $\\dot{x} = \\dot{x}$, $x\\dot{x} = x\\dot{x}$, $x^2\\dot{x} = x^2\\dot{x}$, and so on."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
Expand All @@ -174,16 +181,14 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/alankaptanoglu/pysindy/pysindy/feature_library/sindy_pi_library.py:161: UserWarning: This library is deprecated in PySINDy versions > 1.7. Please use the PDE or WeakPDE libraries instead. \n",
" \"This library is deprecated in PySINDy versions > 1.7. Please \"\n"
"/Users/zack/pysindy/pysindy/feature_library/sindy_pi_library.py:160: UserWarning: This library is deprecated in PySINDy versions > 1.7. Please use the PDE or WeakPDE libraries instead. \n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"(10, 1)\n",
"(1, 10)\n",
"Model 0\n",
"Model 1\n",
"Model 2\n",
Expand Down Expand Up @@ -297,15 +302,13 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 145,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(10, 1)\n",
"(1, 10)\n",
"Model 0\n",
"Model 1\n",
"Model 2\n",
Expand All @@ -315,17 +318,18 @@
"Model 6\n",
"Model 7\n",
"Model 8\n",
"Solver failed on model 8 , setting coefs to zeros\n",
"Model 9\n",
"1 = 5.000 x0 + 8.355 x0_t + 5.552 x0x0_t\n",
"x0 = 0.200 1 + -1.665 x0_t + -1.113 x0x0_t\n",
"x0x0 = 0.012 1 + 0.113 x0 + 0.676 x0x0x0 + -0.578 x0x0_t\n",
"x0x0x0 = 0.008 1 + -0.103 x0 + 0.494 x0x0 + 0.721 x0x0x0x0\n",
"x0x0x0x0 = 0.009 1 + -0.064 x0 + 0.665 x0x0x0\n",
"x0_t = 0.081 1 + -0.483 x0 + 0.376 x0x0\n",
"x0x0_t = 0.056 1 + -0.164 x0 + -0.580 x0x0\n",
"x0x0x0_t = 0.002 1 + 0.043 x0 + -0.133 x0x0 + -0.598 x0x0x0\n",
"x0x0x0x0_t = -0.007 1 + 0.052 x0 + -0.485 x0x0x0\n",
"x0x0x0x0x0_t = -0.006 1 + 0.040 x0 + -0.269 x0x0x0\n"
"1 = 5.000 x0 + 1.667 x0_t + 5.556 x0x0_t\n",
"x0 = 0.200 1 + 0.005 x0x0 + -0.334 x0_t + -1.104 x0x0_t\n",
"x0x0 = 0.019 1 + 0.079 x0 + 0.625 x0x0x0 + -0.672 x0x0_t\n",
"x0x0x0 = -0.008 1 + 0.371 x0x0 + 0.827 x0x0x0x0 + 0.033 x0_t\n",
"x0x0x0x0 = -0.005 1 + 0.858 x0x0x0 + -0.022 x0_t + 0.294 x0x0_t + -0.110 x0x0x0x0x0_t\n",
"x0_t = 0.599 1 + -2.996 x0 + 0.015 x0x0 + -3.307 x0x0_t\n",
"x0x0_t = 0.177 1 + -0.882 x0 + -0.014 x0x0 + -0.293 x0_t\n",
"x0x0x0_t = 0.013 1 + -0.246 x0x0 + -0.406 x0x0x0 + -0.043 x0_t\n",
"x0x0x0x0_t = 0.000\n",
"x0x0x0x0x0_t = 0.001 1 + -0.492 x0x0x0x0 + 0.007 x0_t + -0.068 x0x0_t\n"
]
},
{
Expand All @@ -343,12 +347,24 @@
" 'x0x0x0x0x0_t']"
]
},
"execution_count": 4,
"execution_count": 145,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"library_functions = [\n",
" lambda x: x,\n",
" lambda x, y: x * y,\n",
" lambda x: x ** 2,\n",
" lambda x, y, z: x * y * z,\n",
" lambda x, y: x * y ** 2,\n",
" lambda x: x ** 3,\n",
" lambda x, y, z, w: x * y * z * w,\n",
" lambda x, y, z: x * y * z ** 2,\n",
" lambda x, y: x * y ** 3,\n",
" lambda x: x ** 4,\n",
"]\n",
"library_function_names = [\n",
" lambda x: x,\n",
" lambda x, y: x + y,\n",
Expand All @@ -362,13 +378,20 @@
" lambda x: x + x + x + x,\n",
"]\n",
"\n",
"sindy_opt = ps.SINDyPI(\n",
" threshold=1e-6,\n",
" tol=1e-9,\n",
" thresholder=\"l1\",\n",
" max_iter=20000,\n",
")\n",
"\n",
"sindy_library = ps.WeakPDELibrary(\n",
" library_functions=library_functions,\n",
" spatiotemporal_grid=t,\n",
" spatiotemporal_grid=t[:,np.newaxis],\n",
" function_names=library_function_names,\n",
" include_bias=True,\n",
" implicit_terms=True,\n",
" derivative_order=1\n",
" derivative_order=1, K=2000,\n",
")\n",
"\n",
"model = ps.SINDy(\n",
Expand All @@ -383,15 +406,13 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(10, 1)\n",
"(1, 10)\n",
"Model 0\n",
"Model 1\n",
"Model 2\n",
Expand Down Expand Up @@ -430,7 +451,7 @@
" 'x0x0x0x0x0_t']"
]
},
"execution_count": 5,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -875,7 +896,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -889,7 +910,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.9.12"
}
},
"nbformat": 4,
Expand Down
5 changes: 4 additions & 1 deletion pysindy/feature_library/weak_pde_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,10 @@ def _set_up_weights(self):
for k in range(self.K):
weights2 = []
for j in range(self.num_derivatives):
deriv = np.concatenate([self.multiindices[j], [0]])
if not self.implicit_terms:
deriv = np.concatenate([self.multiindices[j], [0]])
else:
deriv = self.multiindices[j]

ret = np.ones(shapes_k[k])
for i in range(self.grid_ndim):
Expand Down

0 comments on commit f43833a

Please sign in to comment.