mathgenerator.calculus
1import random 2from scipy.integrate import quad 3import sympy 4 5 6def definite_integral(max_coef=100): 7 r"""Definite Integral of Quadratic Equation 8 9 | Ex. Problem | Ex. Solution | 10 | --- | --- | 11 | The definite integral within limits $0$ to $1$ of the equation $28x^2 + 32x + 66 = $ | $91.33$ | 12 """ 13 def integrand(x, a, b, c): 14 return a * x**2 + b * x + c 15 16 a = random.randint(0, max_coef) 17 b = random.randint(0, max_coef) 18 c = random.randint(0, max_coef) 19 20 result = quad(integrand, 0, 1, args=(a, b, c))[0] 21 solution = round(result, 2) 22 23 problem = f"The definite integral within limits $0$ to $1$ of the equation ${a}x^2 + {b}x + {c} = $" 24 return problem, f'${solution}$' 25 26 27def power_rule_differentiation(max_coef=10, max_exp=10, max_terms=5): 28 r"""Power Rule Differentiation 29 30 | Ex. Problem | Ex. Solution | 31 | --- | --- | 32 | Differentiate $1x^{5} + 4x^{7} + 4x^{4}$ | $5x^{4} + 28x^{6} + 16x^{3}$ | 33 """ 34 numTerms = random.randint(1, max_terms) 35 problem = "Differentiate $" 36 solution = "$" 37 38 for i in range(numTerms): 39 if i > 0: 40 problem += " + " 41 solution += " + " 42 coefficient = random.randint(1, max_coef) 43 exponent = random.randint(1, max_exp) 44 45 problem += f'{coefficient}x^{{{exponent}}}' 46 solution += f'{coefficient * exponent}x^{{{exponent - 1}}}' 47 48 return problem + '$', solution + '$' 49 50 51def power_rule_integration(max_coef=10, max_exp=10, max_terms=5): 52 r"""Power Rule Integration 53 54 | Ex. Problem | Ex. Solution | 55 | --- | --- | 56 | Integrate $9x^{6} + 2x^{6} + 4x^{3}$ | $\frac{9}{6}x^{7} + \frac{2}{6}x^{7} + \frac{4}{3}x^{4} + C$ | 57 """ 58 numTerms = random.randint(1, max_terms) 59 problem = "Integrate $" 60 solution = "$" 61 62 for i in range(numTerms): 63 if i > 0: 64 problem += " + " 65 solution += " + " 66 coefficient = random.randint(1, max_coef) 67 exponent = random.randint(1, max_exp) 68 69 problem += f'{coefficient}x^{{{exponent}}}' 70 solution += rf'\frac{{{coefficient}}}{{{exponent}}}x^{{{exponent + 1}}}' 71 72 solution += " + C" 73 74 return problem + '$', solution + '$' 75 76 77def stationary_points(max_exp=3, max_coef=10): 78 r"""Stationary Points 79 80 | Ex. Problem | Ex. Solution | 81 | --- | --- | 82 | $f(x)=6x^3 + 6x^2 + x + 8$ | ${- \frac{1}{3} - \frac{\sqrt{2}}{6}, - \frac{1}{3} + \frac{\sqrt{2}}{6}}$ | 83 """ 84 solution = '' 85 86 # A constant function has no stationary points, and the answer will be Reals. e.g.: 87 # x = sympy.symbols('x') 88 # s = sympy.stationary_points(1 + 0 * x , x) 89 # assert s == sympy.Reals 90 while solution == sympy.Reals or len(solution) == 0: 91 x = sympy.symbols('x') 92 problem = 0 93 for exp in range(max_exp + 1): 94 coefficient = random.randint(0, max_coef) 95 problem += coefficient * pow(x, exp) 96 solution = sympy.stationary_points(problem, x) 97 98 problem = 'f(x)=' + str(problem).replace('**', '^').replace('*', '') 99 return f'${problem}$', f'${sympy.latex(solution)[6:-8]}}}$' 100 101 102def trig_differentiation(): 103 r"""Trigonometric Differentiation 104 105 | Ex. Problem | Ex. Solution | 106 | --- | --- | 107 | $\frac{d}{dx}(\csc)=$ | $-\csc \cdot \cot$ | 108 """ 109 pairs = { 110 r'\sin': r'\cos', 111 r'\cos': r'-\sin', 112 r'\tan': r'\sec^{{2}}', 113 r'\cot': r'-\csc^{{2}}', 114 r'\sec': r'\sec \cdot \tan', 115 r'\csc': r'-\csc \cdot \cot' 116 } 117 problem = random.choice(list(pairs.keys())) 118 solution = f'${pairs[problem]}$' 119 problem = rf'$\frac{{d}}{{dx}}({problem})=$' 120 121 return problem, solution
def
definite_integral(max_coef=100):
7def definite_integral(max_coef=100): 8 r"""Definite Integral of Quadratic Equation 9 10 | Ex. Problem | Ex. Solution | 11 | --- | --- | 12 | The definite integral within limits $0$ to $1$ of the equation $28x^2 + 32x + 66 = $ | $91.33$ | 13 """ 14 def integrand(x, a, b, c): 15 return a * x**2 + b * x + c 16 17 a = random.randint(0, max_coef) 18 b = random.randint(0, max_coef) 19 c = random.randint(0, max_coef) 20 21 result = quad(integrand, 0, 1, args=(a, b, c))[0] 22 solution = round(result, 2) 23 24 problem = f"The definite integral within limits $0$ to $1$ of the equation ${a}x^2 + {b}x + {c} = $" 25 return problem, f'${solution}$'
Definite Integral of Quadratic Equation
| Ex. Problem | Ex. Solution |
|---|---|
| The definite integral within limits $0$ to $1$ of the equation $28x^2 + 32x + 66 = $ | $91.33$ |
def
power_rule_differentiation(max_coef=10, max_exp=10, max_terms=5):
28def power_rule_differentiation(max_coef=10, max_exp=10, max_terms=5): 29 r"""Power Rule Differentiation 30 31 | Ex. Problem | Ex. Solution | 32 | --- | --- | 33 | Differentiate $1x^{5} + 4x^{7} + 4x^{4}$ | $5x^{4} + 28x^{6} + 16x^{3}$ | 34 """ 35 numTerms = random.randint(1, max_terms) 36 problem = "Differentiate $" 37 solution = "$" 38 39 for i in range(numTerms): 40 if i > 0: 41 problem += " + " 42 solution += " + " 43 coefficient = random.randint(1, max_coef) 44 exponent = random.randint(1, max_exp) 45 46 problem += f'{coefficient}x^{{{exponent}}}' 47 solution += f'{coefficient * exponent}x^{{{exponent - 1}}}' 48 49 return problem + '$', solution + '$'
Power Rule Differentiation
| Ex. Problem | Ex. Solution |
|---|---|
| Differentiate $1x^{5} + 4x^{7} + 4x^{4}$ | $5x^{4} + 28x^{6} + 16x^{3}$ |
def
power_rule_integration(max_coef=10, max_exp=10, max_terms=5):
52def power_rule_integration(max_coef=10, max_exp=10, max_terms=5): 53 r"""Power Rule Integration 54 55 | Ex. Problem | Ex. Solution | 56 | --- | --- | 57 | Integrate $9x^{6} + 2x^{6} + 4x^{3}$ | $\frac{9}{6}x^{7} + \frac{2}{6}x^{7} + \frac{4}{3}x^{4} + C$ | 58 """ 59 numTerms = random.randint(1, max_terms) 60 problem = "Integrate $" 61 solution = "$" 62 63 for i in range(numTerms): 64 if i > 0: 65 problem += " + " 66 solution += " + " 67 coefficient = random.randint(1, max_coef) 68 exponent = random.randint(1, max_exp) 69 70 problem += f'{coefficient}x^{{{exponent}}}' 71 solution += rf'\frac{{{coefficient}}}{{{exponent}}}x^{{{exponent + 1}}}' 72 73 solution += " + C" 74 75 return problem + '$', solution + '$'
Power Rule Integration
| Ex. Problem | Ex. Solution |
|---|---|
| Integrate $9x^{6} + 2x^{6} + 4x^{3}$ | $\frac{9}{6}x^{7} + \frac{2}{6}x^{7} + \frac{4}{3}x^{4} + C$ |
def
stationary_points(max_exp=3, max_coef=10):
78def stationary_points(max_exp=3, max_coef=10): 79 r"""Stationary Points 80 81 | Ex. Problem | Ex. Solution | 82 | --- | --- | 83 | $f(x)=6x^3 + 6x^2 + x + 8$ | ${- \frac{1}{3} - \frac{\sqrt{2}}{6}, - \frac{1}{3} + \frac{\sqrt{2}}{6}}$ | 84 """ 85 solution = '' 86 87 # A constant function has no stationary points, and the answer will be Reals. e.g.: 88 # x = sympy.symbols('x') 89 # s = sympy.stationary_points(1 + 0 * x , x) 90 # assert s == sympy.Reals 91 while solution == sympy.Reals or len(solution) == 0: 92 x = sympy.symbols('x') 93 problem = 0 94 for exp in range(max_exp + 1): 95 coefficient = random.randint(0, max_coef) 96 problem += coefficient * pow(x, exp) 97 solution = sympy.stationary_points(problem, x) 98 99 problem = 'f(x)=' + str(problem).replace('**', '^').replace('*', '') 100 return f'${problem}$', f'${sympy.latex(solution)[6:-8]}}}$'
Stationary Points
| Ex. Problem | Ex. Solution |
|---|---|
| $f(x)=6x^3 + 6x^2 + x + 8$ | ${- \frac{1}{3} - \frac{\sqrt{2}}{6}, - \frac{1}{3} + \frac{\sqrt{2}}{6}}$ |
def
trig_differentiation():
103def trig_differentiation(): 104 r"""Trigonometric Differentiation 105 106 | Ex. Problem | Ex. Solution | 107 | --- | --- | 108 | $\frac{d}{dx}(\csc)=$ | $-\csc \cdot \cot$ | 109 """ 110 pairs = { 111 r'\sin': r'\cos', 112 r'\cos': r'-\sin', 113 r'\tan': r'\sec^{{2}}', 114 r'\cot': r'-\csc^{{2}}', 115 r'\sec': r'\sec \cdot \tan', 116 r'\csc': r'-\csc \cdot \cot' 117 } 118 problem = random.choice(list(pairs.keys())) 119 solution = f'${pairs[problem]}$' 120 problem = rf'$\frac{{d}}{{dx}}({problem})=$' 121 122 return problem, solution
Trigonometric Differentiation
| Ex. Problem | Ex. Solution |
|---|---|
| $\frac{d}{dx}(\csc)=$ | $-\csc \cdot \cot$ |