HP Forums

Full Version: (49G) Permutation List 2 Permutation Matrix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
 ;
Reference URL's