4

I want to use (make-array '(4 3 8)) in maxima which is basically to generate multi-d matrix as I am not able to find API to create multi-d matrices including with array(name,d1,d2...dm).

I can execute it using :lisp (make-array '(4 3 8)) but I don't know how I can label it as something like,

arr: :lisp(make-array '(4 3 8))

I also want to know if it is possible to use lisp code inside maxima functions. Any sort of help shall be highly regarded.

Pankaj Sejwal
  • 1,605
  • 1
  • 18
  • 27

2 Answers2

5

To create a named array in Lisp code just exactly the same as array(name, d1, d2, ..., dm), write:

(mfuncall '$array name d1 d2 ... dm)

You can't include Lisp code directly in Maxima functions. But you can call Lisp functions. If the lisp function is named $foo, then in Maxima it's foo; if in Lisp it's foo, then in Maxima it's ?foo. E.g.:

:lisp (defun $foo (x) ...)

f(x) := print (foo (x));

By the way, Maxima's treatment of arrays is still a mess ... maybe someday we'll clean it up.

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

You can use make_array to create arrays directly:

(%i18) make_array(fixnum,4,3,8);
(%o18) {Array:  #3A(((0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0))
    ((0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0))
    ((0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0))
    ((0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0)))}

Or bind results of Lisp invocations like this:

(%i21) :lisp (msetq $foo (make-array '(4 3 8)));

#3A(((NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL))
    ((NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL))
    ((NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL))
    ((NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL)))
(%i21) foo;
(%o21) {Array:  #3A(((NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL))
    ((NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL))
    ((NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL))
    ((NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL)
     (NIL NIL NIL NIL NIL NIL NIL NIL)))}

By the way, array probably worked for you, too. I have never used it and was confused at first, since it is not printed after creation. But after checking the documentation and a Wikibooks article:

(%i22) array(A,2,2,2);
(%o22)                                 A
(%i23) arrayinfo(A);
(%o23)                     [declared, 3, [2, 2, 2]]
(%i24) A[0,1,2]: 2;
(%o24)                                 2
(%i25) listarray(A);
(%o25) [#####, #####, #####, #####, #####, 2, #####, #####, #####, #####, 
#####, #####, #####, #####, #####, #####, #####, #####, #####, #####, #####, 
#####, #####, #####, #####, #####, #####]

There seem to be quite a few options for this kind of thing in Maxima, or, as the above linked Wikibooks article quotes Robert Dodier: "Maxima's current array/matrix semantics are a mess […]"

danlei
  • 14,121
  • 5
  • 58
  • 82
  • I am getting output as `Lisp array [4,3,8]`..do I need to enable some option ? – Pankaj Sejwal Mar 04 '14 at 15:51
  • No, this works for me out of the box in Maxima 5.24.0, but maybe it changed in a more recent version. By the way I expanded the answer to include `array` (which also doesn't print the resulting array after creation by default) and provided some links. Maybe that will help you further. – danlei Mar 04 '14 at 16:01
  • The side way to see it is by inducing some error in the expression that shows the output, for ex-`arrayinfo(t);`=> `Lisp array [2,3,4]` than pass some incomplete indexs and it throws error, example - `t[1][2];` => ` Error in PROGN [or a callee]: #3A(((0 0 0 0) (0 0 0 0) (0 0 0 0))((0 0 0 0) (0 0 0 0) (0 0 0 0))) has wrong rank` – Pankaj Sejwal Mar 05 '14 at 11:23