Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
56 changes: 56 additions & 0 deletions Matrix Operations/addition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
This program implements matrix addition.

It includes a function to check if the dimensions of two matrices match,
a function to add two matrices, and a main function to test the matrix
addition function.

Example:
m1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
m2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
matrix_addition(m1, m2)
[[2, 4, 6], [8, 10, 12], [14, 16, 18]]
"""

from checker import check_matrix_dimension, print_matrix
import sys


def matrix_addition(m1, m2):
"""
This function adds two matrices.

Args:
m1 (list of lists): The first matrix.
m2 (list of lists): The second matrix.

Returns:
list of lists: The sum of the two matrices.

Raises:
sys.exit: If the dimensions of the two matrices do not match.
"""
if check_matrix_dimension(m1, m2) == False:
sys.exit("Matrix dimensions do not match")

n = len(m1)
m = len(m1[0])
answer = [[0 for i in range(m)] for j in range(n)]
for i in range(len(m1)):
for j in range(len(m1[i])):
answer[i][j] = m1[i][j] + m2[i][j]
return answer


def main():
"""
This function tests the matrix addition function.

"""
m1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
m2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print_matrix(matrix_addition(m1, m2))


if __name__ == "__main__":
main()
49 changes: 49 additions & 0 deletions Matrix Operations/adjoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
This module contains a function to calculate the adjoint matrix of a given matrix.

The adjoint matrix of a matrix is the transpose of its cofactor matrix.

The module contains the following functions:

- `adjoint(matrix)`: This function takes a matrix as input and returns its
adjoint matrix.

The module also contains a main function which tests the adjoint function.

Example:
matrix = [[1, -1, 2], [2, 3, 5], [1, 0, 3]]
adjoint(matrix)
# Output:
# [[9, 3, -11], [-1, 1, -1], [-3, -1, 5]]
"""

from checker import print_matrix
from cofactor import cofactor
from transpose import transpose


def adjoint(matrix):
"""
This function takes a matrix as input and returns its adjoint matrix.

Args:
matrix (list of lists): The input matrix.

Returns:
list of lists: The adjoint matrix of the input matrix.

Raises:
sys.exit: If the input matrix is not a square matrix.
"""
matrix = transpose(cofactor(matrix))
return matrix


def main():
m = [[1, -1, 2], [2, 3, 5], [1, 0, 3]]
result = adjoint(m)
print_matrix(result)


if __name__ == "__main__":
main()
131 changes: 131 additions & 0 deletions Matrix Operations/checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"""
This program contains functions to validate a matrix, check if two matrices
can be added or multiplied, and check if a matrix is square. It also contains
a function to print a matrix.

Example:
m1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
m2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
m3 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
m4 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(check_matrix_dimension(m1, m2))
print(check_matrix_multiplication(m1, m2))
print(check_matrix_square(m3))
print(check_matrix_square(m4))
print_matrix(m1)
print_matrix(m2)
"""

def validate_matrix(m):
"""
This function validates a matrix.

It checks if the 2d array is a valid matrix and if the matrix is a list of
lists.

Args:
m (list of lists): The matrix to be validated.

Returns:
bool: True if the matrix is valid, False otherwise.
"""
n = len(m[0])
for i in range(len(m)):
if len(m[i]) != n:
return False
return True


def check_matrix_dimension(m1, m2):
"""
This function checks if two matrices can be added.

It checks if the two matrices have the same dimensions.

Args:
m1 (list of lists): The first matrix.
m2 (list of lists): The second matrix.

Returns:
bool: True if the two matrices can be added, False otherwise.
"""
if (validate_matrix(m1) and validate_matrix(m2)) == False:
return False
n = len(m1)
if (n != len(m2)):
return False
for i in range(n):
if (len(m1[i]) != len(m2[i])):
return False
return True


def check_matrix_multiplication(m1, m2):
"""
This function checks if two matrices can be multiplied.

It checks if the two matrices have the same number of columns as the first
matrix has rows.

Args:
m1 (list of lists): The first matrix.
m2 (list of lists): The second matrix.

Returns:
bool: True if the two matrices can be multiplied, False otherwise.
"""
if (validate_matrix(m1) and validate_matrix(m2)) == False:
return False
n = len(m1[0])
if (n != len(m2)):
return False
return True


