{
  "id": "matrix",
  "version": "a6f410c78df2",
  "status": "published",
  "name": "Matrix Calculator",
  "question": "What is my matrix result?",
  "summary": "Adds, subtracts, and multiplies matrices up to 6 × 6, and finds the transpose, determinant, inverse, power, rank, and trace, in exact fractions and decimals.",
  "category": "math",
  "subcategory": "linear-algebra",
  "url": "https://www.acalculator.org/math/matrix-calculator",
  "markdown": "https://www.acalculator.org/math/matrix-calculator.md",
  "kind": "function",
  "method": "Entry by entry for A ± B and k × A; (AB)ᵢⱼ = Σₖ Aᵢₖ Bₖⱼ; det(A) by elimination to triangular form; A⁻¹ by Gauss-Jordan on [A | I]; Aⁿ by repeated products; rank by row reduction; trace = sum of the diagonal.",
  "assumptions": [
    "Every cell is read as an exact fraction: the nearest fraction with a bottom up to 1,000,000 when it rounds to the same number (so 1/3 is 1/3), otherwise its decimal (0.1 is 1/10).",
    "Matrices are 1 × 1 to 6 × 6. A × B needs as many columns in A as rows in B; A + B and A − B need the same size.",
    "The determinant, inverse, power, and trace need a square A; A⁻¹ exists only when det(A) is not 0."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "op": {
        "title": "Operation",
        "description": "What to work out from matrix A (and B).",
        "type": "string",
        "enum": [
          "mul",
          "add",
          "sub",
          "scalar",
          "transpose",
          "det",
          "inverse",
          "power",
          "rank",
          "trace"
        ]
      },
      "a": {
        "title": "Matrix A",
        "description": "The first matrix, 1 to 6 rows and columns.",
        "type": "array",
        "items": {
          "type": "array",
          "items": {
            "type": "number"
          }
        }
      },
      "b": {
        "title": "Matrix B",
        "description": "The second matrix, for A + B, A − B, and A × B.",
        "type": "array",
        "items": {
          "type": "array",
          "items": {
            "type": "number"
          }
        }
      },
      "k": {
        "title": "Scalar k",
        "description": "The number to multiply A by.",
        "type": "number"
      },
      "n": {
        "title": "Power n",
        "description": "The whole number of times to multiply A by itself, from 0 to 20.",
        "type": "integer",
        "minimum": 0,
        "maximum": 20
      }
    }
  },
  "outputs": {
    "result": {
      "label": "Result",
      "description": "The answer in exact fractions: a matrix with rows separated by semicolons, or a number.",
      "format": "text"
    },
    "decimal": {
      "label": "Result in decimals",
      "description": "The same answer in decimals, each rounded half up to 10 significant figures.",
      "format": "text"
    },
    "size": {
      "label": "Size of the result",
      "description": "Rows × columns of a matrix answer.",
      "format": "text"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "op": "mul",
      "k": 2,
      "n": 2,
      "a": "1,2;3,4",
      "b": "5,6;7,8"
    },
    "outputs": {
      "result": "[19, 22; 43, 50]",
      "decimal": "[19, 22; 43, 50]",
      "size": "2 × 2"
    },
    "text": "The answer is [19, 22; 43, 50]."
  },
  "examples": [
    {
      "given": {
        "op": "mul",
        "a": [
          [
            1,
            2
          ],
          [
            3,
            4
          ]
        ],
        "b": [
          [
            5,
            6
          ],
          [
            7,
            8
          ]
        ]
      },
      "expect": {
        "result": "[19, 22; 43, 50]",
        "size": "2 × 2"
      },
      "source": "OpenStax, Precalculus 2e, §9.5, matrix multiplication (https://openstax.org/books/precalculus-2e/pages/9-5-matrices-and-matrix-operations); hand calculation in content.mdx: row times column, 1 × 5 + 2 × 7 = 19 (Strang, Introduction to Linear Algebra, §2.4)"
    },
    {
      "given": {
        "op": "mul",
        "a": [
          [
            1,
            2,
            3
          ],
          [
            4,
            5,
            6
          ]
        ],
        "b": [
          [
            7,
            8
          ],
          [
            9,
            10
          ],
          [
            11,
            12
          ]
        ]
      },
      "expect": {
        "result": "[58, 64; 139, 154]",
        "size": "2 × 2"
      },
      "source": "hand calculation in content.mdx: 1 × 7 + 2 × 9 + 3 × 11 = 58"
    },
    {
      "given": {
        "op": "add",
        "a": [
          [
            1,
            2
          ],
          [
            3,
            4
          ]
        ],
        "b": [
          [
            0.5,
            -1
          ],
          [
            0.3333333333333333,
            0
          ]
        ]
      },
      "expect": {
        "result": "[3/2, 1; 10/3, 4]",
        "decimal": "[1.5, 1; 3.333333333, 4]"
      },
      "source": "hand calculation in content.mdx: 1 + 1/2 = 3/2, 3 + 1/3 = 10/3 (a cell typed 1/3 is read as 1/3)"
    },
    {
      "given": {
        "op": "sub",
        "a": [
          [
            1,
            2
          ],
          [
            3,
            4
          ]
        ],
        "b": [
          [
            5,
            6
          ],
          [
            7,
            8
          ]
        ]
      },
      "expect": {
        "result": "[-4, -4; -4, -4]",
        "decimal": "[-4, -4; -4, -4]"
      },
      "source": "hand calculation in content.mdx: 1 − 5 = −4 in every place"
    },
    {
      "given": {
        "op": "det",
        "a": [
          [
            2,
            -3,
            1
          ],
          [
            2,
            0,
            -1
          ],
          [
            1,
            4,
            5
          ]
        ]
      },
      "expect": {
        "result": "49",
        "decimal": "49"
      },
      "source": "hand calculation in content.mdx: cofactor expansion 2(0 + 4) + 3(10 + 1) + 1(8 − 0) = 49"
    },
    {
      "given": {
        "op": "inverse",
        "a": [
          [
            4,
            7
          ],
          [
            2,
            6
          ]
        ]
      },
      "expect": {
        "result": "[3/5, -7/10; -1/5, 2/5]",
        "decimal": "[0.6, -0.7; -0.2, 0.4]"
      },
      "source": "hand calculation in content.mdx: 1/(4 × 6 − 7 × 2) × [6, −7; −2, 4]"
    },
    {
      "given": {
        "op": "power",
        "a": [
          [
            1,
            1
          ],
          [
            1,
            0
          ]
        ],
        "n": 10
      },
      "expect": {
        "result": "[89, 55; 55, 34]"
      },
      "source": "hand calculation in content.mdx: the Fibonacci matrix, [[1, 1], [1, 0]]ⁿ = [[F(n+1), F(n)], [F(n), F(n−1)]], F(10) = 55"
    },
    {
      "given": {
        "op": "rank",
        "a": [
          [
            1,
            2,
            3
          ],
          [
            2,
            4,
            6
          ],
          [
            1,
            0,
            1
          ]
        ]
      },
      "expect": {
        "result": "2"
      },
      "source": "hand calculation in content.mdx: row 2 is twice row 1, rows 1 and 3 are independent"
    },
    {
      "given": {
        "op": "trace",
        "a": [
          [
            1,
            2
          ],
          [
            3,
            4
          ]
        ]
      },
      "expect": {
        "result": "5"
      },
      "source": "hand calculation in content.mdx: 1 + 4"
    },
    {
      "given": {
        "op": "transpose",
        "a": [
          [
            1,
            2,
            3
          ],
          [
            4,
            5,
            6
          ]
        ]
      },
      "expect": {
        "result": "[1, 4; 2, 5; 3, 6]",
        "size": "3 × 2"
      },
      "source": "hand calculation in content.mdx: rows become columns"
    },
    {
      "given": {
        "op": "scalar",
        "a": [
          [
            1,
            -2
          ],
          [
            0.1,
            3
          ]
        ],
        "k": 0.5
      },
      "expect": {
        "result": "[1/2, -1; 1/20, 3/2]"
      },
      "source": "hand calculation in content.mdx: ½ × 0.1 = 1/20"
    }
  ],
  "sources": [
    "Gilbert Strang, Introduction to Linear Algebra, 5th edition (2016), §2.4 Rules for Matrix Operations, §2.5 Inverse Matrices, §5.1 The Properties of Determinants.",
    "OpenStax, Precalculus 2e, §9.5 Matrices and Matrix Operations and §9.7 Solving Systems with Inverses. https://openstax.org/books/precalculus-2e/pages/9-5-matrices-and-matrix-operations"
  ],
  "related": [
    "matrix-multiplication",
    "determinant",
    "inverse-matrix",
    "rref",
    "system-of-equations"
  ],
  "changelog": []
}
