Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiogaluppo/83c95c2d64d97ae3f3ab13820f77be48 to your computer and use it in GitHub Desktop.
Save fabiogaluppo/83c95c2d64d97ae3f3ab13820f77be48 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from sympy import *\n",
"from functools import lru_cache"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"x = Symbol('x')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def f(func):\n",
"\treturn func"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1/(2*x + 1)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df0 = f(1/(2*x + 1))\n",
"df0"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def d(func, variable = Symbol('x'), n = 1):\n",
"\treturn diff(func, variable, n)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1/(2*x + 1),\n",
" -2/(2*x + 1)**2,\n",
" 8/(2*x + 1)**3,\n",
" -48/(2*x + 1)**4,\n",
" 384/(2*x + 1)**5)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1 = d(df0)\n",
"df2 = d(df1)\n",
"df3 = d(df2)\n",
"df4 = d(df3)\n",
"df0, df1, df2, df3, df4"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def to_python_function(func, *variables):\n",
"\tif len(variables) == 0:\n",
"\t\treturn lambdify(Symbol('x'), func)\n",
"\treturn lambdify(tuple(variables), func)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1.0, -2.0, 8.0, -48.0, 384.0)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"DF0 = to_python_function(df0)\n",
"DF1 = to_python_function(df1)\n",
"DF2 = to_python_function(df2)\n",
"DF3 = to_python_function(df3)\n",
"DF4 = to_python_function(df4)\n",
"DF0(0), DF1(0), DF2(0), DF3(0), DF4(0)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"t*(-v0 + v1) + v0"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v0, v1, t = Symbol('v0'), Symbol('v1'), Symbol('t')\n",
"lerp = f(v0 + t * (v1 - v0))\n",
"lerp"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.5, 0.6, 16.37)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"LERP = to_python_function(lerp, v0, v1, t)\n",
"LERP(0, 1, 0.5), LERP(0, 1, 0.6), LERP(12, 23.5, 0.38)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{t, v0, v1}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lerp.free_symbols"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def get_variables(func):\n",
"\tfree_symbols = list(func.free_symbols)\n",
"\tfree_symbols.sort(key = lambda s: str(s))\n",
"\treturn tuple(free_symbols)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(t, v0, v1)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_variables(lerp)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# https://www.mathsisfun.com/algebra/completing-square.html\n",
"def completing_the_square(expr):\n",
"\tvs = get_variables(expr)\n",
"\tif (len(vs) != 1):\n",
"\t\traise Exception('expr must have just 1 variable')\n",
"\tpoly = Poly(expr)\n",
"\tif (degree(poly) != 2):\n",
"\t\traise Exception('expr must be degree 2')\n",
"\ta, b, c = poly.all_coeffs()\n",
"\tif (a != 1):\n",
"\t\traise Exception('coefficient of degree 2 must be equal to 1')\n",
"\td = b / 2\n",
"\te = c - d**2\n",
"\tvariable = vs[0]\n",
"\treturn (variable + d)**2 + e"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(x + 3)**2 - 2"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"completing_the_square(x**2 + 6*x + 7)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"x**2 + 6*x + 7"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"expand(completing_the_square(x**2 + 6*x + 7))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(x + 6)**2 - 28"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"completing_the_square(x**2 + 12*x + 8)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"x**2 + 12*x + 8"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"expand(completing_the_square(x**2 + 12*x + 8))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# https://www.codecogs.com/latex/eqneditor.php"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"\\begin{align}\n",
"\\sum_{k=0}^{n}\\frac{f^{(k)}(x)}{k!}x^{k}\n",
"\\end{align}"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%latex\n",
"\\begin{align}\n",
"\\sum_{k=0}^{n}\\frac{f^{(k)}(x)}{k!}x^{k}\n",
"\\end{align}"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"def compute_derivatives(func, n):\n",
"\tvs = get_variables(func)\n",
"\tif (len(vs) != 1):\n",
"\t\traise Exception('func must have just 1 variable')\n",
"\tvariable = vs[0]\n",
"\tf = func\n",
"\tdfs = list()\n",
"\tdfs.append(f)\n",
"\tfor i in range(1, n + 1):\n",
"\t\tdfs.append(d(dfs[i - 1], variable))\n",
"\tDFS = [to_python_function(df) for df in dfs]\n",
"\treturn dfs, DFS"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1/(2*x + 1), -2/(2*x + 1)**2, 8/(2*x + 1)**3, -48/(2*x + 1)**4]"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dfs, DFS = compute_derivatives(1/(2*x + 1), 3)\n",
"dfs"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1.0, -2.0, 8.0, -48.0)"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"DF0, DF1, DF2, DF3 = DFS\n",
"DF0(0), DF1(0), DF2(0), DF3(0)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1/(2*y + 1), -2/(2*y + 1)**2, 8/(2*y + 1)**3, -48/(2*y + 1)**4]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dfs, _ = compute_derivatives(1/(2*Symbol('y') + 1), 3)\n",
"dfs"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"def compute_dividends(func, n):\n",
"\tf = func\n",
"\t_, DFS = compute_derivatives(f, n)\n",
"\treturn np.array([DF(0) for DF in DFS])"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"\\begin{align}\n",
"n!= \n",
"\\begin{cases}\n",
"1 & \\text{ if } x=0 \\, \\vee \\, x=1 \\\\ \n",
"2 & \\text{ if } x=2 \\\\\n",
"n(n-1)! & \\text{ }\n",
"\\end{cases}\n",
"\\end{align}"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%latex\n",
"\\begin{align}\n",
"n!= \n",
"\\begin{cases}\n",
"1 & \\text{ if } n=0 \\, \\vee \\, n=1 \\\\ \n",
"2 & \\text{ if } n=2 \\\\\n",
"n(n-1)! & \\text{ }\n",
"\\end{cases}\n",
"\\end{align}"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"@lru_cache(maxsize = 100)\n",
"def factorial(n):\n",
"\tif n == 0: return 1\n",
"\telif n == 1: return 1\n",
"\telif n == 2: return 2\n",
"\telse: return n * factorial(n - 1)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., -2., 8., -48., 384.])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_dividends(1/(2*x + 1), 4)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3628800, 24, 1405006117752879898543142606244511569936384000000000, 5040)"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"factorial(10), factorial(4), factorial(42), factorial(7)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"def compute_divisors(n):\n",
"\treturn np.array([factorial(i) for i in range(n + 1)])"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 1, 2, 6, 24])"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_divisors(4)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"def compute_Taylor_and_MacLaurin_series(func, n):\n",
"\tdef compute_coefficients(func, n):\n",
"\t\treturn compute_dividends(func, n) / compute_divisors(n)\n",
"\treturn compute_coefficients(func, n)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., -2., 4., -8., 16.])"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_Taylor_and_MacLaurin_series(1/(2*x + 1), 4)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., -1., 1., -1., 1., -1.])"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_Taylor_and_MacLaurin_series(1/(1 + x), 5)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1., 1., 1., 1., 1., 1.])"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_Taylor_and_MacLaurin_series(1/(1 - x), 5)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 0., 1., 3., 6., 10., 15., 21., 28.])"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_Taylor_and_MacLaurin_series(x**2/(1 - x)**3, 8)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0. , 1. , 0.5 , 0.33, 0.25, 0.2 , 0.17, 0.14, 0.12])"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.round(compute_Taylor_and_MacLaurin_series(ln(1/(1 - x)), 8), 2)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"def compute_partial_derivatives(func, n = 1):\n",
"\tf = func\n",
"\tvs = get_variables(f)\n",
"\tdvs = [d(f, v, n) for v in vs]\n",
"\tDVS = [to_python_function(dv, *get_variables(dv)) for dv in dvs]\n",
"\treturn list(vs), dvs, DVS"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[t, v0, v1]"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vs, dvs, DVS = compute_partial_derivatives(lerp)\n",
"vs"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[-v0 + v1, -t + 1, t]"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dvs"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, -1, 2)"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"DV0, DV1, DV2 = DVS[0], DVS[1], DVS[2]\n",
"DV0(1, 2), DV1(2), DV2(2)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"@lru_cache(maxsize = 100)\n",
"def comb(n, k):\n",
"\tif (n < k):\n",
"\t\traise Exception('k must be lesser than n')\n",
"\treturn factorial(n) / (factorial(n - k) * factorial(k))"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"\\begin{align}\n",
"\\binom{3}{2}\n",
"\\end{align}"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%latex\n",
"\\begin{align}\n",
"\\binom{3}{2}\n",
"\\end{align}"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.0"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"comb(3, 2)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"\\begin{align}\n",
"\\binom{5}{2}\n",
"\\end{align}"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%latex\n",
"\\begin{align}\n",
"\\binom{5}{2}\n",
"\\end{align}"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10.0"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"comb(5, 2)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"# https://www.mathsisfun.com/pascals-triangle.html\n",
"def pascal_triangle(n):\n",
"\treturn [comb(n, k) for k in range(n + 1)]"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([1.0], 1.0)"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pascal_triangle(0), sum(pascal_triangle(0))"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([1.0, 1.0], 2.0)"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pascal_triangle(1), sum(pascal_triangle(1))"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([1.0, 2.0, 1.0], 4.0)"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pascal_triangle(2), sum(pascal_triangle(2))"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([1.0, 3.0, 3.0, 1.0], 8.0)"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pascal_triangle(3), sum(pascal_triangle(3))"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([1.0, 4.0, 6.0, 4.0, 1.0], 16.0)"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pascal_triangle(4), sum(pascal_triangle(4))"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([1.0, 5.0, 10.0, 10.0, 5.0, 1.0], 32.0)"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pascal_triangle(5), sum(pascal_triangle(5))"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"def pascal_triangle_symbolic(n):\n",
"\tp = pascal_triangle(n)\n",
"\tx = Symbol('x')\n",
"\treturn sum([p[i] * x**i for i in range(len(p))])"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"\\begin{align}\n",
"\\binom{4}{0}+\\binom{4}{1}x+\\binom{4}{2}x^{2}+\\binom{4}{3}x^{3}+\\binom{4}{4}x^{4}\n",
"\\end{align}"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%latex\n",
"\\begin{align}\n",
"\\binom{4}{0}+\\binom{4}{1}x+\\binom{4}{2}x^{2}+\\binom{4}{3}x^{3}+\\binom{4}{4}x^{4}\n",
"\\end{align}"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0*x**4 + 4.0*x**3 + 6.0*x**2 + 4.0*x + 1.0"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pascal_triangle_symbolic(4)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"def counting_with_gf(expr, *exprs):\n",
"\tdef check_expression(expr, variable):\n",
"\t\tvs = get_variables(expr)\n",
"\t\tif (len(vs) != 1):\n",
"\t\t\traise Exception('expression must have just 1 variable')\n",
"\t\tif (vs[0] != variable):\n",
"\t\t\traise Exception('expression must have x as variable')\n",
"\t\treturn expr\n",
"\tx = Symbol('x')\n",
"\tacc = check_expression(expr, x)\n",
"\tfor expr in exprs:\n",
"\t\tacc = acc * check_expression(expr, x)\n",
"\treturn expand(acc), Poly(acc, x).coeffs()"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(x + 1)*(x**2 + x + 1)"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(x**0 + x**1 + x**2) * (x**0 + x**1)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"x**3 + 2*x**2 + 2*x + 1"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"expand((x**0 + x**1 + x**2) * (x**0 + x**1))"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 2, 1]"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Poly((x**0 + x**1 + x**2) * (x**0 + x**1), x).coeffs()"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(x + 1, [1, 1])"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"counting_with_gf(x**0 + x**1)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"x**3 + 2*x**2 + 2*x + 1"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gf, cs = counting_with_gf((x**0 + x**1 + x**2), (x**0 + x**1))\n",
"gf"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 2, 1]"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cs"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAADuCAYAAADC3kfBAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAIABJREFUeJzt3Xl4FFW+//H3SWffyb4BCXsgkAABogjiOooCbnOF64agDCrjPjoz15+ODuPoHcfrOioq4Iq4MSIoLkhEHCGELQHCngBZyEr2pJNOn98fHTGGQDpJJ5Xl+3qeeuyuOl31aSbzTeXUqVNKa40QQojexcnoAEIIIRxPirsQQvRCUtyFEKIXkuIuhBC9kBR3IYTohaS4CyFELyTFXQgheiEp7kII0QtJcRdCiF7I2agDBwUF6ejoaKMOL4QQPdK2bduKtNbBrbUzrLhHR0eTmppq1OGFEKJHUkodtaeddMsIIUQvJMVdCCF6IbuLu1LKpJTaoZRa08K2uUqpQqXUzsblNsfGFEII0RZt6XO/B8gAfM+wfaXWelFHwtTX15OdnU1tbW1HdiNEu7i7uxMVFYWLi4vRUYToMLuKu1IqCrgC+Btwf2eFyc7OxsfHh+joaJRSnXUYIU6jtaa4uJjs7GxiYmKMjiNEh9l75v4c8BDgc5Y21yqlpgIHgPu01sebN1BKLQAWAISGhpKcnPyr7X5+fgQGBlJZWWlnLCEcx9XVldLS0tN+LoXoiVot7kqpK4EC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FVrvRq4Wyk1E7AAJcDcTswshBCiFb134jAhhOiF7J04TMYRCiFELyTFXQgheiHDumWUUoXAUUMOLoQQPddArXVwa40MK+5CCCE6j3TLCCFELyTFXQgheiEp7kII0QtJcRdCiF5IirsQQvRCUtyFEKIXkuIuhBC9kBR3IYTohaS4CyFEL/T/AXyv5i2gAKiFAAAAAElFTkSuQmCC\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"interval = np.round(np.arange(-0.9, 0.8, 0.025), 3)\n",
"func1 = lambda x: 1/(1 - x)\n",
"points1 = np.round(np.fromiter((func1(x) for x in interval), interval.dtype), 3)\n",
"plt.plot(points1, label = \"1/(1 - x)\")\n",
"func2 = lambda x: 1 + x + x**2 + x**3 + x**4 + x**5 + x**6 + x**7 + x**8 \n",
"points2 = np.round(np.fromiter((func2(x) for x in interval), interval.dtype), 3)\n",
"plt.plot(points2, label = \"1 + x + x**2 + ... + x**8\")\n",
"plt.legend()\n",
"plt.grid(axis = 'y', linestyle = '-')\n",
"plt.gca().axes.get_xaxis().set_visible(False)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"def gf_builder(expr, *actions):\n",
"\tlhs, rhs = 1, expr\n",
"\tfor action in actions:\n",
"\t\tlhs = lhs + action\n",
"\t\trhs = rhs + expand(action * expr)\n",
"\treturn rhs.removeO() / lhs"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1 + x + x**2 + x**3 + O(x**4)"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"S = 1 + x + x**2 + x**3 + O(x**4)\n",
"S"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"x + x**2 + x**3 + x**4 + O(x**5)"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"expand(x*S)"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(S - expand(x*S)).removeO()"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"\\begin{align}\n",
" S = 1+x+x^{2}+x^{3}+x^{4}+... \\\\\n",
"- xS = 0+x+x^{2}+x^{3}+x^{4}+x^{5}+... \\\\\n",
"----------------- \\\\\n",
"(1-x)S = 1 \\\\\n",
"\\\\\n",
"S=\\frac{1}{1-x}\n",
"\\end{align}"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%latex\n",
"\\begin{align}\n",
" S = 1+x+x^{2}+x^{3}+x^{4}+... \\\\\n",
"- xS = 0+x+x^{2}+x^{3}+x^{4}+x^{5}+... \\\\\n",
"----------------- \\\\\n",
"(1-x)S = 1 \\\\\n",
"\\\\\n",
"S=\\frac{1}{1-x}\n",
"\\end{align}"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1/(-x + 1)"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gf_builder(1 + x + x**2 + x**3 + O(x**4), -x)"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1., 1., 1., 1.])"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_Taylor_and_MacLaurin_series(gf_builder(S, -x), 3)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"\\begin{align}\n",
" S = 1-x+x^{2}-x^{3}+x^{4}+... \\\\\n",
"+ xS = 0+x-x^{2}+x^{3}-x^{4}+x^{5}+... \\\\\n",
"----------------- \\\\\n",
"(1+x)S = 1 \\\\\n",
"\\\\\n",
"S=\\frac{1}{1+x}\n",
"\\end{align}"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%latex\n",
"\\begin{align}\n",
" S = 1-x+x^{2}-x^{3}+x^{4}+... \\\\\n",
"+ xS = 0+x-x^{2}+x^{3}-x^{4}+x^{5}+... \\\\\n",
"----------------- \\\\\n",
"(1+x)S = 1 \\\\\n",
"\\\\\n",
"S=\\frac{1}{1+x}\n",
"\\end{align}"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1/(x + 1)"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gf_builder(1 - x + x**2 - x**3 + O(x**4), x)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"\\begin{align}\n",
" S = 2+x+2x^{2}+2x^{3}+2x^{4}+... \\\\\n",
"- xS = 0+2x+2x^{2}+2x^{3}+2x^{4}+2x^{5}+... \\\\\n",
"-------------------- \\\\\n",
"(1-x)S = 2 \\\\\n",
"\\\\\n",
"S=\\frac{2}{1-x}\n",
"\\end{align}"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%latex\n",
"\\begin{align}\n",
" S = 2+x+2x^{2}+2x^{3}+2x^{4}+... \\\\\n",
"- xS = 0+2x+2x^{2}+2x^{3}+2x^{4}+2x^{5}+... \\\\\n",
"-------------------- \\\\\n",
"(1-x)S = 2 \\\\\n",
"\\\\\n",
"S=\\frac{2}{1-x}\n",
"\\end{align}"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2/(-x + 1)"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gf_builder(2 + 2*x + 2*x**2 + 2*x**3 + O(x**4), -x)"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"\\begin{align}\n",
" S = 3+9x+27x^{2}+81x^{3}+... \\\\\n",
"- 3xS = 0+9x+27x^{2}+81x^{3}+... \\\\\n",
"----------------- \\\\\n",
"(1-3x)S = 3 \\\\\n",
"\\\\\n",
"S=\\frac{3}{1-3x}\n",
"\\end{align}"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%latex\n",
"\\begin{align}\n",
" S = 3+9x+27x^{2}+81x^{3}+... \\\\\n",
"- 3xS = 0+9x+27x^{2}+81x^{3}+... \\\\\n",
"----------------- \\\\\n",
"(1-3x)S = 3 \\\\\n",
"\\\\\n",
"S=\\frac{3}{1-3x}\n",
"\\end{align}"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3/(-3*x + 1)"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gf_builder(3 + 9*x + 27*x**2 + 81*x**3 + O(x**4), -3*x)"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/latex": [
"\\begin{align}\n",
" S = 1+3x+5x^{2}+7x^{3}+9x^{4}+... \\\\\n",
"- xS = 0+x+3x^{2}+5x^{3}+7x^{4}+9x^{5}+... \\\\\n",
"-------------------- \\\\\n",
"(1-x)S = 1+2x+2x^{2}+2x^{3}+2x^{4}+2x^{5}+... \\\\\n",
"\\\\\n",
"S=\\frac{1+\\frac{2x}{1-x}}{1-x}=\\frac{1}{1-x}+\\frac{2x}{(1-x)^{2}}=\\frac{1+x}{(1-x)^{2}}\n",
"\\end{align}"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%latex\n",
"\\begin{align}\n",
" S = 1+3x+5x^{2}+7x^{3}+9x^{4}+... \\\\\n",
"- xS = 0+x+3x^{2}+5x^{3}+7x^{4}+9x^{5}+... \\\\\n",
"-------------------- \\\\\n",
"(1-x)S = 1+2x+2x^{2}+2x^{3}+2x^{4}+2x^{5}+... \\\\\n",
"\\\\\n",
"S=\\frac{1+\\frac{2x}{1-x}}{1-x}=\\frac{1}{1-x}+\\frac{2x}{(1-x)^{2}}=\\frac{1+x}{(1-x)^{2}}\n",
"\\end{align}"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2*x**4 + 2*x**3 + 2*x**2 + 2*x + 1)/(-x + 1)"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gf_builder(1 + 3*x + 5*x**2 + 7*x**3 + 9*x**4 + O(x**5), -x)"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1., 3., 5., 7., 9.])"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_Taylor_and_MacLaurin_series((2*x**4 + 2*x**3 + 2*x**2 + 2*x + 1)/(-x + 1), 4)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1., 3., 5., 7., 9.])"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_Taylor_and_MacLaurin_series((1 + x)/(1 - x)**2, 4)"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., 3., 5., 7., 9., 11., 13., 15., 17., 19., 21.])"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_Taylor_and_MacLaurin_series((1 + x)/(1 - x)**2, 10)"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"\\begin{align}\n",
"A(n) = \\begin{cases}\n",
"1 & \\text{ if } x=0 \\\\ \n",
"3 & \\text{ if } x=1 \\\\\n",
"3A(n-1)- 2A(n-2) & \\text{ }\n",
"\\end{cases}\n",
"\\end{align}"
],
"text/plain": [
"<IPython.core.display.Latex object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%latex\n",
"\\begin{align}\n",
"A(n) = \\begin{cases}\n",
"1 & \\text{ if } n=0 \\\\ \n",
"3 & \\text{ if } n=1 \\\\\n",
"3A(n-1)- 2A(n-2) & \\text{ }\n",
"\\end{cases}\n",
"\\end{align}"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [],
"source": [
"@lru_cache(maxsize = 100)\n",
"def A(n):\n",
"\tif n == 0: return 1\n",
"\telif n == 1: return 3\n",
"\telse: return 3 * A(n - 1) - 2 * A(n - 2)"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 3, 7, 15, 31, 63, 127, 255, 511)"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A(0), A(1), A(2), A(3), A(4), A(5), A(6), A(7), A(8)"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1/(2*x**2 - 3*x + 1)"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gfA = gf_builder(1 + 3*x + 7*x**2 + 15*x**3 + 31*x**4 + 63*x**5 + O(x**6), -3*x, 2*x**2)\n",
"gfA"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., 3., 7., 15., 31., 63., 127., 255., 511.])"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"compute_Taylor_and_MacLaurin_series(gfA, 8)"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1 + x + x**2 + x**3 + x**4 + x**5 + O(x**6)"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fps(1/(1-x)).truncate()"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1 - 2*x + 4*x**2 - 8*x**3 + 16*x**4 - 32*x**5 + O(x**6)"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fps(1/(2*x+1)).truncate()"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1 - x + x**2 - x**3 + x**4 - x**5 + O(x**6)"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fps(1/(1+x)).truncate()"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"x**2 + 3*x**3 + 6*x**4 + 10*x**5 + O(x**6)"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fps(x**2/(1-x)**3).truncate()"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"x**2 + 3*x**3 + 6*x**4 + 10*x**5 + 15*x**6 + 21*x**7 + 28*x**8 + O(x**9)"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fps(x**2/(1-x)**3).truncate(9)"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[28, 21, 15, 10, 6, 3, 1, 0, 0]"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Poly(fps(x**2/(1-x)**3).truncate(9).removeO()).all_coeffs()"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 0, 1, 3, 6, 10, 15, 21, 28]"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Poly(fps(x**2/(1-x)**3).truncate(9).removeO()).all_coeffs()[::-1]"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., 1., 2., 3., 5., 8., 13., 21., 34., 55., 89.])"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# http://discrete.openmathbooks.org/dmoi2/section-27.html\n",
"# https://aofa.cs.princeton.edu/30gf/\n",
"compute_Taylor_and_MacLaurin_series(1/(1-x-x**2), 10)"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Poly(fps(1/(1-x-x**2)).truncate(11).removeO()).all_coeffs()[::-1]"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1 + x + x**2 + x**3 + x**4 + x**5 + x**6 + x**7 + x**8 + x**9 + x**10 + O(x**11)"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fps(1/(1-x-x**2)).truncate(11)"
]
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment