{ "cells": [ { "cell_type": "raw", "id": "8cf32803", "metadata": {}, "source": [ "---\n", "title: Artificial Calculus Teacher\n", "description: Generates Derivative and Integral Expressions\n", "show-code : False\n", "---" ] }, { "cell_type": "code", "execution_count": 24, "id": "108761f9", "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{d}{d x} \\left(- \\sqrt[3]{x} + \\sin{\\left(x \\right)}\\right)$" ], "text/plain": [ "Derivative(-x**(1/3) + sin(x), x)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$\\displaystyle \\int \\left(2 x + \\frac{1}{x^{2}}\\right)\\, dx$" ], "text/plain": [ "Integral(2*x + x**(-2), x)" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, { "data": { "text/latex": [ "$\\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}}}$" ], "text/plain": [ "Eq(Derivative(-x**(1/3) + sin(x), x), cos(x) - 1/(3*x**(2/3)))" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$\\displaystyle \\int \\left(2 x + \\frac{1}{x^{2}}\\right)\\, dx = \\frac{x^{3} - 1}{x}$" ], "text/plain": [ "Eq(Integral(2*x + x**(-2), x), (x**3 - 1)/x)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from sympy import sin,cos,log,exp,tan,Function,Derivative,Eq,Integral,Rational\n", "from sympy import factor_terms,simplify, sqrt, cbrt\n", "from sympy.abc import x\n", "import random\n", "f = Function('f')\n", "g = Function('g')\n", "h = Function('h')\n", "def random_math(x):\n", " allowed_values = list(range(-2, 2))\n", " allowed_values.remove(0)\n", " random_value = random.choice(allowed_values)\n", " random_value2 = random.choice(allowed_values)\n", " def power_function(x): \n", " return x**(Rational(random_value,random_value2))\n", " def scalar_function(x):\n", " return x*random_value\n", " def addSUBTR_function(x): \n", " return x+random_value\n", " funs = [sin,power_function,log,exp,cos,tan,sqrt,cbrt,\n", " scalar_function,addSUBTR_function] \n", " operations = [f(g(x)),f(x)+g(x),f(x)-g(x),f(x)/g(x),f(x)*g(x),\n", " 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", " operation = operations[random.randrange(0,len(operations))]\n", " return [[[operation.replace(f, i) for i in funs][random.randrange(0,len(funs))].replace(g, i) for i in funs]\\\n", "[random.randrange(0,len(funs))].replace(h, i) for i in funs][random.randrange(0,len(funs))]\n", "\n", "setup1 = random_math(x)\n", "setup2 = random_math(x)\n", "practice1 = Derivative(simplify(setup1),x)\n", "practice2 = Integral(simplify(setup2),x)\n", "p1eq = Eq(practice1,practice1.doit(),evaluate=False)\n", "p2eq = Eq(practice2,practice2.doit().simplify(),evaluate=False)\n", "if setup1 != 0: \n", " display(p1eq.lhs)\n", "if str(factor_terms(p2eq.lhs)) != str(p2eq.rhs): \n", " if str(p2eq).find(\"Ei\") == -1 and str(p2eq).find(\"gamma\") == -1 and str(p2eq).find(\"Piecewise\") == -1\\\n", " and str(p2eq).find(\"li\") == -1 and str(p2eq).find(\"erf\") == -1 and str(p2eq).find(\"atan\") == -1\\\n", " and str(p2eq).find(\"Si\") == -1 and str(p2eq).find(\"Ci\") == -1 and str(p2eq).find(\"hyper\") == -1\\\n", " and str(p2eq).find(\"fresnel\") == -1 and str(p2eq).find(\"Li\") == -1: \n", " display(p2eq.lhs)\n", " else:\n", " print(\"Error: Complex Integral\")\n", " pass\n", "\n", "else:\n", " print(\"Error: Impossible Integral\") \n", " pass\n", " \n", "for i in range(0,5):\n", " print(\"\\n\")\n", "display(p1eq)\n", "display(p2eq)" ] }, { "cell_type": "markdown", "id": "493bdcbe", "metadata": {}, "source": [ "*Credits to https://people.math.harvard.edu/~knill/teaching/math1a_2011/handouts/46-ai.pdf for inspiration*\n", "\n", "*Thanks to @smichr for .replace suggestion https://stackoverflow.com/a/73000728/17291132*\n", "\n", "**Notebook by github.com/nsc9 - MIT License**\n", "\n", "Donate by sending Bitcoin (BTC) to address: **bc1qtawr2gw52ftufzu0r3r20pnj3vmynssxs0mjl4**" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }