On 2012-07-26, Rupert Swarbrick <rswarbrick at gmail.com> wrote:
> One option would be to stick it in SG itself, in the CAR say, so it
> doesn't get printed.
I don't think there's any guarantee that car flags are preserved, so I
don't recommend that. Also you can't really work with car flags in
Maxima.
> If I can't depend on this behaviour, another option would be storing
> some dictionary keyed by generator and relation lists with EQUALP.
My advice is to attach the cache to the operator. Then the code which
fetches or creates cache values can inspect the operator of an input
expression to find the cache. How about the following. (Code for
make_hash is shown in the PS.)
maybe_hack (x) := block ([a],
a : ensure_cache (x),
if has_value (a, x)
then a [x]
/* a[x] : really_hack(x) assigns to array named 'a ... sigh. */
else arraysetapply (a, [x], really_hack (x)));
ensure_cache (x) := block ([a],
a : get (op (x), 'cache),
if a = false
then (a : gensym (), make_hash (a, 1), put (op (x), a, 'cache)),
a);
/* maybe there's a better way to do this, I dunno */
has_value (a, x) := block ([ax : a [x]], atom (ax) or op (ax) # a);
/* some complicated stuff goes here */
really_hack (x) := random (1000);
With that stuff, here's an example session:
(%i3) trace (make_hash, really_hack);
(%o3) [make_hash, really_hack]
(%i4) maybe_hack (foo (a, b));
1 Enter make_hash [g15973, 1]
1 Exit make_hash g15973
1 Enter really_hack [foo(a, b)]
1 Exit really_hack 612
(%o4) 612
(%i5) maybe_hack (foo (x, y));
1 Enter really_hack [foo(x, y)]
1 Exit really_hack 302
(%o5) 302
(%i6) maybe_hack (foo (a, b));
(%o6) 612
(%i7) maybe_hack (bar (1, 2));
1 Enter make_hash [g15975, 1]
1 Exit make_hash g15975
1 Enter really_hack [bar(1, 2)]
1 Exit really_hack 734
(%o7) 734
> if someone keeps changing the semigroups that
> they work with, it'll "leak memory" since I don't have any way of
> discarding the rewriting systems that can't be used any more.
I wouldn't worry about it. In order for you to have a problem with that,
first you have to get it working ....
best
Robert Dodier
PS.
(defun $make_hash (array-symbol n-dims)
(let ((array-gensym (gensym)))
(mputprop array-symbol array-gensym 'hashar)
(setf (symbol-array array-gensym) (make-array 7 :initial-element nil))
(setf (aref (symbol-array array-gensym) 0) 4)
(setf (aref (symbol-array array-gensym) 1) 0)
(setf (aref (symbol-array array-gensym) 2) n-dims)
array-symbol))