#14167: UniqueRepresentation using default arguments
----------------------------------------------------------+-----------------
Reporter: SimonKing | Owner:
jason
Type: enhancement | Status:
needs_info
Priority: major | Milestone:
sage-5.8
Component: misc | Resolution:
Keywords: UniqueRepresentation default arguments | Work issues:
Report Upstream: N/A | Reviewers:
Authors: Simon King | Merged in:
Dependencies: #14054 | Stopgaps:
----------------------------------------------------------+-----------------
Changes (by SimonKing):
* status: needs_review => needs_info
Comment:
OTOH, I think that my original approach is too intrusive.
New ideas, where C is again a subclass of `CachedRepresentation`:
__What we need__
1. Given `C.__init__`, we need a way to "normalise" given arguments
`(*args,**kwds)` by taking into account the defaults of `C.__init__`. The
result should be a pair of tuples `(Args,Kwds)` such that Args provides
all positional arguments available for `C.__init__` (including those
provided by name in `(*args,**kwds)`, and Kwds provides the remaining
named arguments, sorted by name.
2. The normalised arguments should then be used as cache keys for
`type.__call__(C,*args,**kwds)`, in a weak value dictionary.
__How to achieve it__
Normalizing arguments is the job of
`sage.misc.function_mangling.ArgumentFixer.fix_to_pos()`. I suggest to
define `CachedRepresentation` not by applying the `@weak_cached_function`
decorator, but to define it in `sage.misc.function_mangling`, which is in
Cython and may thus be faster anyway.
There should be a dictionary `arg_fix_dict` of `ArgumentFixer`s, such that
`arg_fix_dict[C.__init__]` returns an `ArgumentFixer` for the default
arguments of `C.__init__`. This is then used to process the arguments
similar to what is done in `sage.misc.cachefunc.CachedFunction.__call__`
using a weak value dictionary as cache, and use `typecall` to create new
instance of C if it can't be found in the cache.
__Advantages__
I think this approach should be less intrusive, more memory friendly
(because only one `ArgumentFixer`s are created for each `C.__init__`,
rather than one `WeakCacheFunction`, including an `ArgumentFixer`, for
each C), and faster (because we have `CachedRepresentation.__classcall__`
defined in Cython).
I'll try to implement the second approach, and then we'll see what is
better.
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/14167#comment:2>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.