erster stand :)
This commit is contained in:
commit
c20f8a484e
11 files changed
+196
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/
|
||||||
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>
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
|
<option name="ignoredIdentifiers">
|
||||||
|
<list>
|
||||||
|
<option value="turtle.Turtle.draw_circle_fragment" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</inspection_tool>
|
||||||
|
</profile>
|
||||||
|
</component>
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
||||||
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>
|
||||||
+134
@@ -0,0 +1,134 @@
|
|||||||
|
"""Compute module providing matrix multiplication functionality.
|
||||||
|
|
||||||
|
This module defines a single public function :func:`matmul`, which performs
|
||||||
|
standard matrix multiplication for two 2‑D lists.
|
||||||
|
|
||||||
|
The function explicitly checks that the number of columns of the first matrix
|
||||||
|
matches the number of rows of the second matrix. If they are mismatched the
|
||||||
|
function prints ``"Matrices not compatible :("`` and returns an empty list.
|
||||||
|
|
||||||
|
All variables are named distinctly to aid readability and to avoid
|
||||||
|
conflicts.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
__all__: list[str] = ["matmul", "transpose"]
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
def matmul(
|
||||||
|
matrix_a: list[list[float]],
|
||||||
|
matrix_b: list[list[float]],
|
||||||
|
) -> list[list[float]]:
|
||||||
|
"""Return the product of two matrices.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
matrix_a: list[list[float]]
|
||||||
|
The left‑hand operand, a 2‑D list where each inner list represents a row.
|
||||||
|
matrix_b: list[list[float]]
|
||||||
|
The right‑hand operand, also a 2‑D list.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
list[list[float]]
|
||||||
|
The resulting matrix as a 2‑D list. If the matrices cannot be
|
||||||
|
multiplied due to incompatible dimensions, an empty list is returned.
|
||||||
|
|
||||||
|
Examples
|
||||||
|
--------
|
||||||
|
# >>> matmul([[1, 2], [3, 4]], [[5, 6], [7, 8]])
|
||||||
|
[[19.0, 22.0], [43.0, 50.0]]
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not matrix_a or not matrix_b:
|
||||||
|
print("Matrices not compatible :(")
|
||||||
|
return []
|
||||||
|
|
||||||
|
rows_a: int = len(matrix_a)
|
||||||
|
cols_a: int = len(matrix_a[0])
|
||||||
|
rows_b: int = len(matrix_b)
|
||||||
|
cols_b: int = len(matrix_b[0])
|
||||||
|
|
||||||
|
if cols_a != rows_b:
|
||||||
|
print("Matrices not compatible :(")
|
||||||
|
return []
|
||||||
|
|
||||||
|
# Initialise result matrix with zeros
|
||||||
|
result: list[list[float]] = [[0.0 for _ in range(cols_b)] for _ in range(rows_a)]
|
||||||
|
|
||||||
|
for i in range(rows_a):
|
||||||
|
for j in range(cols_b):
|
||||||
|
sum_val: float = 0.0
|
||||||
|
for k in range(cols_a):
|
||||||
|
sum_val += matrix_a[i][k] * matrix_b[k][j]
|
||||||
|
result[i][j] = sum_val
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def get_rotate(matrix: list[list[float]], angle:int) -> list[list[float]]:
|
||||||
|
"""Apply a rotation matrix to a base matrix.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
matrix: list[list[float]]
|
||||||
|
The matrix to be rotated.
|
||||||
|
angle: int
|
||||||
|
The angle to be rotated.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
list[list[float]]
|
||||||
|
Result of *matrix* rotated by *angle*.
|
||||||
|
"""
|
||||||
|
|
||||||
|
rows: int = len(matrix)
|
||||||
|
cols: int = len(matrix[0])
|
||||||
|
if cols != 1:
|
||||||
|
print("Can't rotate this matrix! Wrong amount of columns!")
|
||||||
|
return [[0, 0],[0, 0]]
|
||||||
|
if rows == 2:
|
||||||
|
return [[math.cos(angle), -math.sin(angle)], [math.sin(angle), math.cos(angle)]]
|
||||||
|
else:
|
||||||
|
print("Can't rotate this matrix! Wrong amount of rows!")
|
||||||
|
return [[0, 0],[0, 0]]
|
||||||
|
return matmul(rotate_matrix, matrix)
|
||||||
|
|
||||||
|
|
||||||
|
def transpose(matrix: list[list[float]]) -> list[list[float]]:
|
||||||
|
"""Return the transpose of a 2‑D list.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
matrix: list[list[float]]
|
||||||
|
The matrix to transpose.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
list[list[float]]
|
||||||
|
Transposed matrix.
|
||||||
|
"""
|
||||||
|
if not matrix:
|
||||||
|
return []
|
||||||
|
rows = len(matrix)
|
||||||
|
cols = len(matrix[0])
|
||||||
|
result: list[list[float]] = [[None for _ in range(rows)] for _ in range(cols)]
|
||||||
|
for i in range(rows):
|
||||||
|
for j in range(cols):
|
||||||
|
result[j][i] = matrix[i][j]
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(matmul([[1, 2], [3, 4]], [[5, 6], [7, 8]]))
|
||||||
|
print(transpose([[1, 2], [3, 4]]))
|
||||||
|
print(len([[1],[0]]))
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
[project]
|
||||||
|
name = "matrixmania"
|
||||||
|
version = "0.1.0"
|
||||||
Reference in new issue
Block a user