forked from UTSAVS26/PySnippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed Tests Issues nd Organized Test File UTSAVS26#21
- Loading branch information
1 parent
8887a6a
commit aa34c5a
Showing
11 changed files
with
116 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import unittest | ||
from snippets.matrix_operations import add_matrices, multiply_matrices, transpose_matrix | ||
from pysnippets.maths.matrix_operations import matrix_multiplication, matrix_addition, matrix_transpose | ||
|
||
class TestMatrixOperations(unittest.TestCase): | ||
|
||
def test_add_matrices(self): | ||
# Test case for normal behavior | ||
self.assertEqual(add_matrices([[1, 2], [3, 4]], [[5, 6], [7, 8]]), [[6, 8], [10, 12]]) | ||
self.assertEqual(matrix_addition([[1, 2], [3, 4]], [[5, 6], [7, 8]]), [[6, 8], [10, 12]]) | ||
|
||
def test_multiply_matrices(self): | ||
# Test case for normal behavior | ||
self.assertEqual(multiply_matrices([[1, 2], [3, 4]], [[5, 6], [7, 8]]), [[19, 22], [43, 50]]) | ||
self.assertEqual(matrix_multiplication([[1, 2], [3, 4]], [[5, 6], [7, 8]]), [[19, 22], [43, 50]]) | ||
|
||
def test_transpose_matrix(self): | ||
# Test case for normal behavior | ||
self.assertEqual(transpose_matrix([[1, 2, 3], [4, 5, 6]]), [[1, 4], [2, 5], [3, 6]]) | ||
self.assertEqual(matrix_transpose([[1, 2, 3], [4, 5, 6]]), [[1, 4], [2, 5], [3, 6]]) | ||
|
||
def test_add_matrices_invalid(self): | ||
# Test case for invalid input | ||
with self.assertRaises(ValueError): | ||
add_matrices([[1, 2]], [[3, 4], [5, 6]]) | ||
matrix_addition([[1, 2]], [[3, 4], [5, 6]]) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# matrix_multiplication.py | ||
def matrix_addition(A, B): | ||
""" | ||
Add two matrices A and B. | ||
Args: | ||
A (list of list of int/float): First matrix. | ||
B (list of list of int/float): Second matrix. | ||
Returns: | ||
list of list of int/float: The resulting matrix after addition. | ||
Raises: | ||
ValueError: If the dimensions of the two matrices are not the same. | ||
Example: | ||
>>> A = [[1, 2], [3, 4]] | ||
>>> B = [[5, 6], [7, 8]] | ||
>>> add_matrices(A, B) | ||
[[6, 8], [10, 12]] | ||
""" | ||
# Check if the dimensions of the two matrices are the same | ||
if len(A) != len(B) or len(A[0]) != len(B[0]): | ||
raise ValueError("The dimensions of the two matrices must be the same.") | ||
|
||
# Initialize the result matrix with zeros | ||
result = [[0 for _ in range(len(A[0]))] for _ in range(len(A))] | ||
|
||
# Perform matrix addition | ||
for i in range(len(A)): | ||
for j in range(len(A[0])): | ||
result[i][j] = A[i][j] + B[i][j] | ||
|
||
return result | ||
|
||
def matrix_multiplication(A, B): | ||
""" | ||
Multiply two matrices A and B. | ||
Args: | ||
A (list of list of int/float): First matrix. | ||
B (list of list of int/float): Second matrix. | ||
Returns: | ||
list of list of int/float: The resulting matrix after multiplication. | ||
Raises: | ||
ValueError: If the number of columns in A does not match the number of rows in B. | ||
Example: | ||
>>> A = [[1, 2, 3], [4, 5, 6]] | ||
>>> B = [[7, 8], [9, 10], [11, 12]] | ||
>>> matrix_multiply(A, B) | ||
[[58, 64], [139, 154]] | ||
""" | ||
# Check if the number of columns in A matches the number of rows in B | ||
if len(A[0]) != len(B): | ||
raise ValueError("Number of columns in A must match number of rows in B.") | ||
|
||
# Initialize the result matrix with zeros | ||
result = [[0 for _ in range(len(B[0]))] for _ in range(len(A))] | ||
|
||
# Perform matrix multiplication | ||
for i in range(len(A)): | ||
for j in range(len(B[0])): | ||
for k in range(len(B)): | ||
result[i][j] += A[i][k] * B[k][j] | ||
|
||
return result | ||
|
||
def matrix_transpose(A): | ||
""" | ||
Transpose a matrix A. | ||
Args: | ||
A (list of list of int/float): | ||
The matrix to be transposed. | ||
Returns: | ||
list of list of int/float: | ||
The transposed matrix. | ||
Example: | ||
>>> A = [[1, 2, 3], [4, 5, 6]] | ||
>>> transpose_matrix(A) | ||
[[1, 4], [2, 5], [3, 6]] | ||
""" | ||
return [[A[j][i] for j in range(len(A))] for i in range(len(A[0]))] | ||
|
||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
# Example usage of the function | ||
A = [[1, 2, 3], [4, 5, 6]] | ||
B = [[7, 8], [9, 10], [11, 12]] | ||
result = matrix_multiplication(A, B) | ||
print(result) | ||
|
||
|