Spaces:
Sleeping
Sleeping
Update app.py
Browse filesfix error - InterpreterError: The variable math is not defined.
app.py
CHANGED
|
@@ -13,7 +13,7 @@ def monte_carlo_integration(func_expr: str, a: float, b: float, samples: int = 1
|
|
| 13 |
"""A tool that computes the approximate definite integral of a given function between limits a and b using Monte Carlo integration.
|
| 14 |
|
| 15 |
Args:
|
| 16 |
-
func_expr: A string representing a mathematical function of x (e.g., "x**2 + 3*x + 2").
|
| 17 |
a: The lower bound of integration.
|
| 18 |
b: The upper bound of integration.
|
| 19 |
samples: The number of random samples to use for the approximation. Defaults to 10000.
|
|
@@ -24,9 +24,10 @@ def monte_carlo_integration(func_expr: str, a: float, b: float, samples: int = 1
|
|
| 24 |
import random
|
| 25 |
import math
|
| 26 |
|
| 27 |
-
#
|
| 28 |
safe_dict = {k: getattr(math, k) for k in dir(math) if not k.startswith("__")}
|
| 29 |
-
|
|
|
|
| 30 |
# Define the function to integrate safely using eval
|
| 31 |
def f(x):
|
| 32 |
try:
|
|
|
|
| 13 |
"""A tool that computes the approximate definite integral of a given function between limits a and b using Monte Carlo integration.
|
| 14 |
|
| 15 |
Args:
|
| 16 |
+
func_expr: A string representing a mathematical function of x (e.g., "x**2 + 3*x + 2" or "math.sin(x)").
|
| 17 |
a: The lower bound of integration.
|
| 18 |
b: The upper bound of integration.
|
| 19 |
samples: The number of random samples to use for the approximation. Defaults to 10000.
|
|
|
|
| 24 |
import random
|
| 25 |
import math
|
| 26 |
|
| 27 |
+
# Create a safe dictionary of math functions and add the math module itself.
|
| 28 |
safe_dict = {k: getattr(math, k) for k in dir(math) if not k.startswith("__")}
|
| 29 |
+
safe_dict["math"] = math # Allow expressions to use the 'math' prefix
|
| 30 |
+
|
| 31 |
# Define the function to integrate safely using eval
|
| 32 |
def f(x):
|
| 33 |
try:
|