Build Identity Matrix
Implement a function to generate an n×n identity matrix, a fundamental concept in linear algebra. The identity matrix is crucial in various mathematical operations, including matrix multiplication and transformation.
The identity matrix I is defined such that when it multiplies another matrix A, the result is the original matrix A. This property makes the identity matrix the multiplicative identity for matrices. The elements of the identity matrix can be defined using the Kronecker delta δij, where δij=1 if i=j and δij=0 otherwise.
To construct the identity matrix, follow these steps:
- Initialize an n×n matrix with all elements set to 0.
- Iterate over each row and column, setting the element at position (i,j) to 1 if i=j, and 0 otherwise.
This technique is widely used in computer vision for image and signal processing.
Example:
identity_matrix(3)
[[1,0,0],[0,1,0],[0,0,1]]
3×3 identity matrix
Constraints:
- n is a positive integer, 1 ≤ n ≤ 100
More from CV: Introduction to Computer Vision
The key idea is that an identity matrix is a square matrix whose entries are 1 on the main diagonal and 0 elsewhere, and the Kronecker delta δij is exactly the function that captures this pattern. So Iij=\deltaij is just a compact way of saying “put 1 when row index equals column index, otherwise 0.”
1. Background Knowledge
- An identity matrix In of size n×n is defined by
This matrix is the multiplicative identity for matrix multiplication: for any compatible matrix A, AIn=InA=A.
- The Kronecker delta δij is a function of two indices:
Comparing this with the definition above, you can see that the identity matrix entries can be written compactly as Iij=δij.
- In many linear algebra and computer vision texts, writing things in index notation (using subscripts and δij) makes formulas for transformations and tensors shorter and easier to manipulate. This exercise is mainly about making the connection between this notation and the concrete matrix you build in code.
2. Algorithm / General Approach
To build an n×n identity matrix using the Kronecker delta idea:
- Create an n×n matrix (e.g., list of lists, 2D array) initialized with zeros.
- For each pair of indices (i,j):
- Set the element to 1 if i and j are equal (because δij=1), otherwise keep it 0.
- Conceptually, you are implementing the rule: matrix[i][j] = 1 if i == j else 0.
The “Kronecker delta” is just the mathematical notation for this conditional.
3. Step-by-Step Strategy
- Read input size
- Obtain the integer n (size of the matrix).
- Initialize data structure
- Create an empty 2D structure to hold n rows, each with n columns.
- Often, you start with all zeros.
- Loop over indices
- Use a loop for row index i from 0 to n−1.
- Inside it, use another loop for column index j from 0 to n−1.
- Apply the Kronecker delta rule
- If i==j, set the element to 1 (this corresponds to δij=1).
- Else, set (or leave) the element as 0 (this corresponds to δij=0).
- Return or print the matrix
- Output the full n×n matrix in the format your platform expects.
Example sketch in Python-like pseudocode:
def identity_matrix(n):
I = [[0 for j in range(n)] for i in range(n)]
for i in range(n):
for j in range(n):
if i == j: # this is δ_ij
I[i][j] = 1
return I
4. Common Pitfalls
-
Indexing off-by-one:
-
Mathematically, indices often start at 1; in code, they usually start at 0.
-
Ensure you loop correctly from 0 to n-1.
-
Rectangular instead of square:
-
An identity matrix must be n×n, not n×m with m=n.
-
Incorrect initialization:
-
Forgetting to initialize with zeros, or accidentally reusing the same row list for all rows (in some languages) can cause unexpected behavior.
-
Misinterpreting δij:
-
Remember it is not a product. It is a function that evaluates to either 0 or 1 depending on whether i=j.
5. Time & Space Complexity
-
Time complexity:
-
You visit each element of an n×n matrix once and do constant work per element.
-
T(n)=O(n2).
-
Space complexity:
-
You store n2 elements in the matrix.
-
S(n)=O(n2).