(50g) Numeric List Commands
01-16-2019, 11:24 PM (This post was last modified: 01-16-2019 11:28 PM by John Keith.)
Post: #1
 John Keith Senior Member Posts: 416 Joined: Dec 2013
(50g) Numeric List Commands
The following are RPL implementations of some commands from the Wolfram Language (aka Mathematica) that I find useful. Both programs require the ListExt Library.

The first command generates a list of integers based on the linear recurrence of the integer sequence. It is similar to the Wolfram Language command LinearRecurrence except that the order of the lists is reversed.

Level 3 should contain a list of the initial terms of the sequence.
Level 2 should contain a list of coefficients.
Level 1 should contain an integer representing the number of terms to be generated.

The list on level 2 cannot be longer than the list on level 3.

Code:
 \<< I\->R SWAP REV DUP SIZE \-> n c s   \<< OBJ\-> 1. + n     START s DUPN s \->LIST c * LSUM     NEXT n \->LIST   \>> \>>

For example, if level 3 contains the list { 0 1 }, level 2 contains the list { 1 1 }, and level 1 the number 10, the program will return the first 10 Fibonacci numbers.

While not as fast as an efficient program for computing e.g. Fibonacci numbers, this program makes it easy to explore a wide variety of integer sequences. Many examples can be found here.

The next program performs convolution of lists. It is similar to the Wolfram Language command ListConvolve but again the order of the lists is reversed. The input list should be on level 2, and the kernel on level one. Convolution in this sense is related to the dot product of vectors except that the two lists do not have to be the same length.

Code:
 \<< REV DUP SIZE \-> s   \<< SWAP s     \<< s \->LIST     \>> DOSUBS DUP SIZE UNROT LXIL SWAP ROT LMRPT * s LSDIV     \<< \GSLIST     \>> DOLIST LXIL   \>> \>>
01-29-2019, 07:13 PM (This post was last modified: 01-29-2019 07:14 PM by John Keith.)
Post: #2
 John Keith Senior Member Posts: 416 Joined: Dec 2013
RE: (50g) Numeric List Commands
Here is another list convolution program. It takes two lists (which must be the same length) and returns a list of convolutions of increasing subsets of the input lists.

The program will run on the HP48G if UNROT is replaced with ROT ROT.

The 0 + in the program is necessary because \GSLIST will error with a list size of 1.

Code:
 \<< DUP SIZE \-> n   \<< 1. n     FOR k OVER 1. k SUB OVER 1. k SUB REVLIST * 0 + \GSLIST UNROT     NEXT DROP2 n  \->LIST   \>> \>>

An example from http://oeis.org/A000330:

Code:
 Input: { 0 1 2 3 4 5 } { 1 3 5 7 9 11 } Output: { 0 1 5 14 30 55 }
05-18-2019, 09:26 PM
Post: #3
 John Keith Senior Member Posts: 416 Joined: Dec 2013
RE: (50g) Numeric List Commands
Updating this thread to add a program for deconvolution. Given a list on level 2 that is the (suspected) result of the convolution of two lists, and a "test" list on level 1, the program will return the list which must be convolved with the level 1 list to produce the level 2 list.

Note that unlike convolution, deconvolution is sensitive to the order of the lists. If the stack order of the lists is reversed, the program will return nonsense results. Also, the lengths of the two lists must be the same.

As an example, in level 2, { 1 6 20 50 105 196 336 } which are the 4-dimensional pyramidal numbers and on level 1,
{ 1 3 5 7 9 11 13 } - the odd numbers. The program returns { 1 3 6 10 15 21 28 } - the triangular numbers.

Code:
 \<< OVER HEAD OVER HEAD / 1. \->LIST UNROT 2. OVER SIZE   FOR k PICK3 OVER 2. k SUB REVLIST * 0 + \GSLIST PICK3 k GET PICK3 HEAD * SWAP - 4. ROLL SWAP + UNROT   NEXT DROP2 \>>

For HP-48G compatibility, replace UNROT with ROT ROT and replace PICK3 with 3 PICK.
 « Next Oldest | Next Newest »

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