Skip to main content
Glama
by apetta

matrix_decomposition

Decompose matrices using eigenvalues/vectors, SVD, QR, Cholesky, or LU factorisation for linear algebra analysis and computational workflows.

Instructions

Matrix decompositions: eigenvalues/vectors, SVD, QR, Cholesky, LU.

Examples:

EIGENVALUE DECOMPOSITION: matrix=[[4,2],[1,3]], decomposition="eigen" Result: {eigenvalues: [5, 2], eigenvectors: [[0.89,0.45],[0.71,-0.71]]}

SINGULAR VALUE DECOMPOSITION (SVD): matrix=[[1,2],[3,4],[5,6]], decomposition="svd" Result: {U: 3×3, singular_values: [9.5, 0.77], Vt: 2×2}

QR FACTORISATION: matrix=[[1,2],[3,4]], decomposition="qr" Result: {Q: orthogonal, R: upper triangular}

CHOLESKY (symmetric positive definite): matrix=[[4,2],[2,3]], decomposition="cholesky" Result: {L: [[2,0],[1,1.41]]} where A=LL^T

LU DECOMPOSITION: matrix=[[2,1],[4,3]], decomposition="lu" Result: {P: permutation, L: lower, U: upper} where A=PLU

Input Schema

NameRequiredDescriptionDefault
contextNoOptional annotation to label this calculation (e.g., 'Bond A PV', 'Q2 revenue'). Appears in results for easy identification.
output_modeNoOutput format: full (default), compact, minimal, value, or final. See batch_execute tool for details.full
matrixYesMatrix to decompose as 2D nested list (e.g., [[4,2],[1,3]])
decompositionYesDecomposition type: eigen=eigenvalues/vectors, svd=singular value, qr=QR, cholesky=symmetric positive definite, lu=LU factorisation

Input Schema (JSON Schema)

{ "properties": { "context": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": null, "description": "Optional annotation to label this calculation (e.g., 'Bond A PV', 'Q2 revenue'). Appears in results for easy identification." }, "decomposition": { "description": "Decomposition type: eigen=eigenvalues/vectors, svd=singular value, qr=QR, cholesky=symmetric positive definite, lu=LU factorisation", "enum": [ "eigen", "svd", "qr", "cholesky", "lu" ], "type": "string" }, "matrix": { "description": "Matrix to decompose as 2D nested list (e.g., [[4,2],[1,3]])", "items": { "items": { "type": "number" }, "type": "array" }, "type": "array" }, "output_mode": { "default": "full", "description": "Output format: full (default), compact, minimal, value, or final. See batch_execute tool for details.", "enum": [ "full", "compact", "minimal", "value", "final" ], "type": "string" } }, "required": [ "matrix", "decomposition" ], "type": "object" }

Implementation Reference

  • Handler function that performs various matrix decompositions (eigen, SVD, QR, Cholesky, LU) based on the specified decomposition type using SciPy's linalg module.
    async def matrix_decomposition( matrix: Annotated[List[List[float]], Field(description="Matrix to decompose as 2D nested list (e.g., [[4,2],[1,3]])")], decomposition: Annotated[Literal["eigen", "svd", "qr", "cholesky", "lu"], Field(description="Decomposition type: eigen=eigenvalues/vectors, svd=singular value, qr=QR, cholesky=symmetric positive definite, lu=LU factorisation")], ) -> str: """Matrix decompositions using SciPy: eigen (λ,v), SVD (UΣV^T), QR (orthogonal×triangular), Cholesky (LL^T), LU (PLU). For analysis, solving, and numerical stability.""" try: mat = list_to_numpy(matrix) if decomposition == "eigen": if mat.shape[0] != mat.shape[1]: raise ValueError( f"Eigenvalue decomposition requires square matrix. Got shape: {mat.shape}" ) eigenvalues: NDArray[np.complexfloating] eigenvectors: NDArray[np.complexfloating] eigenvalues, eigenvectors = la.eig(mat) # type: ignore[misc] return format_result( { "eigenvalues": eigenvalues.tolist(), "eigenvectors": eigenvectors.tolist(), }, {"decomposition": decomposition} ) elif decomposition == "svd": U, s, Vt = la.svd(mat) return format_result( { "U": U.tolist(), "singular_values": s.tolist(), "Vt": Vt.tolist(), }, {"decomposition": decomposition} ) elif decomposition == "qr": Q: NDArray[np.floating] R: NDArray[np.floating] Q, R = la.qr(mat) # type: ignore[misc] return format_result( {"Q": Q.tolist(), "R": R.tolist()}, {"decomposition": decomposition} ) elif decomposition == "cholesky": if mat.shape[0] != mat.shape[1]: raise ValueError( f"Cholesky decomposition requires square matrix. Got shape: {mat.shape}" ) # Check if matrix is symmetric if not np.allclose(mat, mat.T): raise ValueError("Cholesky decomposition requires symmetric matrix") try: L = la.cholesky(mat, lower=True) return format_result( {"L": L.tolist()}, {"decomposition": decomposition, "note": "A = L * L^T"} ) except np.linalg.LinAlgError: raise ValueError("Matrix is not positive definite") elif decomposition == "lu": P, L, U = la.lu(mat) # type: ignore[misc] return format_result( { "P": P.tolist(), "L": L.tolist(), "U": U.tolist(), }, {"decomposition": decomposition, "note": "A = P * L * U"} ) else: raise ValueError(f"Unknown decomposition: {decomposition}") except Exception as e: if isinstance(e, ValueError): raise raise ValueError(f"Matrix decomposition failed: {str(e)}")
  • MCP tool registration for 'matrix_decomposition' including name, description, examples, and annotations.
    @mcp.tool( name="matrix_decomposition", description="""Matrix decompositions: eigenvalues/vectors, SVD, QR, Cholesky, LU. Examples: EIGENVALUE DECOMPOSITION: matrix=[[4,2],[1,3]], decomposition="eigen" Result: {eigenvalues: [5, 2], eigenvectors: [[0.89,0.45],[0.71,-0.71]]} SINGULAR VALUE DECOMPOSITION (SVD): matrix=[[1,2],[3,4],[5,6]], decomposition="svd" Result: {U: 3×3, singular_values: [9.5, 0.77], Vt: 2×2} QR FACTORISATION: matrix=[[1,2],[3,4]], decomposition="qr" Result: {Q: orthogonal, R: upper triangular} CHOLESKY (symmetric positive definite): matrix=[[4,2],[2,3]], decomposition="cholesky" Result: {L: [[2,0],[1,1.41]]} where A=LL^T LU DECOMPOSITION: matrix=[[2,1],[4,3]], decomposition="lu" Result: {P: permutation, L: lower, U: upper} where A=PLU""", annotations=ToolAnnotations( title="Matrix Decomposition", readOnlyHint=True, idempotentHint=True, ), )
  • Input schema definitions using Pydantic Annotated types for matrix (List[List[float]]) and decomposition type (Literal).
    matrix: Annotated[List[List[float]], Field(description="Matrix to decompose as 2D nested list (e.g., [[4,2],[1,3]])")], decomposition: Annotated[Literal["eigen", "svd", "qr", "cholesky", "lu"], Field(description="Decomposition type: eigen=eigenvalues/vectors, svd=singular value, qr=QR, cholesky=symmetric positive definite, lu=LU factorisation")], ) -> str:

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/apetta/vibe-math-mcp'

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