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. |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
Code Analyzer for HP Calculators - Thomas Klemm - 04-10-2022 07:25 PM
RE: Code Analyzer for HP Calculators - Namir - 04-11-2022, 09:56 AM
RE: Code Analyzer for HP Calculators - Thomas Klemm - 04-11-2022, 06:08 PM
RE: Code Analyzer for HP Calculators - Sylvain Cote - 04-11-2022, 10:44 PM
RE: Code Analyzer for HP Calculators - Thomas Klemm - 04-12-2022, 11:27 PM
|
User(s) browsing this thread: 1 Guest(s)