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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "raw",
|
| 5 |
+
"id": "8cf32803",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"---\n",
|
| 9 |
+
"title: Artificial Calculus Teacher\n",
|
| 10 |
+
"description: Generates Derivative and Integral Expressions\n",
|
| 11 |
+
"show-code : False\n",
|
| 12 |
+
"---"
|
| 13 |
+
]
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"cell_type": "code",
|
| 17 |
+
"execution_count": 24,
|
| 18 |
+
"id": "108761f9",
|
| 19 |
+
"metadata": {
|
| 20 |
+
"scrolled": false
|
| 21 |
+
},
|
| 22 |
+
"outputs": [
|
| 23 |
+
{
|
| 24 |
+
"data": {
|
| 25 |
+
"text/latex": [
|
| 26 |
+
"$\\displaystyle \\frac{d}{d x} \\left(- \\sqrt[3]{x} + \\sin{\\left(x \\right)}\\right)$"
|
| 27 |
+
],
|
| 28 |
+
"text/plain": [
|
| 29 |
+
"Derivative(-x**(1/3) + sin(x), x)"
|
| 30 |
+
]
|
| 31 |
+
},
|
| 32 |
+
"metadata": {},
|
| 33 |
+
"output_type": "display_data"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"data": {
|
| 37 |
+
"text/latex": [
|
| 38 |
+
"$\\displaystyle \\int \\left(2 x + \\frac{1}{x^{2}}\\right)\\, dx$"
|
| 39 |
+
],
|
| 40 |
+
"text/plain": [
|
| 41 |
+
"Integral(2*x + x**(-2), x)"
|
| 42 |
+
]
|
| 43 |
+
},
|
| 44 |
+
"metadata": {},
|
| 45 |
+
"output_type": "display_data"
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"name": "stdout",
|
| 49 |
+
"output_type": "stream",
|
| 50 |
+
"text": [
|
| 51 |
+
"\n",
|
| 52 |
+
"\n",
|
| 53 |
+
"\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"\n",
|
| 56 |
+
"\n",
|
| 57 |
+
"\n",
|
| 58 |
+
"\n",
|
| 59 |
+
"\n",
|
| 60 |
+
"\n"
|
| 61 |
+
]
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"data": {
|
| 65 |
+
"text/latex": [
|
| 66 |
+
"$\\displaystyle \\frac{d}{d x} \\left(- \\sqrt[3]{x} + \\sin{\\left(x \\right)}\\right) = \\cos{\\left(x \\right)} - \\frac{1}{3 x^{\\frac{2}{3}}}$"
|
| 67 |
+
],
|
| 68 |
+
"text/plain": [
|
| 69 |
+
"Eq(Derivative(-x**(1/3) + sin(x), x), cos(x) - 1/(3*x**(2/3)))"
|
| 70 |
+
]
|
| 71 |
+
},
|
| 72 |
+
"metadata": {},
|
| 73 |
+
"output_type": "display_data"
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"data": {
|
| 77 |
+
"text/latex": [
|
| 78 |
+
"$\\displaystyle \\int \\left(2 x + \\frac{1}{x^{2}}\\right)\\, dx = \\frac{x^{3} - 1}{x}$"
|
| 79 |
+
],
|
| 80 |
+
"text/plain": [
|
| 81 |
+
"Eq(Integral(2*x + x**(-2), x), (x**3 - 1)/x)"
|
| 82 |
+
]
|
| 83 |
+
},
|
| 84 |
+
"metadata": {},
|
| 85 |
+
"output_type": "display_data"
|
| 86 |
+
}
|
| 87 |
+
],
|
| 88 |
+
"source": [
|
| 89 |
+
"from sympy import sin,cos,log,exp,tan,Function,Derivative,Eq,Integral,Rational\n",
|
| 90 |
+
"from sympy import factor_terms,simplify, sqrt, cbrt\n",
|
| 91 |
+
"from sympy.abc import x\n",
|
| 92 |
+
"import random\n",
|
| 93 |
+
"f = Function('f')\n",
|
| 94 |
+
"g = Function('g')\n",
|
| 95 |
+
"h = Function('h')\n",
|
| 96 |
+
"def random_math(x):\n",
|
| 97 |
+
" allowed_values = list(range(-2, 2))\n",
|
| 98 |
+
" allowed_values.remove(0)\n",
|
| 99 |
+
" random_value = random.choice(allowed_values)\n",
|
| 100 |
+
" random_value2 = random.choice(allowed_values)\n",
|
| 101 |
+
" def power_function(x): \n",
|
| 102 |
+
" return x**(Rational(random_value,random_value2))\n",
|
| 103 |
+
" def scalar_function(x):\n",
|
| 104 |
+
" return x*random_value\n",
|
| 105 |
+
" def addSUBTR_function(x): \n",
|
| 106 |
+
" return x+random_value\n",
|
| 107 |
+
" funs = [sin,power_function,log,exp,cos,tan,sqrt,cbrt,\n",
|
| 108 |
+
" scalar_function,addSUBTR_function] \n",
|
| 109 |
+
" operations = [f(g(x)),f(x)+g(x),f(x)-g(x),f(x)/g(x),f(x)*g(x),\n",
|
| 110 |
+
" f(g(h(x))),f(h(x))+g(x),f(h(x))-g(x),f(h(x))/g(x),f(x)/g(h(x)),f(h(x))*g(x)]\n",
|
| 111 |
+
" operation = operations[random.randrange(0,len(operations))]\n",
|
| 112 |
+
" return [[[operation.replace(f, i) for i in funs][random.randrange(0,len(funs))].replace(g, i) for i in funs]\\\n",
|
| 113 |
+
"[random.randrange(0,len(funs))].replace(h, i) for i in funs][random.randrange(0,len(funs))]\n",
|
| 114 |
+
"\n",
|
| 115 |
+
"setup1 = random_math(x)\n",
|
| 116 |
+
"setup2 = random_math(x)\n",
|
| 117 |
+
"practice1 = Derivative(simplify(setup1),x)\n",
|
| 118 |
+
"practice2 = Integral(simplify(setup2),x)\n",
|
| 119 |
+
"p1eq = Eq(practice1,practice1.doit(),evaluate=False)\n",
|
| 120 |
+
"p2eq = Eq(practice2,practice2.doit().simplify(),evaluate=False)\n",
|
| 121 |
+
"if setup1 != 0: \n",
|
| 122 |
+
" display(p1eq.lhs)\n",
|
| 123 |
+
"if str(factor_terms(p2eq.lhs)) != str(p2eq.rhs): \n",
|
| 124 |
+
" if str(p2eq).find(\"Ei\") == -1 and str(p2eq).find(\"gamma\") == -1 and str(p2eq).find(\"Piecewise\") == -1\\\n",
|
| 125 |
+
" and str(p2eq).find(\"li\") == -1 and str(p2eq).find(\"erf\") == -1 and str(p2eq).find(\"atan\") == -1\\\n",
|
| 126 |
+
" and str(p2eq).find(\"Si\") == -1 and str(p2eq).find(\"Ci\") == -1 and str(p2eq).find(\"hyper\") == -1\\\n",
|
| 127 |
+
" and str(p2eq).find(\"fresnel\") == -1 and str(p2eq).find(\"Li\") == -1: \n",
|
| 128 |
+
" display(p2eq.lhs)\n",
|
| 129 |
+
" else:\n",
|
| 130 |
+
" print(\"Error: Complex Integral\")\n",
|
| 131 |
+
" pass\n",
|
| 132 |
+
"\n",
|
| 133 |
+
"else:\n",
|
| 134 |
+
" print(\"Error: Impossible Integral\") \n",
|
| 135 |
+
" pass\n",
|
| 136 |
+
" \n",
|
| 137 |
+
"for i in range(0,5):\n",
|
| 138 |
+
" print(\"\\n\")\n",
|
| 139 |
+
"display(p1eq)\n",
|
| 140 |
+
"display(p2eq)"
|
| 141 |
+
]
|
| 142 |
+
},
|
| 143 |
+
{
|
| 144 |
+
"cell_type": "markdown",
|
| 145 |
+
"id": "493bdcbe",
|
| 146 |
+
"metadata": {},
|
| 147 |
+
"source": [
|
| 148 |
+
"*Credits to https://people.math.harvard.edu/~knill/teaching/math1a_2011/handouts/46-ai.pdf for inspiration*\n",
|
| 149 |
+
"\n",
|
| 150 |
+
"*Thanks to @smichr for .replace suggestion https://stackoverflow.com/a/73000728/17291132*\n",
|
| 151 |
+
"\n",
|
| 152 |
+
"**Notebook by github.com/nsc9 - MIT License**\n",
|
| 153 |
+
"\n",
|
| 154 |
+
"Donate by sending Bitcoin (BTC) to address: **bc1qtawr2gw52ftufzu0r3r20pnj3vmynssxs0mjl4**"
|
| 155 |
+
]
|
| 156 |
+
}
|
| 157 |
+
],
|
| 158 |
+
"metadata": {
|
| 159 |
+
"kernelspec": {
|
| 160 |
+
"display_name": "Python 3 (ipykernel)",
|
| 161 |
+
"language": "python",
|
| 162 |
+
"name": "python3"
|
| 163 |
+
},
|
| 164 |
+
"language_info": {
|
| 165 |
+
"codemirror_mode": {
|
| 166 |
+
"name": "ipython",
|
| 167 |
+
"version": 3
|
| 168 |
+
},
|
| 169 |
+
"file_extension": ".py",
|
| 170 |
+
"mimetype": "text/x-python",
|
| 171 |
+
"name": "python",
|
| 172 |
+
"nbconvert_exporter": "python",
|
| 173 |
+
"pygments_lexer": "ipython3",
|
| 174 |
+
"version": "3.8.10"
|
| 175 |
+
}
|
| 176 |
+
},
|
| 177 |
+
"nbformat": 4,
|
| 178 |
+
"nbformat_minor": 5
|
| 179 |
+
}
|