{ "cells": [ { "cell_type": "raw", "id": "8c69730d", "metadata": {}, "source": [ "---\n", "title: Calculus Problem Generator\n", "description: Generates Derivative and Integral Expressions at https://nsc9.github.io/\n", "show-code : False\n", "---" ] }, { "cell_type": "code", "execution_count": 1, "id": "c5197005", "metadata": {}, "outputs": [], "source": [ "# recent change: Added useful links, Added plaintext version of expressions for easy copy/paste to a solver." ] }, { "cell_type": "markdown", "id": "6bdf2faf", "metadata": {}, "source": [ "# Solve with pen and paper:" ] }, { "cell_type": "code", "execution_count": 2, "id": "108761f9", "metadata": { "scrolled": false }, "outputs": [], "source": [ "from sympy.simplify.fu import TR22,TR2i\n", "from sympy import ln,exp,Function,Derivative,Eq,Integral,factor_terms, sqrt, Symbol,Limit\n", "from sympy import sin,cos,tan,Rational,nsimplify\n", "import random\n", "f = Function('f')\n", "g = Function('g')\n", "h = Function('h')\n", "theta = Symbol('theta')\n", "i = 0\n", "dkeywords = {\"polylog\",\"Ei\",\"gamma\",\"Piecewise\",\"li\",\"erf\",\"Si\",\"Ci\",\"hyper\",\"fresnel\",\"Li\",\"expint\",\"zoo\",\n", "\"nan\",\"oo\",\"abs\",\"re\",\"EulerGamma\", \"sinh\",\"tanh\", \"cosh\",'sign','abs','atan','csc','asin'} \n", "ikeywords = {\"polylog\",\"Ei\",\"gamma\",\"Piecewise\", \"li\", \"erf\", \"atan\", \"Si\", \"Ci\", \"hyper\", \"fresnel\", \"Li\", \n", "\"expint\",\"zoo\", \"nan\", \"oo\",\"EulerGamma\",\"sinh\",\"csc\",\"asin\"}\n", "keywords2 = {\"sin\",\"cos\",\"tan\"}\n", "def random_variable(i):\n", " return Symbol(random.choice([i for i in ['v','t','x','z','y']]), real=True)\n", "def random_value(i):\n", " return random.choice([i for i in range(-10,10) if i not in [0]])\n", "def power(a): \n", " return random_value(i)*a**int(random_value(i)/2)\n", "def scalar(a): \n", " return a*random_value(i) + random_value(i)\n", "def addSUBTR(a): \n", " return a+random_value(i)\n", "def dmain(a):\n", " def random_math(a): \n", " funs = [power,scalar,addSUBTR,power,scalar,addSUBTR,ln,exp,sin,cos,tan,sqrt] \n", " operations = [f(a)*g(a)+h(a), f(a)-g(a)*h(a),f(a)/(g(a)-h(a)), f(a)/(-g(a)+h(a)),\n", " f(a)+g(a)+h(a), f(a)-g(a)-h(a),f(a)+g(a)-h(a), f(a)-g(a)+h(a),\n", " (f(a)*g(a))/h(a), f(a)/(g(a)*h(a)),f(g(h(a))),f(h(a))+g(a),f(h(a))-g(a),\n", " f(h(a))/g(a),f(a)/g(h(a)),f(h(a))*g(a),f(a)*h(a)*g(a)]\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", " return random_math(a)\n", "def imain(a):\n", " def random_math2(a): \n", " funs = [power,scalar,addSUBTR,power,scalar,addSUBTR,ln,exp,sin,cos,tan,sqrt] \n", " operations = [f(g(a)),f(a)+g(a),f(a)-g(a),f(a)/g(a),f(a)*g(a),\n", " f(g(a))**2,2*f(a)+g(a),2*f(a)-g(a),2*f(a)/g(a),2*f(a)*g(a)]\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", " return random_math2(a)" ] }, { "cell_type": "code", "execution_count": 8, "id": "5ddcf6a5", "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle a' = \\frac{d}{d \\theta} \\left(- 3 \\theta + \\tan{\\left(\\theta \\right)} - 14\\right)$" ], "text/plain": [ "Eq(a', Derivative(-3*theta + tan(theta) - 14, theta))" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$\\displaystyle B = \\int 3 \\log{\\left(x \\right)}\\, dx$" ], "text/plain": [ "Eq(B, Integral(3*log(x), x))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "derror = True\n", "def dtest():\n", " global setup1\n", " global derror\n", " global practice1\n", " a = random_variable(i)\n", " setup1 = dmain(a)\n", " practice1 = Derivative(setup1,a) \n", " p1eq = TR22(Eq(practice1,practice1.doit(),evaluate=False))\n", " if any(kw in str(setup1) for kw in keywords2):\n", " setup1 = setup1.replace(a,theta)\n", " practice1 = Derivative(setup1,theta) \n", " p1eq = TR22(Eq(practice1,practice1.doit(),evaluate=False))\n", " if p1eq.rhs != 0 and not any(kw in str(p1eq) for kw in dkeywords):\n", " derror = False\n", " return p1eq\n", "while derror == True: \n", " output1 = dtest()\n", "ierror = True\n", "def itest():\n", " global ierror\n", " global practice2\n", " global setup2\n", " a = random_variable(i)\n", " setup2 = imain(a)\n", " practice2 = Integral(setup2,a) \n", " p2eq = TR22(Eq(practice2,practice2.doit(),evaluate=False))\n", " if str(factor_terms(p2eq.lhs)) != str(factor_terms(p2eq.rhs)) and not any(kw in str(p2eq) for kw in ikeywords)\\\n", " and str(p2eq.lhs) != str(-p2eq.rhs): \n", " if any(kw in str(setup2) for kw in keywords2):\n", " setup2 = setup2.replace(a,theta)\n", " practice2 = Integral(setup2,theta) \n", " p2eq = TR22(Eq(practice2,practice2.doit(),evaluate=False))\n", " ierror = False\n", " return p2eq\n", "while ierror == True:\n", " output2 = itest()\n", "a1 = Symbol(\"a'\")\n", "B = Symbol('B')\n", "eq1 = Eq(a1,nsimplify(output1.lhs))\n", "eq2 = Eq(B,nsimplify(output2.lhs))\n", "eq3 = Eq(a1,nsimplify(output1.rhs))\n", "eq4 = Eq(B,nsimplify(output2.rhs))\n", "def lhs():\n", " display(eq1,eq2) \n", " return\n", "def rhs():\n", " return display(eq3,eq4)\n", "lhs()" ] }, { "cell_type": "markdown", "id": "2393180e", "metadata": {}, "source": [ "___________________________________________________________________________________________________________________\n", "If LaTeX display breaks, refresh the page. Runtime is under a second if you run the Jupyter-Notebook locally.\n", "\n", "**Created by GitHub.com/NSC9 - https://nsc9.github.io/ - MIT License - v7.5**\n", "\n", "Latest version source code: https://github.com/NSC9/Sample_of_Work/tree/Main/Artificial_Calculus_Teacher\n", "\n", "Donate by sending Bitcoin (BTC) to address: **bc1qtawr2gw52ftufzu0r3r20pnj3vmynssxs0mjl4**\n", "\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "15c3f34f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Derivative(-3*theta + tan(theta) - 14, theta)\n", "Integral(3*log(x), x)\n" ] } ], "source": [ "print(eq1.rhs)\n", "print(eq2.rhs)" ] }, { "cell_type": "markdown", "id": "30b55c90", "metadata": {}, "source": [ "https://www.derivative-calculator.net/\n", "\n", "https://www.integral-calculator.com/\n", "___________________________________________________________________________________________________________________\n", "\n", "# Answers:" ] }, { "cell_type": "code", "execution_count": 10, "id": "fbec286d", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle a' = \\sec^{2}{\\left(\\theta \\right)} - 3$" ], "text/plain": [ "Eq(a', sec(theta)**2 - 3)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$\\displaystyle B = 3 x \\log{\\left(x \\right)} - 3 x$" ], "text/plain": [ "Eq(B, 3*x*log(x) - 3*x)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rhs()" ] } ], "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 }