Fast Quadratic Formula for the HP-41C



#7

The subject line is the title of a program by Michael Harwood in the HP-41C Software Library:

http://www.hpmuseum.org/software/41/41quad.htm

His program rearranges the quadratic equation y = ax2 + bx + c as y = x2 - bx - c then uses the formula
x = b/2 + Sqrt((b/2)^2 + c). The following version is two steps longer, but preserves what is left of the stack after the three coefficients are entered. Steps 02 through 04 are optional. Any ideas to save one or two more?

01 LBL 'QD
02 'ENTER a, b, c
03 TONE 0
04 PROMPT
05 X<> Z
06 1/X
07 CHS
08 ST* Z
09 *
10 2
11 /
12 STO Z
13 X^2
14 +
15 SQRT
16 X<>Y
17 STO Z
18 X<>Y
19 ST- Z
20 +
21 END


#8

Hi, Gerson;

what about reasoning Reverse-Polish-notation? 2-bytes shorter:

01 LBL 'QD
02 'ENTER c, b, a
03 TONE 0
04 PROMPT
05 X<> Z (and this step moves out...)
06 1/X
07 CHS
08 ST* Z
09 *
10 2
11 /
12 STO Z
13 X^2
14 +
15 SQRT
16 X<>Y
17 STO Z
18 X<>Y
19 ST- Z
20 +
21 END
But I guess the original program is a lot shorter, I remember seeing a very short one in an HP-KeyNotes issue, will try to find it.

Cheers.

Luiz (Brazil)


#9

Hello Luiz,

In all textbooks I know quadratic equations always appear in this form: y = ax2 + bx + c. That's why I think we should keep the a, b, c order. Also, as a convenience, the stack register T should be preserved. Without the first constraint, both versions could be one step shorter, as you have shown. The second constraint makes the program at least two steps longer (unless someone finds a better solution). I think these programs, without the optional steps, are too close to full size-optimization on the HP-41. If we consider other RPN calculators, nothing beats SLVQ on the WP 34S :-)

Cheers,

Gerson.


#10

Hi, Gerson;

please, forgive me: I did not notice the stack contents preservation, just crossed my mind the idea of using fewer steps.

And indeed, quadratic equation is written as you mention. But as we all know, sometimes the entering order in RPN/RPL calculators follows different directions, like entering coordinates: we write (x,y) and enter y [ENTER] x. Yep, I know this is for 'data'<->'stack register' consistency, as it happens with statistics data entering. And so it follows the polar coordinates entering: angle [ENTER] radius.

A different input consistency, kinda 'backwards' as I see if compared to the one above, is observed with the attribute operator '->' in the 28/48 series: if you have something like

-> a b c
, we know that a, b and c must be in stack levels #3, #2 and #1, respectively. So, the first variable after the '->' will 'grab' data in the highest stack level. If we have
-> a b c d
instead, then variable 'a' grabs data in level #4, but it is still the first variable after the attribute operator.

But if we take algebraic input it is just 'as is'.

I must confess I like to write programs considering data is input, say, 'backwards', if I can say so. I like to think X-register will hold the first variable (a, in this case), Y-register will hold the second data (b) and so on. To make this happen, data must be entered 'backwards', right? As you see, the first step of the program - X<> Z - provides data to be arranged this way.

But what matters is the brainy discussion, the learning and the teasing/challenging of optimizing within limits (or rules). Or else the task looses focus.

Thanks for that! And good programming, indeed!

Cheers.

Luiz (Brazil)

Edited: 17 July 2012, 10:11 p.m.


#11

It looks like this kind of program have been fully explored by now. It just happens I've been playing a lot with the HP-41 lately as I didn't have one when I was a kid, like you and most everybody here :-)

(Still deciding whether going 41CL or not)

Cheers,

Gerson.


#12

Hey, hey!

Is it an HP41CV with a silver trim in the keyboard faceplate? I have only seen 41CV's with golden trim. Is it a regular one or the upper case is from a 41C?

And yours is a tall-key 41C!

And both are good looking!

Wow! Congrats!

