Skip to content

Commit ab0f2e0

Browse files
authored
Feature/refactor (#9)
* Describe installation from pypi in the Readme * Make Vector.algebra a class property * Add Vector.basis
1 parent 9178393 commit ab0f2e0

File tree

7 files changed

+26
-31
lines changed

7 files changed

+26
-31
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ The key features are:
1313

1414
# Installation
1515

16-
From source:
17-
18-
git clone [email protected]:wandelbotsgmbh/geometricalgebra.git
19-
pip install ./geometricalgebra/
16+
pip install geometricalgebra
2017

2118
# Example
2219

geometricalgebra/algebra.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class GeometricAlgebra:
5151
_instances: Dict[Tuple[int, ...], "GeometricAlgebra"] = {}
5252

5353
def __repr__(self):
54-
return f"GeometricAlgebra({self.signature()})"
54+
return f"GeometricAlgebra({self.signature})"
5555

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

181181
@lru_cache(2**14)
182182
def grades_of_product(
183-
self, grades_a: Grades, grades_b: Grades, product_type: ProductType = ProductType.GEOMETRIC
183+
self, grades_left: Grades, grades_right: Grades, product_type: ProductType = ProductType.GEOMETRIC
184184
) -> Grades:
185185
"""Return the grades which might have non-zero entries given the grades of the two inputs multivectors
186186
187187
Note: It gives a superset of the grades since depending on the actual values some grades may vanish,
188188
e.g., a & a
189189
190190
Args:
191-
grades_a: the grades of the first argument
192-
grades_b: the grades of the second argument
191+
grades_left: the grades of the left argument
192+
grades_right: the grades of the right argument
193193
product_type: type of the product (inner, outer, geometric)
194194
195195
Returns:
@@ -198,7 +198,7 @@ def grades_of_product(
198198
Raises:
199199
TypeError: if product_type is of wrong type
200200
"""
201-
all_pairs = ((i, j) for i in grades_a for j in grades_b)
201+
all_pairs = ((i, j) for i in grades_left for j in grades_right)
202202
if product_type in (ProductType.GEOMETRIC, ProductType.COMMUTATOR, ProductType.ANTICOMMUTATOR):
203203
# TODO(dv): commutator and anticommutator output grades can be narrowed down (e.g. based on the generator matrix)
204204
result = (i for (a, b) in all_pairs for i in range(abs(a - b), a + b + 1, 2))

geometricalgebra/cga.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def e_inf(cls):
7878

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

9292
@classmethod
9393
def from_euclid_2d(cls: Type[Subtype], vectors: ArrayLike) -> Subtype:
94-
"""Create a positional vector from euclidean points in 2D (via Hestenes mapping) in the xy-plane
94+
"""Create a positional vector from Euclidean points in 2D (via Hestenes mapping) in the xy-plane
9595
9696
Args:
9797
vectors: any array of shape (..., 2)
@@ -398,7 +398,7 @@ def from_motor_estimation(
398398
only_2d: Whether only 2d motors are valid
399399
400400
Returns:
401-
motor that maps p to to the entities q (as far as possible)
401+
motor that maps p to the entities q (as far as possible)
402402
403403
Reference:
404404
Valkenburg, Dorst, "Estimating Motors from a Variety of Geometric Data in 3D Conformal Geometric Algebra" (2011)

geometricalgebra/cga2d.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
class Vector(CGAVector):
1010
"""A 2d CGA Tensor"""
1111

12+
@classmethod # type: ignore
1213
@property
13-
def algebra(self):
14+
def algebra(cls):
1415
return ALGEBRA
1516

1617
@classmethod
@@ -46,10 +47,7 @@ def minkovski_plane(cls):
4647
raise NotImplementedError()
4748

4849

49-
e_1 = Vector([1, 0, 0, 0], grade=1)
50-
e_2 = Vector([0, 1, 0, 0], grade=1)
51-
e_plus = Vector([0, 0, 1, 0], grade=1)
52-
e_minus = Vector([0, 0, 0, 1], grade=1)
50+
e_1, e_2, e_plus, e_minus = Vector.basis()
5351
e_inf = e_plus + e_minus
5452
e_0 = (e_minus - e_plus) / 2
5553
i4 = e_1 ^ e_2 ^ e_inf ^ e_0

geometricalgebra/cga3d.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
class Vector(CGAVector):
2121
"""A 3d CGA Tensor"""
2222

23+
@classmethod # type: ignore
2324
@property
24-
def algebra(self):
25+
def algebra(cls):
2526
return ALGEBRA
2627

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

8990

90-
e_1 = Vector([1.0, 0, 0, 0, 0], grade=1)
91-
e_2 = Vector([0.0, 1, 0, 0, 0], grade=1)
92-
e_3 = Vector([0.0, 0, 1, 0, 0], grade=1)
93-
e_plus = Vector([0.0, 0, 0, 1, 0], grade=1)
94-
e_minus = Vector([0.0, 0, 0, 0, 1], grade=1)
91+
e_1, e_2, e_3, e_plus, e_minus = Vector.basis()
9592
e_inf = e_plus + e_minus
9693
e_0 = (e_minus - e_plus) / 2
9794
i3 = e_1 ^ e_2 ^ e_3

geometricalgebra/cga4d.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
class Vector(CGAVector):
1212
"""A 4d CGA Tensor"""
1313

14+
@classmethod # type: ignore
1415
@property
15-
def algebra(self):
16+
def algebra(cls):
1617
return ALGEBRA
1718

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

6061

61-
e_1 = Vector([1, 0, 0, 0, 0, 0], grade=1)
62-
e_2 = Vector([0, 1, 0, 0, 0, 0], grade=1)
63-
e_3 = Vector([0, 0, 1, 0, 0, 0], grade=1)
64-
e_4 = Vector([0, 0, 0, 1, 0, 0], grade=1)
65-
e_inf = Vector([0, 0, 0, 0, 1, 1], grade=1)
66-
e_0 = Vector([0, 0, 0, 0, -0.5, 0.5], grade=1)
62+
e_1, e_2, e_3, e_4, e_plus, e_minus = Vector.basis()
63+
e_inf = e_plus + e_minus
64+
e_0 = (e_minus - e_plus) / 2
6765
i6 = e_1 ^ e_2 ^ e_3 ^ e_4 ^ e_inf ^ e_0
6866
i4 = e_1 ^ e_2 ^ e_3 ^ e_4

geometricalgebra/vector.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class Vector: # pylint: disable=too-many-public-methods
7575
def framework(cls) -> Framework:
7676
return FRAMEWORK
7777

78+
@classmethod
79+
def basis(cls) -> Vector:
80+
return cls(cls.xnp().eye(cls.algebra.dims_of_grade[1]), grade=1) # type: ignore
81+
7882
@classmethod
7983
def xnp(cls):
8084
return cls.framework().numpy
@@ -104,8 +108,9 @@ def __call__(self: Subtype, grade: Union[int, Iterable[int]]) -> Subtype:
104108
mask = self.algebra.mask_from_grades(grades, self._grades)
105109
return type(self)(self._values[..., mask], grades)
106110

111+
@classmethod # type: ignore
107112
@property
108-
def algebra(self):
113+
def algebra(cls):
109114
raise NotImplementedError()
110115

111116
@property

0 commit comments

Comments
 (0)