Skip to content

Instantly share code, notes, and snippets.

@fabiogaluppo
Created July 22, 2018 13:43
Show Gist options
  • Save fabiogaluppo/a9c8c2b9a2a576c56dc7ba422a27682a to your computer and use it in GitHub Desktop.
Save fabiogaluppo/a9c8c2b9a2a576c56dc7ba422a27682a to your computer and use it in GitHub Desktop.
From axpy to exp
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def axpy(a, x, y):\n",
" return a * x + y"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"axpy(1, 2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"a, b, c, x = 2, 3, 4, .5"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.5"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a + b * x + c * (x * x)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.5"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"axpy(axpy(c, x, b), x, a)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"a, b, c, x = 62, 13, 45, .75"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a + b * x + c * (x * x) == axpy(axpy(c, x, b), x, a)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"97.0625"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a + b * x + c * (x * x)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def horner(x, an, am, *args):\n",
" result = axpy(an, x, am)\n",
" for arg in args:\n",
" result = axpy(result, x, arg)\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"97.0625"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"horner(x, c, b, a)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def horner2(x, *args):\n",
" lst = list(args)\n",
" lst.reverse()\n",
" an, am, *tail = lst\n",
" return horner(x, an, am, *tail)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"a, b, c, d, x = 62, 13, 45, 88, .45"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"84.9815"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a + b * x + c * (x * x) + d * (x * x * x)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"84.9815"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"horner2(x, a, b, c, d)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"89.2297475"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"horner2(x, a, b, c, d, 100, 8)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# https://en.wikipedia.org/wiki/Polynomial_regression#Definition_and_example\n",
"# \"In general, we can model the expected value of y as an nth degree polynomial, \n",
"# yielding the general polynomial regression model\"\n",
"def simple_polynomial_regression(x, e, *betas):\n",
" return horner2(x, *betas) + e"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"84.9815"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"simple_polynomial_regression(x, 0, a, b, c, d)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"85.92517176545691"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"simple_polynomial_regression(x, random.random(), a, b, c, d)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"85.87878414767113"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"simple_polynomial_regression(x, random.random(), a, b, c, d)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"85.57568393572652"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"simple_polynomial_regression(x, random.random(), a, b, c, d)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"ys = [simple_polynomial_regression(x, 1, 2, -3, 1) for x in np.arange(-1.5, 3.5, 0.25)]"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[9.75,\n",
" 8.3125,\n",
" 7.0,\n",
" 5.8125,\n",
" 4.75,\n",
" 3.8125,\n",
" 3.0,\n",
" 2.3125,\n",
" 1.75,\n",
" 1.3125,\n",
" 1.0,\n",
" 0.8125,\n",
" 0.75,\n",
" 0.8125,\n",
" 1.0,\n",
" 1.3125,\n",
" 1.75,\n",
" 2.3125,\n",
" 3.0,\n",
" 3.8125]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ys"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"def to_mathematica_plot(ys):\n",
" return \"ListPlot[{\" + str(ys)[1:-1] + \"}]\""
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ListPlot[{9.75, 8.3125, 7.0, 5.8125, 4.75, 3.8125, 3.0, 2.3125, 1.75, 1.3125, 1.0, 0.8125, 0.75, 0.8125, 1.0, 1.3125, 1.75, 2.3125, 3.0, 3.8125}]'"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# http://home.iitk.ac.in/~shalab/regression/Chapter12-Regression-PolynomialRegression.pdf\n",
"# \"Polynomial regression model in one variable and is called as second order model or quadratic model\"\n",
"# http://reference.wolfram.com/language/tutorial/PlottingListsOfData.html\n",
"to_mathematica_plot(ys)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ListPlot[{-8.875, -7.09375, -5.5, -4.09375, -2.875, -1.84375, -1.0, -0.34375, 0.125, 0.40625, 0.5, 0.40625, 0.125, -0.34375, -1.0, -1.84375, -2.875, -4.09375, -5.5, -7.09375, -8.875, -10.84375}]'"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"to_mathematica_plot([simple_polynomial_regression(x, 1, -2, 3, -1.5) for x in np.arange(-1.5, 4, 0.25)])"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"import functools\n",
"import operator"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"def factorial(n):\n",
" if n == 0 or n == 1:\n",
" return 1\n",
" return functools.reduce(operator.mul, range(1, n + 1))"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 6, 24, 120, 720, 5040]"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[factorial(i) for i in range(1, 8)]"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1.0,\n",
" 0.5,\n",
" 0.16666666666666666,\n",
" 0.041666666666666664,\n",
" 0.008333333333333333,\n",
" 0.001388888888888889,\n",
" 0.0001984126984126984]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[1. / factorial(i) for i in range(1, 8)]"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"def reciprocals_of_factorial(n):\n",
" return [1. / factorial(i) for i in range(0, n)]"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1.0,\n",
" 1.0,\n",
" 0.5,\n",
" 0.16666666666666666,\n",
" 0.041666666666666664,\n",
" 0.008333333333333333,\n",
" 0.001388888888888889]"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reciprocals_of_factorial(7)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"# https://en.wikipedia.org/wiki/Taylor_series#Exponential_function\n",
"# Taylor series for the exponential function = 1 + x/1! + x^2/2! + x^3/3! + x^4/4! + x^5/5! + ...\n",
"def exp(x):\n",
" return horner2(x, *tuple(reciprocals_of_factorial(15)))"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2.7182818284582297,\n",
" 7.389056070325911,\n",
" 20.085523458126694,\n",
" 54.59706179769671,\n",
" 148.37958007973666]"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[exp(i+1) for i in range(5)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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.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