Skip to content
Open
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
66 changes: 66 additions & 0 deletions matrix/matrix_diagonal_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
Matrix Multiplication Algorithm

This function performs matrix multiplication on two valid matrices.
It follows the mathematical definition:
If A is an m×n matrix and B is an n×p matrix,

Check failure on line 6 in matrix/matrix_diagonal_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

matrix/matrix_diagonal_sum.py:6:36: RUF002 Docstring contains ambiguous `×` (MULTIPLICATION SIGN). Did you mean `x` (LATIN SMALL LETTER X)?

Check failure on line 6 in matrix/matrix_diagonal_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

matrix/matrix_diagonal_sum.py:6:13: RUF002 Docstring contains ambiguous `×` (MULTIPLICATION SIGN). Did you mean `x` (LATIN SMALL LETTER X)?
then their product C is an m×p matrix.

Check failure on line 7 in matrix/matrix_diagonal_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

matrix/matrix_diagonal_sum.py:7:29: RUF002 Docstring contains ambiguous `×` (MULTIPLICATION SIGN). Did you mean `x` (LATIN SMALL LETTER X)?

Raises:
ValueError: if matrices have invalid structure or incompatible sizes.

Sources:
https://en.wikipedia.org/wiki/Matrix_multiplication

Examples:
>>> A = [[1, 2], [3, 4]]
>>> B = [[5, 6], [7, 8]]
>>> matrix_multiply(A, B)
[[19, 22], [43, 50]]

>>> matrix_multiply([[1, 2, 3]], [[4], [5], [6]])
[[32]]

# Invalid structure
>>> matrix_multiply([[1, 2], [3]], [[1, 2]])
Traceback (most recent call last):
...
ValueError: Invalid matrix structure

# Incompatible sizes
>>> matrix_multiply([[1, 2]], [[1, 2]])
Traceback (most recent call last):
...
ValueError: Incompatible matrix sizes
"""

from typing import List

Check failure on line 37 in matrix/matrix_diagonal_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

matrix/matrix_diagonal_sum.py:37:1: UP035 `typing.List` is deprecated, use `list` instead


def matrix_multiply(A: List[List[float]], B: List[List[float]]) -> List[List[float]]:

Check failure on line 40 in matrix/matrix_diagonal_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

matrix/matrix_diagonal_sum.py:40:51: UP006 Use `list` instead of `List` for type annotation

Check failure on line 40 in matrix/matrix_diagonal_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

matrix/matrix_diagonal_sum.py:40:46: UP006 Use `list` instead of `List` for type annotation

Check failure on line 40 in matrix/matrix_diagonal_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

matrix/matrix_diagonal_sum.py:40:43: N803 Argument name `B` should be lowercase

Check failure on line 40 in matrix/matrix_diagonal_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

matrix/matrix_diagonal_sum.py:40:29: UP006 Use `list` instead of `List` for type annotation

Check failure on line 40 in matrix/matrix_diagonal_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

matrix/matrix_diagonal_sum.py:40:24: UP006 Use `list` instead of `List` for type annotation

Check failure on line 40 in matrix/matrix_diagonal_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

matrix/matrix_diagonal_sum.py:40:21: N803 Argument name `A` should be lowercase
if not _is_valid_matrix(A) or not _is_valid_matrix(B):
raise ValueError("Invalid matrix structure")

rows_A = len(A)
cols_A = len(A[0])
rows_B = len(B)
cols_B = len(B[0])

if cols_A != rows_B:
raise ValueError("Incompatible matrix sizes")

result = [[0.0 for _ in range(cols_B)] for _ in range(rows_A)]

for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
result[i][j] += A[i][k] * B[k][j]

return result


def _is_valid_matrix(M: List[List[float]]) -> bool:
if not isinstance(M, list) or not M:
return False
first_length = len(M[0])
return all(isinstance(row, list) and len(row) == first_length for row in M)
Loading