Post Reply 
(42S) Polynomial Operations
01-11-2019, 04:59 AM
Post: #1
(42S) Polynomial Operations
Note: These programs presented in today's blog are can also be programmed on the Free42 app and the DM 42. The default setting, 25 memory registers, is assumed to be set. The programs are written use R24 as a counter.

This program uses indirect storage (R24). The maximum order is 23.

HP 42S Program SPOLY

This program asks for the order of a polynomial and then asks the user to store the coefficients. The order of registers are coefficients of powers of x in descending order from n to 0.

For example, for a cubic polynomial, the coefficients are stored as such:
R01 = coefficient of x^3
R02 = coefficient of x^2
R03 = coefficient of x
R04 = constant coefficient

Program:
Code:

00 { 67-Byte Prgm }
01▸LBL "SPOLY"
02 CLX
03 "ORDER?"
04 PROMPT
05 STO 00
06 IP
07 1
08 +
09 1ᴇ-3
10 ×
11 1
12 +
13 STO 24
14▸LBL 00
15 "X↑"
16 RCL 00
17 RCL 24
18 IP
19 -
20 1
21 +
22 ARCL ST X
23 ├" ="
24 CLX
25 PROMPT
26 STO IND 24
27 ISG 24
28 GTO 00
29 "DONE"
30 AVIEW
31 RTN
32 END
Example: Store the polynomial p(x) = 2*x^3 - x^2 + 5*x + 7

Keystrokes: [ XEQ ] (SPOLY)
"ORDER?" 3 [ R/S ]
"X↑3.0000=" 2 [R/S]
"X↑2.0000=" 1 [ +/- ] [R/S]
"X↑1.0000=" 5 [R/S]
"X↑0.0000=" 7 [R/S]
"DONE"

Results
R01 = 2.0000
R02 = -1.0000
R03 = 5.0000
R04 = 7.0000

HP 42S Program HORNER

The HORNER evaulates the polynomial p(x). The assumes that the order of registers are coefficients of powers of x in descending order from n to 0. The user is asked about the order of the polynomial and the value of x.

Program:
Code:

00 { 65-Byte Prgm }
01▸LBL "HORNER"
02 CLX
03 "X?"
04 PROMPT
05 STO 00
06 CLX
07 "ORDER?"
08 PROMPT
09 IP
10 1ᴇ-3
11 ×
12 1
13 +
14 STO 24
15 RCL 00
16 RCL× IND 24
17 ISG 24
18▸LBL 01
19 RCL+ IND 24
20 RCL× 00
21 ISG 24
22 GTO 01
23 RCL+ IND 24
24 "P(X)="
25 AVIEW
26 RTN
27 .END.

Example: Let p(x) be the polynomial, p(x) = 2*x^3 - x^2 + 5*x + 7
Calculate p(-3).

The coefficients are have already been stored (from last example).
R01 = 2.0000
R02 = -1.0000
R03 = 5.0000
R04 = 7.0000

Keystrokes: [XEQ] (HORN)
"X?" 3 [ +/- ] [ R/S ]
"ORDER?" 3 [ R/S ]

Result: -71.0000

HP 42S Program DX_PX

This program calculates coefficients of the derivative of the polynomial p(x), using the form:

d/dx x^n = n * x^(n-1)

The order of registers are coefficients of powers of x in descending order from n to 0.

Program:
Code:

00 { 79-Byte Prgm }
01▸LBL "DX_PX"
02 CLX
03 "ORDER?"
04 PROMPT
05 STO 00
06 IP
07 1ᴇ-3
08 ×
09 1
10 +
11 STO 24
12▸LBL 02
13 RCL IND 24
14 RCL 00
15 RCL 24
16 IP
17 -
18 1
19 +
20 ×
21 STO IND 24
22 "X↑"
23 RCL 00
24 RCL 24
25 IP
26 -
27 ARCL ST X
28 X<>Y
29 AVIEW
30 STOP
31 ISG 24
32 GTO 02
33 1
34 RCL+ 00
35 STO 24
36 CLX
37 STO IND 24
38 "DONE"
39 AVIEW
40 RTN
41 .END.

Example: Calculate the derivative of p(x) = 2*x^3 - x^2 + 5*x + 7. Assume that coefficients from the previous example.

Keystrokes: [ XEQ ] (DX_PX)
"ORDER?" 3 [R/S]

Results:
"X↑2.0000=" 6.0000 [R/S]
"X↑1.0000=" -2.0000 [R/S]
"X↑0.0000=" 5.0000 [R/S]
"DONE"

dp/dx = 6*x^2 - 2*x + 5

Registers:
R1 = 6.0000
R2 = -2.0000
R3 = 5.0000
R4 = 0.0000

Link to blog entry: https://edspi31415.blogspot.com/2019/01/...tions.html
Visit this user's website Find all posts by this user
Quote this message in a reply
01-11-2019, 01:37 PM
Post: #2
RE: (42S) Polynomial Operations
Delving into my archives again..

The following will take a vector with the coefficients in Y and x in X

Y: [ an an-1 ... a1 a0 ]
X: x

and calculate P(x), P'(x) and P"(x) simultaneously:

