(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:
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:
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:
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 |
|||
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 } 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 |
|||
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 |
|||
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 |
|||
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 According to hp42s: Quote:77 matrix wrap, first to last This is a marvel of stackrobatics. Thanks for sharing. Cheers Thomas |
|||
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 |
|||
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.
--Bob Prosperi |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)