Skip to content

Commit

Permalink
Use keyword-only arguments where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-wieser committed Jan 2, 2020
1 parent 8eb8df2 commit d96d65c
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 100 deletions.
90 changes: 49 additions & 41 deletions galgebra/ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,16 +376,24 @@ def __hash__(self):
def __eq__(self, ga):
return self.name == ga.name

def __init__(self, bases, **kwargs):
def __init__(self, bases, *, wedge=True, **kwargs):
"""
Parameters
----------
bases :
Passed as ``basis`` to ``Metric``.
wedge :
Use ``^`` symbol to print basis blades
**kwargs :
See :class:`galgebra.metric.Metric`.
"""

# Each time a geometric algebra is intialized in setup of append
# the printer must be restored to the simple text mode (not
# enhanced text of latex printing) so that when 'str' is used to
# create symbol names the names are not mangled.

kwargs = metric.test_init_slots(metric.Metric.init_slots, **kwargs)

self.wedge_print = kwargs['wedge']
self.wedge_print = wedge

if printer.GaLatexPrinter.latex_flg:
printer.GaLatexPrinter.restore()
Expand Down Expand Up @@ -1898,31 +1906,10 @@ class Sm(Ga):
the coordinates of the submanifold. The inputs required to define
the submanifold are:
Parameters
----------
u :
(``args[0]``) The coordinate map defining the submanifold
which is a list of functions of coordinates of the base
manifold in terms of the coordinates of the submanifold.
for example if the manifold is a unit sphere then -
``u = [sin(u)*cos(v),sin(u)*sin(v),cos(u)]``.
Alternatively (``args[0]``) is a parametric vector function
of the basis vectors of the base manifold. The
coefficients of the bases are functions of the coordinates
(``args[1]``). In this case we would call the submanifold
a "vector" manifold and additional characteristics of the
manifold can be calculated since we have given an explicit
embedding of the manifold in the base manifold.
coords :
(``args[1]``) The coordinate list for the submanifold, for
example ``[u, v]``.
Notes
-----
See 'init_slots' for possible other inputs. The 'Ga' member function
The 'Ga' member function
'sm' can be used to instantiate the submanifold via (o3d is the base
manifold)::
Expand All @@ -1932,24 +1919,49 @@ class Sm(Ga):
(eu,ev) = sm_example.mv()
sm_grad = sm_example.grad
"""
init_slots = {'debug': (False, 'True for debug output'),
'root': ('e', 'Root symbol for basis vectors'),
'name': (None, 'Name of submanifold'),
'norm': (False, 'Normalize basis if True'),
'ga': (None, 'Base Geometric Algebra')}

def __init__(self, *args, **kwargs):
# __u is to emulate a Python 3.8 positional-only argument, witha clearer
# spelling that `*args`.
def __init__(self, __u, __coords, *, ga, norm=False, name=None, root='e', debug=False):
"""
Parameters
----------
u :
The coordinate map defining the submanifold
which is a list of functions of coordinates of the base
manifold in terms of the coordinates of the submanifold.
for example if the manifold is a unit sphere then -
``u = [sin(u)*cos(v),sin(u)*sin(v),cos(u)]``.
Alternatively (``args[0]``) is a parametric vector function
of the basis vectors of the base manifold. The
coefficients of the bases are functions of the coordinates
(``args[1]``). In this case we would call the submanifold
a "vector" manifold and additional characteristics of the
manifold can be calculated since we have given an explicit
embedding of the manifold in the base manifold.
coords :
The coordinate list for the submanifold, for
example ``[u, v]``.
debug :
True for debug output
root : str
Root symbol for basis vectors
name : str
Name of submanifold
norm : bool
Normalize basis if True
ga :
Base Geometric Algebra
"""

#print '!!!Enter Sm!!!'

if printer.GaLatexPrinter.latex_flg:
printer.GaLatexPrinter.restore()
Ga.restore = True

kwargs = metric.test_init_slots(Sm.init_slots, **kwargs)
u = args[0] # Coordinate map or vector embedding to define submanifold
coords = args[1] # List of cordinates
ga = kwargs['ga'] # base geometric algebra
u = __u
coords = __coords
if ga is None:
raise ValueError('Base geometric algebra must be specified for submanifold.')

Expand All @@ -1958,7 +1970,6 @@ def __init__(self, *args, **kwargs):
n_sub = len(coords)

# Construct names of basis vectors
root = kwargs['root']
"""
basis_str = ''
for x in coords:
Expand Down Expand Up @@ -2011,9 +2022,6 @@ def __init__(self, *args, **kwargs):
s += dxdu[k][i] * dxdu[l][j] * g_base[k, l].subs(sub_pairs)
g[i, j] = trigsimp(s)