Code:
00 { 51-Byte Prgm }
01▸LBL "P3"
02 ENTER
03 -
04 STO ST Z
05 X<>Y
06 EDIT
07 ENTER
08 GTO 00
09▸LBL 03
10 R↓
11 X<> ST Z
12 LASTX
13 ×
14 X<>Y
15 STO+ ST Y
16 LASTX
17 ×
18 R↑
19 STO+ ST Y
20 LASTX
21 ×
22 RCLEL
23 STO+ ST Y
24▸LBL 00
25 J+
26 FC? 77
27 GTO 03
28 J-
29 EXITALL
30 R↑
31 STO+ ST X
32 R↓
33 END

So, for your example polynomial P(x) = 2*x^3 - x^2 + 5*x + 7
Create a 1x4 matrix [2 -1 5 7]
put -3 on the stack and execute P3 to get

T: -38
Z: 65
Y: -71
X: [2 -1 5 7]

P(-3) = -71
P'(-3) = 65
P"(-3) = -38

Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
01-11-2019, 03:51 PM (This post was last modified: 01-11-2019 04:12 PM by Albert Chan.)
Post: #3
RE: (42S) Polynomial Operations
Hi Werner

What are commands J+ J- FC? 77 ???

Is the code basically synthetic divisions, applied 3 times ?

2 x³ - x² + 5 x + 7 = (x + 3) (2 x² - 7 x + 26) - 71
2 x² - 7 x + 26 = (x + 3) (2 x - 13) + 65
2 x - 13 = (x + 3) (2) - 19

Thus, P(-3) = -71, P'(-3) = 65, P"(-3) = -19 * 2! = -38
Find all posts by this user
Quote this message in a reply
01-12-2019, 12:27 AM
Post: #4
RE: (42S) Polynomial Operations
(01-11-2019 03:51 PM)Albert Chan Wrote:  What are commands J+ J- FC? 77 ???

They're all in the manual.

J+/J- increment/decrement the column you are working on.

FC? 77 tests if flag 77 is clear - if so, it proceeds to the next step; if not, it skips to the 2nd step.

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
01-12-2019, 06:48 AM
Post: #5
RE: (42S) Polynomial Operations
(01-11-2019 03:51 PM)Albert Chan Wrote:  Is the code basically synthetic divisions, applied 3 times ?

I'd rather say it's Horner's method applied in parallel:

\(\begin{matrix}
2 & -1 & 5 & 7 \\
2 & -7 & 26 & -71 \\
0 & 2 & -13 & 65 \\
0 & 0 & 2 & -19
\end{matrix}\)

Here are the stack diagrams of the relevant lines of code:
Code:
09▸LBL 03               ;   2   2   0   0    ;   -1  -7  2   0     ;   5   26  -13 2
10 R↓                   ;   2   0   0   2    ;   -7  2   0   -1    ;   26  -13 2   5
11 X<> ST Z             ;   0   0   2   2    ;   0   2   -7  -1    ;   2   -13 26  5
12 LASTX                ;   -3  0   0   2    ;   -3  0   2   -7    ;   -3  2   -13 26
13 ×                    ;   0   0   2   2    ;   0   2   -7  -7    ;   -6  -13 26  26
14 X<>Y                 ;   0   0   2   2    ;   2   0   -7  -7    ;   -13 -6  26  26
15 STO+ ST Y            ;   0   0   2   2    ;   2   2   -7  -7    ;   -13 -19 26  26
16 LASTX                ;   -3  0   0   2    ;   -3  2   2   -7    ;   -3  -13 -19 26
17 ×                    ;   0   0   2   2    ;   -6  2   -7  -7    ;   39  -19 26  26
18 R↑                   ;   2   0   0   2    ;   -7  -6  2   -7    ;   26  39  -19 26
19 STO+ ST Y            ;   2   2   0   2    ;   -7  -13 2   -7    ;   26  65  -19 26
20 LASTX                ;   -3  2   2   0    ;   -3  -7  -13 2     ;   -3  26  65  -19
21 ×                    ;   -6  2   0   0    ;   21  -13 2   2     ;   -78 65  -19 -19
22 RCLEL                ;   -1  -6  2   0    ;   5   21  -13 2     ;   7   -78 65  -19
23 STO+ ST Y            ;   -1  -7  2   0    ;   5   26  -13 2     ;   7   -71 65  -19

According to hp42s:
Quote:77 matrix wrap, first to last

This is a marvel of stackrobatics. Thanks for sharing.

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
01-12-2019, 08:41 AM (This post was last modified: 01-12-2019 08:49 AM by Dieter.)
Post: #6
RE: (42S) Polynomial Operations
(01-12-2019 12:27 AM)rprosperi Wrote:  FC? 77 tests if flag 77 is clear - if so, it proceeds to the next step; if not, it skips to the 2nd step.

The 42s sets flag 77 if the matrix pointer jumps from the last element back to the first one (or vice versa), which means that all elements have been processed.
So it acts as a kind af "end of matrix" flag here.

Dieter
Find all posts by this user
Quote this message in a reply
01-12-2019, 02:20 PM
Post: #7
RE: (42S) Polynomial Operations
Well, since Albert asked, I thought I'd leave that detail for him to look-up. After all, all 3 of us had to, along with at least a few other quiet readers. Probably everyone except Werner. Smile

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
Post Reply 




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