|
1 | | -class IndefiniteIntegralSolver: |
2 | | - def __init__(self, expression): |
3 | | - self.expression = expression |
4 | | - |
5 | | - def integrate(self): |
6 | | - try: |
7 | | - terms = self.parse_expression(self.expression) |
8 | | - integrated_terms = [self.integrate_term(term) for term in terms] |
9 | | - result = " + ".join(integrated_terms) + " + C" |
10 | | - return result |
11 | | - except Exception as e: |
12 | | - return str(e) |
13 | | - |
14 | | - def parse_expression(self, expression): |
15 | | - expression = expression.replace(" ", "") |
16 | | - terms = [] |
17 | | - term = "" |
18 | | - in_parentheses = 0 |
19 | | - |
20 | | - for char in expression: |
21 | | - if char == "(": |
22 | | - in_parentheses += 1 |
23 | | - elif char == ")": |
24 | | - in_parentheses -= 1 |
25 | | - |
26 | | - if char == "+" and in_parentheses == 0: |
27 | | - terms.append(term) |
28 | | - term = "" |
29 | | - else: |
30 | | - term += char |
31 | | - |
32 | | - terms.append(term) |
33 | | - return terms |
34 | | - |
35 | | - def integrate_term(self, term): |
36 | | - if "x" not in term: |
37 | | - if "/" in term: |
38 | | - num, denom = term.split("/") |
39 | | - return f"{float(num) / float(denom)}*x" |
40 | | - return f"{term}*x" |
41 | | - |
42 | | - if "^" in term: |
43 | | - base, exponent = term.split("^") |
44 | | - if base == "x": |
45 | | - new_exponent = float(exponent) + 1 |
46 | | - coefficient = 1 / new_exponent |
47 | | - return f"{coefficient}*x^{new_exponent}" |
48 | | - else: |
49 | | - coefficient, base = base.split("*") |
50 | | - new_exponent = float(exponent) + 1 |
51 | | - coefficient = float(coefficient) / new_exponent |
52 | | - return f"{coefficient}*x^{new_exponent}" |
53 | | - elif "*" in term: |
54 | | - coefficient, base = term.split("*") |
55 | | - if base == "x": |
56 | | - return f"{float(coefficient) / 2}*x^2" |
57 | | - else: |
58 | | - raise ValueError("Invalid term") |
59 | | - elif term == "x": |
60 | | - return "0.5*x^2" |
61 | | - elif term == "0": |
62 | | - return "0*x" |
63 | | - else: |
64 | | - raise ValueError("Invalid term") |
| 1 | +def gcd(a, b): |
| 2 | + while b: |
| 3 | + a, b = b, a % b |
| 4 | + return a |
| 5 | + |
| 6 | + |
| 7 | +def to_fraction(x, tolerance=1e-10): |
| 8 | + n = 1 |
| 9 | + while abs(n * x - round(n * x)) > tolerance: |
| 10 | + n *= 10 |
| 11 | + numerator = round(n * x) |
| 12 | + denominator = n |
| 13 | + common_divisor = gcd(numerator, denominator) |
| 14 | + numerator //= common_divisor |
| 15 | + denominator //= common_divisor |
| 16 | + return f"{numerator}/{denominator}" if denominator != 1 else f"{numerator}" |
| 17 | + |
| 18 | + |
| 19 | +def parse_term(term): |
| 20 | + # Remove spaces and handle negative signs |
| 21 | + term = term.replace(' ', '') |
| 22 | + if term[0] == '-': |
| 23 | + sign = -1 |
| 24 | + term = term[1:] |
| 25 | + else: |
| 26 | + sign = 1 |
| 27 | + |
| 28 | + # Split the term into coefficient and variable parts |
| 29 | + if 'x' in term: |
| 30 | + parts = term.split('x') |
| 31 | + if parts[0] == '': |
| 32 | + coefficient = 1 |
| 33 | + elif parts[0] == '-': |
| 34 | + coefficient = -1 |
| 35 | + else: |
| 36 | + coefficient = float(parts[0]) |
| 37 | + if len(parts) > 1 and parts[1] != '': |
| 38 | + exponent = int(parts[1][2:]) # Assuming the form is x**n |
| 39 | + else: |
| 40 | + exponent = 1 |
| 41 | + else: |
| 42 | + coefficient = float(term) |
| 43 | + exponent = 0 |
| 44 | + |
| 45 | + return sign * coefficient, exponent |
| 46 | + |
| 47 | + |
| 48 | +def integrate_term(coefficient, exponent): |
| 49 | + if exponent == -1: |
| 50 | + return coefficient, 'ln|x|' |
| 51 | + new_exponent = exponent + 1 |
| 52 | + new_coefficient = to_fraction(coefficient / new_exponent) |
| 53 | + return new_coefficient, new_exponent |
| 54 | + |
| 55 | + |
| 56 | +def format_term(coefficient, exponent): |
| 57 | + if exponent == 'ln|x|': |
| 58 | + return f"{coefficient}ln|x|" |
| 59 | + |
| 60 | + if coefficient == 0: |
| 61 | + return "" |
| 62 | + |
| 63 | + if exponent == 0: |
| 64 | + return f"{coefficient}" |
| 65 | + elif exponent == 1: |
| 66 | + if coefficient == "1": |
| 67 | + return "x" |
| 68 | + return f"{coefficient}x" |
| 69 | + else: |
| 70 | + if coefficient == "1": |
| 71 | + return f"x^{exponent}" |
| 72 | + return f"{coefficient}x^{exponent}" |
| 73 | + |
| 74 | + |
| 75 | +def integrate_polynomial(expression): |
| 76 | + terms = expression.split('+') |
| 77 | + integrated_terms = [] |
| 78 | + |
| 79 | + for term in terms: |
| 80 | + coefficient, exponent = parse_term(term.strip()) |
| 81 | + new_coefficient, new_exponent = integrate_term(coefficient, exponent) |
| 82 | + integrated_terms.append(format_term(new_coefficient, new_exponent)) |
| 83 | + |
| 84 | + return ' + '.join(integrated_terms) |
| 85 | + |
| 86 | + |
| 87 | +def main(): |
| 88 | + formula = input("Enter the formula: ") |
| 89 | + integral = integrate_polynomial(formula) |
| 90 | + print(f"The indefinite integral is: {integral} + C") |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments