Skip to main content
Glama
sdiehl
by sdiehl

create_matrix

Generate symbolic or numeric matrices using a list of lists. Define matrix elements as numbers or expressions, optionally assign a variable name, and store the result for symbolic algebra operations.

Instructions

Creates a SymPy matrix from the provided data.

Args:
    matrix_data: A list of lists representing the rows and columns of the matrix.
                Each element can be a number or a string expression.
    matrix_var_name: Optional name for storing the matrix. If not provided, a
                     sequential name will be generated.

Example:
    # Create a 2x2 matrix with numeric values
    matrix_key = create_matrix([[1, 2], [3, 4]], "M")

    # Create a matrix with symbolic expressions (assuming x, y are defined)
    matrix_key = create_matrix([["x", "y"], ["x*y", "x+y"]])

Returns:
    A key for the stored matrix.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
matrix_dataYes
matrix_var_nameNo

Implementation Reference

  • The handler function for the 'create_matrix' tool. It parses the input matrix data, creates a SymPy Matrix object, stores it in the global expressions dictionary with a generated or provided key, and returns the key. Decorated with @mcp.tool() for registration.
    def create_matrix(
        matrix_data: List[List[Union[int, float, str]]],
        matrix_var_name: Optional[str] = None,
    ) -> str:
        """Creates a SymPy matrix from the provided data.
    
        Args:
            matrix_data: A list of lists representing the rows and columns of the matrix.
                        Each element can be a number or a string expression.
            matrix_var_name: Optional name for storing the matrix. If not provided, a
                             sequential name will be generated.
    
        Example:
            # Create a 2x2 matrix with numeric values
            matrix_key = create_matrix([[1, 2], [3, 4]], "M")
    
            # Create a matrix with symbolic expressions (assuming x, y are defined)
            matrix_key = create_matrix([["x", "y"], ["x*y", "x+y"]])
    
        Returns:
            A key for the stored matrix.
        """
        global expression_counter
    
        try:
            # Process each element to handle expressions
            processed_data = []
            for row in matrix_data:
                processed_row = []
                for elem in row:
                    if isinstance(elem, (int, float)):
                        processed_row.append(elem)
                    else:
                        # Parse the element as an expression using local variables
                        parse_dict = {**local_vars, **functions}
                        parsed_elem = parse_expr(str(elem), local_dict=parse_dict)
                        processed_row.append(parsed_elem)
                processed_data.append(processed_row)
    
            # Create the SymPy matrix
            matrix = Matrix(processed_data)
    
            # Generate a key for the matrix
            if matrix_var_name is None:
                matrix_key = f"matrix_{expression_counter}"
                expression_counter += 1
            else:
                matrix_key = matrix_var_name
    
            # Store the matrix in the expressions dictionary
            expressions[matrix_key] = matrix
    
            return matrix_key
        except Exception as e:
            return f"Error creating matrix: {str(e)}"
  • server.py:1555-1555 (registration)
    The @mcp.tool() decorator registers the create_matrix function as an MCP tool.
    def create_matrix(

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sdiehl/sympy-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server