Compare commits
No commits in common. "bd962791ff12816a94183103a58c6640c8e93817" and "28ec72c0ae55180f9f8b22007335b2eb4449abcf" have entirely different histories.
bd962791ff
...
28ec72c0ae
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +0,0 @@
|
|||||||
/dist/
|
|
||||||
114
README.md
114
README.md
@ -1,114 +0,0 @@
|
|||||||
# 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,5 +1,5 @@
|
|||||||
import math
|
import math
|
||||||
from typing import List, Tuple
|
from typing import List
|
||||||
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,40 +49,10 @@ 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)],
|
||||||
calculates the rotation matrix in 2D
|
[math.sin(angle), math.cos(angle)]]
|
||||||
:param angle: the angle
|
return rot_matrix
|
||||||
: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__":
|
||||||
|
|
||||||
@ -98,10 +68,9 @@ if __name__ == "__main__":
|
|||||||
]
|
]
|
||||||
|
|
||||||
matrix_c = matmul(matrix_a, matrix_b)
|
matrix_c = matmul(matrix_a, matrix_b)
|
||||||
print("Matrix C:")
|
print("Ergebnis C = A * B:")
|
||||||
print(tabulate(matrix_c))
|
for row in matrix_c:
|
||||||
#for row in matrix_c:
|
print(row)
|
||||||
# print(row)
|
|
||||||
|
|
||||||
matrix = [
|
matrix = [
|
||||||
[1, 2, 3],
|
[1, 2, 3],
|
||||||
@ -109,9 +78,8 @@ if __name__ == "__main__":
|
|||||||
]
|
]
|
||||||
new_matrix = transpose(matrix)
|
new_matrix = transpose(matrix)
|
||||||
print("transposed matrix:")
|
print("transposed matrix:")
|
||||||
print(tabulate(new_matrix))
|
for row in new_matrix:
|
||||||
#for row in new_matrix:
|
print(row)
|
||||||
# print(row)
|
|
||||||
|
|
||||||
#empty_matrix = []
|
#empty_matrix = []
|
||||||
#transpose(empty_matrix)
|
#transpose(empty_matrix)
|
||||||
@ -119,7 +87,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(tabulate(R))
|
print(R)
|
||||||
|
|
||||||
p = [[1, 0]]
|
p = [[1, 0]]
|
||||||
rotated = matmul(p, R)
|
rotated = matmul(p, R)
|
||||||
@ -129,12 +97,3 @@ 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)
|
|
||||||
|
|
||||||
@ -1 +0,0 @@
|
|||||||
from .compute import matmul, transpose, rot_2D, rot_3D, project_ortho
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
[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"]
|
|
||||||
@ -1,24 +0,0 @@
|
|||||||
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]]
|
|
||||||
@ -1,10 +0,0 @@
|
|||||||
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)
|
|
||||||
@ -1,11 +0,0 @@
|
|||||||
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]]
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
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