Post Reply 
Happy New Year 2024 ... and 2023's last teaser !
01-05-2024, 10:11 AM (This post was last modified: 01-05-2024 11:47 AM by Gjermund Skailand.)
Post: #28
RE: Happy New Year 2024 ... and 2023's last teaser !
Update/Small speed improvement
Here is a sys-RPL implementation of Valentin's code for a general matrix computation of permanent :
Compiles to 255.5 bytes, chksum #917Dh
On my HP50g it solves the 12x12 matrix in slightly less than 13,5 minutes.

calculations can be aborted by pushing ON/cancel
Code:

:: 
  CH1NOLASTWD
  CK&DISPATCH2
  #4
  ::
    FPTR2 ^CKNUMARRY                          ( Ensure numeric array )
    DUP FPTR2 ^MDIMS ?SKIP #0             ( 2 dimensions square matrix )
    OVER #<>case FPTR2 ^ErrBadDim      ( give error message if not )
    #0 #0 %0 %0 %0 %0
    {{ P B S T IN K N }}                          ( declare local variables )
    #1 SWAP                                          ( stack: Matrix, bit_index )
    %2 N UNCOERCE %^ COERCE           ( calculate 2^N)
    ONE_DO                                           ( for k )
      INDEX@ !K                                      ( make k easily accessible as K )
      %1 !P
      #0 !IN                                             
      N #1+_ONE_DO                                ( for I=1 to n )
        %0 !S
        TRUE !B
        #1 ROTDROPSWAP                            ( update bit_index )
        n #1+_ONE_DO                                (for j=1 to n )
          K 3PICK #AND #0<> IT                   ( if k AND j )
          :: 
            IN INDEX@ #+ PULLREALEL          ( A[i,j] )
            S %+ !S                                       ( s = s + A[i,j ] )
            B NOT !B                                    ( b = -b )
          ;
          SWAP #2* SWAP                              (update bit index used in the AND clause)
        LOOP                                               ( next j)
        P S %* !P                                          ( p = p*s)
        IN N #+ !IN                                       ( update i*n )
      LOOP                                                  ( next i )
      P B ?SKIP %CHS  T  %+ !T                      ( t = p*b + t )
      ?ATTNQUIT                                            ( abort if ON/cancel is pressed)
    LOOP
    2DROP
    T
    N #1 #AND #1= IT %CHS                      (if n AND 1 change sign )
    ABND
  ;
;
@
Updated formatting
best regards
Gjermund
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Happy New Year 2024 ... and 2023's last teaser ! - Gjermund Skailand - 01-05-2024 10:11 AM



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