NC Final SP 2024
NC Final SP 2024
Islamabad Campus
Solution
13 # Swap rows i n U
14 U [ [ i , max row ] , : ] = U [ [ max row , i ] , : ]
15 # Swap rows i n P
16 P [ [ i , max row ] , : ] = P [ [ max row , i ] , : ]
17 i f i > 0:
18 # Swap rows i n L , but o n l y t h e f i r s t i columns
19 L [ [ i , max row ] , : i ] = L [ [ max row , i ] , : i ]
20
21 # Compute L and U
22 f o r j i n r a n g e ( i +1, n ) :
23 L [ j , i ] = U[ j , i ] / U[ i , i ]
24 U[ j , i : ] −= L [ j , i ] ∗ U[ i , i : ]
25
26 np . f i l l d i a g o n a l (L , 1 )
27 return P, L , U
(a) Using Gauss-Seidel iterative method, approximate the solution of the linear system Ax =
b up to 6
Solution
(c) Write the missing code in following function (on the answer sheet): (8)
Solution
1 d e f g a u s s s e i d e l (A, b , x , t o l = 1 . e −5, maxit = 1 0 0 ) :
2 n = len (b)
3 err = 1.0
4 iters = 0
5
6 # I n i t i a l i z e t h e s o l u t i o n with t h e i n i t i a l g u e s s
7 xnew = np . z e r o s l i k e ( x )
8 # Extract the lower t r i a n g u l a r part of A
9 M = np . t r i l (A)
10 # C o n s t r u c t t h e upper t r i a n g u l a r p a r t o f A
11 U = A −M
12
13 w h i l e ( e r r > t o l and i t e r s < maxit ) :
14 i t e r s += 1
15 # Compute t h e new a p p r o x i m a t i o n
16 xnew = np . dot ( n p l . i n v (M) , b − np . dot (U, x ) )
17 # Estimate c o n v e r g e n c e
18 e r r = n p l . norm ( xnew−x )
19 x = np . copy ( xnew )
20 return x
(b) Comprehend the following piece of python code which contains comments which are
numbered from 1 to 10. This numbering is done so that you only provide the comments
in the answer sheet (without re-producing the source code in the answer sheet) to save
time. Provide the missing comments with comment numbers. (8)
1 import i m a g e i o
2 import numpy a s np
3 import numpy . l i n a l g a s n p l
4
5 # Comment 1 : Read t h e image i n t o t h e a r r a y photo
6 photo = i m a g e i o . imread ( ”Newton . j p g ” )
7
8 #Comment 2 : e x t r a c t Red , Green and Blue c h a n n e l s i n t o s e p a r a t e m a t r i c e s
9 Red [ : , : , 0 ] = photo [ : , : , 0 ]
10 Green [ : , : , 1 ] = photo [ : , : , 1 ]
11 Blue [ : , : , 2 ] = photo [ : , : , 2 ]
12
13 # Comment 3 : perform SVD on each c h a n n e l t o g e t c o r r e s p o n d i n g U, S and V
componenets
14 U r , S r , V r = n p l . svd ( Red )
15 U g , S g , V g = n p l . svd ( Green )
16 U b , S b , V b = n p l . svd ( Blue )
17
1. Gaussian elimination is
(a) a direct method with finite precision in theory
(b) a direct method with infinite precision in theory
(c) an iterative method with finite precision in practice
(d) an iterative method with infinite precision in theory
2. LU factorization is
(a) a modification of Gaussian elimination
(b) a decomposition into lower and upper triangular parts of a matrix
(c) a method for forward substitution
(d) a method for backward substitution
3. Pivoting strategies can resolve numerical issues arising in
(a) forward substitution
(b) backward substitution
(c) LU factorization
(d) all of the above
4. Naı̈ve Gaussian elimination cannot be performed with
(c) Non-negative
(d) Non-positive
1
25. Given x = 2 . ∥x∥1 is
−3
(a) Positive
(b) Negative
(c) Non-negative
(d) Non-positive
26. Which of the following statement about SVD is true?
(a) It can only be applied to square matrices.
(b) It can be used to determine the rank of any matrix.
(c) The singular values in σ are always positive.
(d) The columns of U and V are always linearly independent.
27. Provided AT A is non-singular, pseudo-inverse of a A exists if A is
(a) square non-singular matrix only
(b) square singular matrix only
(c) any rectangular matrix
(d) rectangular singular matrix only
28. What is the computational complexity of Gaussian elimination for solving a system of
linear equations of order n?
(a) O(n)
(b) O(n2 )
(c) O(n3 )
(d) O(2n )
29. SVD is particularly useful for image compression because:
(a) It reduces the computational cost of storing pixel values.
(b) It separates image information into components with varying importance.
(c) It directly removes redundant information from the image.
(d) None of the above
30. Which of the following statements is TRUE about square matrices?
(a) A matrix must have all zero entries to be non-invertible.
(b) A matrix is invertible only if it has an equal number of rows and columns.
(c) A matrix is invertible if and only if its columns (or rows) are linearly independent.
(d) A matrix with a determinant of 0 is always invertible.