def check_matrix_square(m):
"""
This function checks if a matrix is square.

Args:
m (list of lists): The matrix to be checked.

Returns:
bool: True if the matrix is square, False otherwise.
"""
if validate_matrix(m) == False:
return False
n = len(m)
if (n != len(m[0])):
return False
return True


def print_matrix(m):
"""
This function prints a matrix in the terminal for readability.

Args:
m (list of lists): The matrix to be printed.
"""
for i in range(len(m)):
for j in range(len(m[i])):
print(m[i][j], end=" ")
print()
print()


def main():
m1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
m2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
m3 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
m4 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(check_matrix_dimension(m1, m2))
print(check_matrix_multiplication(m1, m2))
print(check_matrix_square(m3))
print(check_matrix_square(m4))
print_matrix(m1)
print_matrix(m2)


if __name__ == "__main__":
main()
74 changes: 74 additions & 0 deletions Matrix Operations/cofactor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
This module contains functions to calculate the cofactor matrix of a given
matrix.

A cofactor matrix is a matrix where each element is the cofactor of the
corresponding element in the original matrix. The cofactor of an element is
defined as the minor of the element multiplied by (-1) to the power of the sum
of the row and column indices.

The module contains the following functions:

- `cofactor(matrix)`: This function takes a matrix as input
and returns its cofactor matrix.

The module also contains a main function which tests the
`calculate_cofactor_matrix` function.

Example usage:
matrix = [
[1, 2, 3],
[0, 4, 5],
[1, 0, 6]
]

result = calculate_cofactor_matrix(matrix)
print_matrix(result)
"""

from checker import print_matrix
from minor import minor
from determinant import determinant


def cofactor(matrix):
"""
This function takes a matrix as input and returns its cofactor matrix.

Args:
matrix (list of lists): The input matrix.

Returns:
list of lists: The cofactor matrix of the input matrix.

Raises:
sys.exit: If the input matrix is not a square matrix.
"""

n = len(matrix)
cofactor_matrix = []
for i in range(n):
cofactor_row = []
for j in range(n):
minor_matrix = minor(matrix, i, j)
cofactor_value = ((-1) ** (i + j)) * determinant(minor_matrix)
cofactor_row.append(cofactor_value)
cofactor_matrix.append(cofactor_row)

return cofactor_matrix


def main():
# Example usage:
matrix = [
[1, 2, 3],
[0, 4, 5],
[1, 0, 6]
]

result = cofactor(matrix)
print_matrix(result)


if __name__ == "__main__":
main()
61 changes: 61 additions & 0 deletions Matrix Operations/determinant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
This module contains functions to calculate the determinant of a matrix.

The determinant of a matrix is a scalar value that can be used to describe the
matrix's linear transformation. It can be used to determine the solvability of a
system of linear equations, invertibility of a matrix, and the volume scaling
factor of the linear transformation.

The module contains the following functions:

- `determinant(matrix)`: This function takes a matrix as input and returns its
determinant.

The module also contains a main function which tests the determinant function.

Example:
matrix = [[1, 2], [2, 9]]
determinant(matrix)
# Output: 7

"""
from checker import check_matrix_square
import sys
from minor import minor

def determinant(matrix):
"""
Calculate the determinant of a matrix.

Parameters
----------
matrix : list of lists
The matrix whose determinant is to be calculated.

Returns
-------
int
The determinant of the matrix.

Raises
------
sys.exit
If the matrix is not square.
"""
if not check_matrix_square(matrix):
sys.exit("Matrix is not square")
if len(matrix) == 1:
return matrix[0][0]
if len(matrix) == 2:
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]
return sum(((-1) ** j) * matrix[0][j] * determinant(minor(matrix, 0, j)) for j in range(len(matrix)))



def main():
matrix = [[1, 2], [2, 9]]
print(determinant(matrix))


if __name__ == "__main__":
main()
Loading