sympy intro

This commit is contained in:
lda committed 2025-02-01 02:11:38 +07:00
1 parent c5bc4a5754
commit 25a9db6346
2 files changed
+279

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
# slice 2
## what we learns
there are a lot of ways you can iteratively find roots if math cant do it easily
also its the way your calculator find roots of functions
## legitimate problems
it seems like something like pylance cant hold up against super complex gigantic modules like scipy, numpy or sympy.
also then they dont type hint their functions? or they do? but whatever the fuck
+266
View File
@@ -0,0 +1,266 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# i still hate this so much\n",
"\n",
"i kinda never liked the syms thing from matlab, probably because i dont understand!!!!\n",
"\n",
"why do some fns declare like a variable, but some others you have to `[o] = function(i) end` ???? \n",
"AND THEN WTF IS @(x)?????????\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## here is an explanation.\n",
"\n",
"imagine this. you import sympy:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "shellscript"
}
},
"outputs": [],
"source": [
"pip install sympy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import sympy\n",
"import numpy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"sympy provides a Symbol class:\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"x_lda = sympy.Symbol(\"x\")\n",
"y_lda = sympy.Symbol(\"y\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"now here is funny: what you just made are symbols. it has proper `__magic__` functions that do things different than a number. check this out:\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle x^{3} - x - 2$"
],
"text/plain": [
"x**3 - x - 2"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f_lda = x_lda**3 - x_lda - 2\n",
"f_lda"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"fancy ass renders from jupyter aside, this thing is now a sympy something construct. it is NOT a function. it is a variable. it is... certainly something.\n",
"\n",
"### symbolic computation\n",
"\n",
"[sympy tutorial][st] explains: [mathematical expressions with unevaluated variables are left in symbolic form.][expl]\n",
"\n",
"i explain: you do math on paper: you dont write the decimal number out, you just write the final answer as $\\frac{\\sqrt 3 + 1}{2}$\n",
"\n",
"so the sympy does the same: it writes that instead of idfk `1.3660254038`\n",
"\n",
"[st]: https://docs.sympy.org/latest/tutorials/intro-tutorial/intro.html\n",
"[expl]: https://docs.sympy.org/latest/tutorials/intro-tutorial/intro.html#:~:text=mathematical%20expressions%20with%20unevaluated%20variables%20are%20left%20in%20symbolic%20form.\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{1 + \\sqrt{3}}{2}$"
],
"text/plain": [
"(1 + sqrt(3))/2"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"root_idk = sympy.factor((sympy.sqrt(3) + 1) / 2)\n",
"\n",
"numpy.testing.assert_approx_equal(root_idk.evalf(), (3**0.5 + 1) / 2) # passes!\n",
"root_idk"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"you see in mat lab they pass this kinda sympy symbolic ahh structure Literally everywhere. i fear that. that is why i dont like this.\n",
"\n",
"from [matlab]:\n",
"\n",
"[matlab]: https://www.mathworks.com/help/symbolic/create-symbolic-numbers-variables-and-expressions.html#:~:text=Solve%20the%20quadratic,f%20%3D%3D%200%2Cx\n",
"\n",
"> Solve the quadratic equation f=0 for x by using solve.\n",
">\n",
"> ```matlab\n",
"> x_0 = solve(f == 0,x)\n",
"> ```\n",
"\n",
"well you can do that with sympy too:\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(-1/2 - sqrt(3)*I/2)*(sqrt(78)/9 + 1)**(1/3) + 1/(3*(-1/2 - sqrt(3)*I/2)*(sqrt(78)/9 + 1)**(1/3)) -0.760689853402284 - 0.857873626595179*I\n",
"1/(3*(-1/2 + sqrt(3)*I/2)*(sqrt(78)/9 + 1)**(1/3)) + (-1/2 + sqrt(3)*I/2)*(sqrt(78)/9 + 1)**(1/3) -0.760689853402284 + 0.857873626595179*I\n",
"1/(3*(sqrt(78)/9 + 1)**(1/3)) + (sqrt(78)/9 + 1)**(1/3) 1.52137970680457\n"
]
}
],
"source": [
"for i in sympy.solve(f_lda):\n",
" print(i, i.evalf())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"god damn\n",
"\n",
"### no need (sometimes)\n",
"\n",
"well thats cool and all if the thing doesnt start to rely on numerical solves: check [this][ssp] out\n",
"\n",
"[ssp]: https://docs.sympy.org/latest/modules/solvers/solvers.html#sympy.solvers.solvers.solve:~:text=Currently%20supported%3A\n",
"\n",
"and then when that happen youd rather:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" import scipy.optimize\n",
"except:\n",
" # %pip install scipy\n",
" get_ipython().run_line_magic('pip', 'install scipy') # prev line resolves to this so i use # why is pylance ass\n",
" import scipy.optimize"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.float64(1.5213797068045676)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f_lda_py = lambda x: x**3 - x - 2\n",
"scipy_res = scipy.optimize.root(f_lda_py, 1)\n",
"scipy_res.x[0] # so much notation to extract the one real root lmfao"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"look: just make a python function? and use an nsolve if you plan to use nsolve anyway? why bother yourself with symbols when you never cares about symbols in your answer?\n",
"\n",
"what if i never had a chance to get a concrete answer like this: $\\frac{1}{3\\left(\\frac{\\sqrt{78}}{9} + 1\\right)^{\\frac{1}{3}}} + \\left(\\frac{\\sqrt{78}}{9} + 1\\right)^{\\frac{1}{3}}$? and i dont want to see such shitass anyway. \n",
"a number like this: `1.5213797068045676` works way better\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}