calculate_tensor
Compute tensors from a specified metric using EinsteinPy's symbolic library. Supports tensor type selection and result simplification for streamlined symbolic algebra operations.
Instructions
Calculates a tensor from a metric using einsteinpy.symbolic.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| metric_key | Yes | ||
| simplify_result | No | ||
| tensor_type | Yes |
Implementation Reference
- server.py:723-815 (handler)The core handler function for the 'calculate_tensor' MCP tool. It validates the metric and tensor type, maps to einsteinpy classes, computes the tensor, stores it, and returns a key. Decorated with @mcp.tool() for automatic registration and schema generation.@mcp.tool() def calculate_tensor( metric_key: str, tensor_type: str, simplify_result: bool = True ) -> str: """Calculates a tensor from a metric using einsteinpy.symbolic. Args: metric_key: The key of the stored metric object. tensor_type: The type of tensor to calculate (e.g., "RICCI_TENSOR", "EINSTEIN_TENSOR"). simplify_result: Whether to apply sympy simplification to the result. Returns: A key for the stored tensor object. """ if metric_key not in metrics: return f"Error: Metric key '{metric_key}' not found." metric_obj = metrics[metric_key] # Convert string to Tensor enum tensor_enum = None try: # Handle if tensor_type is already a Tensor enum if isinstance(tensor_type, Tensor): tensor_enum = tensor_type else: # Try to match by enum value for tensor in Tensor: if tensor.value.lower() == tensor_type.lower(): tensor_enum = tensor break # If it didn't match any enum value, try to match by enum name if tensor_enum is None: try: # Try exact name match tensor_enum = Tensor[tensor_type.upper()] except KeyError: # Try normalized name (remove spaces, underscores, etc.) normalized_name = "".join( c.upper() for c in tensor_type if c.isalnum() ) for t in Tensor: if "".join(c for c in t.name if c.isalnum()) == normalized_name: tensor_enum = t break if tensor_enum is None: return f"Error: Invalid tensor type '{tensor_type}'. Available types are: {', '.join(t.value for t in Tensor)}" except Exception as e: return f"Error parsing tensor type: {str(e)}" tensor_map = { Tensor.RICCI_TENSOR: RicciTensor, Tensor.RICCI_SCALAR: RicciScalar, Tensor.EINSTEIN_TENSOR: EinsteinTensor, Tensor.WEYL_TENSOR: WeylTensor, Tensor.RIEMANN_CURVATURE_TENSOR: ChristoffelSymbols, Tensor.STRESS_ENERGY_MOMENTUM_TENSOR: StressEnergyMomentumTensor, } try: if tensor_enum not in tensor_map: return f"Error: Tensor type '{tensor_enum.value}' not implemented. Available types are: {', '.join(t.value for t in Tensor)}" tensor_class = tensor_map[tensor_enum] # Special case for RicciScalar which takes a RicciTensor if tensor_enum == Tensor.RICCI_SCALAR: ricci_tensor = RicciTensor.from_metric(metric_obj) tensor_obj = RicciScalar.from_riccitensor(ricci_tensor) else: tensor_obj = tensor_class.from_metric(metric_obj) tensor_key = f"{tensor_enum.value.lower()}_{metric_key}" tensor_objects[tensor_key] = tensor_obj # Store the tensor expression if tensor_enum == Tensor.RICCI_SCALAR: # Scalar has expr attribute tensor_expr = tensor_obj.expr if simplify_result: tensor_expr = sympy.simplify(tensor_expr) expressions[tensor_key] = tensor_expr else: # Other tensors have tensor() method tensor_expr = tensor_obj.tensor() expressions[tensor_key] = tensor_expr return tensor_key except Exception as e: return f"Error calculating tensor: {str(e)}"
- server.py:775-782 (helper)Mapping dictionary from Tensor enum values to einsteinpy tensor classes used in calculate_tensor.tensor_map = { Tensor.RICCI_TENSOR: RicciTensor, Tensor.RICCI_SCALAR: RicciScalar, Tensor.EINSTEIN_TENSOR: EinsteinTensor, Tensor.WEYL_TENSOR: WeylTensor, Tensor.RIEMANN_CURVATURE_TENSOR: ChristoffelSymbols, Tensor.STRESS_ENERGY_MOMENTUM_TENSOR: StressEnergyMomentumTensor, }
- vars.py:138-141 (helper)Enum defining valid tensor types used for validation in calculate_tensor tool.class Tensor(Enum): RICCI_SCALAR = "RicciScalar" RICCI_TENSOR = "RicciTensor" RIEMANN_CURVATURE_TENSOR = "RiemannCurvatureTensor"