{ "cells": [ { "cell_type": "raw", "id": "8c69730d", "metadata": {}, "source": [ "---\n", "title: Artificial Calculus Teacher\n", "description: Generates Derivative and Integral Expressions\n", "show-code : False\n", "---" ] }, { "cell_type": "code", "execution_count": 1, "id": "c5197005", "metadata": {}, "outputs": [], "source": [ "# changelog: title, removed cbrt" ] }, { "cell_type": "code", "execution_count": 5, "id": "108761f9", "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Solve\n" ] }, { "data": { "text/latex": [ "$\\displaystyle \\frac{d}{d x} \\left(x + 2\\right) \\tan{\\left(x \\right)}$" ], "text/plain": [ "Derivative((x + 2)*tan(x), x)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$\\displaystyle \\int \\left(\\sqrt{x} + 2 x\\right)\\, dx$" ], "text/plain": [ "Integral(sqrt(x) + 2*x, x)" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Solutions:\n" ] }, { "data": { "text/latex": [ "$\\displaystyle \\frac{d}{d x} \\left(x + 2\\right) \\tan{\\left(x \\right)} = \\left(x + 2\\right) \\sec^{2}{\\left(x \\right)} + \\tan{\\left(x \\right)}$" ], "text/plain": [ "Eq(Derivative((x + 2)*tan(x), x), (x + 2)*sec(x)**2 + tan(x))" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$\\displaystyle \\int \\left(\\sqrt{x} + 2 x\\right)\\, dx = \\frac{2 x^{\\frac{3}{2}}}{3} + x^{2}$" ], "text/plain": [ "Eq(Integral(sqrt(x) + 2*x, x), 2*x**(3/2)/3 + x**2)" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "-----------------------------------------------------------------------------------------------------------\n" ] } ], "source": [ "from sympy.simplify.fu import TR22\n", "from sympy import sin,cos,ln,exp,tan,Function,Derivative,Eq,Integral\n", "from sympy import factor_terms,simplify, sqrt\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, 6))\n", " random_value = random.choice(allowed_values)\n", " random_value\n", " def power_function(x): \n", " return x**random_value\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,ln,exp,cos,tan,sqrt,\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), f(x)/g(x)/h(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), f(x)*g(x)*h(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", "def random_math2(x):\n", " allowed_values = list(range(2, 6))\n", " random_value = random.choice(allowed_values)\n", " random_value\n", " def power_function(x): \n", " return x**random_value\n", " def scalar_function(x):\n", " return x*random_value\n", " def addSUBTR_function(x): \n", " return x+random_value\n", " \n", " funs = [sin,power_function,ln,exp,cos,tan,sqrt,\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", " 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))]\n", "setup1 = random_math(x)\n", "setup2 = random_math2(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", "print(\"Solve\")\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,6):\n", " print(\"\\n\")\n", "print(\"Solutions:\")\n", "display(TR22(p1eq))\n", "display(TR22(p2eq))\n", "print(\" \")\n", "print(\" \")" ] }, { "cell_type": "markdown", "id": "b3f5b9fb", "metadata": {}, "source": [ "If LaTeX display breaks, refresh the page and run again.\n", "\n", "Helpful resources: https://www.derivative-calculator.net/ & https://www.integral-calculator.com/ \n", "\n", "**Made 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 }