From deba331de96488c338c757a8e629db9cc71f1b09 Mon Sep 17 00:00:00 2001 From: BrSh31 Date: Tue, 4 Nov 2025 20:53:08 +0100 Subject: [PATCH] rot_3d added --- README.md | 24 +++++++++++++++---- matrixmania/__init__.py | 2 +- matrixmania/compute.py | 53 +++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 2 +- 4 files changed, 75 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a35ba43..2e86e67 100644 --- a/README.md +++ b/README.md @@ -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]] diff --git a/matrixmania/__init__.py b/matrixmania/__init__.py index 975a585..c812c86 100644 --- a/matrixmania/__init__.py +++ b/matrixmania/__init__.py @@ -1 +1 @@ -from .compute import matmul, transpose, rot_2D \ No newline at end of file +from .compute import matmul, transpose, rot_2D, rot_3D \ No newline at end of file diff --git a/matrixmania/compute.py b/matrixmania/compute.py index 9dc447e..ce30e61 100644 --- a/matrixmania/compute.py +++ b/matrixmania/compute.py @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 6083f2a..e3db717 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" }