Please, hold these babes as they are. Your HP41CV case is in the oven (metaphorically speaking, of course...), just wait until it cooks. My vacations (school recess, matter of fact) did not begin yet, I have been 'paying my debts' for I have not yet concluded the 'daily reports' and posted the final grades. I feel tired just by talking about this...

Our CL's will come, I can almost feel that... We must go with that.

Best regards, my friend.

Luiz (Brazil)


#13

The HP-41CV (2219S43636) indeed has a silver trim. It is a gift from a friend, who told me it was part a military equipment and apparently had never been opened. Well, I eventually opened it last month because the display and keyboard started failing after two or three weeks of (somewhat intensive) use. I hardwired both boards together (hope this is not a heresy, but I could not ajust the zebra connector again, or it was definitely defective) and it is ok now, a real workhorse! The contrast of the display is excellent, much better than the display of the newly acquired HP-41C (2121B58157). I have also the card reader, the infrared and the Advantage modules.

Yes, I will keep those unchanged. Thanks again for your kind offer!

Best regards,

Gerson.


#14

Hi, Gerson.

What you have done is not en heresy, you are actually re-discovering someone else's far distant proposal, 12 years ago... (I remember the pictures were a lot better, I still have some copies of the good ones somewhere, should find them...) You found your own solution for the same problem. Bright minds think alike...

Cheers.

Luiz (Brazil)


#15

Hello Luiz,

I hope you still think so when you see the picture:

I should have used thinner wires, but that's what I had at hand. Anyway, the post are excellent and it all fitted well in place.

Cheers,

Gerson.

#16

Luiz, I presume this only works on EQs with real roots only, right?


#17

Hi.

I'd guess so, because there is no test for negative values of 'b2-4ac'. Anyway, the proposed solution in the thread pointed in the next follow-up not only uses FLAG 0 to signalize the occurrence of a complex root but also computes it.

Cheers.

Luiz (Brazil)

Edited: 19 July 2012, 1:40 a.m.


#18

Hello Luiz,

First, let me present you a 17-step version of my first program:

01 LBL 'QD
02 X<> Z
03 CHS
04 ST/ Z
05 /
06 2
07 /
08 STO Z
09 X^2
10 +
11 SQRT
12 X<>Y
13 STO Z
14 X<>Y
15 ST- Z
16 +
17 END
Somehow I missed this one-step saving (See the first three steps in your reference).

Secondly, here is a 23-step program that handles real roots and preserves the stack register T:

01 LBL 'QD
02 CF 00
03 X<> Z
04 CHS
05 ST/ Z
06 /
07 2
08 /
09 STO Z
10 X^2
11 +
12 X<0?
13 SF 00
14 ABS
15 SQRT
16 X<>Y
17 FS? 00
18 RTN
19 STO Z
20 X<>Y
21 ST- Z
22 +
23 END

Input Output Output
(Flag 00 is clear) (Flag 00 is set)

T: T T T
Z: a T T
Y: b x1 |Im(x1,2)|
X: c x2 Re(x1,2)

(a, b and c are real)

Examples:


162 ENTER 949.2996661 CHS ENTER 1383.436944 XEQ ALPHA QD ALPHA --> 3.141592654 (Flag 00 is clear --> real roots)
X<>Y --> 2.718281828

2 ENTER 3 ENTER 4 XEQ ALPHA QD ALPHA --> -0.750000000 (Flag 00 is set --> complex roots)
X<>Y --> 1.198957881

An HP-41 program that accepts complex coefficients would be a bit more complex, I guess :-)
In this case I would recommend the HP-42S or the good old HP-15C (not to mention the RPL calculators).

http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv019.cgi?read=173639

http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv017.cgi?read=114345

Cheers,

Gerson.


Edited: 20 July 2012, 7:28 p.m.


#19

Perfect!

No doubts, these are gonna be amongst other examples of efficient programming.

Thanks!

Luiz (Brasil)


#20

Here is a slightly more efficient HP-42S version, now two bytes shorter:-)

00 { 26-Byte Prgm }                        
01 LBL "Q"
02 X<> ST Z
03 +/-
04 STO/ ST Z
05 /
06 2
07 /
08 STO ST Z
09 X^2
10 +
11 SQRT
12 RCL+ ST Y
13 X<>Y
14 RCL- ST L
15 END

