-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculus.py
144 lines (114 loc) · 4.47 KB
/
calculus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# calculus.py
##############################################################################
# Imports #
##############################################################################
from sympy import symbols, limit, diff, sin, integrate, oo, Function, dsolve, Eq, sympify, lambdify
import matplotlib.pyplot as plt
import numpy as np
##############################################################################
# CONSTANTS #
##############################################################################
x = symbols('x')
def plot_function_from_string(function_string: str, x_range: tuple) -> bool | None:
"""
Plots a function given as a string.
Parameters:
function_string (str): A string representation of the function to plot.
x_range (tuple): A tuple specifying the range of x-values for the plot.
Returns:
bool: True if the function was plotted successfully, False otherwise.
"""
try:
f = sympify(function_string)
f_lambdified = lambdify(x, f, "numpy")
x_values = np.linspace(*x_range, 400)
y_values = f_lambdified(x_values)
plt.plot(x_values, y_values)
plt.show()
return True
except Exception as e:
print(f"Error in plotting the function: {e}")
return False
def calculate_limit(function: str, point: float) -> float | None:
"""
Calculates the limit of a function at a given point.
Parameters:
function (str): The function to calculate the limit of.
point (float): The point to calculate the limit at.
Returns:
float: The calculated limit value or None if an error occurs.
"""
try:
return limit(sympify(function), x, point)
except Exception as e:
print(f"Error in calculating the limit: {e}")
return None
def calculate_derivative(function: str) -> str | None:
"""
Calculates the derivative of a function.
Parameters:
function (str): The function to calculate the derivative of.
Returns:
str: The derivative of the function as a string or None if an error occurs.
"""
try:
return str(diff(sympify(function), x))
except Exception as e:
print(f"Error in calculating the derivative: {e}")
return None
def calculate_integral(function: str) -> str | None:
"""
Calculates the indefinite integral of a function.
Parameters:
function (str): The function to calculate the integral of.
Returns:
str: The indefinite integral of the function as a string or None if an error occurs.
"""
try:
return str(integrate(sympify(function), x))
except Exception as e:
print(f"Error in calculating the integral: {e}")
return None
def calculate_definite_integral(function: str, a: float, b: float) -> str | None:
"""
Calculates the definite integral of a function from a to b.
Parameters:
function (str): The function to calculate the integral of.
a, b (float): The limits of integration.
Returns:
str: The definite integral of the function as a string or None if an error occurs.
"""
try:
return str(integrate(sympify(function), (x, a, b)))
except Exception as e:
print(f"Error in calculating the definite integral: {e}")
return None
def calculate_improper_integral(function: str, a: float) -> float | None:
"""
Calculates the improper integral of a function from a to infinity.
Parameters:
function (str): The function to calculate the integral of.
a (float): The lower limit of integration.
Returns:
float: The calculated improper integral value or None if an error occurs.
"""
try:
expr = sympify(function)
result = integrate(expr, (x, a, oo)).evalf() # Evaluate the integral numerically
return float(result)
except Exception as e:
print(f"Error in calculating the improper integral: {e}")
return None
def calculate_double_derivative(function: str) -> str | None:
"""
Calculates the second derivative of a function.
Parameters:
function (str): The function to calculate the second derivative of.
Returns:
str: The second derivative of the function as a string or None if an error occurs.
"""
try:
return str(diff(sympify(function), x, x))
except Exception as e:
print(f"Error in calculating the double derivative: {e}")
return None