HP Forums
List transpose using built-in? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: List transpose using built-in? (/thread-11691.html)



List transpose using built-in? - StephenG1CMZ - 10-28-2018 11:46 PM

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?


RE: List transpose using built-in? - Albert Chan - 10-29-2018 01:19 AM

(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.


RE: List transpose using built-in? - Didier Lachieze - 10-31-2018 12:59 PM

(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.


RE: List transpose using built-in? - Tim Wessman - 10-31-2018 01:04 PM

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")


RE: List transpose using built-in? - Didier Lachieze - 10-31-2018 01:38 PM

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.


RE: List transpose using built-in? - compsystems - 10-31-2018 01:55 PM

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


RE: List transpose using built-in? - StephenG1CMZ - 11-01-2018 08:08 AM

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.