rot_3d added

This commit is contained in:
BrSh31 2025-11-04 20:53:08 +01:00
parent d0086d6ae5
commit deba331de9
4 changed files with 75 additions and 6 deletions

View File

@ -2,12 +2,28 @@
A small teaching package for basic linear algebra operations.
## Overview
**MatrixMania** is a lightweight educational package for performing basic linear algebra operations such as matrix multiplication, transposition, and generating 2D or 3D rotation matrices.
It is intended for learning and demonstration purposes.
---
## Functions
### `matmul(A, B)`
Matrix multiplication between two 2D lists.
Performs matrix multiplication between two 2D lists (matrices).
**Parameters**
- `A` (`List[List[float]]`): The first matrix.
- `B` (`List[List[float]]`): The second matrix.
**Returns**
- `List[List[float]]`: The resulting matrix product.
**Example**
```python
>>> from matrixmania import matmul
>>> matmul([[1, 2]], [[3], [4]])
[[11]]
from matrixmania import matmul
print(matmul([[1, 2]], [[3], [4]]))
# [[11]]

View File

@ -1 +1 @@
from .compute import matmul, transpose, rot_2D
from .compute import matmul, transpose, rot_2D, rot_3D

View File

@ -66,6 +66,59 @@ def rot_2D(theta: float) -> List[List[float]]:
[sin_t, cos_t]
]
def rot_3D(angle: float, axis: str) -> List[List[float]]:
"""
Returns a 3D rotation matrix for a given angle and axis.
Rotates points in 3D space around the specified axis by the given angle.
The rotation follows the right-hand rule (counterclockwise when looking
along the axis of rotation).
Rotation matrices:
x-axis:
[[1, 0, 0],
[0, cosθ, -sinθ],
[0, sinθ, cosθ]]
y-axis:
[[ cosθ, 0, sinθ],
[ 0, 1, 0],
[-sinθ, 0, cosθ]]
z-axis:
[[cosθ, -sinθ, 0],
[sinθ, cosθ, 0],
[ 0, 0, 1]]
:param angle: Rotation angle in radians.
:param axis: Axis of rotation ('x', 'y', or 'z').
:return: A 3×3 rotation matrix as a list of lists.
:raises ValueError: If the axis is not 'x', 'y', or 'z'.
"""
cos_t = math.cos(angle)
sin_t = math.sin(angle)
if axis == 'x':
return [
[1, 0, 0],
[0, cos_t, -sin_t],
[0, sin_t, cos_t]
]
elif axis == 'y':
return [
[cos_t, 0, sin_t],
[0, 1, 0],
[-sin_t, 0, cos_t]
]
elif axis == 'z':
return [
[cos_t, -sin_t, 0],
[sin_t, cos_t, 0],
[0, 0, 1]
]
else:
raise ValueError("Axis must be 'x', 'y', or 'z'.")
def main():
# Test matrix multiplication

View File

@ -1,6 +1,6 @@
[project]
name = "matrixmania_shehibr"
version = "0.1.0"
version = "0.2.0"
description = "MatrixMania: Simple linear algebra functions for teaching (matmul, transpose,rot_2D)."
authors = [
{ name="shehibr", email="shehibr98345@th-nuernberg.de" }