![]() |
Short Quadratic Solver (HP-42S) - 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: Short Quadratic Solver (HP-42S) (/thread-173639.html) |
Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-21-2010 Here is a 42S-specific quadratic solver. It is 28-byte long, uses only the stack and preserves the original stack register X. I have also a 27-byte version, but it requires one extra step.
QUADRATIC SOLVER - HP-42S Re: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-21-2010 Slightly different, but still 28-byte and 16-step long:
QUADRATIC SOLVER - HP-42S Re: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-21-2010 One step shorter:
00 { 28-Byte Prgm } Re: Short Quadratic Solver (HP-42S) - Paul Dale - 10-22-2010 Very nicely done!
- Pauli
Re: Short Quadratic Solver (HP-42S) - Ángel Martin - 10-22-2010 Does it also work with complex numbers? I guess so, since the 42 can hold them in the normal stack? Reminds me those contests for the shortest implementation of a given program, and I'd have to say the following takes the prize: 01 QROOT
(see SandMath module - of course that's cheating, as the MCODE listing is longer than that :)
Re: Short Quadratic Solver (HP-42S) - Paul Dale - 10-22-2010 But when are you doing MCODE analytic solutions to higher order polynomials? :-)
Re: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-22-2010 Hello Ángel,
Quote: Yes, as in examples #2 and #3 above. I've been using this formula:
(Presented by Allen in this thread (Message #2). The HP-15C handle complex numbers also, as you know: http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv017.cgi?read=114345 Regards,
Gerson.
Re: Short Quadratic Solver (HP-42S) - Ángel Martin - 10-22-2010 Touché :)
Re: Short Quadratic Solver (HP-42S) - Ángel Martin - 10-22-2010 Since the 41 lacks the "native" ability to handle complex numbers on its stack, I added the following short routine to the 41Z to solve the quadratic roots in such case: 1 LBL "ZQRT"just 24 steps (or Z-steps, rather). It takes the three coefs in complex stack leves 1,2,3, leaving both solutions on complex stack levels 1 and 2.
Cheers,
Edited: 22 Oct 2010, 8:25 a.m.
Re: Short Quadratic Solver (HP-42S) - Werner - 10-22-2010 From my archives ;-) x := -b/(2*a); Re: Short Quadratic Solver (HP-42S) - Eddie W. Shore - 10-22-2010 Wow, nice job!
Re: Short Quadratic Solver (HP-42S) - Thomas Chrapkiewicz - 10-22-2010 Very nice! You are to be highly commended for the high level of documentation of the process!! This is all too often negelected, both in calculator and computer algorithms.
TomC
Re: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-22-2010 Hello Werner,
Quote: These are valuable features. Michael Harwood's program in the Software Library (Fast Quadratic Formula for the HP-41C) is 16-step long (when the prompt is removed) and would require 25 bytes on the 42S, but it may not be as accurate as yours. I have been trying to preserve the stack register X. Without this constraint, I suspect a 42S-specific program might break the 15-step length barrier. Regards,
Gerson.
Re: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-22-2010 Thanks! However the documentation is still incomplete. The formulas used in the first version are, as we have seen,
'x1=-b/(2*a)+sqrt((b/(2*a))^2-c/a)'
which is the quadratic formula for 'x^2-b/a*x+c/a=0'In the second and third versions, an attempt has been made to carry this one step further, by making the inputs coefficients a, b and c equal to 1, b/a and c/a, respectively. This requires additional steps, but the formulas become somewhat simpler: 'x1=b/2+sqrt((b/2)^2-c' Regards, Gerson.
Re: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-22-2010 Thanks Paul!
I hope the 34s has a built-in QUAD command to do it in only one step :-)
Re: Short Quadratic Solver (HP-42S) - Paul Dale - 10-22-2010 The thought had occurred to me to drop your code in as a built in function :-)
- Pauli
Re: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-22-2010 Accuracy-wise, Werner's code is better though. For instance, for a = 1E-13, b = -2 and c = 1 it returns the correct 12-digit results, that is 2E13 and 0.5. Mine returns 2E13 and 0. Unless, of course, you want to preserve at least the stack register X.
Gerson.
Re: Short Quadratic Solver (HP-42S) - Paul Dale - 10-22-2010 Werner's code fails for b=c=0 BTW. Easy enough to detect and return the correct answer for but that means more steps.
Re: Short Quadratic Solver (HP-42S) - Chuck - 10-22-2010 Gerson, I had done this many many years ago for, I think, my HP-11c, but I believe mine was slightly different. (Actually, looking again, it's the same.) Start with the usual ax^2 + bx + c = 0. Then define: m = -b/(2a) and n = c/a The solutions become x1 = m + sqrt( m^2 - n) x2 = m - sqrt(m^2 - n) I can't remember if I used the stack or storage registers. Usually the upfront calculations of m and n are trivial, and allows for a very compact program, and no fractions in the final calculations. For something like: 3x^2+5x-8=0 I'd key -5/6 run LBL A, -8/3, press R/S and get the solutions. I forget the actual program length, but it was quite small. I'll have to dig out the 11C and try it again.
CHUCK
Re: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-22-2010 It also fails for example #3, I noticed that soon after I wrote. I had checked only the second example. But this can be fixed. By the way, I never needed to solve a quadratic equation with complex coefficients before. I simply expanded (x-(1+2i))*(x-(3+4i)) and there it was. Regards, Gerson.
typo correction Edited: 22 Oct 2010, 10:10 p.m.
Re: Short Quadratic Solver (HP-42S) - Ángel Martin - 10-22-2010 Or alternatively, drop the MCODE (oh if only CPU's were compatible :)
094 "T"
Re: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-22-2010 Chuck, This appears quite suitable for hand calculations also, as the formula is easier to use and remember. My 14-year old daughter has been studying quadratic equations and application problems in school, but so far she's been taught the traditional method only. Regards, Gerson.
Re: Short Quadratic Solver (HP-42S) - Thomas Klemm - 10-23-2010 This program returns only one of the possible solutions of the equation x2 + px + q = 0. The quadratic solver hidden in ACOSH is used:
00 { 17-Byte Prgm } With a minor change (-2 -> 2 CHS) it may be used with the HP-15C as well. However it won't work with the HP-35s due to its lack of proper support of complex functions.
Usage: 1) x2 - 5x + 6 = 0
Since neither addition nor subtraction is used there's no loss
Best regards PS: Finding the other solution is left as an exercise.
Edited: 23 Oct 2010, 2:41 a.m. after one or more responses were posted
Re: Short Quadratic Solver (HP-42S) - Ángel Martin - 10-23-2010 The "classic" formula is still taught in high schools (also on my kids' case), perhaps on the grounds of being easier to memorize (?) - yet the other one is much more suitable for programming. That's the one I used in the MCODE listing, it saves time and storage space. Cheers
Re: Short Quadratic Solver (HP-42S) - Ángel Martin - 10-23-2010 Very nice idea - now we're shortening the program listing although at the expense of using other functions - a valid rule in the "brevity contest" :-)
What about using P-R instead of ACOSH for the 41?
Re: Short Quadratic Solver (HP-42S) - Thomas Klemm - 10-23-2010 This could only be used when q <= 0. But even then you'd have to calculate the square root of -q previously to use R-P. So I'm afraid we won't gain anything. Only now I see you mentioned P-R, not R-P. What did you have in mind? Edited: 23 Oct 2010, 11:02 a.m.
Re: Short Quadratic Solver (HP-42S) - Thomas Klemm - 10-23-2010 Okay the following works only for certain values of p and q: 0 < 4q < p2
00 { 20-Byte Prgm } 0 < -4q < p2
00 { 21-Byte Prgm } So all we have to do is combine these two programs into one.
BTW: I used here the following formulas: 2
Cheers Edited: 23 Oct 2010, 7:15 a.m.
Re: Short Quadratic Solver (HP-42S) - Walter B - 10-23-2010 Input of a, b, c as z, y, x and output of possibly complex x1 and x2 in x, y, z, and t ? Do we need more reasons for an 8 level stack? ;)
Re: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-23-2010 Hi Thomas,
Quote: Still unoptimized, as I'm in a hurry:
00 { 25-Byte Prgm } Regards,
Gerson.
Re: Short Quadratic Solver (HP-42S) - Thomas Klemm - 10-23-2010 A little bit shorter:
00 { 21-Byte Prgm }
However I had something else in my mind. Edited: 23 Oct 2010, 7:51 a.m.
Re: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-23-2010 You're right! x2 = q/x1, for x1 <> 0, is shorter than x2 = -(x1+p). Thanks for presenting your interesting insight. Regards, Gerson.
00 { 31-Byte Prgm } It does work for my three examples:
1) Given x2 - 5x + 6 = 0, compute 123456*x1 and 123456*x2 It will work for a=1E-13, b=-2 and c=1 also:
1.99999999996E13 Re: Short Quadratic Solver (HP-42S) - Thomas Klemm - 10-23-2010 It seems to be a recurring theme that we discuss every few years. Thanks for starting that thread (again).
Best regards Re: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-23-2010 Werner, that's the 27-byte version I had mentioned. It is one step shorter and will solve all my three examples as stated. Neither HP-41 compatible nor accurate enough for the case you handle though. Doing it using only HP-41 instructions would be quite challenging.
QUADRATIC SOLVER - HP-42S Regards,
Gerson.
Re: Short Quadratic Solver (HP-42S) - Csaba Tizedes (Hungary) - 10-24-2010 Maybe the HP's SOLVE algorithm is accurate? Here is my solution (from my memories...) - not shortest, uses variables, but not uses the classical quadratic formula. The 32SII (low-end) version:
Solve for real roots of an 'a*x^2+b*x+c=0' equation:
Enjoy!
Re: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 10-25-2010 Hello Csaba,
Quote: It's quite accurate, as we know. The idea is getting the shortest possible HP-42S quadratic solver. By short I mean less number of steps (I know on the 42S less bytes would be more appropriate, but I guess less steps means faster execution - to be sure, I would have to check out a table of T-states per instruction, but I don't have any). Anyway, you approach using the SOLVER is interesting, thanks for posting it. When I tested it on the 33s I added the lines below to make it even easier to use, but then I noticed you had done that already here :-)
... I expected the equivalent 42S version to handle complex cases, but it didn't. Perhaps I just didn't implement it right... Best regards,
Gerson.
Re: Short Quadratic Solver (HP-42S) - Werner - 10-26-2010 Strange how you can always improve upon an old program when you look at it again:
00 { 25-Byte Prgm } X Y Z T
To be sure, it exhibits the same pro's and con's as the previous: + 41C compatible |