Spaces:
Runtime error
Runtime error
modularized
Browse files- maths/calculus/calculate_limit.py +39 -0
- maths/calculus/calculus.py +0 -265
- maths/calculus/calculus_interface.py +7 -5
- maths/calculus/derivative_polynomial.py +21 -0
- maths/calculus/fourier_series_example.py +33 -0
- maths/calculus/integral_polynomial.py +20 -0
- maths/calculus/multiple_integral.py +36 -0
- maths/calculus/partial_derivative.py +36 -0
- maths/calculus/taylor_series_expansion.py +22 -0
maths/calculus/calculate_limit.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sympy
|
2 |
+
|
3 |
+
def calculate_limit(expression_str: str, variable_str: str, point_str: str, direction: str = '+-') -> str:
|
4 |
+
"""
|
5 |
+
Calculates the limit of an expression as a variable approaches a point.
|
6 |
+
|
7 |
+
Args:
|
8 |
+
expression_str: The mathematical expression as a string (e.g., "sin(x)/x").
|
9 |
+
variable_str: The variable in the expression (e.g., "x").
|
10 |
+
point_str: The point the variable is approaching (e.g., "0", "oo" for infinity).
|
11 |
+
direction: The direction of the limit ('+', '-', or '+-' for both sides).
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
A string representing the limit or an error message.
|
15 |
+
"""
|
16 |
+
try:
|
17 |
+
var = sympy.Symbol(variable_str)
|
18 |
+
local_dict = {variable_str: var}
|
19 |
+
expr = sympy.parse_expr(expression_str, local_dict=local_dict, transformations='all')
|
20 |
+
|
21 |
+
if point_str.lower() == 'oo':
|
22 |
+
point = sympy.oo
|
23 |
+
elif point_str.lower() == '-oo':
|
24 |
+
point = -sympy.oo
|
25 |
+
else:
|
26 |
+
point = sympy.sympify(point_str)
|
27 |
+
|
28 |
+
if direction == '+':
|
29 |
+
limit_val = sympy.limit(expr, var, point, dir='+')
|
30 |
+
elif direction == '-':
|
31 |
+
limit_val = sympy.limit(expr, var, point, dir='-')
|
32 |
+
else:
|
33 |
+
limit_val = sympy.limit(expr, var, point)
|
34 |
+
|
35 |
+
return str(limit_val)
|
36 |
+
except (sympy.SympifyError, TypeError, SyntaxError) as e:
|
37 |
+
return f"Error parsing expression or point: {e}. Ensure valid Sympy syntax (e.g. use 'oo' for infinity, ensure variables match)."
|
38 |
+
except Exception as e:
|
39 |
+
return f"An unexpected error occurred: {e}"
|
maths/calculus/calculus.py
DELETED
@@ -1,265 +0,0 @@
|
|
1 |
-
"""
|
2 |
-
Calculus operations for university level.
|
3 |
-
"""
|
4 |
-
import sympy
|
5 |
-
from sympy.parsing.mathematica import parse_mathematica # For robust expression parsing
|
6 |
-
import numpy as np # For numerical integration if needed
|
7 |
-
from typing import Callable, Union, List, Tuple
|
8 |
-
|
9 |
-
# Define common symbols for Sympy expressions
|
10 |
-
x, y, z, n = sympy.symbols('x y z n')
|
11 |
-
|
12 |
-
def derivative_polynomial(coefficients):
|
13 |
-
"""
|
14 |
-
Calculate the derivative of a polynomial function.
|
15 |
-
|
16 |
-
Args:
|
17 |
-
coefficients (list): List of coefficients from highest to lowest degree
|
18 |
-
|
19 |
-
Returns:
|
20 |
-
list: Coefficients of the derivative polynomial
|
21 |
-
"""
|
22 |
-
if len(coefficients) <= 1:
|
23 |
-
return [0]
|
24 |
-
|
25 |
-
result = []
|
26 |
-
for i, coef in enumerate(coefficients[:-1]):
|
27 |
-
power = len(coefficients) - i - 1
|
28 |
-
result.append(coef * power)
|
29 |
-
|
30 |
-
return result
|
31 |
-
|
32 |
-
def integral_polynomial(coefficients, c=0):
|
33 |
-
"""
|
34 |
-
Calculate the indefinite integral of a polynomial function.
|
35 |
-
|
36 |
-
Args:
|
37 |
-
coefficients (list): List of coefficients from highest to lowest degree
|
38 |
-
c (float): Integration constant
|
39 |
-
|
40 |
-
Returns:
|
41 |
-
list: Coefficients of the integral polynomial including constant term
|
42 |
-
"""
|
43 |
-
result = []
|
44 |
-
for i, coef in enumerate(coefficients):
|
45 |
-
power = len(coefficients) - i
|
46 |
-
result.append(coef / power)
|
47 |
-
|
48 |
-
result.append(c) # Add integration constant
|
49 |
-
return result
|
50 |
-
|
51 |
-
|
52 |
-
def calculate_limit(expression_str: str, variable_str: str, point_str: str, direction: str = '+-') -> str:
|
53 |
-
"""
|
54 |
-
Calculates the limit of an expression as a variable approaches a point.
|
55 |
-
|
56 |
-
Args:
|
57 |
-
expression_str: The mathematical expression as a string (e.g., "sin(x)/x").
|
58 |
-
variable_str: The variable in the expression (e.g., "x").
|
59 |
-
point_str: The point the variable is approaching (e.g., "0", "oo" for infinity).
|
60 |
-
direction: The direction of the limit ('+', '-', or '+-' for both sides).
|
61 |
-
|
62 |
-
Returns:
|
63 |
-
A string representing the limit or an error message.
|
64 |
-
"""
|
65 |
-
try:
|
66 |
-
var = sympy.Symbol(variable_str)
|
67 |
-
# Using parse_mathematica for more robust parsing than sympify alone
|
68 |
-
# It handles expressions like "x^2" or "sin(x)" more reliably.
|
69 |
-
# We need to ensure the variable is available in the parsing context.
|
70 |
-
local_dict = {variable_str: var}
|
71 |
-
expr = sympy.parse_expr(expression_str, local_dict=local_dict, transformations='all')
|
72 |
-
|
73 |
-
|
74 |
-
if point_str.lower() == 'oo':
|
75 |
-
point = sympy.oo
|
76 |
-
elif point_str.lower() == '-oo':
|
77 |
-
point = -sympy.oo
|
78 |
-
else:
|
79 |
-
point = sympy.sympify(point_str) # Can handle numbers or symbolic constants like pi
|
80 |
-
|
81 |
-
if direction == '+':
|
82 |
-
limit_val = sympy.limit(expr, var, point, dir='+')
|
83 |
-
elif direction == '-':
|
84 |
-
limit_val = sympy.limit(expr, var, point, dir='-')
|
85 |
-
else: # '+-' default
|
86 |
-
limit_val = sympy.limit(expr, var, point)
|
87 |
-
|
88 |
-
return str(limit_val)
|
89 |
-
except (sympy.SympifyError, TypeError, SyntaxError) as e:
|
90 |
-
return f"Error parsing expression or point: {e}. Ensure valid Sympy syntax (e.g. use 'oo' for infinity, ensure variables match)."
|
91 |
-
except Exception as e:
|
92 |
-
return f"An unexpected error occurred: {e}"
|
93 |
-
|
94 |
-
|
95 |
-
def taylor_series_expansion(expression_str: str, variable_str: str = 'x', point: float = 0, order: int = 5) -> str:
|
96 |
-
"""
|
97 |
-
Computes the Taylor series expansion of an expression around a point.
|
98 |
-
|
99 |
-
Args:
|
100 |
-
expression_str: The mathematical expression (e.g., "exp(x)").
|
101 |
-
variable_str: The variable (default 'x').
|
102 |
-
point: The point around which to expand (default 0).
|
103 |
-
order: The order of the Taylor polynomial (default 5).
|
104 |
-
|
105 |
-
Returns:
|
106 |
-
String representation of the Taylor series or an error message.
|
107 |
-
"""
|
108 |
-
try:
|
109 |
-
var = sympy.Symbol(variable_str)
|
110 |
-
expr = sympy.parse_expr(expression_str, local_dict={variable_str: var}, transformations='all')
|
111 |
-
|
112 |
-
series = expr.series(var, x0=point, n=order).removeO() # removeO() removes the O(x^n) term
|
113 |
-
return str(series)
|
114 |
-
except Exception as e:
|
115 |
-
return f"Error calculating Taylor series: {e}"
|
116 |
-
|
117 |
-
|
118 |
-
def fourier_series_example(function_type: str = "sawtooth", n_terms: int = 5) -> str:
|
119 |
-
"""
|
120 |
-
Provides an example of a Fourier series for a predefined function.
|
121 |
-
|
122 |
-
Args:
|
123 |
-
function_type: "sawtooth" or "square" wave.
|
124 |
-
n_terms: Number of terms to compute in the series.
|
125 |
-
|
126 |
-
Returns:
|
127 |
-
String representation of the Fourier series.
|
128 |
-
"""
|
129 |
-
try:
|
130 |
-
L = sympy.pi # Periodicity, assuming 2L = 2*pi for standard examples
|
131 |
-
x = sympy.Symbol('x')
|
132 |
-
|
133 |
-
if function_type == "sawtooth":
|
134 |
-
# Sawtooth wave: f(x) = x for -pi < x < pi
|
135 |
-
# a0 = 0
|
136 |
-
# an = 0
|
137 |
-
# bn = 2/L * integral(x*sin(n*pi*x/L), (x, 0, L))
|
138 |
-
# = 2/pi * integral(x*sin(nx), (x, 0, pi))
|
139 |
-
# = 2/pi * [-x*cos(nx)/n + sin(nx)/n^2]_0^pi
|
140 |
-
# = 2/pi * [-pi*cos(n*pi)/n] = -2*(-1)^n / n
|
141 |
-
series = sympy.S(0) # Start with zero, as a0 is 0
|
142 |
-
for i in range(1, n_terms + 1):
|
143 |
-
bn_coeff = -2 * ((-1)**i) / i
|
144 |
-
series += bn_coeff * sympy.sin(i * x)
|
145 |
-
return f"Fourier series for sawtooth wave (f(x)=x on [-pi,pi]): {str(series)}"
|
146 |
-
|
147 |
-
elif function_type == "square":
|
148 |
-
# Square wave: f(x) = -1 for -pi < x < 0, f(x) = 1 for 0 < x < pi
|
149 |
-
# a0 = 0
|
150 |
-
# an = 0
|
151 |
-
# bn = 2/L * integral(f(x)*sin(n*pi*x/L), (x, 0, L)) where f(x)=1 for (0,L)
|
152 |
-
# = 2/pi * integral(sin(nx), (x, 0, pi))
|
153 |
-
# = 2/pi * [-cos(nx)/n]_0^pi
|
154 |
-
# = 2/(n*pi) * (1 - cos(n*pi)) = 2/(n*pi) * (1 - (-1)^n)
|
155 |
-
# This means bn is 4/(n*pi) if n is odd, and 0 if n is even.
|
156 |
-
series = sympy.S(0)
|
157 |
-
for i in range(1, n_terms + 1):
|
158 |
-
if i % 2 != 0: # n is odd
|
159 |
-
bn_coeff = 4 / (i * sympy.pi)
|
160 |
-
series += bn_coeff * sympy.sin(i * x)
|
161 |
-
return f"Fourier series for square wave (f(x)=1 on [0,pi], -1 on [-pi,0]): {str(series)}"
|
162 |
-
else:
|
163 |
-
return "Error: Unknown function type for Fourier series. Choose 'sawtooth' or 'square'."
|
164 |
-
except Exception as e:
|
165 |
-
return f"Error generating Fourier series: {e}"
|
166 |
-
|
167 |
-
|
168 |
-
def partial_derivative(expression_str: str, variables_str: List[str]) -> str:
|
169 |
-
"""
|
170 |
-
Computes partial derivatives of an expression with respect to specified variables.
|
171 |
-
Example: expression_str="x**2*y**3", variables_str=["x", "y"] will compute d/dx then d/dy.
|
172 |
-
If you want d^2f/dx^2, use variables_str=["x", "x"].
|
173 |
-
|
174 |
-
Args:
|
175 |
-
expression_str: The mathematical expression (e.g., "x**2*y + y*z**2").
|
176 |
-
variables_str: A list of variable names (strings) to differentiate by, in order.
|
177 |
-
(e.g., ["x", "y"] for d/dy(d/dx(expr)) ).
|
178 |
-
|
179 |
-
Returns:
|
180 |
-
String representation of the partial derivative or an error message.
|
181 |
-
"""
|
182 |
-
try:
|
183 |
-
# Create symbols for all variables that might appear in the expression
|
184 |
-
# For simplicity, we'll assume 'x', 'y', 'z' are common, but user must specify which to derive by.
|
185 |
-
# A more robust way would be to parse expression_str to find all symbols.
|
186 |
-
symbols_in_expr = {s.name: s for s in sympy.parse_expr(expression_str, transformations='all').free_symbols}
|
187 |
-
|
188 |
-
# Ensure variables to differentiate by are symbols
|
189 |
-
diff_vars_symbols = []
|
190 |
-
for var_name in variables_str:
|
191 |
-
if var_name in symbols_in_expr:
|
192 |
-
diff_vars_symbols.append(symbols_in_expr[var_name])
|
193 |
-
else:
|
194 |
-
# If a variable to differentiate by is not in free_symbols, it might be an error
|
195 |
-
# or the expression doesn't depend on it. Sympy handles differentiation by non-present vars as 0.
|
196 |
-
# We'll create the symbol anyway to pass to diff.
|
197 |
-
diff_vars_symbols.append(sympy.Symbol(var_name))
|
198 |
-
|
199 |
-
if not diff_vars_symbols:
|
200 |
-
return "Error: No variables specified for differentiation."
|
201 |
-
|
202 |
-
expr = sympy.parse_expr(expression_str, local_dict=symbols_in_expr, transformations='all')
|
203 |
-
|
204 |
-
# Compute partial derivatives iteratively
|
205 |
-
current_expr = expr
|
206 |
-
for var_sym in diff_vars_symbols:
|
207 |
-
current_expr = sympy.diff(current_expr, var_sym)
|
208 |
-
|
209 |
-
return str(current_expr)
|
210 |
-
except (sympy.SympifyError, TypeError, SyntaxError) as e:
|
211 |
-
return f"Error parsing expression or variables: {e}"
|
212 |
-
except Exception as e:
|
213 |
-
return f"An unexpected error occurred: {e}"
|
214 |
-
|
215 |
-
|
216 |
-
def multiple_integral(expression_str: str, integration_vars: List[Tuple[str, Union[str, float], Union[str, float]]]) -> str:
|
217 |
-
"""
|
218 |
-
Computes definite multiple integrals.
|
219 |
-
|
220 |
-
Args:
|
221 |
-
expression_str: The mathematical expression (e.g., "x*y**2").
|
222 |
-
integration_vars: A list of tuples, where each tuple contains:
|
223 |
-
(variable_name_str, lower_bound_str_or_float, upper_bound_str_or_float).
|
224 |
-
Example: [("x", "0", "1"), ("y", "0", "x")] for integral from 0 to 1 dx of integral from 0 to x dy of (x*y**2).
|
225 |
-
The order in the list is the order of integration (inner to outer).
|
226 |
-
|
227 |
-
Returns:
|
228 |
-
String representation of the integral result or an error message.
|
229 |
-
"""
|
230 |
-
try:
|
231 |
-
# Create symbols for all variables that might appear in the expression
|
232 |
-
# and in integration bounds.
|
233 |
-
# A more robust approach might involve parsing the expression and bounds to identify all symbols.
|
234 |
-
# For now, we'll default to x, y, z if not explicitly mentioned.
|
235 |
-
present_symbols = {sym.name: sym for sym in sympy.parse_expr(expression_str, transformations='all').free_symbols}
|
236 |
-
|
237 |
-
integration_params_sympy = []
|
238 |
-
for var_name, lower_bound, upper_bound in integration_vars:
|
239 |
-
var_sym = present_symbols.get(var_name, sympy.Symbol(var_name))
|
240 |
-
if var_sym.name not in present_symbols: # Add if it wasn't in expression but is an integration var
|
241 |
-
present_symbols[var_sym.name] = var_sym
|
242 |
-
|
243 |
-
# Bounds can be numbers or expressions involving other variables
|
244 |
-
# We need to ensure these other variables are available in the context for sympify
|
245 |
-
lower_s = sympy.sympify(lower_bound, locals=present_symbols)
|
246 |
-
upper_s = sympy.sympify(upper_bound, locals=present_symbols)
|
247 |
-
integration_params_sympy.append((var_sym, lower_s, upper_s))
|
248 |
-
|
249 |
-
if not integration_params_sympy:
|
250 |
-
return "Error: No integration variables specified."
|
251 |
-
|
252 |
-
expr = sympy.parse_expr(expression_str, local_dict=present_symbols, transformations='all')
|
253 |
-
|
254 |
-
# Compute integral iteratively (from inner to outer)
|
255 |
-
# Sympy's integrate function takes tuples like (x, 0, 1)
|
256 |
-
# The order of integration_params_sympy should be from inner-most integral to outer-most.
|
257 |
-
# For sympy.integrate, the order of variables in the list is outer to inner. So we reverse.
|
258 |
-
|
259 |
-
integral_val = sympy.integrate(expr, *integration_params_sympy)
|
260 |
-
|
261 |
-
return str(integral_val)
|
262 |
-
except (sympy.SympifyError, TypeError, SyntaxError) as e:
|
263 |
-
return f"Error parsing expression, bounds, or variables: {e}. Ensure bounds are numbers or valid expressions."
|
264 |
-
except Exception as e:
|
265 |
-
return f"An unexpected error occurred during integration: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
maths/calculus/calculus_interface.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
-
from maths.calculus.
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
7 |
import json # For parsing list of tuples for multiple integrals
|
8 |
|
9 |
# University Math Tab
|
|
|
1 |
import gradio as gr
|
2 |
+
from maths.calculus.derivative_polynomial import derivative_polynomial
|
3 |
+
from maths.calculus.integral_polynomial import integral_polynomial
|
4 |
+
from maths.calculus.calculate_limit import calculate_limit
|
5 |
+
from maths.calculus.taylor_series_expansion import taylor_series_expansion
|
6 |
+
from maths.calculus.fourier_series_example import fourier_series_example
|
7 |
+
from maths.calculus.partial_derivative import partial_derivative
|
8 |
+
from maths.calculus.multiple_integral import multiple_integral
|
9 |
import json # For parsing list of tuples for multiple integrals
|
10 |
|
11 |
# University Math Tab
|
maths/calculus/derivative_polynomial.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sympy
|
2 |
+
|
3 |
+
def derivative_polynomial(coefficients):
|
4 |
+
"""
|
5 |
+
Calculate the derivative of a polynomial function.
|
6 |
+
|
7 |
+
Args:
|
8 |
+
coefficients (list): List of coefficients from highest to lowest degree
|
9 |
+
|
10 |
+
Returns:
|
11 |
+
list: Coefficients of the derivative polynomial
|
12 |
+
"""
|
13 |
+
if len(coefficients) <= 1:
|
14 |
+
return [0]
|
15 |
+
|
16 |
+
result = []
|
17 |
+
for i, coef in enumerate(coefficients[:-1]):
|
18 |
+
power = len(coefficients) - i - 1
|
19 |
+
result.append(coef * power)
|
20 |
+
|
21 |
+
return result
|
maths/calculus/fourier_series_example.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sympy
|
2 |
+
|
3 |
+
def fourier_series_example(function_type: str = "sawtooth", n_terms: int = 5) -> str:
|
4 |
+
"""
|
5 |
+
Provides an example of a Fourier series for a predefined function.
|
6 |
+
|
7 |
+
Args:
|
8 |
+
function_type: "sawtooth" or "square" wave.
|
9 |
+
n_terms: Number of terms to compute in the series.
|
10 |
+
|
11 |
+
Returns:
|
12 |
+
String representation of the Fourier series.
|
13 |
+
"""
|
14 |
+
try:
|
15 |
+
L = sympy.pi
|
16 |
+
x = sympy.Symbol('x')
|
17 |
+
if function_type == "sawtooth":
|
18 |
+
series = sympy.S(0)
|
19 |
+
for i in range(1, n_terms + 1):
|
20 |
+
bn_coeff = -2 * ((-1)**i) / i
|
21 |
+
series += bn_coeff * sympy.sin(i * x)
|
22 |
+
return f"Fourier series for sawtooth wave (f(x)=x on [-pi,pi]): {str(series)}"
|
23 |
+
elif function_type == "square":
|
24 |
+
series = sympy.S(0)
|
25 |
+
for i in range(1, n_terms + 1):
|
26 |
+
if i % 2 != 0:
|
27 |
+
bn_coeff = 4 / (i * sympy.pi)
|
28 |
+
series += bn_coeff * sympy.sin(i * x)
|
29 |
+
return f"Fourier series for square wave (f(x)=1 on [0,pi], -1 on [-pi,0]): {str(series)}"
|
30 |
+
else:
|
31 |
+
return "Error: Unknown function type for Fourier series. Choose 'sawtooth' or 'square'."
|
32 |
+
except Exception as e:
|
33 |
+
return f"Error generating Fourier series: {e}"
|
maths/calculus/integral_polynomial.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sympy
|
2 |
+
|
3 |
+
def integral_polynomial(coefficients, c=0):
|
4 |
+
"""
|
5 |
+
Calculate the indefinite integral of a polynomial function.
|
6 |
+
|
7 |
+
Args:
|
8 |
+
coefficients (list): List of coefficients from highest to lowest degree
|
9 |
+
c (float): Integration constant
|
10 |
+
|
11 |
+
Returns:
|
12 |
+
list: Coefficients of the integral polynomial including constant term
|
13 |
+
"""
|
14 |
+
result = []
|
15 |
+
for i, coef in enumerate(coefficients):
|
16 |
+
power = len(coefficients) - i
|
17 |
+
result.append(coef / power)
|
18 |
+
|
19 |
+
result.append(c) # Add integration constant
|
20 |
+
return result
|
maths/calculus/multiple_integral.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sympy
|
2 |
+
from typing import List, Tuple, Union
|
3 |
+
|
4 |
+
def multiple_integral(expression_str: str, integration_vars: List[Tuple[str, Union[str, float], Union[str, float]]]) -> str:
|
5 |
+
"""
|
6 |
+
Computes definite multiple integrals.
|
7 |
+
|
8 |
+
Args:
|
9 |
+
expression_str: The mathematical expression (e.g., "x*y**2").
|
10 |
+
integration_vars: A list of tuples, where each tuple contains:
|
11 |
+
(variable_name_str, lower_bound_str_or_float, upper_bound_str_or_float).
|
12 |
+
Example: [("x", "0", "1"), ("y", "0", "x")] for integral from 0 to 1 dx of integral from 0 to x dy of (x*y**2).
|
13 |
+
The order in the list is the order of integration (inner to outer).
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
String representation of the integral result or an error message.
|
17 |
+
"""
|
18 |
+
try:
|
19 |
+
present_symbols = {sym.name: sym for sym in sympy.parse_expr(expression_str, transformations='all').free_symbols}
|
20 |
+
integration_params_sympy = []
|
21 |
+
for var_name, lower_bound, upper_bound in integration_vars:
|
22 |
+
var_sym = present_symbols.get(var_name, sympy.Symbol(var_name))
|
23 |
+
if var_sym.name not in present_symbols:
|
24 |
+
present_symbols[var_sym.name] = var_sym
|
25 |
+
lower_s = sympy.sympify(lower_bound, locals=present_symbols)
|
26 |
+
upper_s = sympy.sympify(upper_bound, locals=present_symbols)
|
27 |
+
integration_params_sympy.append((var_sym, lower_s, upper_s))
|
28 |
+
if not integration_params_sympy:
|
29 |
+
return "Error: No integration variables specified."
|
30 |
+
expr = sympy.parse_expr(expression_str, local_dict=present_symbols, transformations='all')
|
31 |
+
integral_val = sympy.integrate(expr, *integration_params_sympy)
|
32 |
+
return str(integral_val)
|
33 |
+
except (sympy.SympifyError, TypeError, SyntaxError) as e:
|
34 |
+
return f"Error parsing expression, bounds, or variables: {e}. Ensure bounds are numbers or valid expressions."
|
35 |
+
except Exception as e:
|
36 |
+
return f"An unexpected error occurred during integration: {e}"
|
maths/calculus/partial_derivative.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sympy
|
2 |
+
from typing import List
|
3 |
+
|
4 |
+
def partial_derivative(expression_str: str, variables_str: List[str]) -> str:
|
5 |
+
"""
|
6 |
+
Computes partial derivatives of an expression with respect to specified variables.
|
7 |
+
Example: expression_str="x**2*y**3", variables_str=["x", "y"] will compute d/dx then d/dy.
|
8 |
+
If you want d^2f/dx^2, use variables_str=["x", "x"].
|
9 |
+
|
10 |
+
Args:
|
11 |
+
expression_str: The mathematical expression (e.g., "x**2*y + y*z**2").
|
12 |
+
variables_str: A list of variable names (strings) to differentiate by, in order.
|
13 |
+
(e.g., ["x", "y"] for d/dy(d/dx(expr)) ).
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
String representation of the partial derivative or an error message.
|
17 |
+
"""
|
18 |
+
try:
|
19 |
+
symbols_in_expr = {s.name: s for s in sympy.parse_expr(expression_str, transformations='all').free_symbols}
|
20 |
+
diff_vars_symbols = []
|
21 |
+
for var_name in variables_str:
|
22 |
+
if var_name in symbols_in_expr:
|
23 |
+
diff_vars_symbols.append(symbols_in_expr[var_name])
|
24 |
+
else:
|
25 |
+
diff_vars_symbols.append(sympy.Symbol(var_name))
|
26 |
+
if not diff_vars_symbols:
|
27 |
+
return "Error: No variables specified for differentiation."
|
28 |
+
expr = sympy.parse_expr(expression_str, local_dict=symbols_in_expr, transformations='all')
|
29 |
+
current_expr = expr
|
30 |
+
for var_sym in diff_vars_symbols:
|
31 |
+
current_expr = sympy.diff(current_expr, var_sym)
|
32 |
+
return str(current_expr)
|
33 |
+
except (sympy.SympifyError, TypeError, SyntaxError) as e:
|
34 |
+
return f"Error parsing expression or variables: {e}"
|
35 |
+
except Exception as e:
|
36 |
+
return f"An unexpected error occurred: {e}"
|
maths/calculus/taylor_series_expansion.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sympy
|
2 |
+
|
3 |
+
def taylor_series_expansion(expression_str: str, variable_str: str = 'x', point: float = 0, order: int = 5) -> str:
|
4 |
+
"""
|
5 |
+
Computes the Taylor series expansion of an expression around a point.
|
6 |
+
|
7 |
+
Args:
|
8 |
+
expression_str: The mathematical expression (e.g., "exp(x)").
|
9 |
+
variable_str: The variable (default 'x').
|
10 |
+
point: The point around which to expand (default 0).
|
11 |
+
order: The order of the Taylor polynomial (default 5).
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
String representation of the Taylor series or an error message.
|
15 |
+
"""
|
16 |
+
try:
|
17 |
+
var = sympy.Symbol(variable_str)
|
18 |
+
expr = sympy.parse_expr(expression_str, local_dict={variable_str: var}, transformations='all')
|
19 |
+
series = expr.series(var, x0=point, n=order).removeO()
|
20 |
+
return str(series)
|
21 |
+
except Exception as e:
|
22 |
+
return f"Error calculating Taylor series: {e}"
|