HP Forums
(35s) Stack-only quadratic solver - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (35s) Stack-only quadratic solver (/thread-16220.html)



(35s) Stack-only quadratic solver - Liam Hays - 01-24-2021 11:55 PM

I know, I know, this is another quadratic equation solver for the 35s. This one, though, is a little different in two ways: a) it operates fully in the stack, modifying no variables, and b) it doesn't use any equations, so while it has more lines, it should theoretically be faster as well.

I also know this isn't one of the awesome high-performance solvers, but figuring out all the stack manipulation was a fun challenge and I'm proud of what I've made. It will find complex roots.

Usage: enter a, b, and c onto the stack, in that order, and XEQ Q (or whatever label you choose). You'll get the roots back, or if there's one root, that root on both the X and Y levels.

The checksum on my machine is 2F04, with a length of 160.

Code:
Q001 LBL Q
Q002 Rdown
Q003 x^2
Q004 Rup
Q005 4
Q006 *
Q007 Rup
Q008 *
Q009 ENTER
Q010 Rdown
Q011 Rdown
Q012 rootx
Q013 ENTER
Q014 x^2
Q015 Rup
Q016 -
Q017 x<0?
Q018 GTO A020
Q019 GTO A025
Q020 ABS
Q021 rootx
Q022 i
Q023 *
Q024 GTO A026
Q025 rootx
Q026 ENTER
Q027 Rdown
Q028 Rdown
Q029 +/-
Q030 ENTER
Q031 Rup
Q032 +
Q033 LASTx
Q034 x<>y
Q035 Rdown
Q036 -
Q037 x<>y
Q038 /
Q039 ENTER
Q040 Rdown
Q041 Rdown
Q042 LASTx
Q043 /
Q044 2
Q045 /
Q046 Rdown
Q047 Rdown
Q048 2
Q049 /
Q050 Rup
# swap to make output like the three-var one
Q051 x<>y
Q052 RTN

Try it out: say we want to solve 4*x^2 - 8*x + 3 = 0. To do this, you would follow these steps:
Code:
4 [ENTER] -8 [ENTER] 3
XEQ Q

The screen should read -0.5 and -1.5.


RE: (35s) Stack-only quadratic solver - mchris - 06-06-2021 11:56 PM

(01-24-2021 11:55 PM)Liam Hays Wrote:  I know, I know, this is another quadratic equation solver for the 35s. This one, though, is a little different in two ways: a) it operates fully in the stack, modifying no variables, and b) it doesn't use any equations, so while it has more lines, it should theoretically be faster as well.

Take a look at this <https://www.hpmuseum.org/forum/thread-15052.html>
It is smaller. I don't know if the commands REGZ and REGT I used considered equations or not.


RE: (35s) Stack-only quadratic solver - Liam Hays - 06-07-2021 01:26 AM

I honestly like yours better. I should have realized that I could have used the REG* functions.

mchris Wrote:I don't know if the commands REGZ and REGT I used considered equations or not.

I'm not 100% sure, but I think it works like this: if those functions sit alone on a single line, they aren't equations. I think equations are only triggered when you specifically enter one with the EQN key.


RE: (35s) Stack-only quadratic solver - mchris - 06-07-2021 09:41 AM

(06-07-2021 01:26 AM)Liam Hays Wrote:  I'm not 100% sure, but I think it works like this: if those functions sit alone on a single line, they aren't equations. I think equations are only triggered when you specifically enter one with the EQN key.

But to trigger REGx commands in 35s you have to press EQN and RDN. Anyway I considered them short equations to help the flow of the program. For your information, in HP42s there are equivalent commands like RCL STCK x.

In any case I don't consider any gain having 100ms faster code except for a routine which is to be called 100 times a second, like functions for integration. In that last case I have the suspicion that when the program is in ALG mode or has an EQN inside, the 35s translate the whole program before executing it, so the speed remains the same. More tests must be done...


RE: (35s) Stack-only quadratic solver - Didier Lachieze - 06-07-2021 09:48 AM

(06-07-2021 09:41 AM)mchris Wrote:  But to trigger REGx commands in 35s you have to press EQN and RDN.

You can also get REGx with MEM RDN.


RE: (35s) Stack-only quadratic solver - mchris - 06-07-2021 06:51 PM

(06-07-2021 09:48 AM)Didier Lachieze Wrote:  You can also get REGx with MEM RDN.
Ah yes! And in that case is a proper RPN command, not an equation, which means smaller and faster.