Maxima: convert matrix to list
Asked Answered
U

2

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 ?

Uruguay answered 31/12, 2011 at 15:23 Comment(0)
K
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]]
Kinesiology answered 26/9, 2012 at 6:50 Comment(2)
@Grzegorz Can you move the checkmark to Robert's answer, as it seems to be the more reasonable way of doing things.Burnedout
@Burnedout 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!Uruguay
B
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.

Burnedout answered 7/1, 2012 at 23:12 Comment(1)
Thank you @Burnedout . 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.Uruguay

© 2022 - 2024 — McMap. All rights reserved.