HP Forums
(49G) Permutation List 2 Permutation Matrix - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (49G) Permutation List 2 Permutation Matrix (/thread-4022.html)



(49G) Permutation List 2 Permutation Matrix - Gerald H - 05-30-2015 07:04 AM

Given a permutation list as input, eg

{ 4 3 1 2 6 5 }

the programme returns the corresponding permutation matrix, in this case

[[ 0 0 0 1 0 0 ]
[ 0 0 1 0 0 0 ]
[ 1 0 0 0 0 0 ]
[ 0 1 0 0 0 0 ]
[ 0 0 0 0 0 1 ]
[ 0 0 0 0 1 0 ]].

As a check, applying the matrix to

[[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]]

returns

[[ 4 ]
[ 3 ]
[ 1 ]
[ 2 ]
[ 6 ]
[ 5 ]].

Code:
::
  CK1
  FPTR 7 17B
  DUPLENCOMP
  DUP'
  FPTR2 ^COERCE2Z
  4ROLL
  2Ob>Seco
  '
  ::
    ROT
    NTHCOMPDROP
    FPTR2 ^Z2BIN
    #=ITE
    Z1_
    Z0_
  ;
  &COMP
  FPTR2 ^MAKE2DMATRIX
 ;