Maxima: convert matrix to list

大兔子大兔子 提交于 2019-12-22 03:58:45

问题


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 ?


回答1:


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]]



回答2:


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.



来源:https://stackoverflow.com/questions/8688906/maxima-convert-matrix-to-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!