Cheers,

Gerson.

#21

Hi, Gerson.

I went through digging the archives and found this thread. You can check the first follow-up (mine) and the listing with a reference to the HP Key Notes V6 N2, p. 14, necessary credits to Paul Baker. The listing has 20 steps + END, but it uses 26 bytes (not counting the global label) and signalizes when roots are complex with flag 0. There's also some extra development in the follow-ups, and the removal of the first step - X<> Z - can also be accomplished if we enter c, b and a instead of a, b and c as well.

I actually think this is one of the most efficient program I have ever seen based on what it does and the memory it uses. In fact, I use it as a personal reference till these days.

Cheers.

Luiz (Brazil)


#22

Hi Luiz,

Thanks for the link! Those appear to be interesting, but I would insist on a, b, c order and preserving the T stack register. It need be no "Cadillac" quadratic solver, just as shortest as possible. Most real-life quadratic equations have real roots.

Cheers,

Gerson.

#23

I confess I am at a loss. How can you "rearrange" the first equation algebraically to look like the second one? I must be missing something.


#24

Divide through by -a.

We're solving for y = 0 so this doesn't change that side of the formula.


- Pauli


#25

OK, but divide the right side by -a (your comment) and you get

y = -x^2 -(b/a)x - (c/a). Not positive x^2, not -bx, not -c. I think he must have left a step out. Anyway, author got y = x^2 - bx -c and gave an answer for x, but x = 1/2 (sqrt(b^2+4c)+b) as verified by Wolfram Alpha. I must be overlooking something simple. I even read the article link and I could see no where where the equation was altered in that code.


#26

I am not sure whether "rearrangement" is the right word. From the HP-41 Advantage manual:

"A quadratic equation x2 + a1x + a0 = 0 is solved by the formula

x1,2 = - a1/2 + Sqrt(a12/4 - a0)"

For convenience, I changed the signs of the coefficients a1 and a0 (this requires changing the signs of both coefficients in the beginning of the program, at the cost of one single step, but saves two steps ahead). My first program was 19 steps longs (without the optional lines). When I compared it with Michael Harwood's I noticed about 10 steps in the middle of both programs matched exactly! I made a slight modification in my final steps and borrowed his two last steps thus saving one step.


#27

Gotcha! Understood. I thought you were saying the 1st equation was algebraically manipulated to get the second. One of the pitfalls of pure mathematics training rather than applied I suppose. Thanks for the reply. Now I have it. Dang I love this forum!

Edited: 18 July 2012, 4:56 a.m.


#28

It was my fault not being clear and rigorous enough in the first place. Thanks for the asking!


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to manage formula in prime fabrice48 5 2,532 12-05-2013, 01:09 PM
Last Post: Steve Simpkin
  A fast Bernoulli Number method for the HP Prime Namir 16 5,607 11-22-2013, 04:46 PM
Last Post: Namir
  Quadratic & Cubic Regression RPN progs Matt Agajanian 9 2,989 09-17-2013, 11:37 AM
Last Post: Jeff O.
  WP34s program submission: Quadratic fit Andrew Nikitin 2 1,274 06-13-2013, 02:44 AM
Last Post: Paul Dale
  Very fast modified TEA for HP 48 and up! Raymond Del Tondo 0 926 11-23-2012, 08:43 PM
Last Post: Raymond Del Tondo
  New Quadratic Lagrangian Interpolation Method Namir 2 1,391 07-20-2012, 04:32 PM
Last Post: Namir
  Cubic Formula: HP 71B Eddie W. Shore 8 2,998 07-03-2012, 11:14 PM
Last Post: Eddie W. Shore
  Quadratic formula program help Chris C 18 5,043 06-16-2012, 12:14 AM
Last Post: Matt Agajanian
  HP42S freeze after "Fast mode" Tom Grydeland 3 2,031 02-23-2012, 05:41 AM
Last Post: Tom Grydeland
  HP 15C LE programming, does CPU run fast? designnut 1 1,124 02-12-2012, 05:13 PM
Last Post: Jeff O.

Forum Jump: