Skip to content

Commit

Permalink
Feature/refactor (#9)
Browse files Browse the repository at this point in the history
* Describe installation from pypi in the Readme
* Make Vector.algebra a class property
* Add Vector.basis
  • Loading branch information
antemons authored Mar 8, 2024
1 parent 9178393 commit ab0f2e0
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 31 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ The key features are:

# Installation

From source:

git clone [email protected]:wandelbotsgmbh/geometricalgebra.git
pip install ./geometricalgebra/
pip install geometricalgebra

# Example

Expand Down
10 changes: 5 additions & 5 deletions geometricalgebra/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class GeometricAlgebra:
_instances: Dict[Tuple[int, ...], "GeometricAlgebra"] = {}

def __repr__(self):
return f"GeometricAlgebra({self.signature()})"
return f"GeometricAlgebra({self.signature})"

def __new__(cls, signature: Tuple[int, ...]):
if signature not in cls._instances:
Expand Down Expand Up @@ -180,16 +180,16 @@ def mask_from_grades(self, a: Grades, other: Optional[Grades] = None) -> Union[s

@lru_cache(2**14)
def grades_of_product(
self, grades_a: Grades, grades_b: Grades, product_type: ProductType = ProductType.GEOMETRIC
self, grades_left: Grades, grades_right: Grades, product_type: ProductType = ProductType.GEOMETRIC
) -> Grades:
"""Return the grades which might have non-zero entries given the grades of the two inputs multivectors
Note: It gives a superset of the grades since depending on the actual values some grades may vanish,
e.g., a & a
Args:
grades_a: the grades of the first argument
grades_b: the grades of the second argument
grades_left: the grades of the left argument
grades_right: the grades of the right argument
product_type: type of the product (inner, outer, geometric)
Returns:
Expand All @@ -198,7 +198,7 @@ def grades_of_product(
Raises:
TypeError: if product_type is of wrong type
"""
all_pairs = ((i, j) for i in grades_a for j in grades_b)
all_pairs = ((i, j) for i in grades_left for j in grades_right)
if product_type in (ProductType.GEOMETRIC, ProductType.COMMUTATOR, ProductType.ANTICOMMUTATOR):
# TODO(dv): commutator and anticommutator output grades can be narrowed down (e.g. based on the generator matrix)
result = (i for (a, b) in all_pairs for i in range(abs(a - b), a + b + 1, 2))
Expand Down
6 changes: 3 additions & 3 deletions geometricalgebra/cga.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def e_inf(cls):

@classmethod
def from_euclid(cls: Type[Subtype], vectors: ArrayLike) -> Subtype:
"""Create a positional vector from euclidean points (via Hestenes mapping)
"""Create a positional vector from Euclidean points (via Hestenes mapping)
Args:
vectors: any array of shape (..., 3)
Expand All @@ -91,7 +91,7 @@ def from_euclid(cls: Type[Subtype], vectors: ArrayLike) -> Subtype:

@classmethod
def from_euclid_2d(cls: Type[Subtype], vectors: ArrayLike) -> Subtype:
"""Create a positional vector from euclidean points in 2D (via Hestenes mapping) in the xy-plane
"""Create a positional vector from Euclidean points in 2D (via Hestenes mapping) in the xy-plane
Args:
vectors: any array of shape (..., 2)
Expand Down Expand Up @@ -398,7 +398,7 @@ def from_motor_estimation(
only_2d: Whether only 2d motors are valid
Returns:
motor that maps p to to the entities q (as far as possible)
motor that maps p to the entities q (as far as possible)
Reference:
Valkenburg, Dorst, "Estimating Motors from a Variety of Geometric Data in 3D Conformal Geometric Algebra" (2011)
Expand Down
8 changes: 3 additions & 5 deletions geometricalgebra/cga2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
class Vector(CGAVector):
"""A 2d CGA Tensor"""

@classmethod # type: ignore
@property
def algebra(self):
def algebra(cls):
return ALGEBRA

@classmethod
Expand Down Expand Up @@ -46,10 +47,7 @@ def minkovski_plane(cls):
raise NotImplementedError()


e_1 = Vector([1, 0, 0, 0], grade=1)
e_2 = Vector([0, 1, 0, 0], grade=1)
e_plus = Vector([0, 0, 1, 0], grade=1)
e_minus = Vector([0, 0, 0, 1], grade=1)
e_1, e_2, e_plus, e_minus = Vector.basis()
e_inf = e_plus + e_minus
e_0 = (e_minus - e_plus) / 2
i4 = e_1 ^ e_2 ^ e_inf ^ e_0
Expand Down
9 changes: 3 additions & 6 deletions geometricalgebra/cga3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
class Vector(CGAVector):
"""A 3d CGA Tensor"""

@classmethod # type: ignore
@property
def algebra(self):
def algebra(cls):
return ALGEBRA

@classmethod
Expand Down Expand Up @@ -87,11 +88,7 @@ def to_barycentric_coordinates_full(self, grade=1) -> Tuple[Array, Vector]: # t
return self.xnp().einsum("ij,...j->...i", a, self._values), control_points


e_1 = Vector([1.0, 0, 0, 0, 0], grade=1)
e_2 = Vector([0.0, 1, 0, 0, 0], grade=1)
e_3 = Vector([0.0, 0, 1, 0, 0], grade=1)
e_plus = Vector([0.0, 0, 0, 1, 0], grade=1)
e_minus = Vector([0.0, 0, 0, 0, 1], grade=1)
e_1, e_2, e_3, e_plus, e_minus = Vector.basis()
e_inf = e_plus + e_minus
e_0 = (e_minus - e_plus) / 2
i3 = e_1 ^ e_2 ^ e_3
Expand Down
12 changes: 5 additions & 7 deletions geometricalgebra/cga4d.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
class Vector(CGAVector):
"""A 4d CGA Tensor"""

@classmethod # type: ignore
@property
def algebra(self):
def algebra(cls):
return ALGEBRA

@classmethod
Expand Down Expand Up @@ -58,11 +59,8 @@ def __init__(self, values, grade: Union[int, Iterable[int]], algebra=None):
super().__init__(values, grade)


e_1 = Vector([1, 0, 0, 0, 0, 0], grade=1)
e_2 = Vector([0, 1, 0, 0, 0, 0], grade=1)
e_3 = Vector([0, 0, 1, 0, 0, 0], grade=1)
e_4 = Vector([0, 0, 0, 1, 0, 0], grade=1)
e_inf = Vector([0, 0, 0, 0, 1, 1], grade=1)
e_0 = Vector([0, 0, 0, 0, -0.5, 0.5], grade=1)
e_1, e_2, e_3, e_4, e_plus, e_minus = Vector.basis()
e_inf = e_plus + e_minus
e_0 = (e_minus - e_plus) / 2
i6 = e_1 ^ e_2 ^ e_3 ^ e_4 ^ e_inf ^ e_0
i4 = e_1 ^ e_2 ^ e_3 ^ e_4
7 changes: 6 additions & 1 deletion geometricalgebra/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class Vector: # pylint: disable=too-many-public-methods
def framework(cls) -> Framework:
return FRAMEWORK

@classmethod
def basis(cls) -> Vector:
return cls(cls.xnp().eye(cls.algebra.dims_of_grade[1]), grade=1) # type: ignore

@classmethod
def xnp(cls):
return cls.framework().numpy
Expand Down Expand Up @@ -104,8 +108,9 @@ def __call__(self: Subtype, grade: Union[int, Iterable[int]]) -> Subtype:
mask = self.algebra.mask_from_grades(grades, self._grades)
return type(self)(self._values[..., mask], grades)

@classmethod # type: ignore
@property
def algebra(self):
def algebra(cls):
raise NotImplementedError()

@property
Expand Down

0 comments on commit ab0f2e0

Please sign in to comment.