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,
 28                               max_exp=10,
 29                               max_terms=5):
 30    r"""Power Rule Differentiation
 31
 32    | Ex. Problem | Ex. Solution |
 33    | --- | --- |
 34    | Differentiate $1x^{5} + 4x^{7} + 4x^{4}$ | $5x^{4} + 28x^{6} + 16x^{3}$ |
 35    """
 36    numTerms = random.randint(1, max_terms)
 37    problem = "Differentiate $"
 38    solution = "$"
 39
 40    for i in range(numTerms):
 41        if i > 0:
 42            problem += " + "
 43            solution += " + "
 44        coefficient = random.randint(1, max_coef)
 45        exponent = random.randint(1, max_exp)
 46
 47        problem += f'{coefficient}x^{{{exponent}}}'
 48        solution += f'{coefficient * exponent}x^{{{exponent - 1}}}'
 49
 50    return problem + '$', solution + '$'
 51
 52
 53def power_rule_integration(max_coef=10,
 54                           max_exp=10,
 55                           max_terms=5):
 56    r"""Power Rule Integration
 57
 58    | Ex. Problem | Ex. Solution |
 59    | --- | --- |
 60    | Integrate $9x^{6} + 2x^{6} + 4x^{3}$ | $\frac{9}{6}x^{7} + \frac{2}{6}x^{7} + \frac{4}{3}x^{4} + C$ |
 61    """
 62    numTerms = random.randint(1, max_terms)
 63    problem = "Integrate $"
 64    solution = "$"
 65
 66    for i in range(numTerms):
 67        if i > 0:
 68            problem += " + "
 69            solution += " + "
 70        coefficient = random.randint(1, max_coef)
 71        exponent = random.randint(1, max_exp)
 72
 73        problem += f'{coefficient}x^{{{exponent}}}'
 74        solution += rf'\frac{{{coefficient}}}{{{exponent}}}x^{{{exponent + 1}}}'
 75
 76    solution += " + C"
 77
 78    return problem + '$', solution + '$'
 79
 80
 81def stationary_points(max_exp=3, max_coef=10):
 82    r"""Stationary Points
 83
 84    | Ex. Problem | Ex. Solution |
 85    | --- | --- |
 86    | $f(x)=6*x^3 + 6*x^2 + x + 8$ | ${- \frac{1}{3} - \frac{\sqrt{2}}{6}, - \frac{1}{3} + \frac{\sqrt{2}}{6}}$ |
 87    """
 88    solution = ''
 89    while len(solution) == 0:
 90        x = sympy.symbols('x')
 91        problem = 0
 92        for exp in range(max_exp + 1):
 93            coefficient = random.randint(0, max_coef)
 94            problem += coefficient * pow(x, exp)
 95        solution = sympy.stationary_points(problem, x)
 96
 97    problem = 'f(x)=' + str(problem).replace('**', '^')
 98    return f'${problem}$', f'${sympy.latex(solution)[6:-8]}}}$'
 99
100
101def trig_differentiation():
102    r"""Trigonometric Differentiation
103
104    | Ex. Problem | Ex. Solution |
105    | --- | --- |
106    | $\frac{d}{dx}(\csc)=$ | $-\csc \cdot \cot$ |
107    """
108    pairs = {
109        r'\sin': r'\cos',
110        r'\cos': r'-\sin',
111        r'\tan': r'\sec^{{2}}',
112        r'\cot': r'-\csc^{{2}}',
113        r'\sec': r'\sec \cdot \tan',
114        r'\csc': r'-\csc \cdot \cot'
115    }
116    problem = random.choice(list(pairs.keys()))
117    solution = f'${pairs[problem]}$'
118    problem = rf'$\frac{{d}}{{dx}}({problem})=$'
119
120    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,
29                               max_exp=10,
30                               max_terms=5):
31    r"""Power Rule Differentiation
32
33    | Ex. Problem | Ex. Solution |
34    | --- | --- |
35    | Differentiate $1x^{5} + 4x^{7} + 4x^{4}$ | $5x^{4} + 28x^{6} + 16x^{3}$ |
36    """
37    numTerms = random.randint(1, max_terms)
38    problem = "Differentiate $"
39    solution = "$"
40
41    for i in range(numTerms):
42        if i > 0:
43            problem += " + "
44            solution += " + "
45        coefficient = random.randint(1, max_coef)
46        exponent = random.randint(1, max_exp)
47
48        problem += f'{coefficient}x^{{{exponent}}}'
49        solution += f'{coefficient * exponent}x^{{{exponent - 1}}}'
50
51    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):
54def power_rule_integration(max_coef=10,
55                           max_exp=10,
56                           max_terms=5):
57    r"""Power Rule Integration
58
59    | Ex. Problem | Ex. Solution |
60    | --- | --- |
61    | Integrate $9x^{6} + 2x^{6} + 4x^{3}$ | $\frac{9}{6}x^{7} + \frac{2}{6}x^{7} + \frac{4}{3}x^{4} + C$ |
62    """
63    numTerms = random.randint(1, max_terms)
64    problem = "Integrate $"
65    solution = "$"
66
67    for i in range(numTerms):
68        if i > 0:
69            problem += " + "
70            solution += " + "
71        coefficient = random.randint(1, max_coef)
72        exponent = random.randint(1, max_exp)
73
74        problem += f'{coefficient}x^{{{exponent}}}'
75        solution += rf'\frac{{{coefficient}}}{{{exponent}}}x^{{{exponent + 1}}}'
76
77    solution += " + C"
78
79    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):
82def stationary_points(max_exp=3, max_coef=10):
83    r"""Stationary Points
84
85    | Ex. Problem | Ex. Solution |
86    | --- | --- |
87    | $f(x)=6*x^3 + 6*x^2 + x + 8$ | ${- \frac{1}{3} - \frac{\sqrt{2}}{6}, - \frac{1}{3} + \frac{\sqrt{2}}{6}}$ |
88    """
89    solution = ''
90    while 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('**', '^')
99    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():
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

Trigonometric Differentiation

Ex. Problem Ex. Solution
$\frac{d}{dx}(\csc)=$ $-\csc \cdot \cot$