1
This commit is contained in:
commit
aa94d99376
12 files changed
+191
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
.venv/
|
||||||
|
__pycache__/
|
||||||
|
.env
|
||||||
Generated
+10
@@ -0,0 +1,10 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Ignored default folder with query files
|
||||||
|
/queries/
|
||||||
|
# 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
+14
@@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module external.system.id="pyproject.toml" type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="jdk" jdkName="~/PycharmProjects/MatrixMania/.venv" jdkType="Python SDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
<component name="PackageRequirementsSettings" />
|
||||||
|
<component name="PyDocumentationSettings" />
|
||||||
|
<component name="ReSTService" />
|
||||||
|
<component name="TestRunnerService" />
|
||||||
|
</module>
|
||||||
Generated
+4
@@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="~/PycharmProjects/MatrixMania/.venv" 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,4 @@
|
|||||||
|
# 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`.
|
||||||
+101
@@ -0,0 +1,101 @@
|
|||||||
|
"""Matrix multiplication with explicit dimension checks."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Sequence
|
||||||
|
|
||||||
|
Matrix = Sequence[Sequence[float]]
|
||||||
|
|
||||||
|
|
||||||
|
def matmul(left: Matrix, right: Matrix) -> list[list[float]]:
|
||||||
|
"""Multiply two matrices and return a new matrix.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
left: Matrix of shape (rows_left, cols_left).
|
||||||
|
right: Matrix of shape (rows_right, cols_right).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A new matrix of shape (rows_left, cols_right).
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If either input is empty, ragged, or if
|
||||||
|
cols_left != rows_right.
|
||||||
|
"""
|
||||||
|
_validate(left, "left")
|
||||||
|
_validate(right, "right")
|
||||||
|
|
||||||
|
cols_left = len(left[0])
|
||||||
|
rows_right = len(right)
|
||||||
|
if cols_left != rows_right:
|
||||||
|
raise ValueError(
|
||||||
|
f"dimension mismatch: left has {cols_left} columns, "
|
||||||
|
f"right has {rows_right} rows"
|
||||||
|
)
|
||||||
|
|
||||||
|
cols_right = len(right[0])
|
||||||
|
result: list[list[float]] = []
|
||||||
|
for i in range(len(left)):
|
||||||
|
row: list[float] = []
|
||||||
|
for j in range(cols_right):
|
||||||
|
total = 0.0
|
||||||
|
for k in range(cols_left):
|
||||||
|
total += left[i][k] * right[k][j]
|
||||||
|
row.append(total)
|
||||||
|
result.append(row)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def _validate(matrix: Matrix, name: str) -> None:
|
||||||
|
"""Check that *matrix* is a non-empty, rectangular 2D sequence."""
|
||||||
|
if len(matrix) == 0:
|
||||||
|
raise ValueError(f"{name} matrix is empty")
|
||||||
|
width = len(matrix[0])
|
||||||
|
if width == 0:
|
||||||
|
raise ValueError(f"{name} matrix has no columns")
|
||||||
|
for index, row in enumerate(matrix):
|
||||||
|
if len(row) != width:
|
||||||
|
raise ValueError(
|
||||||
|
f"{name} matrix is ragged: row 0 has {width} columns, "
|
||||||
|
f"row {index} has {len(row)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Example 1: standard 2x3 * 3x2 multiplication.
|
||||||
|
a = [
|
||||||
|
[1, 2, 3],
|
||||||
|
[4, 5, 6],
|
||||||
|
]
|
||||||
|
b = [
|
||||||
|
[7, 8],
|
||||||
|
[9, 10],
|
||||||
|
[11, 12],
|
||||||
|
]
|
||||||
|
expected = [
|
||||||
|
[58, 64],
|
||||||
|
[139, 154],
|
||||||
|
]
|
||||||
|
assert matmul(a, b) == expected, f"example 1 failed: {matmul(a, b)}"
|
||||||
|
print("example 1 passed:", matmul(a, b))
|
||||||
|
|
||||||
|
# Example 2: multiplication with the identity matrix changes nothing.
|
||||||
|
identity = [
|
||||||
|
[1, 0, 0],
|
||||||
|
[0, 1, 0],
|
||||||
|
[0, 0, 1],
|
||||||
|
]
|
||||||
|
c = [
|
||||||
|
[2, -1, 0],
|
||||||
|
[0, 3, 4],
|
||||||
|
[5, 6, -2],
|
||||||
|
]
|
||||||
|
assert matmul(c, identity) == c, "example 2 failed"
|
||||||
|
print("example 2 passed:", matmul(c, identity))
|
||||||
|
|
||||||
|
# Example 3: mismatched dimensions must raise ValueError.
|
||||||
|
try:
|
||||||
|
matmul([[1, 2, 3]], [[1, 2]])
|
||||||
|
except ValueError as error:
|
||||||
|
print("example 3 passed:", error)
|
||||||
|
else:
|
||||||
|
raise AssertionError("example 3 failed: no ValueError raised")
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
# This is a sample Python script.
|
||||||
|
|
||||||
|
# Press Shift+F10 to execute it or replace it with your code.
|
||||||
|
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
|
||||||
|
|
||||||
|
|
||||||
|
def print_hi(name):
|
||||||
|
# Use a breakpoint in the code line below to debug your script.
|
||||||
|
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
|
||||||
|
|
||||||
|
|
||||||
|
# Press the green button in the gutter to run the script.
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print_hi('PyCharm')
|
||||||
|
|
||||||
|
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://opencode.ai/config.json",
|
||||||
|
"provider": {
|
||||||
|
"thn": {
|
||||||
|
"npm": "@ai-sdk/openai-compatible",
|
||||||
|
"options": {
|
||||||
|
"baseURL": "http://141.75.33.244:8000/v1",
|
||||||
|
"apiKey": "sk-QXj1p40XOwNSze_5-eYrBzVh3qIgAfqoWPkAjNVqOS8"
|
||||||
|
},
|
||||||
|
"models": {
|
||||||
|
"gpt-oss:20b": { "name": "GPT-OSS 20B" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"model": "thn/gpt-oss:20b"
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
[project]
|
||||||
|
name = "matrixmania"
|
||||||
|
version = "0.1.0"
|
||||||
Reference in new issue
Block a user