▼
Posts: 2,761
Threads: 100
Joined: Jul 2005
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
▼
Posts: 591
Threads: 16
Joined: Feb 2012
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)
▼
Posts: 2,761
Threads: 100
Joined: Jul 2005
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.
▼
Posts: 591
Threads: 16
Joined: Feb 2012
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.
▼
Posts: 2,761
Threads: 100
Joined: Jul 2005
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.
▼
Posts: 591
Threads: 16
Joined: Feb 2012
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)
▼
Posts: 2,761
Threads: 100
Joined: Jul 2005
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.
▼
Posts: 591
Threads: 16
Joined: Feb 2012
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)
▼
Posts: 2,761
Threads: 100
Joined: Jul 2005
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.
Posts: 980
Threads: 239
Joined: Aug 2006
Luiz, I presume this only works on EQs with real roots only, right?
▼
Posts: 591
Threads: 16
Joined: Feb 2012
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.
▼
Posts: 2,761
Threads: 100
Joined: Jul 2005
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.
▼
Posts: 591
Threads: 16
Joined: Feb 2012
Perfect!
No doubts, these are gonna be amongst other examples of efficient programming.
Thanks!
Luiz (Brasil)
▼
Posts: 2,761
Threads: 100
Joined: Jul 2005
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.
Posts: 591
Threads: 16
Joined: Feb 2012
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)
▼
Posts: 2,761
Threads: 100
Joined: Jul 2005
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.
Posts: 253
Threads: 20
Joined: Jun 2012
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.
▼
Posts: 3,229
Threads: 42
Joined: Jul 2006
Divide through by -a.
We're solving for y = 0 so this doesn't change that side of the formula.
- Pauli
▼
Posts: 253
Threads: 20
Joined: Jun 2012
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.
▼
Posts: 2,761
Threads: 100
Joined: Jul 2005
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.
▼
Posts: 253
Threads: 20
Joined: Jun 2012
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.
▼
Posts: 2,761
Threads: 100
Joined: Jul 2005
It was my fault not being clear and rigorous enough in the first place. Thanks for the asking!
|