norm = kwargs['norm']
debug = kwargs['debug']

if Ga.restore: # restore printer to appropriate enhanced mode after sm is instantiated
printer.GaLatexPrinter.redirect()

Expand Down
22 changes: 13 additions & 9 deletions galgebra/lt.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ class Lt(object):
"""

mat_fmt = False
init_slots = {'ga': (None, 'Name of metric (geometric algebra)'),
'f': (False, 'True if Lt if function of coordinates.'),
'mode': ('g', 'g:general, s:symmetric, a:antisymmetric transformation.')}

@staticmethod
def setup(ga):
Expand All @@ -164,13 +161,20 @@ def format(mat_fmt=False):
Lt.mat_fmt = mat_fmt
return

def __init__(self, *args, **kwargs):
kwargs = metric.test_init_slots(Lt.init_slots, **kwargs)

def __init__(self, *args, ga, f=False, mode='g'):
"""
Parameters
----------
ga :
Name of metric (geometric algebra)
f : bool
True if Lt if function of coordinates
mode : str
g:general, s:symmetric, a:antisymmetric transformation
"""
mat_rep = args[0]
ga = kwargs['ga']
self.fct_flg = kwargs['f']
self.mode = kwargs['mode'] # General g, s, or a transformation
self.fct_flg = f
self.mode = mode
self.Ga = ga
self.coords = ga.lt_coords
self.X = ga.lt_x
Expand Down
56 changes: 35 additions & 21 deletions galgebra/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,6 @@ class Metric(object):

count = 1

init_slots = {'g': (None, 'metric tensor'),
'coords': (None, 'manifold/vector space coordinate list/tuple'),
'X': (None, 'vector manifold function'),
'norm': (False, 'True to normalize basis vectors'),
'debug': (False, 'True to print out debugging information'),
'gsym': (None, 'String s to use "det("+s+")" function in reciprocal basis'),
'sig': ('e', 'Signature of metric, default is (n,0) a Euclidean metric'),
'Isq': ('-', "Sign of square of pseudo-scalar, default is '-'"),
'wedge': (True, 'Use ^ symbol to print basis blades')}

@staticmethod
def dot_orthogonal(V1, V2, g=None):
"""
Expand Down Expand Up @@ -574,24 +564,48 @@ def signature(self):
raise ValueError(str(self.sig) + ' is not allowed value for self.sig')


def __init__(self, basis, **kwargs):

kwargs = test_init_slots(Metric.init_slots, **kwargs)
def __init__(self, basis, *,
g=None,
coords=None,
X=None,
norm=False,
debug=False,
gsym=None,
sig='e',
Isq='-'
):
"""
Parameters
----------
basis :
string specification
g :
metric tensor
coords :
manifold/vector space coordinate list/tuple (sympy symbols)
X :
vector manifold function
norm :
True to normalize basis vectors
debug :
True to print out debugging information
gsym :
String s to use ``"det("+s+")"`` function in reciprocal basis
sig :
Signature of metric, default is (n,0) a Euclidean metric
Isq :
Sign of square of pseudo-scalar, default is '-'
"""

self.name = 'GA' + str(Metric.count)
Metric.count += 1

if not isinstance(basis, str):
raise TypeError('"' + str(basis) + '" must be string')

X = kwargs['X'] # Vector manifold
g = kwargs['g'] # Explicit metric or base metric for vector manifold
debug = kwargs['debug']
coords = kwargs['coords'] # Manifold coordinates (sympy symbols)
norm = kwargs['norm'] # Normalize basis vectors
self.sig = kwargs['sig'] # Hint for metric signature
self.gsym = kwargs['gsym']
self.Isq = kwargs['Isq'] #: Sign of I**2, only needed if I**2 not a number
self.sig = sig # Hint for metric signature
self.gsym = gsym
self.Isq = Isq #: Sign of I**2, only needed if I**2 not a number

