Erster Stand: compute.py und Projektstruktur
This commit is contained in:
commit
3711e67d15
10 files changed
+219
No files matched your search
@@ -0,0 +1,3 @@
|
||||
.venv/
|
||||
__pycache__/
|
||||
.env
|
||||
Generated
+5
@@ -0,0 +1,5 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
+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,3 @@
|
||||
- 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`.
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
from typing import List, Sequence
|
||||
import math
|
||||
|
||||
|
||||
def matmul(a: Sequence[Sequence[float]], b: Sequence[Sequence[float]]) -> List[List[float]]:
|
||||
"""
|
||||
Multiply two matrices.
|
||||
|
||||
Args:
|
||||
a: Left matrix as a sequence of sequences of numbers.
|
||||
b: Right matrix as a sequence of sequences of numbers.
|
||||
|
||||
Returns:
|
||||
The product matrix as a list of lists.
|
||||
|
||||
Raises:
|
||||
ValueError: If inputs are not valid matrices or dimensions are incompatible.
|
||||
"""
|
||||
# ---- Validate a ----
|
||||
if not a or not isinstance(a, Sequence):
|
||||
raise ValueError("First argument must be a non-empty matrix.")
|
||||
if not all(isinstance(row, Sequence) for row in a):
|
||||
raise ValueError("First argument must be a sequence of sequences.")
|
||||
n_rows_a = len(a)
|
||||
n_cols_a = len(a[0])
|
||||
if n_cols_a == 0:
|
||||
raise ValueError("First matrix rows cannot be empty.")
|
||||
for row in a:
|
||||
if len(row) != n_cols_a:
|
||||
raise ValueError("First matrix rows must have the same length.")
|
||||
|
||||
# ---- Validate b ----
|
||||
if not b or not isinstance(b, Sequence):
|
||||
raise ValueError("Second argument must be a non-empty matrix.")
|
||||
if not all(isinstance(row, Sequence) for row in b):
|
||||
raise ValueError("Second argument must be a sequence of sequences.")
|
||||
n_rows_b = len(b)
|
||||
n_cols_b = len(b[0])
|
||||
if n_cols_b == 0:
|
||||
raise ValueError("Second matrix rows cannot be empty.")
|
||||
for row in b:
|
||||
if len(row) != n_cols_b:
|
||||
raise ValueError("Second matrix rows must have the same length.")
|
||||
|
||||
# ---- Dimension compatibility ----
|
||||
if n_cols_a != n_rows_b:
|
||||
raise ValueError(
|
||||
f"Incompatible dimensions: {n_rows_a}x{n_cols_a} cannot be "
|
||||
f"multiplied by {n_rows_b}x{n_cols_b}"
|
||||
)
|
||||
|
||||
# ---- Compute product ----
|
||||
result: List[List[float]] = [
|
||||
[0.0 for _ in range(n_cols_b)] for _ in range(n_rows_a)
|
||||
]
|
||||
|
||||
for i in range(n_rows_a):
|
||||
for j in range(n_cols_b):
|
||||
sum_prod = 0.0
|
||||
for k in range(n_cols_a):
|
||||
sum_prod += a[i][k] * b[k][j]
|
||||
result[i][j] = sum_prod
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def transpose(matrix: Sequence[Sequence[float]]) -> List[List[float]]:
|
||||
"""
|
||||
Return the transpose of a matrix.
|
||||
|
||||
Raises
|
||||
-------
|
||||
ValueError
|
||||
If the matrix is empty or rows have differing lengths.
|
||||
"""
|
||||
if not matrix or not isinstance(matrix, Sequence):
|
||||
raise ValueError("Argument must be a non-empty matrix.")
|
||||
if not all(isinstance(row, Sequence) for row in matrix):
|
||||
raise ValueError("Argument must be a sequence of sequences.")
|
||||
n_rows = len(matrix)
|
||||
n_cols = len(matrix[0])
|
||||
if n_cols == 0:
|
||||
raise ValueError("Matrix rows cannot be empty.")
|
||||
for row in matrix:
|
||||
if len(row) != n_cols:
|
||||
raise ValueError("All rows must have the same length.")
|
||||
return [[matrix[i][j] for i in range(n_rows)] for j in range(n_cols)]
|
||||
|
||||
|
||||
def rotate(matrix: Sequence[Sequence[float]]) -> List[List[float]]:
|
||||
"""
|
||||
Rotate the matrix 90 degrees clockwise.
|
||||
|
||||
Raises
|
||||
-------
|
||||
ValueError
|
||||
If the matrix is empty or rows have differing lengths.
|
||||
"""
|
||||
if not matrix or not isinstance(matrix, Sequence):
|
||||
raise ValueError("Argument must be a non-empty matrix.")
|
||||
if not all(isinstance(row, Sequence) for row in matrix):
|
||||
raise ValueError("Argument must be a sequence of sequences.")
|
||||
n_rows = len(matrix)
|
||||
n_cols = len(matrix[0])
|
||||
if n_cols == 0:
|
||||
raise ValueError("Matrix rows cannot be empty.")
|
||||
for row in matrix:
|
||||
if len(row) != n_cols:
|
||||
raise ValueError("All rows must have the same length.")
|
||||
transposed = transpose(matrix)
|
||||
return [list(reversed(row)) for row in transposed]
|
||||
|
||||
|
||||
def rot_2D(theta_degrees: float) -> List[List[float]]:
|
||||
"""
|
||||
Return a 2x2 rotation matrix for the given angle in degrees.
|
||||
|
||||
Args:
|
||||
theta_degrees: Rotation angle in degrees.
|
||||
|
||||
Returns:
|
||||
A 2x2 matrix [[cos, -sin], [sin, cos]].
|
||||
|
||||
Raises:
|
||||
ValueError: If theta_degrees is not a number.
|
||||
"""
|
||||
|
||||
if not isinstance(theta_degrees, (int, float)):
|
||||
raise ValueError("Angle must be a number.")
|
||||
theta = math.radians(theta_degrees)
|
||||
cos_theta = math.cos(theta)
|
||||
sin_theta = math.sin(theta)
|
||||
return [[cos_theta, -sin_theta], [sin_theta, cos_theta]]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("matmul test 1")
|
||||
a = [[1, 2, 3], [4, 5, 6]]
|
||||
b = [[7, 8], [9, 10], [11, 12]]
|
||||
print(matmul(a, b))
|
||||
|
||||
print("matmul test 2")
|
||||
a = [[1, 0], [0, 1]]
|
||||
b = [[5, 6], [7, 8]]
|
||||
print(matmul(a, b))
|
||||
|
||||
print("\ntranspose test 1")
|
||||
m = [[1, 2, 3], [4, 5, 6]]
|
||||
print(transpose(m))
|
||||
|
||||
print("\ntranspose test 2")
|
||||
m = [[1, 2], [3, 4], [5, 6]]
|
||||
print(transpose(m))
|
||||
|
||||
print("\nrotate test 1")
|
||||
m = [[1, 2, 3], [4, 5, 6]]
|
||||
print(rotate(m))
|
||||
|
||||
print("\nrotate test 2")
|
||||
m = [[1, 2], [3, 4]]
|
||||
print(rotate(m))
|
||||
|
||||
print("\nrot_2D test 1")
|
||||
print(rot_2D(90))
|
||||
|
||||
print("\nrot_2D test 2")
|
||||
print(rot_2D(45))
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
[project]
|
||||
name = "matrixmania"
|
||||
version = "0.1.0"
|
||||
Reference in new issue
Block a user