Maxima has two kinds of hash tables. One is a hash table which is attached to a symbol as a property, and the other is attached to a symbol as a value. (This is similar to the distinction between named functions, which are attached as properties, and lambda expressions, which are values.)
The syntax to retrieve a value from a hash table is a[x]
where a
is the symbol to which the hash table is attached and x
is the key. The syntax to store a value in a hash table is a[x] : y
where y
is the value to be stored. There is no literal hash table construct; the only way to store multiple values is to repeatedly do a[x] : y
for different x
and y
. Of course one could devise a function to loop over lists of keys and values and store them one by one.
Hash tables are symbol properties by default; you can enable hash tables as values by setting use_fast_arrays:true
. (The flag is very confusingly named, I agree.) A hash table is created the first time a value is stored to it. E.g.
footable[mykey] : myvalue;
creates a hash table attached to the symbol footable
either as a property or a value (depending on use_fast_arrays
).
Keys can be any Maxima expression. There can be multiple keys.
Here is a little example.
(%i2) footable[x + 1, x - 1] : "v1" $
(%i3) footable["hmm", 12345] : 1 - a*b $
(%i4) footable[[1,2,3], sin(1)] : cos(2) $
(%i5) footable;
(%o5) footable
(%i6) footable[[1,2,3], sin(1)];
(%o6) cos(2)
(%i7) footable[1,2];
(%o7) footable
1, 2
(%i8) kill(footable) $
(%i9) use_fast_arrays : true $
(%i10) footable[x + 1, x - 1] : "v1" $
(%i11) footable["hmm", 12345] : 1 - a*b $
(%i12) footable[[1,2,3], sin(1)] : cos(2) $
(%i13) footable;
(%o13) {Lisp Array:
#S(HASH-TABLE TEST FASTHASH-EQUAL ((((MLIST SIMP) 1 2 3) ((%SIN \
SIMP) 1)) . ((%COS SIMP) 2))
((hmm 12345) . ((MPLUS SIMP) 1 ((MTIMES SIMP) -1 $A $B))) (((\
(MPLUS SIMP) 1 $X) ((MPLUS SIMP) -1 $X)) . v1))}
(%i14) footable[[1,2,3], sin(1)];
(%o14) cos(2)
(%i15) footable[1,2];
(%o15) false
Note that value hash tables return false
for keys which are not in the table, while a property hash table returns a subscript expression. Note also that the value of a value hash table is a Lisp hash table object, while the value of a property hash table (i.e. %o5
) is just the symbol itself.
See also arrayinfo
and listarray
to get a list of keys or values, respectively.
I can't tell what you want to achieve with the example you showed; maybe you can explain a little more.