self.debug = debug
self.is_ortho = False # Is basis othogonal
Expand Down
46 changes: 23 additions & 23 deletions galgebra/mv.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def _make_odd(ga, __name, **kwargs):
_make_grade2 = _make_bivector
_make_even = _make_spinor

def __init__(self, *args, **kwargs):
def __init__(self, *args, ga, recp=None, coords=None, **kwargs):
"""
__init__(self, *args, ga, recp=None, **kwargs)
Expand Down Expand Up @@ -326,9 +326,8 @@ def __init__(self, *args, **kwargs):
used with multivector functions.
"""
kw = _KwargParser('__init__', kwargs)
self.Ga = kw.pop('ga')
self.recp = kw.pop('recp', None) # not used
kw.pop('coords', None) # ignored
self.Ga = ga
self.recp = recp # not used

self.char_Mv = False
self.i_grade = None # if pure grade mv, grade value
Expand Down Expand Up @@ -1551,10 +1550,8 @@ def __str__(self):
def __repr__(self):
return str(self)

def __init__(self, *args, **kwargs):
kwargs = metric.test_init_slots(Sdop.init_slots, **kwargs)

self.Ga = kwargs['ga'] # Associated geometric algebra (coords)
def __init__(self, *args, ga):
self.Ga = ga # Associated geometric algebra (coords)

if self.Ga is None:
raise ValueError('In Sdop.__init__ self.Ga must be defined.')
Expand Down Expand Up @@ -1685,7 +1682,7 @@ def __eq__(self,A):
return True
return False

def __init__(self, __arg, **kwargs):
def __init__(self, __arg, *, ga):
"""
The partial differential operator is a partial derivative with
respect to a set of real symbols (variables). The allowed
Expand All @@ -1696,9 +1693,7 @@ def __init__(self, __arg, **kwargs):
"""

kwargs = metric.test_init_slots(Pdop.init_slots, **kwargs)

self.Ga = kwargs['ga'] # Associated geometric algebra
self.Ga = ga # Associated geometric algebra

if self.Ga is None:
raise ValueError('In Pdop.__init__ self.Ga must be defined.')
Expand Down Expand Up @@ -1899,22 +1894,27 @@ class Dop(object):
terms : list of tuples
"""

init_slots = {'ga': (None, 'Associated geometric algebra'),
'cmpflg': (False, 'Complement flag for Dop'),
'debug': (False, 'True to print out debugging information'),
'fmt_dop': (1, '1 for normal dop partial derivative formating')}

def __init__(self, *args, **kwargs):

kwargs = metric.test_init_slots(Dop.init_slots, **kwargs)
def __init__(self, *args, ga, cmpflg=False, debug=False, fmt_dop=1):
"""
Parameters
----------
ga :
Associated geometric algebra
cmpflg : bool
Complement flag for Dop
debug : bool
True to print out debugging information
fmt_dop :
1 for normal dop partial derivative formatting
"""

self.cmpflg = kwargs['cmpflg'] # Complement flag (default False)
self.Ga = kwargs['ga']
self.cmpflg = cmpflg
self.Ga = ga

if self.Ga is None:
raise ValueError('In Dop.__init__ self.Ga must be defined.')

self.dop_fmt = kwargs['fmt_dop'] # Partial derivative output format (default 1)
self.dop_fmt = fmt_dop
self.title = None

if len(args) == 2:
Expand Down
7 changes: 1 addition & 6 deletions galgebra/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def coef_simplify(expr):
return expr


def oprint(*args, **kwargs):
def oprint(*args, dict_mode=False):
"""
Debug printing for iterated (list/tuple/dict/set) objects. args is
of form (title1,object1,title2,object2,...) and prints:
Expand All @@ -210,11 +210,6 @@ def oprint(*args, **kwargs):
If you only wish to print a title set object = None.
"""

if 'dict_mode' in kwargs:
dict_mode = kwargs['dict_mode']
else:
dict_mode = False

if isinstance(args[0], str) or args[0] is None:
titles = list(islice(args, None, None, 2))
objs = tuple(islice(args, 1, None, 2))
Expand Down

0 comments on commit d96d65c

Please sign in to comment.