Code Analyzer for HP Calculators
|
04-10-2022, 07:25 PM
Post: #1
|
|||
|
|||
Code Analyzer for HP Calculators
The Simulator
This Python program simulates an HP calculator: Code: X, Y, Z, T, L = [0] * 5 The stack and the use of registers is implemented. Also the most common functions are provided. Writing Programs It is straight forward to write a program using these functions. Area of a Circle This program calculates the area of a circle based on the radius \(r\): \( A = \pi r^2 \) Code: def area(r): Quadratic Equation This program calculates the solution of the quadratic equation based on the coefficients \(a\), \(b\) and \(c\): \( a x^2 + b x + c = 0 \) Code: def quadratic_equation(a, b, c): Numeric Evaluation Import the math Library If we want to analyze the numeric values we import the math library: Code: from math import ( X: 0.000000000000e+00 Y: 0.000000000000e+00 Z: 0.000000000000e+00 T: 0.000000000000e+00 L: 0.000000000000e+00 : CLST Area of a Circle Code: r = 4 X: 1.600000000000e+01 Y: 0.000000000000e+00 Z: 0.000000000000e+00 T: 0.000000000000e+00 L: 4.000000000000e+00 : XT2 X: 3.141592653590e+00 Y: 1.600000000000e+01 Z: 0.000000000000e+00 T: 0.000000000000e+00 L: 4.000000000000e+00 : PI X: 5.026548245744e+01 Y: 0.000000000000e+00 Z: 0.000000000000e+00 T: 0.000000000000e+00 L: 3.141592653590e+00 : MUL Quadratic Equation Code: a, b, c = 1, -1, -1 X: -1.000000000000e+00 Y: -1.000000000000e+00 Z: -1.000000000000e+00 T: 1.000000000000e+00 L: 0.000000000000e+00 : DUP X: 1.000000000000e+00 Y: -1.000000000000e+00 Z: -1.000000000000e+00 T: -1.000000000000e+00 L: 0.000000000000e+00 : RUP X: -1.000000000000e+00 Y: -1.000000000000e+00 Z: -1.000000000000e+00 T: -1.000000000000e+00 L: 1.000000000000e+00 : DIV X: -1.000000000000e+00 Y: -1.000000000000e+00 Z: -1.000000000000e+00 T: -1.000000000000e+00 L: 1.000000000000e+00 : RUP X: 1.000000000000e+00 Y: -1.000000000000e+00 Z: -1.000000000000e+00 T: -1.000000000000e+00 L: 1.000000000000e+00 : LASTX X: -1.000000000000e+00 Y: -1.000000000000e+00 Z: -1.000000000000e+00 T: -1.000000000000e+00 L: 1.000000000000e+00 : DIV X: -2.000000000000e+00 Y: -1.000000000000e+00 Z: -1.000000000000e+00 T: -1.000000000000e+00 L: 1.000000000000e+00 : number -2 X: 5.000000000000e-01 Y: -1.000000000000e+00 Z: -1.000000000000e+00 T: -1.000000000000e+00 L: -2.000000000000e+00 : DIV X: 5.000000000000e-01 Y: 5.000000000000e-01 Z: -1.000000000000e+00 T: -1.000000000000e+00 L: -2.000000000000e+00 : DUP X: 5.000000000000e-01 Y: 5.000000000000e-01 Z: 5.000000000000e-01 T: -1.000000000000e+00 L: -2.000000000000e+00 : DUP X: 2.500000000000e-01 Y: 5.000000000000e-01 Z: 5.000000000000e-01 T: -1.000000000000e+00 L: 5.000000000000e-01 : XT2 X: -1.000000000000e+00 Y: 2.500000000000e-01 Z: 5.000000000000e-01 T: 5.000000000000e-01 L: 5.000000000000e-01 : RUP X: 1.250000000000e+00 Y: 5.000000000000e-01 Z: 5.000000000000e-01 T: 5.000000000000e-01 L: -1.000000000000e+00 : SUB X: 1.118033988750e+00 Y: 5.000000000000e-01 Z: 5.000000000000e-01 T: 5.000000000000e-01 L: 1.250000000000e+00 : SQRT X: -6.180339887499e-01 Y: 5.000000000000e-01 Z: 5.000000000000e-01 T: 5.000000000000e-01 L: 1.118033988750e+00 : SUB X: 5.000000000000e-01 Y: -6.180339887499e-01 Z: 5.000000000000e-01 T: 5.000000000000e-01 L: 1.118033988750e+00 : SWAP X: 1.118033988750e+00 Y: 5.000000000000e-01 Z: -6.180339887499e-01 T: 5.000000000000e-01 L: 1.118033988750e+00 : LASTX X: 1.618033988750e+00 Y: -6.180339887499e-01 Z: 5.000000000000e-01 T: 5.000000000000e-01 L: 1.118033988750e+00 : ADD Symbolic Evaluation Import the sympy Library If we want to analyze the symbolic values we import the sympy library: Code: from sympy import ( X: 0 Y: 0 Z: 0 T: 0 L: 0 : CLST Area of a Circle Code: r = symbols("r") X: r**2 Y: 0 Z: 0 T: 0 L: r : XT2 X: pi Y: r**2 Z: 0 T: 0 L: r : PI X: pi*r**2 Y: 0 Z: 0 T: 0 L: pi : MUL Quadratic Equation Code: a, b, c = symbols("a b c") X: c Y: c Z: b T: a L: 0 : DUP X: a Y: c Z: c T: b L: 0 : RUP X: c/a Y: c Z: b T: b L: a : DIV X: b Y: c/a Z: c T: b L: a : RUP X: a Y: b Z: c/a T: c L: a : LASTX X: b/a Y: c/a Z: c T: c L: a : DIV X: -2 Y: b/a Z: c/a T: c L: a : number -2 X: -b/(2*a) Y: c/a Z: c T: c L: -2 : DIV X: -b/(2*a) Y: -b/(2*a) Z: c/a T: c L: -2 : DUP X: -b/(2*a) Y: -b/(2*a) Z: -b/(2*a) T: c/a L: -2 : DUP X: b**2/(4*a**2) Y: -b/(2*a) Z: -b/(2*a) T: c/a L: -b/(2*a) : XT2 X: c/a Y: b**2/(4*a**2) Z: -b/(2*a) T: -b/(2*a) L: -b/(2*a) : RUP X: -c/a + b**2/(4*a**2) Y: -b/(2*a) Z: -b/(2*a) T: -b/(2*a) L: c/a : SUB X: sqrt(-c/a + b**2/(4*a**2)) Y: -b/(2*a) Z: -b/(2*a) T: -b/(2*a) L: -c/a + b**2/(4*a**2) : SQRT X: -sqrt(-c/a + b**2/(4*a**2)) - b/(2*a) Y: -b/(2*a) Z: -b/(2*a) T: -b/(2*a) L: sqrt(-c/a + b**2/(4*a**2)) : SUB X: -b/(2*a) Y: -sqrt(-c/a + b**2/(4*a**2)) - b/(2*a) Z: -b/(2*a) T: -b/(2*a) L: sqrt(-c/a + b**2/(4*a**2)) : SWAP X: sqrt(-c/a + b**2/(4*a**2)) Y: -b/(2*a) Z: -sqrt(-c/a + b**2/(4*a**2)) - b/(2*a) T: -b/(2*a) L: sqrt(-c/a + b**2/(4*a**2)) : LASTX X: sqrt(-c/a + b**2/(4*a**2)) - b/(2*a) Y: -sqrt(-c/a + b**2/(4*a**2)) - b/(2*a) Z: -b/(2*a) T: -b/(2*a) L: sqrt(-c/a + b**2/(4*a**2)) : ADD Ad Hoc Calculations We can also do some ad hoc calculations: Code: x = symbols("x") X: 0 Y: 0 Z: 0 T: 0 L: 0 : CLST X: atan(x) Y: 0 Z: 0 T: 0 L: x : ATAN X: x/sqrt(x**2 + 1) Y: 0 Z: 0 T: 0 L: atan(x) : SIN LaTeX Support These results can be transformed into LaTeX: Code: print(latex(X)) \frac{x}{\sqrt{x^{2} + 1}} \( \frac{x}{\sqrt{x^{2} + 1}} \) If we want all results printed in LaTeX we can modify the fmt function: Code: def fmt(s): Complex Numbers I haven't tried this yet, but we could import the functions from the cmath library. However, the formatting will likely require some adjustments. Caveat
Still, I hope this might be useful. |
|||
04-11-2022, 09:56 AM
Post: #2
|
|||
|
|||
RE: Code Analyzer for HP Calculators
Very nice work!! Your Python code is elegant.
Namir |
|||
04-11-2022, 06:08 PM
Post: #3
|
|||
|
|||
RE: Code Analyzer for HP Calculators
(04-11-2022 09:56 AM)Namir Wrote: Your Python code is elegant. Well I must admit that I cobbled that together in a Jupyter notebook. And that's probably the best environment to run it. The state of the calculator is stored in these global variables, which I usually avoid. So I had to declare them global in each of the functions. But that along with the trace decorator makes them short and hopefully easy to understand. (04-10-2022 07:25 PM)Thomas Klemm Wrote: I haven't tried this yet, but we could import the functions from the cmath library. If we want to analyze complex values we import the cmath library: Code: from cmath import ( The trace function needs minor adjustments: Code: print(( And now we can also solve a quadratic equation with complex solutions: Code: a, b, c = 1, 1, 1 X: 1.000000e+00 Y: 1.000000e+00 Z: 1.000000e+00 T: 1.000000e+00 L: 0.000000e+00 : DUP X: 1.000000e+00 Y: 1.000000e+00 Z: 1.000000e+00 T: 1.000000e+00 L: 0.000000e+00 : RUP X: 1.000000e+00 Y: 1.000000e+00 Z: 1.000000e+00 T: 1.000000e+00 L: 1.000000e+00 : DIV X: 1.000000e+00 Y: 1.000000e+00 Z: 1.000000e+00 T: 1.000000e+00 L: 1.000000e+00 : RUP X: 1.000000e+00 Y: 1.000000e+00 Z: 1.000000e+00 T: 1.000000e+00 L: 1.000000e+00 : LASTX X: 1.000000e+00 Y: 1.000000e+00 Z: 1.000000e+00 T: 1.000000e+00 L: 1.000000e+00 : DIV X: -2.000000e+00 Y: 1.000000e+00 Z: 1.000000e+00 T: 1.000000e+00 L: 1.000000e+00 : number -2 X: -5.000000e-01 Y: 1.000000e+00 Z: 1.000000e+00 T: 1.000000e+00 L: -2.000000e+00 : DIV X: -5.000000e-01 Y: -5.000000e-01 Z: 1.000000e+00 T: 1.000000e+00 L: -2.000000e+00 : DUP X: -5.000000e-01 Y: -5.000000e-01 Z: -5.000000e-01 T: 1.000000e+00 L: -2.000000e+00 : DUP X: 2.500000e-01 Y: -5.000000e-01 Z: -5.000000e-01 T: 1.000000e+00 L: -5.000000e-01 : XT2 X: 1.000000e+00 Y: 2.500000e-01 Z: -5.000000e-01 T: -5.000000e-01 L: -5.000000e-01 : RUP X: -7.500000e-01 Y: -5.000000e-01 Z: -5.000000e-01 T: -5.000000e-01 L: 1.000000e+00 : SUB X: 0.000000e+00+8.660254e-01j Y: -5.000000e-01 Z: -5.000000e-01 T: -5.000000e-01 L: -7.500000e-01 : SQRT X: -5.000000e-01-8.660254e-01j Y: -5.000000e-01 Z: -5.000000e-01 T: -5.000000e-01 L: 0.000000e+00+8.660254e-01j : SUB X: -5.000000e-01 Y: -5.000000e-01-8.660254e-01j Z: -5.000000e-01 T: -5.000000e-01 L: 0.000000e+00+8.660254e-01j : SWAP X: 0.000000e+00+8.660254e-01j Y: -5.000000e-01 Z: -5.000000e-01-8.660254e-01j T: -5.000000e-01 L: 0.000000e+00+8.660254e-01j : LASTX X: -5.000000e-01+8.660254e-01j Y: -5.000000e-01-8.660254e-01j Z: -5.000000e-01 T: -5.000000e-01 L: 0.000000e+00+8.660254e-01j : ADD |
|||
04-11-2022, 10:44 PM
Post: #4
|
|||
|
|||
RE: Code Analyzer for HP Calculators | |||
04-12-2022, 11:27 PM
Post: #5
|
|||
|
|||
RE: Code Analyzer for HP Calculators
Good to know it works for others too.
Thanks for checking that. Meanwhile, I posted a simulator for the HP-80 CPU in this other thread. Actually, I wrote that first and then thought to myself that it also fits for the analysis of programs from HP calculators. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)