📘
Identity and Eye Matrices
Problem Statement
Create identity matrices using NumPy.
Background
- np.eye(n): Creates n×n identity matrix (1s on diagonal, 0s elsewhere)
- np.identity(n): Same as eye, but only square matrices
- Identity matrix: I × A = A (identity element for matrix multiplication)
Your Task
Write a function create_identity(n) that returns a dictionary with:
- "identity": n×n identity matrix as nested list
- "trace": Sum of diagonal elements (should equal n)
- "shape": Shape as list [n, n]
Output Format
Return a dictionary with exactly these three keys.
Example:
Input:
n = 3
Output:
{'identity': [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]], 'trace': 3.0, 'shape': [3, 3]}Reasoning:
3x3 identity has 1s on diagonal, trace = 1+1+1 = 3
Constraints:
- Use np.eye() or np.identity()
- n will be a positive integer
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.