12

I convert list to matrix in Maxima in following way:

DataL : [ [1,2], [2,4], [3,6], [4,8] ];
DataM: apply('matrix,DataL);

How to do it the other way ? How to convert given matrix DataM into list DataL ?

Grzegorz Wierzowiecki
  • 10,545
  • 9
  • 50
  • 88

2 Answers2

17

I know it's late in the game, but for what it's worth, there is a simpler way.

my_matrix : matrix ([a, b, c], [d, e, f]);
my_list : args (my_matrix);
 => [[a, b, c], [d, e, f]]
Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • 1
    @Grzegorz Can you move the checkmark to Robert's answer, as it seems to be the more reasonable way of doing things. – Simon Sep 27 '13 at 10:00
  • 1
    @Simon Fair point - Just did. However, I can't recall why, because I was writing longer Maxima script, in my case your solution worked better or was more convenient. Anyway - tick moved. Thanks both of you for help, Cheers! – Grzegorz Wierzowiecki Oct 21 '13 at 18:26
7

I'm far from a Maxima expert, but since you asked me to look at this question, here's what I have after a quick look through the documentation.

First, looking at the documentation on matrices yielded only one way of turning matrices in to lists, which is list_matrix_entries. However, this returns a flat list of the entries. To get a nested list structure, something like the following works

DataL : [[1, 2], [2, 4], [3, 6], [4, 8]];  /* Using your example list */
DataM : apply('matrix, DataL);              /* and matrix             */

DataML : makelist(list_matrix_entries(row(DataM, i)), i, 1, 4);
is(DataML = DataL);   /*  true  */

This is clumsy and probably inefficient. Using the underlying Lisp structure in Maxima (and analogy to Mathematica, which I'm more familiar with) you can examine the heads of DataL and DataM using part:

part(DataL, 0);  /*  [       */
part(DataM, 0);  /*  matrix  */

Then to convert between the two structures, you can use substpart

is(substpart(matrix, DataL, 0) = DataM);   /*  true  */
is(substpart( "[",   DataM, 0) = DataL);   /*  true  */

Using substpart at level 0 is almost the same as using apply, except it works on more than just lists.

Community
  • 1
  • 1
Simon
  • 14,631
  • 4
  • 41
  • 101
  • Thank you @Simon . Your solution works great. I've been walking forward and backward documentation with no success. As I use least squares method and few others operating on different types (some restricts to matrix, while others operates on lists) two way conversion is big help. – Grzegorz Wierzowiecki Jan 08 '12 at 11:39