Skip to content

Matrix

The /matrix endpoint performs mathematical operations on matrices. It supports arithmetic, scalar multiplication, transposition, and linear algebra operations on up to 20×20 matrices.

How it works?

Determinant is computed via Gaussian elimination with partial pivoting. Matrix inverse uses Gauss-Jordan elimination on an augmented matrix. All inputs are validated: values must be finite numbers, rows must have uniform length, and the matrix must not exceed 20×20.

Parameters

The request body must be JSON:

ParameterRequiredDescription
operationYesThe operation to perform (see available operations)
matrixYesThe input matrix (2D array of numbers)
matrix2DependsSecond matrix — required for add, subtract, multiply
scalarDependsScalar value — required for scalar, used by identity

Available Operations

OperationDescriptionRequires
addElement-wise additionmatrix2
subtractElement-wise subtractionmatrix2
multiplyMatrix multiplicationmatrix2
scalarMultiply all elements by a scalarscalar
transposeTranspose rows and columns
determinantCompute the determinant (square matrices only)
inverseCompute the inverse (square, non-singular matrices)
identityGenerate an identity matrix of size scalar×scalarscalar

Response Fields

The response format depends on the operation:

Operationresult type
add, subtract, multiply, scalar, transpose, inverse, identitynumber[][]
determinantnumber

Code Examples

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "operation": "determinant",
    "matrix": [
      [
        1,
        2
      ],
      [
        3,
        4
      ]
    ]
  }' \
  "https://api.sylvain.sh/v5/matrix"

Try It

Error Handling

If parameters are missing or invalid, the API will return an error:

Error MessageDescription
Please provide a valid operationThe operation parameter is missing or invalid. Valid values: add, subtract, multiply, scalar, transpose, determinant, inverse, identity
Matrix must be a non-empty 2D arrayThe matrix is missing or malformed
All rows must have the same lengthThe matrix rows are not uniform
Matrix values must be finite numbersThe matrix contains non-numeric values
Matrix exceeds maximum size of 20×20The matrix is larger than 20×20
Both matrices must have the same dimensionsDimension mismatch for add/subtract
Matrix dimensions incompatible for multiplicationColumn count of A ≠ row count of B
Matrix must be squareOperation requires a square matrix
Matrix is singular and cannot be invertedThe matrix has no inverse