Skip to content

Commit ffb9722

Browse files
Add files via upload
1 parent cda62b9 commit ffb9722

File tree

2 files changed

+151
-64
lines changed

2 files changed

+151
-64
lines changed

MathCalc/meth.py

Lines changed: 94 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,94 @@
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()

MathCalc/test_broken.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Test cases for IndefiniteIntegralSolver
2+
from meth import IndefiniteIntegralSolver
3+
4+
5+
def test_indefinite_integral_solver():
6+
test_cases = [
7+
# Easy cases
8+
("3*x^2 + 2*x + 1", "1.0*x^3 + 1.0*x^2 + 1*x + C"),
9+
("x^2 + x + 1", "0.3333333333333333*x^3 + 0.5*x^2 + 1*x + C"),
10+
("5*x + 3", "2.5*x^2 + 3*x + C"),
11+
12+
# Harder cases
13+
("4*x^3 + 3*x^2 + 2*x + 1", "1.0*x^4 + 1.0*x^3 + 1.0*x^2 + 1*x + C"),
14+
("6*x^5 + 5*x^4 + 4*x^3 + 3*x^2 + 2*x + 1", "1.0*x^6 + 1.0*x^5 + 1.0*x^4 + 1.0*x^3 + 1.0*x^2 + 1*x + C"),
15+
16+
# Cases with brackets
17+
("(3*x^2) + (2*x) + 1", "1.0*x^3 + 1.0*x^2 + 1*x + C"),
18+
("(4*x^3) + (3*x^2) + (2*x) + 1", "1.0*x^4 + 1.0*x^3 + 1.0*x^2 + 1*x + C"),
19+
20+
# Edge cases
21+
("x", "0.5*x^2 + C"),
22+
("1", "1*x + C"),
23+
("x^0.5", "0.6666666666666666*x^1.5 + C"),
24+
("1/2", "0.5*x + C"),
25+
("1/x", "-1/2*x^-2 + C"),
26+
("0", "0*x + C"),
27+
("x^2", "0.3333333333333333*x^3 + C"),
28+
("x^3", "0.25*x^4 + C"),
29+
("x^4", "0.2*x^5 + C"),
30+
("x^5", "0.16666666666666666*x^6 + C"),
31+
("(1/2)*(x^2)", "0.16666666666666666*x^3 + C"),
32+
("(-1/2)*(x^-1)", "x^-2"),
33+
("(1/2)*(x^-1/2)", "x^0.5 + C"),
34+
35+
# Invalid cases (should handle gracefully)
36+
("3*x^2 + 2*y + 1", "1.0*x^3 + 2.0*x + 1*x + C"), # y is not a valid variable for integration
37+
("3*x^2 + 2* + 1", "1.0*x^3 + 2.0*x + 1*x + C"), # Invalid term "2*"
38+
]
39+
40+
counter = 0
41+
fail_counter = 0
42+
for expression, expected in test_cases:
43+
solver = IndefiniteIntegralSolver(expression)
44+
result = solver.integrate()
45+
try:
46+
assert result == expected, f"Test failed for expression: {expression}. Expected: {expected}, Got: {result}"
47+
print(f"✅ Test passed for expression: {expression}. Result: {result}")
48+
except AssertionError:
49+
print(f"❌ Test failed for expression: {expression}. Expected: {expected}, Got: {result}")
50+
fail_counter += 1
51+
counter += 1
52+
print(f"\nTotal tests 🧪: {counter}, "
53+
f"\nPassed ✅: {counter - fail_counter}, "
54+
f"\nFailed ❌: {fail_counter}")
55+
56+
# Run the tests
57+
test_indefinite_integral_solver()

0 commit comments

Comments
 (0)