calculate_gradient
Compute the gradient of a scalar field to obtain its vector field representation, enabling advanced symbolic algebra operations with precise mathematical results.
Instructions
Calculates the gradient of a scalar field using SymPy's gradient function.
Args:
scalar_field_key: The key of the scalar field expression.
Example:
# First create a coordinate system
create_coordinate_system("R")
# Create a scalar field f = x^2 + y^2 + z^2
scalar_field = introduce_expression("R_x**2 + R_y**2 + R_z**2")
# Calculate gradient
grad_result = calculate_gradient(scalar_field)
# Returns (2x, 2y, 2z)
Returns:
A key for the gradient vector field expression.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| scalar_field_key | Yes |
Implementation Reference
- server.py:1270-1309 (handler)The core handler implementation for the 'calculate_gradient' MCP tool. It retrieves a scalar field expression by key, computes its gradient using SymPy's vector.gradient function (which requires a coordinate system context from prior tool calls), stores the resulting vector field with a generated key, and returns that key. The @mcp.tool() decorator registers it as an MCP tool.@mcp.tool() def calculate_gradient(scalar_field_key: str) -> str: """Calculates the gradient of a scalar field using SymPy's gradient function. Args: scalar_field_key: The key of the scalar field expression. Example: # First create a coordinate system create_coordinate_system("R") # Create a scalar field f = x^2 + y^2 + z^2 scalar_field = introduce_expression("R_x**2 + R_y**2 + R_z**2") # Calculate gradient grad_result = calculate_gradient(scalar_field) # Returns (2x, 2y, 2z) Returns: A key for the gradient vector field expression. """ global expression_counter if scalar_field_key not in expressions: return f"Error: Scalar field with key '{scalar_field_key}' not found." try: scalar_field = expressions[scalar_field_key] # Calculate gradient grad_result = gradient(scalar_field) # Store the result result_key = f"vector_{expression_counter}" expressions[result_key] = grad_result expression_counter += 1 return result_key except Exception as e: return f"Error calculating gradient: {str(e)}"
- server.py:1270-1270 (registration)The @mcp.tool() decorator registers the calculate_gradient function as an MCP tool.@mcp.tool()
- tests/test_calculus.py:215-235 (handler)Unit test demonstrating usage of calculate_gradient tool.def test_calculate_gradient(self): # Create coordinate system create_coordinate_system("R") # Introduce variables intro("x", [Assumption.REAL], []) intro("y", [Assumption.REAL], []) intro("z", [Assumption.REAL], []) # Create a simple scalar field scalar_field_key = introduce_expression("x**2 + y**2 + z**2") # Calculate gradient grad_key = calculate_gradient(scalar_field_key) # Check if gradient calculation was successful if "error" not in grad_key.lower(): assert grad_key.startswith("vector_") else: assert False, f"Failed to calculate gradient: {grad_key}"