This commit is contained in:
lda committed 2025-02-01 02:55:26 +07:00
1 parent 25a9db6346
commit d3ca75fa45
2 files changed
+99 -14

No files matched your search

+1 -1
View File
@@ -10,4 +10,4 @@ also its the way your calculator find roots of functions
it seems like something like pylance cant hold up against super complex gigantic modules like scipy, numpy or sympy. 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 pylance dont fw jupyter notebook is what i can say
+98 -13
View File
@@ -36,7 +36,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -53,7 +53,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 2,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -70,7 +70,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 3,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@@ -82,7 +82,7 @@
"x**3 - x - 2" "x**3 - x - 2"
] ]
}, },
"execution_count": 7, "execution_count": 3,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -112,7 +112,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 19, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@@ -124,7 +124,7 @@
"(1 + sqrt(3))/2" "(1 + sqrt(3))/2"
] ]
}, },
"execution_count": 19, "execution_count": 4,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -157,7 +157,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 21, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@@ -192,7 +192,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -200,13 +200,15 @@
" import scipy.optimize\n", " import scipy.optimize\n",
"except:\n", "except:\n",
" # %pip install scipy\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", " get_ipython().run_line_magic(\n",
" \"pip\", \"install scipy\"\n",
" ) # prev line resolves to this so i use # why is pylance ass\n",
" import scipy.optimize" " import scipy.optimize"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@@ -215,7 +217,7 @@
"np.float64(1.5213797068045676)" "np.float64(1.5213797068045676)"
] ]
}, },
"execution_count": 3, "execution_count": 7,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -223,7 +225,7 @@
"source": [ "source": [
"f_lda_py = lambda x: x**3 - x - 2\n", "f_lda_py = lambda x: x**3 - x - 2\n",
"scipy_res = scipy.optimize.root(f_lda_py, 1)\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" "scipy_res.x[0] # so much notation to extract the one real root lmfao"
] ]
}, },
{ {
@@ -239,7 +241,90 @@
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [] "source": [
"## bisection (pure python cuz i cant work with modules)\n",
"\n",
"bisection step is:\n",
"\n",
"step 1: check low. high, mid\n",
"\n",
"step 1.5: know that high is higher than 0 and low is lower than 0\n",
"\n",
"step 2: if mid is low then mid moves to low \n",
"and if mid is high then mid moves to high\n",
"\n",
"step 3: step 1\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from typing import Callable\n",
"from numbers import Real\n",
"from math import isclose\n",
"\n",
"\n",
"def bis(\n",
" f: Callable[[Real], Real],\n",
" low: Real,\n",
" high: Real,\n",
" *,\n",
" rel_tol: Real = 1e-9,\n",
" abs_tol: Real = 0,\n",
" max_loop: int = 100,\n",
") -> tuple[Real, int]:\n",
" ldaprx = lambda a, b: isclose(a, b, rel_tol=rel_tol, abs_tol=abs_tol)\n",
" steps = 0\n",
" fl = f(low)\n",
" fh = f(high)\n",
" # raise ValueError(f\"at {low =} f(x) is {fl}, at {high =} f(x) is {fh} which has the same sign\")\n",
"\n",
" if ldaprx(0, fl):\n",
" return (low, 0)\n",
" elif ldaprx(0, fh):\n",
" return (high, 0)\n",
"\n",
" while (steps := steps + 1) and (max_loop := max_loop - 1) + 1:\n",
" mid = (low + high) / 2\n",
" match f(mid):\n",
" case x if ldaprx(0, x):\n",
" return (mid, steps)\n",
" case x if x < 0:\n",
" low = mid\n",
" case x if x > 0:\n",
" high = mid\n",
" return (mid, steps)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1.5213797068045676, 52)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bis(f_lda_py, 1, 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"52 loops! damn\n"
]
} }
], ],
"metadata": { "metadata": {