Explore the world of Python programming with 'Complete Python Mastery'! Our repository, led by Pankaj, offers a series of in-depth tutorials under the banner 'Codes with Pankaj.' Dive into hands-on coding examples, insightful explanations, and practical projects as Pankaj guides you through mastering Python.
Objective:
This assignment is aimed at testing your ability to perform advanced matrix operations using Python. You will implement various matrix functions without relying on external libraries like NumPy or Pandas. Instead, you'll write pure Python code to manipulate matrices.
Tasks:
1. Matrix Addition and Subtraction
Task: Implement two functions, matrix_addition(A, B) and matrix_subtraction(A, B), where A and B are two-dimensional lists (matrices). The functions should return the sum and difference of the matrices, respectively.
Constraints:
Ensure both matrices have the same dimensions.
Handle edge cases where matrices are empty or contain invalid data types.
2. Matrix Multiplication
Task: Implement a function matrix_multiplication(A, B) that performs matrix multiplication of two matrices A and B.
Constraints:
Ensure the number of columns in matrix A equals the number of rows in matrix B.
Handle edge cases where matrices are incompatible for multiplication.
3. Matrix Transpose
Task: Implement a function matrix_transpose(A) that returns the transpose of a given matrix A.
Constraints:
Handle non-square matrices as well.
Ensure that empty matrices are handled gracefully.
4. Determinant of a Matrix
Task: Implement a function matrix_determinant(A) that computes the determinant of a square matrix A.
Constraints:
Handle matrices of size 2x2 and higher using recursion.
Ensure the function is optimized to handle large matrices efficiently.
5. Inverse of a Matrix
Task: Implement a function matrix_inverse(A) that computes the inverse of a square matrix A.
Constraints:
Ensure that the matrix is invertible (determinant is non-zero).
Handle cases where the matrix is non-invertible by raising an appropriate exception.
6. Eigenvalues and Eigenvectors
Task: Implement a function eigen_decomposition(A) that computes the eigenvalues and eigenvectors of a square matrix A.
Constraints:
Use a basic iterative method (like the power iteration) to approximate the eigenvalues and eigenvectors.
Ensure the function can handle symmetric and non-symmetric matrices.
7. Singular Value Decomposition (SVD)
Task: Implement a function singular_value_decomposition(A) that performs the Singular Value Decomposition (SVD) of a matrix A.
Constraints:
Split the matrix A into three matrices: U, Σ, and V^T.
Handle edge cases where the matrix has more rows than columns or vice versa.
8. Matrix Rank
Task: Implement a function matrix_rank(A) that computes the rank of a matrix A.
Constraints:
Use Gaussian elimination or another suitable method to determine the rank.
Handle cases where the matrix is singular.
9. Solving a System of Linear Equations
Task: Implement a function solve_linear_system(A, b) that solves the system of linear equations Ax = b where A is a matrix and b is a vector.
Constraints:
Use Gaussian elimination or LU decomposition to find the solution vector x.
Ensure the system has a unique solution, and handle cases where no solution or infinite solutions exist.
10. Matrix Decomposition (LU Decomposition)
Task: Implement a function lu_decomposition(A) that performs LU decomposition of a square matrix A.
Constraints:
Split the matrix A into two matrices: L (lower triangular) and U (upper triangular).
Ensure the function is optimized for large matrices.
Submission Guidelines:
Code Quality: Your code should be clean, well-documented, and follow best practices for Python programming.
Testing: Write test cases for each function to ensure correctness. Consider edge cases such as empty matrices, singular matrices, and invalid inputs.
Performance: Optimize your code for performance, especially for tasks like matrix multiplication and determinant calculation.
Optional Challenges:
Implement the above matrix operations using Python's numpy library and compare the performance with your pure Python implementation.
Extend the solve_linear_system function to handle overdetermined systems (least squares solution).
This assignment is designed to push your skills in Python programming, particularly in handling complex mathematical operations without relying on third-party libraries. Good luck!
Objective: This assignment is aimed at testing your ability to perform advanced matrix operations using Python. You will implement various matrix functions without relying on external libraries like NumPy or Pandas. Instead, you'll write pure Python code to manipulate matrices.
Tasks:
1. Matrix Addition and Subtraction
matrix_addition(A, B)
andmatrix_subtraction(A, B)
, whereA
andB
are two-dimensional lists (matrices). The functions should return the sum and difference of the matrices, respectively.2. Matrix Multiplication
matrix_multiplication(A, B)
that performs matrix multiplication of two matricesA
andB
.A
equals the number of rows in matrixB
.3. Matrix Transpose
matrix_transpose(A)
that returns the transpose of a given matrixA
.4. Determinant of a Matrix
matrix_determinant(A)
that computes the determinant of a square matrixA
.2x2
and higher using recursion.5. Inverse of a Matrix
matrix_inverse(A)
that computes the inverse of a square matrixA
.6. Eigenvalues and Eigenvectors
eigen_decomposition(A)
that computes the eigenvalues and eigenvectors of a square matrixA
.7. Singular Value Decomposition (SVD)
singular_value_decomposition(A)
that performs the Singular Value Decomposition (SVD) of a matrixA
.A
into three matrices:U
,Σ
, andV^T
.8. Matrix Rank
matrix_rank(A)
that computes the rank of a matrixA
.9. Solving a System of Linear Equations
solve_linear_system(A, b)
that solves the system of linear equationsAx = b
whereA
is a matrix andb
is a vector.x
.10. Matrix Decomposition (LU Decomposition)
lu_decomposition(A)
that performs LU decomposition of a square matrixA
.A
into two matrices:L
(lower triangular) andU
(upper triangular).Submission Guidelines:
Optional Challenges:
numpy
library and compare the performance with your pure Python implementation.solve_linear_system
function to handle overdetermined systems (least squares solution).This assignment is designed to push your skills in Python programming, particularly in handling complex mathematical operations without relying on third-party libraries. Good luck!