; 2026.1105 Mueck_p42.txt
; Program suite for all Mueck_p42 Free42 skins - mick.mueck@mac.com
; This is the same as Mueck_f42.txt except TVM has been removed.

; NOTE: After installing this suite, go into program mode and enter XEQ "LOCK". 
; It'll look like you're adding a line of code, but you're actually performing 
; a write protect function. That will avoid future grief if you accidentally 
; modify or delete any code. The program pointer turns "gray" to indicate the 
; LOCK was successful. The process can similarly be undone with XEQ "UNLOCK".

; To run this skin in Plus42, use the Mueck_p42 program suite instead.
; It's the same thing but without TVM (because Plus42 has its own TVM).

; Usage instructions for .CONST, .POLY and TVM are given in the 
; headers of each of those programs.

; My usage model for local labels (00-99, A-J, a-e) is:
;   NUMERIC 00-99: Can loop to be found.
;   PRIMARY   A-J: Can loop to be found.
;   SECONDARY a-e: Are heavily reused to ONLY work searching downwards. 
; LBLs 05-06 are currently the only remaining ones available!

;--------------------------------------------------------------
; FIX02 Toggles the current display state with FIX 02
; Flags 40-41 store FIX/SCI/ENG/ALL, flags 36-39 store #digits
; FIX 02 defined by: Flags 36-41 CLEAR except for 38 & 40 
;--------------------------------------------------------------
LBL ".FIX02"
FUNC 00
SF 25
FC? 41       ; See if the current display state is FIX 02
FC? 40
GTO a        ; Not FIX 02
FC? 39
FC? 38
GTO a        ; Not FIX 02
FC? 37
FS? 36
GTO a        ; Not FIX 02
RCL ".DISP"  ; Is FIX 02, recall saved display state
FC?C 25
GTO a        ; .DISP is uninitialized, the current state is FIX 02
36.41
STOFLAG      ; Set the previously saved display state 
RTN
LBL a        ; Not FIX 02. Save the display state and set FIX 02
RCLFLAG      ; In case .DISP hasn't been defined yet
STO ".DISP"  ; Save the current display state
FIX 02
RTN

;--------------------------------------------------------------
; X³ → X⁹   Hot-keys ⌥3 → ⌥9   Mimics a built-in function
;--------------------------------------------------------------
LBL ".X↑9"
FUNC 11
9
GTO a

LBL ".X↑8"
FUNC 11
8
GTO a

LBL ".X↑7"
FUNC 11
7
GTO a

LBL ".X↑6"
FUNC 11
6
GTO a

LBL ".X↑5"
FUNC 11
5
GTO a

LBL ".X↑4"
FUNC 11
4
GTO a

LBL ".X↑3"
FUNC 11
3
GTO a

LBL a
SF 25
Y↑X
FS?C 25
RTN
ERRNO
RTNERR IND ST X


;--------------------------------------------------------------
; ³√X → ⁹√X   Hot-keys ⇧⌥3 → ⇧⌥9   Mimics a built-in function
; Yields intuitive results e.g. ³√-27 = -3 vs 3 angle 60°
;--------------------------------------------------------------
LBL ".9√X"
FUNC 11
9
GTO a

LBL ".8√X"
FUNC 11
8
GTO a

LBL ".7√X"
FUNC 11
7
GTO a

LBL ".6√X"
FUNC 11
6
GTO a

LBL ".5√X"
FUNC 11
5
GTO a

LBL ".4√X"
FUNC 11
4
GTO a

LBL ".3√X"
FUNC 11
3 

LBL a
SF 25
X<>Y
SIGN            ; If complex, returns it's unit vector
LASTX
REAL?
ABS             ; Returns the magnitude of a complex number as a real
RCL ST Z
1/X
Y↑X
REAL?
*
FS?C 25
RTN
ERRNO
RTNERR IND ST X

;--------------------------------------------------------------
; E↑iX   Hot-key ⇧⌥e   Mimics a built-in function
;--------------------------------------------------------------
LBL ".E↑iX" 
FUNC 11
SF 25
-1
SQRT
×
E^X
FS?C 25
RTN
ERRNO
RTNERR IND ST X

