Post Reply 
(48g/49/50) Matrix Permanent
01-07-2024, 08:27 PM (This post was last modified: 01-24-2024 03:23 PM by John Keith.)
Post: #5
RE: (48g/49/50) Matrix Permanent
A new and much improved program for the HP 49 and 50 using Glynn's formula. This program is based on Albert Chan's optimization of a program by GitHub user "lesshaste". See discussion in this thread. Thanks also to Valentin Albillo, whose recent thread inspired the current flurry of activity on matrix permanents.

The program should be used in Exact mode for larger matrices to avoid overflow.

Code:

\<< DUP 2 *                            @ Multiply matrix by 2
  AXL DUP SIZE 1.                      @ Convert matrix to nested list
  IF >                                 @ Size > 1?
  THEN SWAP TRN AXL                    @ List of columns of original matrix
  :: \GSLIST DOSUBS                    @ Column sums
  DUP \PILIST UNROT                    @ Product of column sums
  DUP SIZE 2. OVER 1. - ^ \-> n w      @ n = size, w = 2^(n-1)
    \<< 1. w 1. -                      @ 2^(n-1) - 1 iterations
      FOR k 1. k                       @ Row index = 1
        WHILE DUP 2. MOD NOT           @ While even
        REPEAT 2. / SWAP 1. + SWAP     @ Divide by 2 and increment index
        END 4. PICK ROT GET            @ Get matrix row
        SWAP 4. MOD 3. SAME            @ Odd part MOD 4 = 3?
        :: ADD :: - IFTE               @ Add or subtract sums
        DUP \PILIST 4. ROLL - UNROT    @ Update product
      NEXT DROP2                       @ Drop lists
      NEG w R\->I /                    @ Return negative product / w
    \>>
  ELSE DROP 1. GET                     @ Else drop list, get single element
  END
\>>

Updated 2024/01/08 to clean up code.
Newer version 2024/01/10, a bit smaller and simpler, now 256 bytes.
Latest version 2024/01/24, slightly larger but 33% faster.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(48g/49/50) Matrix Permanent - John Keith - 04-28-2023, 06:02 PM
RE: (48g/49/50) Matrix Permanent - John Keith - 01-07-2024 08:27 PM



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