3

SourceForge is currently down, so I can't access Maxima's documentation and came here with this question. I'm building a question in Moodle that would benefit from this container type. I've tried

optionmap : {"key1" : value1, "key2" : value2};
minmaxlist : [[max(resurssiMap["key1"],resurssiMap["key2"]),true,<key of maximum value>],
[min(resurssiMap["key1"],resurssiMap["key2"]),false,<key of minimum value>]];

but this produces the error assignment: cannot assign to key1.

Is there a way to fix this. In the code sample above, I would like to insert the keys of the maximum and minimum values (which I won't know until the execution of the max()- and min()-functions) the their respective places as indicated, which I don't know how to do either.

Any ideas on how to achieve this?

sesodesa
  • 1,473
  • 2
  • 15
  • 24

1 Answers1

3

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.

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48