Skip to content

Commit 3ab7892

Browse files
authored
Add test for ga.ReciprocalFrame (#308)
Also deprecates passing `mode="nonsense"`, and improves the docstring.
1 parent 9f5416f commit 3ab7892

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

galgebra/ga.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from itertools import combinations
88
import functools
99
from functools import reduce
10-
from typing import Tuple, TypeVar, Callable, Dict
10+
from typing import Tuple, TypeVar, Callable, Dict, Sequence
1111
from ._backports.typing import OrderedDict
1212

1313
from sympy import (
@@ -1976,14 +1976,27 @@ def connection(self, rbase, key_base, mode, left):
19761976
self.connect[mode_key].append((key, C))
19771977
return C
19781978

1979-
def ReciprocalFrame(self, basis, mode='norm'):
1980-
"""
1981-
If ``basis`` is a list/tuple of vectors, ``ReciprocalFrame()`` returns a tuple of reciprocal vectors.
1979+
def ReciprocalFrame(self, basis: Sequence[_mv.Mv], mode: str = 'norm') -> Tuple[_mv.Mv]:
1980+
r"""
1981+
Compute the reciprocal frame :math:`v^i` of a set of vectors :math:`v_i`.
19821982
1983-
If ``mode=norm`` the vectors are normalized.
1984-
If ``mode`` is anything other than ``norm`` the vectors are unnormalized
1985-
and the normalization coefficient is added to the end of the tuple.
1986-
One must divide by this coefficient to normalize the vectors.
1983+
Parameters
1984+
----------
1985+
basis :
1986+
The sequence of vectors :math:`v_i` defining the input frame.
1987+
mode :
1988+
* ``"norm"`` -- indicates that the reciprocal vectors should be
1989+
normalized such that their product with the input vectors is 1,
1990+
:math:`v^i \cdot v_j = \delta_{ij}`.
1991+
* ``"append"`` -- indicates that instead of normalizing, the
1992+
normalization coefficient :math:`E^2` should be appended to the returned tuple.
1993+
One can divide by this coefficient to normalize the vectors.
1994+
The returned vectors are such that
1995+
:math:`v^i \cdot v_j = E^2\delta_{ij}`.
1996+
1997+
.. deprecated:: 0.5.0
1998+
Arbitrary strings are interpreted as ``"append"``, but in
1999+
future will be an error
19872000
"""
19882001
dim = len(basis)
19892002

@@ -2017,11 +2030,17 @@ def ReciprocalFrame(self, basis, mode='norm'):
20172030
rbasis.append(recpv)
20182031
sgn = -sgn
20192032

2020-
if mode != 'norm':
2021-
rbasis.append(E_sq)
2022-
else:
2033+
if mode == 'norm':
20232034
for i in range(dim):
20242035
rbasis[i] = rbasis[i] / E_sq
2036+
else:
2037+
if mode != 'append':
2038+
# galgebra 0.5.0
2039+
warnings.warn(
2040+
"Mode {!r} not understood, falling back to {!r} but this "
2041+
"is deprecated".format(mode, 'append'),
2042+
DeprecationWarning, stacklevel=2)
2043+
rbasis.append(E_sq)
20252044

20262045
return tuple(rbasis)
20272046

test/test_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,33 @@ def test_extracting_vectors_from_conformal_2_blade(self):
363363
aB = a|B
364364
assert str(aB) == '-(P2.a)*P1 + (P1.a)*P2'
365365

366+
def test_ReciprocalFrame(self):
367+
ga, *basis = Ga.build('e*u|v|w')
368+
369+
r_basis = ga.ReciprocalFrame(basis)
370+
371+
for i, base in enumerate(basis):
372+
for r_i, r_base in enumerate(r_basis):
373+
if i == r_i:
374+
assert (base | r_base).simplify() == 1
375+
else:
376+
assert (base | r_base).simplify() == 0
377+
378+
def test_ReciprocalFrame_append(self):
379+
ga, *basis = Ga.build('e*u|v|w')
380+
*r_basis, E_sq = ga.ReciprocalFrame(basis, mode='append')
381+
382+
for i, base in enumerate(basis):
383+
for r_i, r_base in enumerate(r_basis):
384+
if i == r_i:
385+
assert (base | r_base).simplify() == E_sq
386+
else:
387+
assert (base | r_base).simplify() == 0
388+
389+
# anything that isn't 'norm' means 'append', but this is deprecated
390+
with pytest.warns(DeprecationWarning):
391+
assert ga.ReciprocalFrame(basis, mode='nonsense') == (*r_basis, E_sq)
392+
366393
def test_reciprocal_frame_test(self):
367394

368395
g = '1 # #,'+ \

0 commit comments

Comments
 (0)