Compare commits
7 Commits
28ec72c0ae
...
bd962791ff
| Author | SHA1 | Date | |
|---|---|---|---|
| bd962791ff | |||
| e0891258d2 | |||
| 23e7c63d11 | |||
| f5afa78ff9 | |||
| 848326ab4d | |||
| 3da55a264a | |||
| cc56ea68de |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/dist/
|
||||||
114
README.md
Normal file
114
README.md
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
# MatrixMania
|
||||||
|
|
||||||
|
This Package contains a few fundamental function which can be used for **matrices**.
|
||||||
|
|
||||||
|
## Functions:
|
||||||
|
|
||||||
|
- matmul function
|
||||||
|
- transpose function
|
||||||
|
- rot_2D function
|
||||||
|
- rot_3D function
|
||||||
|
|
||||||
|
## matmul function:
|
||||||
|
|
||||||
|
This function calculates the matrix multiplication with 2 matrices.
|
||||||
|
It also checks if the matrices are compatible for multiplication.
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
|
||||||
|
```
|
||||||
|
matrix_a = [[3, 4, -1, 4],
|
||||||
|
[-2, 2, 5, 1]]
|
||||||
|
|
||||||
|
matrix_b = [[1, 3, -2],
|
||||||
|
[2, 5, 1],
|
||||||
|
[-1, 4, -4],
|
||||||
|
[2, 3, 6]]
|
||||||
|
|
||||||
|
matrix_c = matmul(matrix_a, matrix_b)
|
||||||
|
print("matrix c:")
|
||||||
|
print(tabulate(matrix_c))
|
||||||
|
```
|
||||||
|
|
||||||
|
**Result:**
|
||||||
|
|
||||||
|
```
|
||||||
|
matrix c:
|
||||||
|
-- -- --
|
||||||
|
20 37 26
|
||||||
|
-1 27 -8
|
||||||
|
-- -- --
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## transpose function:
|
||||||
|
|
||||||
|
This function transposes a matrix.
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
|
||||||
|
```
|
||||||
|
matrix = [[1, 2, 3],
|
||||||
|
[4, 5, 6]]
|
||||||
|
|
||||||
|
new_matrix = transpose(matrix)
|
||||||
|
print("transposed matrix:")
|
||||||
|
print(tabulate(new_matrix))
|
||||||
|
```
|
||||||
|
|
||||||
|
**Result:**
|
||||||
|
|
||||||
|
```
|
||||||
|
transposed matrix:
|
||||||
|
- -
|
||||||
|
1 4
|
||||||
|
2 5
|
||||||
|
3 6
|
||||||
|
- -
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## rot_2D function:
|
||||||
|
|
||||||
|
This function returns the rotation matrix for a given angle.
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
|
||||||
|
```
|
||||||
|
theta = math.pi / 2
|
||||||
|
R = rot_2D(theta)
|
||||||
|
|
||||||
|
print("rotation matrix for 90°:")
|
||||||
|
print(tabulate(R))
|
||||||
|
```
|
||||||
|
|
||||||
|
**Result**
|
||||||
|
|
||||||
|
```
|
||||||
|
rotation matrix for 90°:
|
||||||
|
----------- ------------
|
||||||
|
6.12323e-17 -1
|
||||||
|
1 6.12323e-17
|
||||||
|
----------- ------------
|
||||||
|
```
|
||||||
|
|
||||||
|
## rot_3D function:
|
||||||
|
This function calculates the rotation matrix for one 3D axis (x, y, z) for a certain angle.
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
```
|
||||||
|
theta = math.pi / 2
|
||||||
|
R = rot_3D(theta, "y")
|
||||||
|
|
||||||
|
print("rotation matrix for 90° on the y axis:")
|
||||||
|
print(tabulate(R))
|
||||||
|
```
|
||||||
|
**Result:**
|
||||||
|
```
|
||||||
|
rotation matrix for 90° on the y axis:
|
||||||
|
------------ - -----------
|
||||||
|
6.12323e-17 0 1
|
||||||
|
0 1 0
|
||||||
|
-1 0 6.12323e-17
|
||||||
|
------------ - -----------
|
||||||
|
```
|
||||||
1
matrixmania/__init__.py
Normal file
1
matrixmania/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
from .compute import matmul, transpose, rot_2D, rot_3D, project_ortho
|
||||||
@ -1,5 +1,5 @@
|
|||||||
import math
|
import math
|
||||||
from typing import List
|
from typing import List, Tuple
|
||||||
from tabulate import tabulate
|
from tabulate import tabulate
|
||||||
|
|
||||||
def matmul(matrix_a:List[List[float]], matrix_b:List[List[float]]) -> List[List[float]]:
|
def matmul(matrix_a:List[List[float]], matrix_b:List[List[float]]) -> List[List[float]]:
|
||||||
@ -49,10 +49,40 @@ def transpose(matrix:List[List[int]]) -> List[List[int]]:
|
|||||||
|
|
||||||
|
|
||||||
def rot_2D(angle: float) -> List[List[float]]:
|
def rot_2D(angle: float) -> List[List[float]]:
|
||||||
rot_matrix = [[math.cos(angle), -math.sin(angle)],
|
'''
|
||||||
[math.sin(angle), math.cos(angle)]]
|
calculates the rotation matrix in 2D
|
||||||
return rot_matrix
|
:param angle: the angle
|
||||||
|
:return: rotation matrix
|
||||||
|
'''
|
||||||
|
return [[math.cos(angle), -math.sin(angle)],
|
||||||
|
[math.sin(angle), math.cos(angle)]]
|
||||||
|
|
||||||
|
def rot_3D(angle: float, axis: str) -> List[List[float]]:
|
||||||
|
'''
|
||||||
|
calculates the rotation matrix for one 3D axis
|
||||||
|
:param angle: the angle
|
||||||
|
:param axis: the axis
|
||||||
|
:return: rotation matrix for the wanted axis
|
||||||
|
'''
|
||||||
|
axis = axis.lower()
|
||||||
|
if axis == "x":
|
||||||
|
return [[1, 0, 0],
|
||||||
|
[0, math.cos(angle), -math.sin(angle)],
|
||||||
|
[0, math.sin(angle), math.cos(angle)]]
|
||||||
|
elif axis == "y":
|
||||||
|
return [[math.cos(angle), 0, math.sin(angle)],
|
||||||
|
[0, 1, 0],
|
||||||
|
[-math.sin(angle), 0, math.cos(angle)]]
|
||||||
|
elif axis == "z":
|
||||||
|
return [[math.cos(angle), -math.sin(angle), 0],
|
||||||
|
[math.sin(angle), math.cos(angle), 0],
|
||||||
|
[0, 0, 1]]
|
||||||
|
else:
|
||||||
|
raise ValueError("Axis not valid")
|
||||||
|
|
||||||
|
def project_ortho(point: Tuple[float, float, float], scale: float = 1) -> Tuple[float, float]:
|
||||||
|
point_2D = (point[0] * scale, point[1] * scale)
|
||||||
|
return point_2D
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
@ -68,9 +98,10 @@ if __name__ == "__main__":
|
|||||||
]
|
]
|
||||||
|
|
||||||
matrix_c = matmul(matrix_a, matrix_b)
|
matrix_c = matmul(matrix_a, matrix_b)
|
||||||
print("Ergebnis C = A * B:")
|
print("Matrix C:")
|
||||||
for row in matrix_c:
|
print(tabulate(matrix_c))
|
||||||
print(row)
|
#for row in matrix_c:
|
||||||
|
# print(row)
|
||||||
|
|
||||||
matrix = [
|
matrix = [
|
||||||
[1, 2, 3],
|
[1, 2, 3],
|
||||||
@ -78,8 +109,9 @@ if __name__ == "__main__":
|
|||||||
]
|
]
|
||||||
new_matrix = transpose(matrix)
|
new_matrix = transpose(matrix)
|
||||||
print("transposed matrix:")
|
print("transposed matrix:")
|
||||||
for row in new_matrix:
|
print(tabulate(new_matrix))
|
||||||
print(row)
|
#for row in new_matrix:
|
||||||
|
# print(row)
|
||||||
|
|
||||||
#empty_matrix = []
|
#empty_matrix = []
|
||||||
#transpose(empty_matrix)
|
#transpose(empty_matrix)
|
||||||
@ -87,7 +119,7 @@ if __name__ == "__main__":
|
|||||||
theta = math.pi / 2
|
theta = math.pi / 2
|
||||||
R = rot_2D(theta)
|
R = rot_2D(theta)
|
||||||
print("Rotatiosnmatrix für 90 Grad:")
|
print("Rotatiosnmatrix für 90 Grad:")
|
||||||
print(R)
|
print(tabulate(R))
|
||||||
|
|
||||||
p = [[1, 0]]
|
p = [[1, 0]]
|
||||||
rotated = matmul(p, R)
|
rotated = matmul(p, R)
|
||||||
@ -97,3 +129,12 @@ if __name__ == "__main__":
|
|||||||
print("Matrix A:")
|
print("Matrix A:")
|
||||||
print(tabulate(matrix_a))
|
print(tabulate(matrix_a))
|
||||||
|
|
||||||
|
theta = math.pi / 2
|
||||||
|
R = rot_3D(theta, "y")
|
||||||
|
|
||||||
|
print("rotation matrix for 90° on the y axis:")
|
||||||
|
print(tabulate(R))
|
||||||
|
|
||||||
|
print("Projection:")
|
||||||
|
print(project_ortho((0.5, -0.5, 10), scale=200)) # -> (100, -100)
|
||||||
|
|
||||||
16
pyproject.toml
Normal file
16
pyproject.toml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
[project]
|
||||||
|
name = "matrixmania_benkertmo99686"
|
||||||
|
version = "0.2.0"
|
||||||
|
description = "MatrixMania: Simple linear algebra functions for teaching (matmul, transpose, rot_2D)."
|
||||||
|
authors = [
|
||||||
|
{ name="Mona Benkert", email="benkertmo99686@th-nuernberg.de" }
|
||||||
|
]
|
||||||
|
readme = "README.md"
|
||||||
|
license = { text = "MIT" }
|
||||||
|
requires-python = ">=3.7"
|
||||||
|
dependencies = ["tabulate"]
|
||||||
|
[build-system]
|
||||||
|
requires = ["hatchling"]
|
||||||
|
build-backend = "hatchling.build"
|
||||||
|
[tool.hatch.build.targets.wheel]
|
||||||
|
packages = ["matrixmania"]
|
||||||
0
test/__init__py.py
Normal file
0
test/__init__py.py
Normal file
24
test/test_matmul.py
Normal file
24
test/test_matmul.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
from matrixmania import matmul
|
||||||
|
|
||||||
|
def test_matmul_1():
|
||||||
|
a = [[4, 3, 2],
|
||||||
|
[1, 2, 3]]
|
||||||
|
|
||||||
|
b = [[2],
|
||||||
|
[3],
|
||||||
|
[4]]
|
||||||
|
|
||||||
|
assert matmul(a, b) == [[25],
|
||||||
|
[20]]
|
||||||
|
|
||||||
|
def test_matmul_2():
|
||||||
|
a = [[3, 4, -1, 4],
|
||||||
|
[-2, 2, 5, 1]]
|
||||||
|
|
||||||
|
b = [[1, 3, -2],
|
||||||
|
[2, 5, 1],
|
||||||
|
[-1, 4, -4],
|
||||||
|
[2, 3, 6]]
|
||||||
|
|
||||||
|
assert matmul(a, b) == [[20, 37, 26],
|
||||||
|
[-1, 27, -8]]
|
||||||
10
test/test_projection.py
Normal file
10
test/test_projection.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from matrixmania import project_ortho
|
||||||
|
|
||||||
|
def test_projection_1():
|
||||||
|
point = (1.0, 2.0, 3.0)
|
||||||
|
assert project_ortho(point) == (1.0, 2.0)
|
||||||
|
|
||||||
|
def test_projection_2():
|
||||||
|
point = (1.0, 2.0, 3.0)
|
||||||
|
scale = 100
|
||||||
|
assert project_ortho(point, scale) == (100.0, 200.0)
|
||||||
11
test/test_rot.py
Normal file
11
test/test_rot.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from matrixmania import rot_2D
|
||||||
|
import math
|
||||||
|
|
||||||
|
def test_rot_1():
|
||||||
|
angle = 0
|
||||||
|
assert rot_2D(angle) == [[1.0, -0.0],
|
||||||
|
[0.0, 1.0]]
|
||||||
|
|
||||||
|
def test_rot_2():
|
||||||
|
angle = math.pi
|
||||||
|
assert rot_2D(angle) == [[-1.0, -1.2246467991473532e-16], [1.2246467991473532e-16, -1.0]]
|
||||||
16
test/test_transpose.py
Normal file
16
test/test_transpose.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from matrixmania import transpose
|
||||||
|
|
||||||
|
def test_transpose_1():
|
||||||
|
a = [[1, 2, 3],
|
||||||
|
[4, 5, 6]]
|
||||||
|
assert transpose(a) == [[1, 4],
|
||||||
|
[2, 5],
|
||||||
|
[3, 6]]
|
||||||
|
|
||||||
|
def test_transpose_2():
|
||||||
|
a = [[3, 4, -1, 4],
|
||||||
|
[-2, 2, 5, 1]]
|
||||||
|
assert transpose(a) == [[3, -2],
|
||||||
|
[4, 2],
|
||||||
|
[-1, 5],
|
||||||
|
[4, 1]]
|
||||||
Loading…
x
Reference in New Issue
Block a user