From d96d65cbbf284aab7d8d03eed4392278ee6dae9d Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Thu, 2 Jan 2020 16:44:54 +0000 Subject: [PATCH] Use keyword-only arguments where possible --- galgebra/ga.py | 90 ++++++++++++++++++++++++--------------------- galgebra/lt.py | 22 ++++++----- galgebra/metric.py | 56 +++++++++++++++++----------- galgebra/mv.py | 46 +++++++++++------------ galgebra/printer.py | 7 +--- 5 files changed, 121 insertions(+), 100 deletions(-) diff --git a/galgebra/ga.py b/galgebra/ga.py index 50cdf8a3..1b76de0f 100644 --- a/galgebra/ga.py +++ b/galgebra/ga.py @@ -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() @@ -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):: @@ -1932,13 +1919,40 @@ 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!!!' @@ -1946,10 +1960,8 @@ def __init__(self, *args, **kwargs): 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.') @@ -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: @@ -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() diff --git a/galgebra/lt.py b/galgebra/lt.py index addd89d4..a2a88cfe 100644 --- a/galgebra/lt.py +++ b/galgebra/lt.py @@ -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): @@ -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 diff --git a/galgebra/metric.py b/galgebra/metric.py index 1f73b04a..ad5667ed 100644 --- a/galgebra/metric.py +++ b/galgebra/metric.py @@ -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): """ @@ -574,9 +564,38 @@ 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 @@ -584,14 +603,9 @@ def __init__(self, basis, **kwargs): 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 diff --git a/galgebra/mv.py b/galgebra/mv.py index 0fca1161..2363ed6d 100644 --- a/galgebra/mv.py +++ b/galgebra/mv.py @@ -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) @@ -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 @@ -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.') @@ -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 @@ -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.') @@ -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: diff --git a/galgebra/printer.py b/galgebra/printer.py index 3f4f094c..4bd9c765 100644 --- a/galgebra/printer.py +++ b/galgebra/printer.py @@ -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: @@ -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))