Files
matrixmania/compute.py
T

187 lines
5.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Utility functions for matrix operations.
This module contains:
* ``matmul`` – standard matrix multiplication.
* ``transpose`` – row↔column swapping.
* ``rotate`` – 90‑degree clockwise rotations.
All functions use type hints and raise informative errors where
dimensions are incompatible or matrices are non‑rectangular.
"""
from __future__ import annotations
from tabulate import tabulate
Matrix = list[list[int | float]]
def matmul(a: Matrix, b: Matrix) -> Matrix:
"""Return the matrix product a @ b.
Parameters
----------
a : Matrix
Left‑hand matrix.
b : Matrix
Right‑hand matrix.
Returns
-------
Matrix
Product matrix.
Raises
------
ValueError
If matrices are not rectangular or dimensions are incompatible.
"""
def _rect(mat: Matrix) -> bool:
return all(len(row) == len(mat[0]) for row in mat)
if not a or not b:
raise ValueError("matrices may not be empty")
if not _rect(a) or not _rect(b):
raise ValueError("input matrices must be rectangular")
r1, c1 = len(a), len(a[0])
r2, c2 = len(b), len(b[0])
if c1 != r2:
raise ValueError("first matrix columns must equal second matrix rows")
result: Matrix = [[0 for _ in range(c2)] for _ in range(r1)]
for i in range(r1):
for j in range(c2):
s = 0
for k in range(c1):
s += a[i][k] * b[k][j]
result[i][j] = s
return result
def transpose(matrix: Matrix) -> Matrix:
"""Return the transpose of a 2‑D matrix.
The transpose operation swaps rows and columns: each row in the
result is a column from the input matrix.
Parameters
----------
matrix : Matrix
Two‑dimensional list of numbers. All rows must contain the
same number of columns.
Returns
-------
Matrix
Transposed matrix.
Raises
------
ValueError
If *matrix* is empty or if the rows have differing lengths.
"""
if not matrix:
raise ValueError("transpose requires a non‑empty matrix")
if any(len(row) != len(matrix[0]) for row in matrix):
raise ValueError("matrix must be rectangular to transpose")
rows, cols = len(matrix), len(matrix[0])
return [[matrix[i][j] for i in range(rows)] for j in range(cols)]
def rot_2D(theta: float) -> list[list[float]]:
"""Return the 2‑D rotation matrix for angle *theta* in degrees.
The matrix is
``[[cos(θ), -sin(θ)],
[sin(θ), cos(θ)]]``
where θ is interpreted as a clockwise rotation.
Parameters
----------
theta : float
Rotation angle in degrees (clockwise).
Returns
-------
list[list[float]]
2x2 rotation matrix.
"""
import math
rad = math.radians(theta)
cos_t = math.cos(rad)
sin_t = math.sin(rad)
return [[cos_t, -sin_t], [sin_t, cos_t]]
def rotate(matrix: Matrix, degrees: float) -> Matrix:
"""Rotate the matrix clockwise by arbitrary degrees.
The function maps each element to a new integer coordinate that
results from rotating the original coordinate around the centre of
the matrix using a 2×2 rotation matrix.
Elements that are mapped outside the original bounds are discarded.
In case multiple elements map to the same location the last one
wins.
Parameters
----------
matrix : Matrix
degrees : float
Rotation angle in degrees (clockwise).
Returns
-------
Matrix
"""
if not matrix:
return []
n_rows, n_cols = len(matrix), len(matrix[0])
# rotation matrix
r = rot_2D(degrees)
cos_t, sin_t = r[0][0], r[1][0] # cos and sin from 2x2 matrix
# centre of the matrix (half‑index)
c_row = (n_rows - 1) / 2.0
c_col = (n_cols - 1) / 2.0
# create empty result
result = [[0 for _ in range(n_cols)] for _ in range(n_rows)]
for i in range(n_rows):
for j in range(n_cols):
# coordinates relative to centre
y = i - c_row
x = j - c_col
# rotate
new_x = cos_t * x - sin_t * y
new_y = sin_t * x + cos_t * y
# back to integer index
new_i = round(new_y + c_row)
new_j = round(new_x + c_col)
if 0 <= new_i < n_rows and 0 <= new_j < n_cols:
result[new_i][new_j] = matrix[i][j]
return result
if __name__ == "__main__":
# matmul tests
a1 = [[1, 2], [3, 4]]
b1 = [[5, 6], [7, 8]]
assert matmul(a1, b1) == [[19, 22], [43, 50]], "matmul example 1 failed"
a2 = [[1], [2], [3]]
b2 = [[4, 5, 6]]
assert matmul(a2, b2) == [[4, 5, 6], [8, 10, 12], [12, 15, 18]], "matmul example 2 failed"
# transpose test
mat = [[1, 2, 3], [4, 5, 6]]
assert transpose(mat) == [[1, 4], [2, 5], [3, 6]], "transpose failed"
# rotation tests
mat3 = [[1, 2], [3, 4], [5, 6]]
assert rotate(mat3, 90) == [[5, 3, 1], [6, 4, 2]], "rotate 90 failed"
assert rotate(mat3, 180) == [[6, 5], [4, 3], [2, 1]], "rotate 180 failed"
assert rotate(mat3, 270) == [[2, 4, 6], [1, 3, 5]], "rotate 270 failed"
assert rotate(mat3, 0) == mat3, "rotate 0 failed"
print(tabulate(rot_2D(1.0)))