Camel.toolkits.sympy toolkit
SymPyToolkit
A toolkit for performing symbolic computations using SymPy. This includes methods for Algebraic manipulation calculus and Linear Algebra.
init
Initializes the toolkit with a default variable and logging.
Parameters:
- default_variable (str): The default variable for operations (default: :obj:
x
)
simplify_expression
Simplifies a mathematical expression.
Parameters:
- expression (str): The mathematical expression to simplify, provided as a string.
Returns:
str: JSON string containing the simplified mathematical
expression in the "result"
field. If an error occurs,
the "status"
field will be set to "error"
with a
corresponding "message"
.
expand_expression
Expands an algebraic expression.
Parameters:
- expression (str): The algebraic expression to expand, provided as a string.
Returns:
str: JSON string containing the expanded algebraic expression
in the "result"
field. If an error occurs, the JSON
string will include an "error"
field with the corresponding
error message.
factor_expression
Factors an algebraic expression.
Parameters:
- expression (str): The algebraic expression to factor, provided as a string.
Returns:
str: JSON string containing the factored algebraic expression
in the "result"
field. If an error occurs, the JSON string
will include an "error"
field with the corresponding error
message.
solve_linear_system
Solves a system of linear equations.
Parameters:
- equations (List[str]): A list of strings representing the linear equations to be solved.
- variables (List[str]): A list of strings representing the variables involved in the equations.
Returns:
str: JSON string containing the solution to the system of equations
in the "result"
field. Each solution is represented as
a tuple of values corresponding to the variables. If an
error occurs, the JSON string will include an "error"
field with the corresponding error message.
solve_nonlinear_system
Solves a system of nonlinear equations.
Parameters:
- sympy_equations (List[str]): A list of strings representing the nonlinear equations to be solved. The equation to solve, must be compatible with SymPy, provided as a string.
- variables (List[str]): A list of strings representing the variables involved in the equations.
Returns:
str: JSON string containing the solutions to the system of
equations in the "result"
field. Each solution is
represented as a tuple of values corresponding to the
variables. If an error occurs, the JSON string will
include an "error"
field with the corresponding
error message.
solve_univariate_inequality
Solves a single-variable inequality.
Parameters:
- inequality (str): A string representing the inequality to be solved.
- variable (str): The variable in the inequality.
Returns:
str: JSON string containing the solution to the inequality in the
"result"
field. The solution is represented in a symbolic
format (e.g., intervals or expressions). If an error occurs,
the JSON string will include an "error"
field with the
corresponding error message.
reduce_inequalities
Reduces a system of inequalities.
Parameters:
- inequalities (List[str]): A list of strings representing the inequalities to be reduced.
Returns:
str: JSON string containing the reduced system of inequalities
in the "result"
field. The solution is represented in
a symbolic format (e.g., combined intervals or expressions).
If an error occurs, the JSON string will include an "error"
field with the corresponding error message.
polynomial_representation
Represents an expression as a polynomial.
Parameters:
- expression (str): The mathematical expression to represent as a polynomial, provided as a string.
- variable (str): The variable with respect to which the polynomial representation will be created.
Returns:
str: JSON string containing the polynomial representation of the
expression in the "result"
field. The polynomial is returned
in a symbolic format. If an error occurs, the JSON string will
include an "error"
field with the corresponding error
message.
polynomial_degree
Returns the degree of a polynomial.
Parameters:
- expression (str): The polynomial expression for which the degree is to be determined, provided as a string.
- variable (str): The variable with respect to which the degree of the polynomial is calculated.
Returns:
str: JSON string containing the degree of the polynomial in the
"result"
field. If an error occurs, the JSON string will
include an "error"
field with the corresponding error
message.
polynomial_coefficients
Returns the coefficients of a polynomial.
Parameters:
- expression (str): The polynomial expression from which the coefficients are to be extracted, provided as a string.
- variable (str): The variable with respect to which the polynomial coefficients are determined.
Returns:
str: JSON string containing the list of coefficients of the
polynomial in the "result"
field. The coefficients are
ordered from the highest degree term to the constant term.
If an error occurs, the JSON string will include an `“error”
field with the corresponding error message.
solve_equation
Solves an equation for a specific variable.
Parameters:
- sympy_equation (str): The equation to solve, must be compatible with SymPy, provided as a string.
- variable (str, optional): The variable to solve for. If not specified, the function will use the default variable.
Returns:
str: JSON string containing the solutions to the equation in the
"result"
field. Each solution is represented as a string.
If an error occurs, the JSON string will include an "error"
field with the corresponding error message.
find_roots
Finds the roots of a polynomial or algebraic equation.
Parameters:
- expression (str): The polynomial or algebraic equation for which the roots are to be found, provided as a string.
Returns:
str: JSON string containing the roots of the expression in the
"result"
field. The roots are represented as a list of
solutions. If an error occurs, the JSON string will include
a "status"
field set to "error"
and a "message"
field
with the corresponding error description.
differentiate
Differentiates an expression with respect to a variable.
Parameters:
- expression (str): The mathematical expression to differentiate, provided as a string.
- variable (str, optional): The variable with respect to which the differentiation is performed. If not specified, the default variable is used.
Returns:
str: JSON string containing the derivative of the expression in the
"result"
field. If an error occurs, the JSON string will
include an "error"
field with the corresponding error
message.
integrate
Integrates an expression with respect to a variable.
Parameters:
- expression (str): The mathematical expression to integrate, provided as a string.
- variable (str, optional): The variable with respect to which the integration is performed. If not specified, the default variable is used.
Returns:
str: JSON string containing the integral of the expression in the
"result"
field. If an error occurs, the JSON string will
include an "error"
field with the corresponding error
message.
definite_integral
Computes the definite integral of an expression within given bounds.
Parameters:
- expression (str): The mathematical expression to integrate, provided as a string.
- variable (str): The variable with respect to which the definite integration is performed.
- lower (float): The lower limit of the integration.
- upper (float): The upper limit of the integration.
Returns:
str: JSON string containing the result of the definite integral
in the "result"
field. If an error occurs, the JSON string
will include an "error"
field with the corresponding error
message.
series_expansion
Expands an expression into a Taylor series around a given point up to a specified order.
Parameters:
- expression (str): The mathematical expression to expand, provided as a string.
- variable (str): The variable with respect to which the series expansion is performed.
- point (float): The point around which the Taylor series is expanded.
- order (int): The order up to which the series expansion is computed.
Returns:
str: JSON string containing the Taylor series expansion of the
expression in the "result"
field. If an error occurs,
the JSON string will include an "error"
field with the
corresponding error message.
compute_limit
Computes the limit of an expression as a variable approaches a point.
Parameters:
- expression (str): The mathematical expression for which the limit is to be computed, provided as a string.
- variable (str): The variable with respect to which the limit is computed.
- point (float): The point that the variable approaches.
Returns:
str: JSON string containing the computed limit of the expression
in the "result"
field. If an error occurs, the JSON string
will include an "error"
field with the corresponding error
message.
find_critical_points
Finds the critical points of an expression by setting its derivative to zero.
Parameters:
- expression (str): The mathematical expression for which critical points are to be found, provided as a string.
- variable (str): The variable with respect to which the critical points are determined.
Returns:
str: JSON string containing the critical points of the expression
in the "result"
field. The critical points are returned as a
list of values corresponding to the variable. If an error
occurs, the JSON string will include an "error"
field with
the corresponding error message.
check_continuity
Checks if an expression is continuous at a given point.
Parameters:
- expression (str): The mathematical expression to check for continuity, provided as a string.
- variable (str): The variable with respect to which continuity is checked.
- point (float): The point at which the continuity of the expression is checked.
Returns:
str: JSON string containing the result of the continuity check in
the "result"
field. The result will be "True"
if the
expression is continuous at the given point, otherwise
"False"
. If an error occurs, the JSON string will include
an "error"
field with the corresponding error message.
compute_determinant
Computes the determinant of a matrix.
Parameters:
- matrix (List[List[float]]): A two-dimensional list representing the matrix for which the determinant is to be computed.
Returns:
str: JSON string containing the determinant of the matrix in the
"result"
field. If an error occurs, the JSON string will
include an "error"
field with the corresponding error
message.
compute_inverse
Computes the inverse of a matrix.
Parameters:
- matrix (List[List[float]]): A two-dimensional list representing the matrix for which the inverse is to be computed.
Returns:
str: JSON string containing the inverse of the matrix in the
"result"
field. The inverse is represented in a symbolic
matrix format. If an error occurs, the JSON string will
include an "error"
field with the corresponding error
message.
compute_eigenvalues
Computes the eigenvalues of a matrix.
Parameters:
- matrix (List[List[float]]): A two-dimensional list representing the matrix for which the eigenvalues are to be computed.
Returns:
str: JSON string containing the eigenvalues of the matrix in the
"result"
field. The eigenvalues are represented as a
dictionary where keys are the eigenvalues (as strings) and
values are their multiplicities (as strings). If an error
occurs, the JSON string will include an "error"
field
with the corresponding error message.
compute_eigenvectors
Computes the eigenvectors of a matrix.
Parameters:
- matrix (List[List[float]]): A two-dimensional list representing the matrix for which the eigenvectors are to be computed.
Returns:
str: JSON string containing the eigenvectors of the matrix in the
"result"
field. Each eigenvalue is represented as a
dictionary with the following keys:
"eigenvalue"
: The eigenvalue (as a string)."multiplicity"
: The multiplicity of the eigenvalue (as an integer)."eigenvectors"
: A list of eigenvectors (each represented as a string).
If an error occurs, the JSON string will include an "error"
field with the corresponding error message.
compute_nullspace
Computes the null space of a matrix.
Parameters:
- matrix (List[List[float]]): A two-dimensional list representing the matrix for which the null space is to be computed.
Returns:
str: JSON string containing the null space of the matrix in the
"result"
field. The null space is represented as a list of
basis vectors, where each vector is given as a string in
symbolic format. If an error occurs, the JSON string will
include an "error"
field with the corresponding error
message.
compute_rank
Computes the rank of a matrix.
Parameters:
- matrix (List[List[float]]): A two-dimensional list representing the matrix for which the rank is to be computed.
Returns:
str: JSON string containing the rank of the matrix in the
"result"
field. The rank is represented as an integer.
If an error occurs,the JSON string will include an
"error"
field with the corresponding error message.
compute_inner_product
Computes the inner (dot) product of two vectors.
Parameters:
- vector1 (List[float]): The first vector as a list of floats.
- vector2 (List[float]): The second vector as a list of floats.
Returns:
str: JSON string containing the inner product in the "result"
field. If an error occurs, the JSON string will include an
"error"
field with the corresponding error message.
handle_exception
Handles exceptions by logging and returning error details.
Parameters:
- func_name (str): The name of the function where the exception occurred.
- error (Exception): The exception object containing details about the error.
Returns:
str: JSON string containing the error details. The JSON includes:
"status"
: Always set to"error"
."message"
: A string representation of the exception message.
get_tools
Returns:
List[FunctionTool]: A list of FunctionTool
objects representing
the toolkit’s methods, making them accessible to the agent.