File size: 6,927 Bytes
b2a51ca
 
 
 
 
 
 
 
d229b05
b2a51ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f0c3c4
b2a51ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f0c3c4
b2a51ca
93b4d17
b2a51ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f0c3c4
b2a51ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b4d17
b2a51ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
{
 "cells": [
  {
   "cell_type": "raw",
   "id": "8c69730d",
   "metadata": {},
   "source": [
    "---\n",
    "title: Calculus Problem Generator\n",
    "description: Generates Derivative and Integral Expressions\n",
    "show-code : False\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c5197005",
   "metadata": {},
   "outputs": [],
   "source": [
    "# changelog: removes custom_ln cause i don't like it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "108761f9",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solve\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{d}{d x} x^{3} \\tan{\\left(x \\right)}$"
      ],
      "text/plain": [
       "Derivative(x**3*tan(x), x)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\int \\left(\\sqrt[3]{x} - e^{x}\\right)\\, dx$"
      ],
      "text/plain": [
       "Integral(x**(1/3) - exp(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} x^{3} \\tan{\\left(x \\right)} = x^{3} \\sec^{2}{\\left(x \\right)} + 3 x^{2} \\tan{\\left(x \\right)}$"
      ],
      "text/plain": [
       "Eq(Derivative(x**3*tan(x), x), x**3*sec(x)**2 + 3*x**2*tan(x))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\int \\left(\\sqrt[3]{x} - e^{x}\\right)\\, dx = \\frac{3 x^{\\frac{4}{3}}}{4} - e^{x}$"
      ],
      "text/plain": [
       "Eq(Integral(x**(1/3) - exp(x), x), 3*x**(4/3)/4 - exp(x))"
      ]
     },
     "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,Rational\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),f(x)*g(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",
    "    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,8):\n",
    "    print(\"\\n\")\n",
    "print(\"Solutions:\")\n",
    "display(TR22(p1eq))\n",
    "display(TR22(p2eq))\n",
    "print(\"-----------------------------------------------------------------------------------------------------------\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3f5b9fb",
   "metadata": {},
   "source": [
    "If LaTeX display breaks, refresh the page.\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
}