Post Reply 
List transpose using built-in?
10-28-2018, 11:46 PM (This post was last modified: 10-28-2018 11:50 PM by StephenG1CMZ.)
Post: #1
List transpose using built-in?
I have just added a list transpose implementation to my List API
Code:

 //TRY WITH ({{1,2,3},{4,5,6}})
EXPORT ListTRANSPOSE(LST)
//LIST TRANSPOSE
//WITH A 2D LST TRN(TRN(LST)) RETURNS ORIGINAL LIST
//A 1D LIST HAS NO PROPER TRANSPOSE AND MIGHT NOT RETURN USEFUL LST
//NOTE This is similar to the obvious builtin
//PRINT(mat2list(transpose(list2mat({{1,2,3},{4,5,6}}))));
//But the builtin unexpectedly flattens too 
BEGIN
LOCAL II,JJ;
LOCAL OUT:={};

FOR II FROM 1 TO SIZE(LST) DO
IF TYPE(LST(II))==TYPE({}) THEN //2D
FOR JJ FROM 1 TO SIZE(LST(II)) DO
OUT(JJ,II):=LST(II,JJ);
END;//FOR
ELSE 
//GUARD FLAT LIST//WHATS GOOD HERE? AN ERR MAY BE BETTER
//NEITHER OF THESE ARE IDEAL IN ALL CASES
OUT(II):=LST(II); 
//OUT(II):={LST(II)};
END;//IF
END;//FOR
 PRINT(mat2list(transpose(list2mat(LST)))); //why are these different?
 PRINT(OUT); //and which is correct?
RETURN OUT;
END;
But I was expecting the built-in sequence mat2list(transpose(list2mat)) to give the same results.
It is similar, but flatter than my implementation.
Am I wrong?

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
10-29-2018, 01:19 AM
Post: #2
RE: List transpose using built-in?
(10-28-2018 11:46 PM)StephenG1CMZ Wrote:  I was expecting the built-in sequence mat2list(transpose(list2mat)) to give the same results.
It is similar, but flatter than my implementation.

I think matrix is already a 2D list (list of list).
a := [[1, 2, 3], [4,5,6]]
a[0] ==> [1, 2, 3]

mat2list(...) just return coefficient of the matrix, thus flattened it.
mat2list(a) ==> [1, 2, 3, 4, 5, 6]

The name is misleading. mat2list is more like mat2vec.
Find all posts by this user
Quote this message in a reply
10-31-2018, 12:59 PM
Post: #3
RE: List transpose using built-in?
(10-28-2018 11:46 PM)StephenG1CMZ Wrote:  I was expecting the built-in sequence mat2list(transpose(list2mat)) to give the same results.
It is similar, but flatter than my implementation.

Here is another way to transpose a list of lists using built-in commands:

MAKELIST(mat2list(col(LST,I)),I,1,colDim(LST))

it should be faster than the double FOR loop.


Note: it seems, at least with the latest Prime beta firmware, that the column index start value is different between the Virtual Prime and the real Prime/Android Prime. On the Windows Virtual Prime, you would need to do: MAKELIST(mat2list(col(LST,I)),I,0,colDim(LST)-1)
I've reported this to calcbeta.
Find all posts by this user
Quote this message in a reply
10-31-2018, 01:04 PM (This post was last modified: 10-31-2018 01:15 PM by Tim Wessman.)
Post: #4
RE: List transpose using built-in?
Were you playing around with unsupported, non-documented, please don't use, CAS python syntax at all? That changes the index for CAS commands system wide... :/ (not a bug but by "design")

TW

Although I work for HP, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
10-31-2018, 01:38 PM (This post was last modified: 10-31-2018 01:58 PM by Didier Lachieze.)
Post: #5
RE: List transpose using built-in?
Well, I agree that col(list, integer) may be extending a bit the definition of col(matrix, integer), but using a matrix instead of a list gives the same results:
  • col([[1,2,3],[4,5,6]],1) returns [2 5] on the Windows Virtual calc
  • col([[1,2,3],[4,5,6]],1) returns [1 4] on the the physical Prime and the Android app
both in Home and in CAS mode with the latest beta version.

EDIT: interestingly I restarted the CAS with 'restart' and now I get the expected [1 4] result on the Windows Virtual calc. However I didn't intentionally tried the Python syntax as far as I remember, or not in the recent past. Anyway, this is quite dangerous to have this kind of system wide setting being silently changed and being persistent with no user warning.
Find all posts by this user
Quote this message in a reply
10-31-2018, 01:55 PM (This post was last modified: 10-31-2018 02:03 PM by compsystems.)
Post: #6
RE: List transpose using built-in?
I think it has to do with the internal variable, variable index:=1 or index:=0

Xcas mode
index:=0; col([[1,2,3],[4,5,6]],1) returns [2 5]
index:=1; col([[1,2,3],[4,5,6]],1) returns [1 4]

other variables that are not documented or do not appear in the catalog.
http://www.hpmuseum.org/forum/thread-11554.html
Find all posts by this user
Quote this message in a reply
11-01-2018, 08:08 AM (This post was last modified: 11-01-2018 08:12 AM by StephenG1CMZ.)
Post: #7
RE: List transpose using built-in?
Thanks everyone.
One advantage of simple nested FOR loops...
They may be slower, but the code is more portable as your indexing is under your control.

I did use never-use-Python in my Savage Benchmark program, but not in List.

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)