Matrix Operations
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:
[[1, 2], [3, 4]]
{'transpose': [[1, 3], [2, 4]], 'trace': 5, 'determinant': -2.0, 'is_symmetric': False}trace = 1+4 = 5, det = 14 - 23 = -2, not symmetric since [0,1] ≠ [1,0]
Constraints:
- mat is a square matrix
- Round determinant to 2 decimal places
More from NumPy Foundations
1. Background Knowledge
Matrix fundamentals form the basis of linear algebra, where a matrix is a rectangular array of numbers arranged in rows and columns, representing linear transformations or systems of equations. Key properties include the transpose (flipping rows and columns, denoted AT), trace (sum of diagonal elements, trace(A)=\sum_i A_{ii}),∗∗determinant∗∗(scalarvalueindicatingvolumescalingandinvertibilityforsquarematrices,\det(A) \neq 0forinvertiblematrices),and∗∗symmetry∗∗(amatrixissymmetricif A = A^T $). These operations are foundational in fields like physics, computer graphics, and machine learning.
NumPy, Python's library for numerical computing, provides efficient array operations via ndarrays, enabling vectorized computations that avoid slow Python loops. Functions like np.trace(), np.linalg.det(), and A.T leverage optimized C backends for speed, while np.linalg.inv() computes inverses (though not required here). Understanding NumPy's broadcasting and shape handling is crucial, as matrices must be square for trace/determinant.
2. Algorithm/Approach
The approach follows a direct computation pattern common in linear algebra problems:
- Extract matrix properties using built-in NumPy functions.
- Convert results to required formats (e.g., NumPy array to list for transpose).
- Perform conditional checks (e.g., symmetry via equality comparison).
- Package into a dictionary for structured output.
This is O(n) time for most operations (n = matrix dimension), emphasizing NumPy's vectorized efficiency over manual loops.
3. Step-by-Step Strategy
- Validate input: Ensure mat is a square NumPy array (use mat.shape to check rows == columns).
- Compute transpose: Use mat.T and convert to nested list via mat.T.tolist().
- Calculate trace: Apply np.trace(mat) for diagonal sum.
- Compute determinant: Use np.linalg.det(mat) and round to 2 decimals with round(value, 2).
- Check symmetry: Compare np.array_equal(mat, mat.T) (accounts for floating-point precision).
- Return dictionary: Populate with keys "transpose", "trace", "determinant", "is_symmetric".
Test with sample: For \begin{bmatrix} 1 & 2 \ 3 & 4 \end{bmatrix} $, expect trace=5, det=-2.0, transpose=[[1,3],[2,4]], not symmetric.
4. Common Pitfalls
- Non-square matrices: det() and trace() fail or give misleading results—always check mat.shape == mat.shape.
- Data types: Input may be list; convert with np.array(mat) to enable NumPy ops.
- Floating-point precision: Use np.allclose(mat, mat.T) instead of == for symmetry if floats present.
- Output format: Transpose must be list of lists, not NumPy array—use .tolist().
- Rounding: Apply round() only to determinant; trace may be integer.
- Import: Forget import numpy as np → all functions fail.
5. Time & Space Complexity
- Time: O(n) per operation (n = side length), dominated by det (Gaussian elimination ~O(n³) worst-case, but NumPy optimizes). Total: O(n³).
- Space: O(n²) for transpose copy; dictionary holds O(1) scalars + O(n²) list. O(n²) overall.
NumPy's C backend ensures efficiency scales to large n (e.g., 1000x1000 in seconds on modern hardware).