;--------------------------------------------------------------
; √-1   Hot-keys i and j  (don't use FUNC because it will void LASTx)
;--------------------------------------------------------------
LBL ".i"
CSLD?
CLX
LASTX		; Preserve LASTx
LSTO "X"
CLX
-1		; This way you don't have to worry about the complex MODE setting 
SQRT
STO ST L	; Store answer in LASTX
CLX
RCL "X"
X<> ST L
RTN

;--------------------------------------------------------------
; RSSdB & RDSdB   i.e. dB(√((dB⁻¹Y)² ± (dB⁻¹X)²)
; Hot-keys ⌃⌥v and ⌃⇧⌥v   Mimics a built-in function
;--------------------------------------------------------------
LBL ".RDSdB"
FUNC 21
SF 25
20
/
10^X
X^2
X<>Y
20
/
10^X
X^2
X<>Y
-
SQRT
LOG
20
*
FS?C 25
RTN
ERRNO
RTNERR IND ST X

LBL ".RSSdB"
FUNC 21
SF 25
20
/
10^X
X^2
X<>Y
20
/
10^X
X^2
+
SQRT
LOG
20
*
FS?C 25
RTN
ERRNO
RTNERR IND ST X

;--------------------------------------------------------------
; RSS & RDS i.e. √(Y²±X²)   Hot-keys ⌥v and ⇧⌥v   Mimics a built-in function
;--------------------------------------------------------------
LBL ".RDS"
FUNC 21
SF 25
X^2
X<>Y
X^2
X<>Y
-
SQRT
FS?C 25
RTN
ERRNO
RTNERR IND ST X

LBL ".RSS"
FUNC 21
SF 25
X^2
X<>Y
X^2
+
SQRT
FS?C 25
RTN
ERRNO
RTNERR IND ST X

;--------------------------------------------------------------
; dB & dB⁻¹   Hot-keys ⌥d and ⇧⌥d    Mimics a built-in function
;--------------------------------------------------------------
LBL ".dB-1"
FUNC 11
SF 25
20
/
10^X
FS?C 25
RTN
ERRNO
RTNERR IND ST X

LBL ".dB" 
FUNC 11
SF 25
LOG
20
×
FS?C 25
RTN
ERRNO
RTNERR IND ST X

;--------------------------------------------------------------
; LOG₂ & 2↑X   Hot-keys ⌥b and ⇧⌥b   Mimics a built-in function
;--------------------------------------------------------------
LBL ".2↑X"
FUNC 11
SF 25
2
X<>Y
Y^X
FS?C 25
RTN
ERRNO
RTNERR IND ST X

LBL ".LOG2" 
FUNC 11
SF 25
LOG
2
LOG
/
FS?C 25
RTN
ERRNO
RTNERR IND ST X

;--------------------------------------------------------------
; .CONST 2026.1105 - 4STK and NSTK compliant
; If you're unsure what the symbol on the button is, do ⇧Button
;   to just reveal the description without entering any data.
;   Note: doing that voids any CSLD (Caller Stack Lift Disable)
; To tailor the CONSTANT menu to your own needs:
; - Edit the contents of each menu level as needed.
; - Maintain the systematic labeling used in each level. 
; - Add or delete levels as needed (8 at the most).
; - Edit "NUMBER OF MENU LEVELS USED" (in TWO places)
; Uses LBLs 00-01 and reserves LBLs 10→16 → 80→86
;--------------------------------------------------------------

LBL ".CONST"
XEQ a            ; Initialization
;--------------------------------------------------------------
LBL 00           ; RUN LOOP (ONLY modifies the Alpha-register)
;--------------------------------------------------------------
XASTO ".areg"    ; Save the Alpha-register (menu building will destroy it)
CLMENU
; It can be dangerous to use non-unique LBLs in KEY commands.
; If an error occurs the program pointer can be in a random 
; place and pressing the menu buttons could XEQ an unwanted 
; function. For CONST, there is no danger of errors, so it's OK.
; More free unique LBLs means more available CONST menu levels.
KEY 7 XEQ d      ; Menu UP
KEY 8 XEQ c      ; Menu DOWN
KEY 9 GTO b      ; EXIT
XEQ IND ".conl"  ; Build the currently selected menu level
CLA
ARCL ".areg"     ; Restore the Alpha-register
CLV ".areg"      ; Reduce clutter
MENU1            ; Won't be dismissed when exiting other menus
STOP             ; Menu functions start their XEQs from here
GTO 00
;--------------------------------------------------------------
LBL a            ; INITIALIZATION
FUNC 00
10               ; Menu level must be a multiple of 10
STO ".conl"      ; Set the initial menu level
SF 33            ; For Menu1 operation (not needed, but just in case)
RTN
;--------------------------------------------------------------
LBL b            ; EXIT
CLV ".areg"
CLV ".conl"
CLMENU
EXITALL
STOP
;--------------------------------------------------------------
LBL c            ; MENU DOWN
FUNC 00
10               ; Menu level must be a multiple of 10
STO/ ".conl"
3                ; <~~~~~~ "NUMBER OF MENU LEVELS USED" (Max=8)
X=? ".conl"
STO- ".conl"
ISG ".conl"
NOP
GTO a
;--------------------------------------------------------------
LBL d            ; MENU UP
FUNC 00
10               ; Menu level must be a multiple of 10
STO/ ".conl"
3                ; <~~~~~~ "NUMBER OF MENU LEVELS USED" (Max=8)
DSE ".conl"
GTO a
STO ".conl"
LBL a
DROP
STO* ".conl"
RTN
;--------------------------------------------------------------
LBL 01           ; Show the Alpha-register for a PSE delay time
SF 33            ; Allows menu buttons to be responsive during the delay.
AVIEW
PSE
CLD
CLA
ARCL ".areg"     ; Restore the Alpha-register
RTN
;--------------------------------------------------------------
LBL 10           ; LEVEL 1 menu
"g"
KEY 1 XEQ 11
"G"
KEY 2 XEQ 12
"c"
KEY 3 XEQ 13
"R"
KEY 4 XEQ 14
"Na"
KEY 5 XEQ 15
" "
KEY 6 XEQ 16
RTN

LBL 11           ; Button 1 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Earth Gravity"
├"[LF](m/s^2)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
9.80665
GTO 01           ; View the constant name
LBL 12           ; Button 2 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Gravitational"
├" Constant"
├"[LF](m^3/kg/s^2)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
6.6743E-11
GTO 01           ; View the constant name
LBL 13           ; Button 3 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Speed of Light"
├"[LF](m/s)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
299792458
GTO 01           ; View the constant name
LBL 14           ; Button 4 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Gas Constant"
├"[LF](J/mol/K)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
8.314462618
GTO 01           ; View the constant name
LBL 15           ; Button 5 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Avogadro Number"
├"[LF](/mol)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
6.02214076E23
GTO 01           ; View the constant name
LBL 16           ; Button 6 <----------------------
RTN              ; An unassigned menu button
;--------------------------------------------------------------
LBL 20           ; LEVEL 2 menu
"h"
KEY 1 XEQ 21
"k"
KEY 2 XEQ 22
"q"
KEY 3 XEQ 23
"e0"
KEY 4 XEQ 24
"μ0"
KEY 5 XEQ 25
"Z0"
KEY 6 XEQ 26
RTN

LBL 21           ; Button 1 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Planck Constant"
├"[LF](J/Hz)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
6.62607015E-34
GTO 01           ; View the constant name
LBL 22           ; Button 2 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Boltzmann "
├"Constant"
├"[LF](J/K)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
1.380649ᴇ-23
GTO 01           ; View the constant name
LBL 23           ; Button 3 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Electron Charge"
├"[LF](C)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
1.602176634ᴇ-19
GTO 01           ; View the constant name
LBL 24           ; Button 4 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Permittivity of"
├" Vacuum"
├"[LF](F/m)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
8.8541878128ᴇ-12
GTO 01           ; View the constant name
LBL 25           ; Button 5 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Permeability of"
├" Vacuum"
├"[LF](N/A^2)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
1.25663706212ᴇ-6
GTO 01           ; View the constant name
LBL 26           ; Button 6 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Impedance of"
├" Vacuum"
├"[LF](Ohms)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
376.730313668
GTO 01

;--------------------------------------------------------------
LBL 30           ; LEVEL 3 menu
"er_ox"
KEY 1 XEQ 31
"er_ni"
KEY 2 XEQ 32
"P_Al"
KEY 3 XEQ 33
"P_Cu"
KEY 4 XEQ 34
"µe"
KEY 5 XEQ 35
"µh"
KEY 6 XEQ 36
RTN

LBL 31           ; Button 1 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"SiO2 Rel "
├"Permittivity"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
3.9
GTO 01           ; View the constant name
LBL 32           ; Button 2 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Si3N4 Rel "
├"Permittivity"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
7.5
GTO 01           ; View the constant name
LBL 33           ; Button 3 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Aluminum "
├"Resistivity"
├"[LF](Ohm.m)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
2.65E-8
GTO 01           ; View the constant name
LBL 34           ; Button 4 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Copper "
├"Resistivity"
├"[LF](Ohm.m)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
1.68E-8
GTO 01           ; View the constant name
LBL 35           ; Button 5 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Si Electron "
├"Mobility"
├"[LF](cm^2/V/s)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
1360
GTO 01           ; View the constant name
LBL 36           ; Button 6 <----------------------
XASTO ".areg"    ; Save the Alpha-register
"Si Hole "
├"Mobility"
├"[LF](cm^2/V/s)"
FS? 64           ; See if shift was pressed
GTO 01           ; Yes, only VIEW the constant name
CSLD?            ; MUST use KEYX, not KEYG
CLX
480
GTO 01

;----------------------------------------------------------------------------
; 2026.1105: ".POLY" - Mick Mueck mick.mueck@mac.com
;----------------------------------------------------------------------------
; With thanks to Thomas Sigrist.
; Formula solutions for quadratic, cubic & quartic polynomials
; 4STK and NSTK compliant, also works with complex coefficients.

; MENU Level 1:
; Coefficients entered via the menus are stored in registers named:
;       .X^4, .X^3, .X^2, .X^1, .X^0
;   Note that coefficients can be both real and complex.
; SOLV solves the polynomial implied by the highest order non-zero
;   coefficient. 
; ⇧SOLV resets all coefficients to zero.
; Roots are stored in registers named  ".√4"→".√1"
;   quadratic uses .√2→.√1, cubic uses .√3→.√1, quartic uses .√4→.√1
; Coefficients are NOT reset when you start .POLY in case you need to
;   leave .POLY to calculate the coefficients.
; Any variables not used by a polynomial solution are reset to zero.
; To VIEW a coefficient for ~1 sec without disrupting the stack, ⇧press
;   the coefficient's menu button. You can also do RCL (or STO or VIEW)
;   and select a coefficient from that menu.

; MENU Level 2:
; QUAD, CUBIC, QRTC: A coefficient entry GUI is nice, but I prefer
;   entering them on the stack and then press QUAD, CUBIC, QRTC.
; A program can populate the stack and do XEQ ".QUAD" etc.
;   Because of the 4 level stack limitation, when doing XEQ ".QRTC" you 
;   must modify the coefficients so .X^4=1. That does NOT apply for NSTK.
; When using this stack method, the coefficients are also stored in the
;   .X^n registers so that you can subsequently use the SOLV method on  
;   them if a typo was made with e.g. just one of the coefficients.
; EVAL evaluates the polynomial using the value in the X-register.
;   This can be also used to confirm the validity of a root.
; ⇧EVAL puts all coefficients on the stack. In 4STK, .X^4 is put in LASTX.

; Uses LBLs unused by .CONST & TVM: 02-04, 07-09,17-19,27-29,37-39,95-99


LBL ".POLY"
XEQ c            ; Initialization
;----------------------------------------------------------------------------
LBL 99           ; MAIN RUN LOOP (ONLY modifies the Alpha-register)
;----------------------------------------------------------------------------
XASTO ".areg"    ; Save the Alpha-register (menu building will destroy it)
XEQ d            ; Make RCL menu variable order match TVM's menu
CLMENU
0>? ".plyl"      ; Build the currently selected menu level
GTO a
"X^4"
XEQ e            ; Badge menu label with "." if zero, "!" if illegal
KEY 1 GTO 07     ; "X^4"
"X^3"
XEQ e            ; Badge menu label with "." if zero, "!" if illegal
KEY 2 GTO 08     ; "X^3"
"X^2"
XEQ e            ; Badge menu label with "." if zero, "!" if illegal
KEY 3 GTO 09     ; "X^2"
"X^1"
XEQ e            ; Badge menu label with "." if zero, "!" if illegal
KEY 4 GTO 17     ; "X^1"
"X^0"
XEQ e            ; Badge menu label with "." if zero, "!" if illegal
KEY 5 GTO 18     ; "X^0"
"SOLV"
KEY 6 GTO 19     ; "SOLV"
GTO b

LBL a            ; Create the 2nd Level menu
"QUAD"
KEY 1 GTO 27     ; Quadratic (Stack entry method)
"CUBIC"
KEY 2 GTO 28     ; Cubic     (Stack entry method)
"QRTC"
KEY 3 GTO 29     ; Quartic   (Stack entry method)
" "
KEY 4 GTO 99     ; NOP
" "
KEY 5 GTO 99     ; NOP
"EVAL"
KEY 6 GTO 95     ; Evaluate the polynomial
LBL b
KEY 7 GTO 96     ; Menu UP
KEY 8 GTO 96     ; Menu DOWN
KEY 9 GTO 97     ; EXIT
CLA
ARCL ".areg"     ; Restore the Alpha-register
CLV ".areg"      ; Reduce clutter
MENU1            ; Robust against being dismissed when exiting other menus
STOP             ; Menu functions start their XEQs from this location
GTO 99           ; Ideally this instruction never gets executed due to KEYG
;----------------------------------------------------------------------------
LBL 97           ; EXIT - restore affected original environment
CLV ".areg"
CLV ".mvar"
LCLV ".plyx"
CLV ".plyl"
CLMENU           ; From page 148 of the HP42S manual
EXITALL
;RTN             ; If execution stopped at some random place due to an
                 ; error, this will RTN there and likely yield an error.
STOP             ; May leave debris on the stack, but better than RTN
;----------------------------------------------------------------------------
LBL 96           ; Invert the menu level
XEQ a            ; FUNC will protect the stack & LASTX
GTO 99           ; Back to the Run Loop
LBL a
FUNC 00
-1
STO* ".plyl"
RTN
;----------------------------------------------------------------------------
LBL c            ; POLY INITIALIZATION (modifies X-register so use FUNC 00)
FUNC 00
XASTO ".areg"    ; Save the Alpha-register
1
STO ".plyl"      ; Set the POLY menu level (instead of using a global flag)
RTN
;----------------------------------------------------------------------------
LBL d            ; Delete/recreate X^4 → X^0 in reverse order to match what
;                  you see when you press RCL. Zero them if they don't exist.
FUNC 00
XSTR ".√4"
XEQ a
XSTR ".√3"
XEQ a
XSTR ".√2"
XEQ a
XSTR ".√1"
XEQ a
XSTR ".X^0"
XEQ a
XSTR ".X^1"
XEQ a
XSTR ".X^2"
XEQ a
XSTR ".X^3"
XEQ a
XSTR ".X^4"
XEQ a
CLV ".mvar"
RTN
; CLEAR and RECREATE the variable referenced by X. Default value is in Y.
LBL a
SF 25
RCL IND ST X     ; Make sure it exists
FC?C 25
0                ; default value, Y-register now still holds variable name
CLV IND ST Y
STO IND ST Y
RTN
;----------------------------------------------------------------------------
LBL e            ; Create MENU hints: "!" → Illegal, ":" → Complex, "." → =0
FUNC 00
ASTO ST X
REV
XSTR "."
APPEND
REV
RCL IND ST X     ; RCL variable prefixed by "."
CPX?             ; Ensure X is legal (REAL or COMPLEX)
SKIP
REAL?
GTO a
├" !"
RTN
LBL a
CPX?
├":"             ; Badge with ":" if complex
CPX?
ABS
X=0? 
├"."             ; Badge with "." if zero
RTN
;----------------------------------------------------------------------------
LBL 07           ; X^4
XASTO ".areg"    ; Save the Alpha-register
".X^4"
GTO a
;----------------------------------------------------------------------------
LBL 08           ; X^3
XASTO ".areg"    ; Save the Alpha-register
".X^3"
GTO a
;----------------------------------------------------------------------------
LBL 09           ; X^2
XASTO ".areg"    ; Save the Alpha-register
".X^2"
GTO a
;----------------------------------------------------------------------------
LBL 17           ; X^1
XASTO ".areg"    ; Save the Alpha-register
".X^1"
GTO a
;----------------------------------------------------------------------------
LBL 18           ; X^0
XASTO ".areg"    ; Save the Alpha-register
".X^0"
LBL a
XEQ 39
CLA
ARCL ".areg"     ; Restore the Alpha-register
GTO 99           ; Back to the Run Loop
;----------------------------------------------------------------------------
LBL 39           ; Manage STO & VIEW button actions for X^4 → X^0
;                  This routine does NOT modify the stack or LASTx 
ASTO ".mvar"     ; Store the coefficient name into ".mvar"
FS? 64           ; See if shift was pressed
GTO a            ; Yes, VIEW the variable for 1 sec if it exists
; STORE Result
SF 25            ; Check for trying a STO with an empty NSTK
STO ST X
FC?C 25
GTO b            ; Trying to STO an empty X-register :-/
CPX?             ; Ensure X is legal (REAL or COMPLEX)
SKIP
REAL?
GTO c
├": Invalid Type"
AVIEW            ; View an error message that you need to dismiss
RTN
LBL c            ; Number is either REAL or COMPLEX
STO IND ".mvar"  ; Store the error checked variable
├"= "            ; "=" signifies a STO operation 
ARCL ST X
GTO 01           ; View the Alpha-register during a PSE

LBL a            ; ⇧MENU: VIEW the variable for 1 sec if it exists 
XEQ e            ; Existence check of variable in the Alpha-register 
GTO b            ; Variable doesn't exist, show a stick error message
├": "            ; ":" signifies a VIEW operation
ARCL IND ".mvar"
AVIEW
PSE
CLD
RTN
LBL b
├": Nonexistent"
AVIEW            ; View an error message that you need to dismiss
RTN

LBL e            ; Alpha-register variable name existence test  
FUNC 00
ASTO ST X        ; Store the variable name into X
SF 25
RCL IND ST X 
FC?C 25
RTNYES           ; Does not exist
RTNNO            ; Does exist
;----------------------------------------------------------------------------
LBL 19           ; SOLV
XASTO ".areg"    ; Save the Alpha-register
FC? 64           ; See if shift was pressed
GTO b            ; No, Process the calculations 
XEQ a
GTO 99           ; Back to the Run Loop
LBL a
FUNC 00
0
STO ".X^0"
STO ".X^1"
STO ".X^2"
STO ".X^3"
STO ".X^4"
RTN

;----------------------------------------------------------------------------
LBL b            ; Solves the polynomial, maintains stack & Alpha-register
XEQ a
CLA
ARCL ".areg"     ; Restore the Alpha-register
GTO 99           ; Back to the Run Loop
LBL a
0≠? ".X^4"       ; If the coefficient of x⁴ ≠ 0 go to quartic solver
GTO b
0≠? ".X^3"       ; If the coefficient of x³ ≠ 0 go to cubic solver
GTO c
0≠? ".X^2"       ; If the coefficient of x² ≠ 0 go to quadratic solver
GTO d
0=? ".X^1"       ; If the coefficient of x¹ = 0 the equation is illegal
RTNERR 5         ; Error message Invalid Data
FUNC 01          ; Valid linear equation
RCL ".X^0"
RCL/ ".X^1"
+/-
STO ".√1"
RTN              ; Back to the Run Loop
LBL b            ; Get the stack in order for a .QRTC call
FUNC 04
RCL  ".X^3"
RCL/ ".X^4"
RCL  ".X^2"
RCL/ ".X^4"
RCL  ".X^1"
RCL/ ".X^4"
RCL  ".X^0"
RCL/ ".X^4"
XEQ 04           ; Don't XEQ "QRTC" - it sets ".X^4"=1
RTN              ; Back to the Run Loop
LBL c            ; Get the stack in order for a .CUBIC call
FUNC 03
RCL ".X^3"
RCL ".X^2"
RCL ".X^1"
RCL ".X^0"       ; All menu register coefficients now on the stack
XEQ ".CUBIC"
RTN              ; Back to the Run Loop
LBL d            ; Get the stack in order for a .QUAD call 
FUNC 02
RCL ".X^2"
RCL ".X^1"
RCL ".X^0"       ; All menu register coefficients now on the stack
XEQ ".QUAD"
RTN              ; Back to the Run Loop
;----------------------------------------------------------------------------
LBL 27           ; Quadratic: DROPs 2 elements, returns 2 roots
XASTO ".areg"    ; Save the Alpha-register
XEQ ".QUAD"
GTO a
;----------------------------------------------------------------------------
LBL 28           ; Cubic: DROPs 3 elements, returns 3 roots
XASTO ".areg"    ; Save the Alpha-register
XEQ ".CUBIC"
GTO a
;----------------------------------------------------------------------------
LBL 29           ; Quartic: DROPs 4 elements, returns 4 roots
FS? 64           ; See if shift was pressed
GTO b            ; Yes, fill the stack ready for a 4STK solve 
XASTO ".areg"    ; Save the Alpha-register
XEQ ".QRTC"
LBL a
CLA
ARCL ".areg"     ; Restore the Alpha-register
GTO 99           ; Back to the Run Loop
LBL b
RCL ".X^0"
STO ST L
DROP
RCL ".X^3"
RCL ".X^2"
RCL ".X^1"
RCL ".X^4"
STO/ ST Y
STO/ ST Z
STO/ ST T
STO/ ST L
X<> ST L
RTN
;----------------------------------------------------------------------------
LBL 95           ; Evaluate the polynomial
FS? 64           ; See if shift was pressed
GTO b            ; Yes, put all coefficients onto the stack 
XEQ a
GTO 99           ; Back to the Run Loop
LBL a            ; Plug X into the implied equation and solve
FUNC 11
ENTER
RCL* ST Y
ENTER
RCL* ST Z
ENTER
RCL* ST T        ; X→T: x⁴, x³, x², x
RCL* ".X^4"
X<>Y
RCL* ".X^3"
+
X<>Y
RCL* ".X^2"
+
X<>Y
RCL* ".X^1"
+
RCL+ ".X^0"
XEQ 98           ; Clean up tiny numerical precision errors, turns a+i0 → a
RTN
;-----------
LBL b            ; ⇧EVAL puts all coefficients on the stack
RCL ".X^4"
FC? 80           ; If in 4STK put ".X^4" in L
STO ST L
RCL ".X^3"
RCL ".X^2"
RCL ".X^1"
RCL ".X^0"
GTO 99

;-----------------------------------------------------------------------
; QUARTIC equation solver (can use complex coeffs, 4STK/NSTK compliant)
; 4STK: Solves ax⁴+bx³+cx²+dx+e = 0  via   a + b ↑ c ↑ d ↑ e XEQ ".QRTC" 
; NSTK: Solves ax⁴+bx³+cx²+dx+e = 0  via   a ↑ b ↑ c ↑ d ↑ e XEQ ".QRTC" 
; Note: For 4STK, "a" must be stored in LASTX (hence the "+", or whatever)
; Roots are stored in ".√4" → ".√1" (which can hold complex numbers)
; Coefficients are stored in ".X^4" → ".X^0"
;   This enables checking coefficients that would otherwise be lost and
;   subsequently using the SOLV menu button to repeat the calculation.
; Works with complex coefficients.
; A useful website: https://www.1728.org/quartic2.htm
; Some examples:
; 2x⁴-20x³+70x²-100x+48 = 0
;                    (2, -20, 70, -100, 48)  →  Roots: 1, 2, 3, 4
; x⁴-9x³+45x²-87x+50 = 0
;                         (-9, 45, -87, 50)  →  Roots: 1, 2, 3±i4
; -4x⁴+32x³-168x²+320x-500 = 0
;                 (-4, 32, -168, 320, -500)  →  Roots: 1±i2, 3±i4
; x⁴-2x³+x² = 0            (1, -2, 1, 0, 0)  →  Roots: 1, 1, 0, 0
; x⁴-x³ = 0                (1, -1, 0, 0, 0)  →  Roots: 1, 0, 0, 0
; x⁴+x² = 0                 (1, 0, 1, 0, 0)  →  Roots: 0, 0, 0±i
; x⁴+x = 0                  (1, 0, 0, 1, 0)  →  Roots: 0, -1, 0.5±i√3/2
; x⁴+1 = 0                  (1, 0, 0, 0, 1)  →  Roots: √2±i√2, -√2±i√2
; x⁴ = 0                    (1, 0, 0, 0, 0)  →  Roots: 0, 0, 0, 0
; x⁴-2x²+1 = 0             (1, 0, -2, 0, 1)  →  Roots: 1, 1, -1, -1
; x⁴+2x²+1 = 0              (1, 0, 2, 0, 1)  →  Roots: i, i, -i, -i
; x⁴-4x³+6x²-4x+1 = 0      (1, -4, 6, -4, 1) →  Roots: 1, 1, 1, 1
; x⁴-(7+i13)x³-(45-i67)x²+(191+i21)x-(60+i155) = 0
;   (1, -7-i13, -45+i67, 191+i21, -60-i155)  →  Roots: 1+i2, 3+i4, 2+i3, 1+i4
;-----------------------------------------------------------------------
LBL ".QRTC"
; Populates the ".X^0" → ".X^4" variables (sets ".X^4"=1 in 4STK mode)
; You get an NSTK error if you try to call this with too few arguments!
FC? 80           ; 4STK mode?
GTO a
R↑N 5
0≠? ST X         ; Legal Quartic equation? coefficient of x⁴ is non-zero.
GTO b
R↓N 5            ; Restore stack, LASTX untouched, bail out
RTNERR 5         ; Error message Invalid Data
LBL b
X<> ST L
R↓N 5            ; Save NSTK's LASTX in level 5
LBL a            ; X→T: e, d, c, b  L=a  for both 4STK and NSTK
0=? ST L         ; Illegal Quartic equation: coefficient of x⁴ is zero.
RTNERR 5         ; Error message Invalid Data
STO ".X^0"       ; X→T: e, d, c, b  L=a
R↓N 4
STO ".X^1"
R↓N 4
STO ".X^2"
R↓N 4
STO ".X^3"
R↓N 4
X<> ST L
STO ".X^4"       ; X→T: a, d, c, b  L=e, all coefficients stored
LSTO ".plya"
X<> ST L         ; 4STK stack/LASTX restored ready for FUNC
FC? 80           ; 4STK mode?
GTO c
R↑N 5
X<> ST L
R↓N 5
LBL c            ; Both 4STK and NSTK environments restored ready for FUNC
FUNC 44          ; Now prepare the stack assuming the coefficient of x⁴ is 1
X<> ".plya"      ; e is now in ".plya", LASTX=0 inside FUNC
STO/ ST T
STO/ ST Z
STO/ ST Y
STO/ ".plya"
X<> ".plya"
LCLV ".plya"
XEQ 04
RTN
;-----------------------------------------------------------------------
; BEWARE: This requires the coefficient of x⁴ be 1. DO NOT access ".X^n"
LBL 04           ; QRTC entry point (".X^0" → ".X^4" already populated)
0=? ".X^4"       ; Illegal Quartic equation: coefficient of x⁴ is zero.
RTNERR 5         ; Error message Invalid Data
FUNC 44          ; X→T: d, c, b, a
; Filter out the trivial case of 1 zero root leaving just a CUBIC to do
; This also avoids some significant accumulating numerical precision errors.
0≠? ST X         ; Note: If .X4=i and .X0=0 then .X0/.X4 = 0+i0 :-/ 
GTO a            ; X→T: d, c, b, a    Do the full quartic solve
RE               ; Turns 0+i0 → 0
STO ".√4"        ; X→T: 0, c, b, a
CLX
1
R↓N 4            ; X→T: c, b, a, 1
XEQ 03           ; Solve the CUBIC equation
RTN
LBL a            ; X→T: d, c, b, a   Do the full quartic solve
LSTO "d"
R↓N 4
LSTO "c"
R↓N 4
LSTO "b"
R↓N 4
LSTO "a"         ; X→T: a, d, c, b 
2
/
ENTER
X^2
-1.5
RCL* ST Y        ; X→T: -3/2*(a/2)², (a/2)², a/2
RCL+ "b"         ; X→T: b-3/2*(a/2)², (a/2)², a/2
STO ".√1"        ; Temporary storage for "f"
DROP
RCL- "b"
RCL* ST Y
RCL+ "c"         ; X→T: c+a/2*((a/2)²-b), a/2
STO ".√4"        ; Temporary storage for "g"
X<>Y
ENTER
X^2
ENTER
X^2
-0.1875          ; X→T: -3/16, (a/2)⁴, (a/2)², a/2
*
RCL+ "d"
RCL "c"
RCL* ST T        ; X→T: c*a/2, d-3/16*(a/2)⁴, (a/2)², a/2
2
/
-                ; X→T: d-3/16*(a/2)⁴-c*a/4, (a/2)², a/2
X<>Y
RCL* "b"
4
/
+                ; X→T: d-3/16*(a/2)⁴-c*a/4+b/4*(a/2)²   This is h
RCL ".√1"        ; Temporary storage for "f",  Build CUBIC coefficients
2
/
ENTER
X^2
RCL- ST Z        ; X→T: (f/2)²-h, f/2, h
4
/
RCL ".√4"        ; Temporary storage for "g"
X^2
-64
/                ; X→T: -g²/64, ((f/2)²-h)/4, f/2, h
1
R↓N 4            ; X→T: 1, f/2, ((f/2)²-h)/4, -g²/64
XEQ 03           ; Solve the CUBIC equation
; The order of the roots is now important!
; A zero root must be in Z (X & Y shouldn't be zero). Next,
; For one real and 2 complex roots, the real root must be in Z.
0=? ST X         ; If a root is 0 put it in Z
GTO a
X<> ST Z
0=? ST X
GTO a
X<>Y
0=? ST X
GTO a
REAL?            ; If a root is real put it in Z
GTO a
X<> ST Z
REAL?
GTO a
X<>Y
LBL a
R↓N 3
SQRT             ; p
X<>Y
SQRT             ; q
RCL ".√4"        ; Temporary storage for "g"
0=? ST X
GTO a            ; g=0 
-8
/                ; X→T: g/8, q, p
RCL ST Y
RCL* ST T        ; X→T: p*q, g/8, q, p
0≠? ST X
/
LBL a
-0.25
RCL* "a"         ; X→T: -s, r, q, p
STO ".√1"
STO ".√2"
STO ".√3"
STO ".√4"
DROP
STO+ ".√1"
STO- ".√2"
STO- ".√3"
STO+ ".√4"
DROP
STO+ ".√1"
STO- ".√2"
STO+ ".√3"
STO- ".√4"
DROP
STO+ ".√1"       ; root1 =  p + q + r - s
STO+ ".√2"       ; root1 =  p - q - r - s
STO- ".√3"       ; root1 = -p + q - r - s
STO- ".√4"       ; root1 = -p - q + r - s
RCL ".√4"
XEQ 98           ; Clean up tiny numerical precision errors, turns a+i0 → a
STO ".√4"
RCL ".√3"
XEQ 98           ; Clean up tiny numerical precision errors, turns a+i0 → a
STO ".√3"
RCL ".√2"
XEQ 98           ; Clean up tiny numerical precision errors, turns a+i0 → a
STO ".√2"
RCL ".√1"
XEQ 98           ; Clean up tiny numerical precision errors, turns a+i0 → a
STO ".√1"
RTN

;-----------------------------------------------------------------------
; CUBIC equation solver (can use complex coeffs, 4STK/NSTK compliant)
; Solves ax³+bx²+cx+d = 0   via   a ↑ b ↑ c ↑ d  XEQ ".CUBIC"
; Drops the stack 4 levels and then pushes the 3 roots onto the stack
; Roots are stored in ".√3" → ".√1" (which can hold complex numbers)
; 1st cubic root → X=".√3" and the quadratic roots → Y=".√2" & Z=".√1". 
; Coefficients are stored in ".X^3" → ".X^0"
;   This enables checking coefficients that would otherwise be lost and
;   subsequently using the SOLV menu button to repeat the calculation.
; A useful website: https://www.1728.org/cubic2.htm
; I used the following website to code up this formula:
; math.stackexchange.com/questions/15865/why-not-write-the-solutions-of-a-cubic-this-way/18873#18873
; Some examples:
;  2x³-12x²+22x-12 = 0       (2, -12, 22, -12)  →  Roots: 1, 2, 3
;   x³+6x²+12x+8 = 0         (1, 6, 12, 8)      →  Roots: -2, -2, -2
;   x³+2x²-x-2 = 0           (1, 2, -1, -2)     →  Roots: 1, -1, -2
; -2x³+10x²-22x+30 = 0       (-2, 10, -22, 30)  →  Roots: 1±i2, 3
; x³-(6+i9)x²-(15-i34)x+(40-i5) = 0
;                   (1, -6-i9, -15+i34, 40-i5)  →  Roots: 1+i2, 3+i4, 2+i3
;-----------------------------------------------------------------------
LBL ".CUBIC"
; Populates the ".X^0" → ".X^3" variables (sets ".X^4" = ".√4" = 0)
; You get an NSTK error if you try to call this with too few arguments!
0=? ST T         ; Illegal Cubic equation: coefficient of x³ is zero.
RTNERR 5         ; Error message Invalid Data
FUNC 43          ; X→T: d, c, b, a
STO ".X^0"
R↓N 4            ; X→T: c, b, a, d
STO ".X^1"
R↓N 4            ; X→T: b, a, d, c
STO ".X^2"
R↓N 4            ; X→T: a, d, c, b
STO ".X^3"
R↓N 4            ; X→T: d, c, b, a
CLX              ; Reset variables not pertaining to a cubic solution
0                ; It's an NSTK thing...
STO ".X^4"
STO ".√4"
CLX
RCL ".X^0"       ; End of coefficient storage, stack restored
XEQ 03
RTN
;-----------------------------------------------------------------------
LBL 03           ; CUBIC entry point (".X^0" → ".X^4" already populated)
0=? ST T         ; Illegal Cubic equation: coefficient of x³ is zero.
RTNERR 5         ; Error message Invalid Data
FUNC 43          ; X→T: d, c, b, a
RCL/ ST T        ; X→T: d/a, c, b, a
X<> ST Z
RCL/ ST T        ; X→T: b/a, c, d/a, a
X<>Y
RCL/ ST T        ; X→T: c/a, b/a, d/a, a
3
STO/ ST Y
STO/ ST Z        ; X→T: 3, c/3a, b/3a, d/a
R↓N 4
X<>Y
+/-              ; X→T: -b/3a, c/3a, d/a, 3
STO ".√1"        ; Temporary storage: -b/3a
X^2
RCL- ST Y
LSTO ".√2"       ; X→T: (b/3a)²-c/3a, c/3a, d/a, 3  (Temporary storage: B's numerator)
-0.5
RCL* ST Z
+                ; X→T: (b/3a)²-1.5c/3a, c/3a, d/a
RCL* ".√1"
+/-              ; X→T: b/3a*((b/3a)²-1.5*c/3a), c/3a, d/a
0.5
RCL* ST T
+                ; Final "g"   X→T: b/3a*((b/3a)²-1.5*c/3a)+d/2a, c/3a, d/a
XEQ 98           ; Clean up tiny numerical precision errors, turns a+i0 → a
LASTX
X^2
+/-              ; -(d/2)², g, c/3a, d/a
RCL ST T
RCL* ST Z
+                ; X→T: g*d/a-(d/2a)², g, c/3a
-0.75
RCL* ".√1"
RCL* ".√1"       ; X→T: -0.75*(b/3a)², g*d/a-(d/2a)², g, c/3a
RCL ST T
+
LASTX
X^2              ; X→T: (c/3a)², c/3a-0.75*(b/3a)², g*d/a-(d/2a)², g
*
+                ; X→T: (c/3a)²(c/3a-0.75*(b/3a)²)+g*d/a-(d/2a)², g
SQRT             ; This is the determinant - the cube root term can't = 0
XEQ 98           ; Clean up tiny numerical precision errors, turns a+i0 → a
X=Y?             ; Select the other root if A will become 0
+/-
RCL- ST Y
3
1/X
Y^X              ; A is now complete
0≠? ST X
STO/ ".√2"       ; B is now complete
0=? ST X
STO ".√2"
; X-register holds A, the value of B in ".√2" is now final
RCL ".√1"        ; X→T: -b/3a, A
STO ".√3"
X<> ".√2"        ; X→T: B, A   .√1→3 now hold -b/3a
STO+ ".√1"
X<>Y
STO+ ".√1"       ; X→T: A, B    ".√1" = -b/3a+A+B is the final value
-0.5
0.75
SQRT
RCOMPLX          ; X→T: (-1+i√3)/2, A, B
RCL* ST Y
STO+ ".√3"
LASTX
RCL* ST T
STO+ ".√2"
DROPN 2
LASTX
CONJ
RCL* ST Y
STO+ ".√2"       ; ".√2" = -b/3a+A*(-1-i√3)/2+B*(-1+i√3)/2 is the final value
LASTX
RCL* ST T
STO+ ".√3"       ; ".√3" = -b/3a+A*(-1+i√3)/2+B*(-1-i√3)/2 is the final value
RCL ".√1"
XEQ 98           ; Clean up tiny numerical precision errors, turns a+i0 → a
STO ".√1"
RCL ".√2"
XEQ 98           ; Clean up tiny numerical precision errors, turns a+i0 → a
STO ".√2"
RCL ".√3"
XEQ 98           ; Clean up tiny numerical precision errors, turns a+i0 → a
STO ".√3"
RTN

;-----------------------------------------------------------------------
; QUADRATIC equation solver (can use complex coeffs, 4STK/NSTK compliant)
; Solves ax²+bx+c = 0   via   a ↑ b ↑ c XEQ ".QUAD"
; Drops the stack 3 levels and then pushes the 2 roots onto the stack
; Roots are stored in ".√2" → ".√1" (which can hold complex numbers)
; Coefficients are stored in ".X^2" → ".X^0" with ".X^3"=0 
;   This enables checking coefficients that would otherwise be lost and
;   subsequently using the SOLV menu button to repeat the calculation.
; Works with complex coefficients.
; Some examples:
;  2x²-6x+4 = 0             (2, -6, 4)          →  Roots: 1, 2
; -2x²+4x-10 = 0            (-2, 4, -10)        →  Roots: 1±i2
; x²-(4+i6)x-(5-i10) = 0    (1, -4-i6, -5+i10)  →  Roots: 1+i2, 3+i4
;-----------------------------------------------------------------------
LBL ".QUAD"
0=? ST Z         ; Illegal Quadratic equation: coefficient of x² is zero.
RTNERR 5         ; Error message Invalid Data
FUNC 32 
; You get an NSTK error if you try to call this with too few arguments!
0                ; Reset variables not pertaining to a quadratic solution
STO ".X^4"
STO ".X^3"
STO ".√4"
STO ".√3"
DROP             ; X→T: c, b, a, ?
STO ".X^0"
R↓N 3            ; X→T: b, a, c, ?
STO ".X^1"
R↓N 3            ; X→T: a, c, b, ?
STO ".X^2"
R↓N 3            ; X→T: c, b, a, ?
                 ; End of coefficient storage, stack restored
XEQ 02
RTN
;-----------------------------------------------------------------------
LBL 02           ; QUAD entry point (".X^0" → ".X^4" already populated)
0=? ST Z         ; Illegal Quadratic equation: coefficient of x² is zero.
RTNERR 5         ; Error message Invalid Data
FUNC 32          ; X→T: c, b, a
X<> ST Z
STO/ ST Z
STO+ ST X
/
+/-
X^2
LASTX
X<> Z
-                ; X = (-b/2a)² - c/a, Y = -b/2a
SQRT             ; can be complex
ENTER
+/-
RCL ST Z
STO+ ST Z
+                ; The solve is complete, now just store the results
XEQ 98           ; Clean up tiny numerical precision errors, turns a+i0 → a
STO ".√2"        ; 2nd root (-b - √determinant)/2a
X<>Y
XEQ 98           ; Clean up tiny numerical precision errors, turns a+i0 → a
STO ".√1"        ; 1st root (-b + √determinant)/2a
RTN

;-----------------------------------------------------------------------
LBL 98           ; Zeros out numbers very close to zero. Turns N+i0 into N
LSTO ".plyx"     ; Holds the original X-register
X<> ST L         ; Preserves original LASTX 
FUNC 11          ; NOTE: This puts X into LASTX when it returns 
1E-30
X<> ".plyx"      ; Restores X, saves the clipping threshold
ENTER
IM               ; Imaginary part
XEQ a
X<>Y
RE               ; Real part
XEQ a
LCLV ".plyx"
0=? ST Y
RTN 
X<>Y
RCOMPLX
RTN
LBL a
ABS
X≤? ".plyx"
0
X>? ".plyx"
LASTX
X<>Y
DROP
RTN
END