From: jhmeyers@miu.edu (John H Meyers)
Subject: Revisiting the APLY program - all new, fast, no bugs
Date: 21 Jul 1996 00:00:00 GMT
Message-ID: <4stio9$dr3@news.iastate.edu>
organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
reply-to: jhmeyers@miu.edu
newsgroups: comp.sys.hp48


It was probably noticed several years ago, before I came here,
that HP's APLY program (which applies a function to all the
individual elements of an array) has had bugs, both in ROM
(in the G/GX TEACH function) and as re-printed in the AUR.

In my level M calculator, the TEACH version of APLY would never work on a
vector and then on a matrix, because it relies on setting a flag to remember
that the input is a vector, but it forgets to first make sure the flag
is not already set to begin with, and it leaves it on at the end
(if your version does not have "1 CF" very near the beginning,
then it still has this bug).

In my AUR Edition 1, these errors have been corrected on pages 2-30,31
by the addition of "1 CF" near both the beginning and end, and also by
replacing the erroneous FS?C in the middle of the program with FS?
(there is a subsequent repeat test of the same flag in the IFERR
structure, and it would not have been good to have already reset the flag).

All the same, there is still a bug in my AUR version; if the program
applied to the array elements creates any symbolic elements, the
IFERR trap in APLY relies on automatically recovering the LASTARG's
of the \->ARRY function when it fails -- but without ever making sure
that flag -55 has been cleared!  It is the original default for flag -55
to be clear, but Murphy's law says that it doesn't always have to remain so.

All these "flag bugs" are just the thing to watch out for in User RPL
programs (is this any hint re the current programming contest?).

Evidently there were intentions to add this to AUR, because the byte
count and checksum in AUR do not agree with the listing (but it would
be hard to squeeze -55 CF into just 5 more bytes, wouldn't it?).

Anyway, that version of APLY is not very structured, is on the verge
of collapse due to high-flying flag reliance, and uses weird gimmicks
to turn vectors into a matrix and then back again (which is what the
flag setting was for).

In case you actually use APLY and want it to always work, here is
an improved version below; it is for G/GX only, unlike the original,
and contains some possibly useful sub-programs.

May you always apply yourself with perfect success to every task in life!

With best wishes from:  John H Meyers  ( jhmeyers@mum.edu )

-------------------------------------------------------------

Replacement for HP48G/GX "APLY" program for applying
an object ( <<program>> ) to each element of an array.

No flag-setting bugs, no "last arg disabled" bug;
shorter program ( 260.5 bytes, checksum #731h = #1841d )
demonstrates power & simplicity of object-oriented operations.

The array -> list and list -> array functions within this program
may also be of use elsewhere.

This looks neater in fixed-with font!

--------------------------------------------------------------

%%HP: T(3)A(D)F(.); @ Kermit modes (note: translation mode 3)

\<<             @ begin main program

  SWAP          @ move object to level 2, array to level 1

  @ This function may have general use:
  @ [ vector ] -> { value ... } ; [[ matrix ]] -> { { row } ... }
  \->ROW        @ decompose array into rows, or vector into values
  \->LIST       @ list of row vectors, or list of vector values
  1 \<<         @ prepare for DOLIST on outer list
        DUP TYPE 1 >              @ if not a scalar (real or complex)
        { OBJ\-> EVAL \->LIST }   @ then row vector -> inner list
        IFT                       @ else leave scalar as-is
    \>> DOLIST  @ apply above program fragment to outer list

  SWAP          @ return object to level 1, list to level 2

  @ This function may have general use:
  @ Apply an object to each individual element of (possibly nested) list:
  \-> q \<< 1   @ save object in local variable, prepare for DOLIST
            \<< IF DUP TYPE 5 ==  @ if matrix list contains row lists
                THEN 1 q DOLIST   @ then apply object to each row list
                ELSE q EVAL       @ else apply object to vector values
                END
            \>> DOLIST  @ apply above program fragment to outer list
        \>>     @ end of local variable structure

  DEPTH \-> d   @ save stack depth, prepare for error trap
  \<< DUP       @ keep extra copy of { { row } ... } or { value ... }
    IFERR       @ any symbolic results will cause error trap

      @ This function may have general use:
      @ { { row } ... } -> [[ matrix ]] ; { value ... } -> [ vector ]
      1 \<<     @ prepare for DOLIST on outer list
          DUP TYPE 5 == { OBJ\-> \->ARRY } IFT  @ inner list -> row vector
        \>> DOLIST
      OBJ\->    @ list of row vectors, or list of vector values
      ROW\->    @ assemble into final matrix or vector result

    THEN DEPTH d - DROPN  @ invalid array element(s), clean up stack
    ELSE SWAP DROP @ return { { row } ... } or { value ... } instead
    END         @ normal return is [[ matrix ]] or [ vector ]
  \>>  @ end of local variable structure

\>>             @ end of main program

