Erster Stand: compute.py mit matmul, transpose, rot_2D
This commit is contained in:
commit
6a487075df
10 files changed
+173
No files matched your search
@@ -0,0 +1,3 @@
|
||||
.venv/
|
||||
__pycache__/
|
||||
.env
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
||||
Generated
+10
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="Python 3.13 (matrixmania)" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.13 (matrixmania)" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.13 (matrixmania)" project-jdk-type="Python SDK" />
|
||||
</project>
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/matrixmania.iml" filepath="$PROJECT_DIR$/.idea/matrixmania.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -0,0 +1,5 @@
|
||||
# Regeln für dieses Projekt
|
||||
|
||||
- Code nach PEP 8, Zeilenlänge maximal 88 Zeichen.
|
||||
- Jede Funktion und Methode hat einen Docstring und Type Hints.
|
||||
- Vor jedem Commit: `ruff check` und `ruff format`.
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
"""Utility functions for matrix operations."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
Matrix = list[list[int | float]]
|
||||
|
||||
|
||||
def transpose(matrix: Matrix) -> Matrix:
|
||||
"""Return the transpose of a matrix.
|
||||
|
||||
Args:
|
||||
matrix: Rectangular, non-empty matrix in row-major format.
|
||||
|
||||
Returns:
|
||||
The transposed matrix.
|
||||
|
||||
Raises:
|
||||
ValueError: If the matrix is empty or has irregular rows.
|
||||
"""
|
||||
if not matrix:
|
||||
raise ValueError("Cannot transpose an empty matrix")
|
||||
row_length = len(matrix[0])
|
||||
if row_length == 0:
|
||||
raise ValueError("Matrix rows must have at least one column")
|
||||
for row in matrix:
|
||||
if len(row) != row_length:
|
||||
raise ValueError("Inconsistent row lengths; matrix must be rectangular")
|
||||
return [list(column) for column in zip(*matrix)]
|
||||
|
||||
|
||||
def matmul(a: Matrix, b: Matrix) -> Matrix:
|
||||
"""Return the matrix product of two matrices.
|
||||
|
||||
Args:
|
||||
a: Matrix with m rows and n columns.
|
||||
b: Matrix with n rows and p columns.
|
||||
|
||||
Returns:
|
||||
The product matrix with m rows and p columns.
|
||||
|
||||
Raises:
|
||||
ValueError: If a matrix is empty or the inner dimensions do not match.
|
||||
"""
|
||||
if not a or not b:
|
||||
raise ValueError("Both matrices must be non-empty")
|
||||
if not a[0] or not b[0]:
|
||||
raise ValueError("Matrices must have at least one column")
|
||||
|
||||
rows_a, cols_a = len(a), len(a[0])
|
||||
rows_b, cols_b = len(b), len(b[0])
|
||||
|
||||
if cols_a != rows_b:
|
||||
raise ValueError(
|
||||
f"Inner matrix dimensions must agree: got {cols_a} and {rows_b}"
|
||||
)
|
||||
|
||||
result: Matrix = []
|
||||
for i in range(rows_a):
|
||||
result_row: list[int | float] = []
|
||||
for j in range(cols_b):
|
||||
total: int | float = 0
|
||||
for k in range(cols_a):
|
||||
total += a[i][k] * b[k][j]
|
||||
result_row.append(total)
|
||||
result.append(result_row)
|
||||
return result
|
||||
|
||||
|
||||
def rot_2D(theta: float) -> Matrix:
|
||||
"""Create a 2D rotation matrix.
|
||||
|
||||
Args:
|
||||
theta: Rotation angle in radians. Positive values rotate
|
||||
counter-clockwise.
|
||||
|
||||
Returns:
|
||||
A 2x2 rotation matrix [[cos, -sin], [sin, cos]].
|
||||
"""
|
||||
return [
|
||||
[math.cos(theta), -math.sin(theta)],
|
||||
[math.sin(theta), math.cos(theta)],
|
||||
]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
matrix_a = [
|
||||
[3, 4, -1, 4],
|
||||
[-2, 2, 5, 1],
|
||||
]
|
||||
matrix_b = [
|
||||
[1, 3, -2],
|
||||
[2, 5, 1],
|
||||
[-1, 4, -4],
|
||||
[2, 3, 6],
|
||||
]
|
||||
assert matmul(matrix_a, matrix_b) == [[20, 37, 26], [-1, 27, -8]]
|
||||
assert matmul([[1, 2]], [[3], [4]]) == [[11]]
|
||||
|
||||
assert transpose([[1, 2, 3], [4, 5, 6]]) == [[1, 4], [2, 5], [3, 6]]
|
||||
|
||||
for bad_matrix in ([], [[1, 2], [3, 4, 5]]):
|
||||
try:
|
||||
transpose(bad_matrix)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("ValueError erwartet")
|
||||
|
||||
point = [[1.0], [0.0]]
|
||||
rotated = matmul(rot_2D(math.pi / 2), point)
|
||||
assert math.isclose(rotated[0][0], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(rotated[1][0], 1.0, abs_tol=1e-9)
|
||||
assert rot_2D(0) == [[1.0, 0.0], [0.0, 1.0]]
|
||||
|
||||
print("Alle Tests bestanden.")
|
||||
@@ -0,0 +1,3 @@
|
||||
[project]
|
||||
name = "matrixmania"
|
||||
version = "0.1.0"
|
||||
Reference in new issue
Block a user