![]() |
Fast Quadratic Formula for the HP-41C - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: Fast Quadratic Formula for the HP-41C (/thread-227187.html) |
Fast Quadratic Formula for the HP-41C - Gerson W. Barbosa - 07-17-2012 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
01 LBL 'QD
Re: Fast Quadratic Formula for the HP-41C - Luiz C. Vieira (Brazil) - 07-17-2012 Hi, Gerson;
what about reasoning Reverse-Polish-notation? 2-bytes shorter: 01 LBL 'QDBut 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)
Re: Fast Quadratic Formula for the HP-41C - Luiz C. Vieira (Brazil) - 07-17-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)
Re: Fast Quadratic Formula for the HP-41C - Gerson W. Barbosa - 07-17-2012 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.
Re: Fast Quadratic Formula for the HP-41C - Gerson W. Barbosa - 07-17-2012 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.
Re: Fast Quadratic Formula for the HP-41C - Luiz C. Vieira (Brazil) - 07-17-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 dinstead, 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.
Re: Fast Quadratic Formula for the HP-41C - Gerson W. Barbosa - 07-17-2012 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.
Re: Fast Quadratic Formula for the HP-41C - Luiz C. Vieira (Brazil) - 07-17-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)
Re: Fast Quadratic Formula for the HP-41C - Les Koller - 07-18-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.
Re: Fast Quadratic Formula for the HP-41C - Paul Dale - 07-18-2012 Divide through by -a. We're solving for y = 0 so this doesn't change that side of the formula.
Re: Fast Quadratic Formula for the HP-41C - Les Koller - 07-18-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.
Re: Fast Quadratic Formula for the HP-41C - Gerson W. Barbosa - 07-18-2012 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. Best regards,
Gerson.
Re: Fast Quadratic Formula for the HP-41C - Gerson W. Barbosa - 07-18-2012 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.
Re: Fast Quadratic Formula for the HP-41C - Les Koller - 07-18-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.
Re: Fast Quadratic Formula for the HP-41C - Luiz C. Vieira (Brazil) - 07-18-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)
Re: Fast Quadratic Formula for the HP-41C - Gerson W. Barbosa - 07-18-2012 It was my fault not being clear and rigorous enough in the first place. Thanks for the asking!
Re: Fast Quadratic Formula for the HP-41C - Gerson W. Barbosa - 07-18-2012 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.
Re: Fast Quadratic Formula for the HP-41C - Matt Agajanian - 07-19-2012 Luiz, I presume this only works on EQs with real roots only, right?
Re: Fast Quadratic Formula for the HP-41C - Luiz C. Vieira (Brazil) - 07-19-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.
Re: Fast Quadratic Formula for the HP-41C - Complex Roots - Gerson W. Barbosa - 07-20-2012 Hello Luiz,
First, let me present you a 17-step version of my first program: 01 LBL 'QDSomehow 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
An HP-41 program that accepts complex coefficients would be a bit more complex, I guess :-) 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.
Re: Fast Quadratic Formula for the HP-41C - Complex Roots - Luiz C. Vieira (Brazil) - 07-20-2012 Perfect! No doubts, these are gonna be amongst other examples of efficient programming. Thanks!
Luiz (Brasil)
Re: Fast Quadratic Formula for the HP-41C - Complex Roots - Gerson W. Barbosa - 07-20-2012 Here is a slightly more efficient HP-42S version, now two bytes shorter:-)
00 { 26-Byte Prgm } Cheers, Gerson.
|