matrix_multiplication
Multiply two matrices with 64-bit floating-point precision. Input matrices as nested lists, ensuring compatible dimensions. Supports integers, fractions, and decimal elements for precise computation.
Instructions
Multiplies two matrices with 64 bit floating point precision and returns the result as a matrix. You need to provide them in the format of nested lists. For example, [[1, 2], [3, 4]] would represent a 2x2 matrix. Each sub list represents a row in the matrix, and each element in the sub list represents a column. The matrices must be compatible for multiplication, meaning the number of columns in the first matrix must equal the number of rows in the second matrix. You can also use fractions for individual elements if you want to, like [1/2, 1/3, 1/4].
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| matrix_1 | Yes | ||
| matrix_2 | Yes |
Implementation Reference
- src/math_server.py:107-144 (handler)The 'matrix_multiplication' tool handler: validates input matrices, converts to NumPy arrays, checks dimensions, performs matrix multiplication with np.matmul, rounds to 64-bit float precision, and returns as list of lists.@math_mcp.tool def matrix_multiplication( matrix_1: list[list[int | float]], matrix_2: list[list[int | float]], ) -> list[list[float]]: """Multiplies two matrices with 64 bit floating point precision and returns the result as a matrix. You need to provide them in the format of nested lists. For example, [[1, 2], [3, 4]] would represent a 2x2 matrix. Each sub list represents a row in the matrix, and each element in the sub list represents a column. The matrices must be compatible for multiplication, meaning the number of columns in the first matrix must equal the number of rows in the second matrix. You can also use fractions for individual elements if you want to, like [1/2, 1/3, 1/4]. """ # Convert matrices to numpy arrays for efficient multiplication matrix_1 = np.array(matrix_1, dtype=np.float64) matrix_2 = np.array(matrix_2, dtype=np.float64) # This is technically allowed by Fast MCP, but it is an error here if not matrix_1.size: logging.error("Matrix 1 is empty.") raise ValueError("Matrix 1 cannot be empty.") if not matrix_2.size: logging.error("Matrix 2 is empty.") raise ValueError("Matrix 2 cannot be empty.") # Check if matrices have compatible dimensions for multiplication if len(matrix_1[0]) != len(matrix_2): logging.error("Incompatible matrix dimensions for multiplication.") raise ValueError("Incompatible matrix dimensions.") # Use numpy for fast matrix multiplication result = np.round(np.matmul(matrix_1, matrix_2), decimals=SIXTY_FOUR_BIT_FLOAT_DECIMAL_PLACES) logging.info(f"Multiplying matrices: {matrix_1} * {matrix_2} -> Result: {result}") # Convert result to list of lists so Fast MCP can serialize it properly result = result.tolist() return result