v9.1.1
Menu

Matrix Operations

easyFREELinear Algebra BasicsNumPy Linear AlgebraNumPy Foundations

Problem Statement

Perform common matrix operations.

Background

NumPy linear algebra operations:

  • np.linalg.inv(A) - matrix inverse
  • np.linalg.det(A) - determinant
  • np.trace(A) - trace (sum of diagonal)
  • A.T - transpose

Your Task

Write a function matrix_ops(mat) that computes matrix properties.

Output Format

Return a dictionary with:

  • "transpose": Transposed matrix (list)
  • "trace": Sum of diagonal elements
  • "determinant": Determinant (rounded to 2 decimals)
  • "is_symmetric": True if mat == mat.T

Example

Input
[[1, 2], [3, 4]]
Output
{'transpose': [[1, 3], [2, 4]], 'trace': 5, 'determinant': -2.0, 'is_symmetric': False}
Reasoning
  • Input: mat is the 2×22 \times 2 matrix A=(1234)A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}, so a11=1a_{11}=1, a12=2a_{12}=2, a21=3a_{21}=3, a22=4a_{22}=4.
  • Transpose: ATA^T swaps rows and columns, (AT)ij=aji(A^T)_{ij} = a_{ji}. Row 1 of AA, (1,2)(1, 2), becomes column 1, giving [[1, 3], [2, 4]] as a plain nested list.
  • Trace: the sum of the main diagonal, tr⁡(A)=a11+a22=1+4=5\operatorname{tr}(A) = a_{11} + a_{22} = 1 + 4 = 5, a whole number since every entry is an integer.
  • Determinant: for a 2×22 \times 2 matrix, det⁡A=a11a22−a12a21=1⋅4−2⋅3=−2\det A = a_{11}a_{22} - a_{12}a_{21} = 1 \cdot 4 - 2 \cdot 3 = -2. Computed numerically it can come out as −2.0000000000000004-2.0000000000000004; rounded to 2 decimal places it is the float -2.0.
  • Symmetry: AA is symmetric only if A=ATA = A^T, i.e. aij=ajia_{ij} = a_{ji} for every pair. Here a12=2a_{12} = 2 but a21=3a_{21} = 3, so is_symmetric is False.
  • Notice that the trace is unchanged by transposing (the diagonal stays put), and so is the determinant: det⁡AT=det⁡A\det A^T = \det A for any square matrix.

Constraints

  • mat is a square matrix
  • Round determinant to 2 decimal places

Learn this in

More from NumPy Foundations

solution.py

Test results

Sign in to run your code against the test cases.