The programs listed here provide functions for calculations with
dual numbers, which are similar to complex numbers but instead of the imaginary unit
i they use the infinitesimal unit ε. The programs are mostly simple translations of functions in Thomas Klemm's HP42S program
here and in Ángel Martin’s
Double ROM.
These programs use complex numbers as a convenient stand-in for dual numbers but these numbers are
not actually complex numbers, and mixing the two will most likely yield nonsense results. However, the operations of addition, subtraction and negation
are the same as for complex numbers, and the absolute value of a dual number is just the real part. Thus there are no programs for these functions.
The first listing is a directory of programs providing arithmetic and transcendental functions, as well as the program
DC→R which is a modification of
C→R which accepts either real or complex numbers. This is to allow easy mixing of dual numbers and real numbers, which are interpreted as (x, 0).
Code:
@ Dual number functions.
DIR
DMUL @ Multiply two dual numbers.
\<< DC\->R ROT DC\->R
OVER 5 PICK * 5 ROLLD
ROT ROT * ROT ROT * + R\->C
\>>
DDIV @ Divide two dual numbers.
\<< DC\->R ROT DC\->R
4 PICK * OVER 4 ROLL * -
SWAP 3 PICK / ROT ROT SWAP SQ / R\->C
\>>
DINV @ Inverse (reciprocal) of a dual number.
\<< DC\->R OVER INV ROT ROT
NEG SWAP SQ / R\->C
\>>
DSQ @ Square of a dual number.
\<< DC\->R OVER SQ ROT ROT
* 2 * R\->C
\>>
DSQRT @ Square root of a dual number.
\<< DC\->R SWAP \v/ SWAP OVER 2 * / R\->C
\>>
DLN @ Natural logarithm of a dual number.
\<< DC\->R OVER / SWAP LN SWAP R\->C
\>>
DEXP @ Exponentiation (natural antilog) of a dual number.
\<< DC\->R SWAP EXP SWAP OVER * R\->C
\>>
DIPWR @ Dual number to integer power.
\<< SWAP DC\->R OVER 4 PICK ^
4 ROLLD SWAP 3 PICK 1 - ^ * * R\->C
\>>
DDPWR @ Dual number to dual number power.
\<< DC\->R ROT DC\->R OVER 5 PICK ^ 5 ROLLD
OVER 5 ROLL SWAP OVER 1 - ^ * *
SWAP LN ROT * 3 PICK * + R\->C
\>>
DNROOT @ Nth root of dual number for integer n.
\<< SWAP DC\->R OVER 4 PICK INV ^
4 ROLLD SWAP ABS 3 PICK INV 1 - ^
* SWAP / R\->C
\>>
DC\->R @ Safe C-\>R for either real or dual number.
\<< DUP TYPE 1 SAME
\<< C\->R
\>> 0 IFTE
\>>
END
The next listing is the same directory optimized for the HP 49/50 using newer stack commands and decimal points for real numbers.
Code:
@ Dual number functions.
DIR
DMUL @ Multiply two dual numbers.
\<< DC\->R ROT DC\->R
OVER 5. PICK * 5. ROLLD
UNROT * UNROT * + R\->C
\>>
DDIV @ Divide two dual numbers.
\<< DC\->R ROT DC\->R
4. PICK * OVER 4. ROLL * -
SWAP PICK3 / UNROT SWAP SQ / R\->C
\>>
DINV @ Inverse (reciprocal) of a dual number.
\<< DC\->R OVER INV UNROT
NEG SWAP SQ / R\->C
\>>
DSQ @ Square of a dual number.
\<< DC\->R OVER SQ UNROT
* 2. * R\->C
\>>
DSQRT @ Square root of a dual number.
\<< DC\->R SWAP \v/ SWAP OVER 2. * / R\->C
\>>
DLN @ Natural logarithm of a dual number.
\<< DC\->R OVER / SWAP LN SWAP R\->C
\>>
DEXP @ Exponentiation (natural antilog) of a dual number.
\<< DC\->R SWAP EXP SWAP OVER * R\->C
\>>
DIPWR @ Dual number to integer power.
\<< SWAP DC\->R OVER 4. PICK ^
4. ROLLD SWAP PICK3 1 - ^ * * R\->C
\>>
DDPWR @ Dual number to dual number power.
\<< DC\->R ROT DC\->R OVER 5. PICK ^ 5. ROLLD
OVER 5. ROLL SWAP OVER 1. - ^ * *
SWAP LN ROT * PICK3 * + R\->C
\>>
DNROOT @ Nth root of dual number for integer n.
\<< SWAP DC\->R OVER 4. PICK XROOT
4. ROLLD SWAP ABS PICK3 INV 1. - ^
* SWAP / R\->C
\>>
DC\->R @ Safe C-\>R for either real or dual number.
\<< DUP TYPE 1. SAME
:: C\->R 0. IFTE
\>>
END
Next we have a directory of trig functions including hyperbolic functions, arg and rectangular/polar conversions. They require the command
DC→R from the directory above, so that command should be added to this directory if it is not in your path.
Code:
@ Dual number trig functions. All require DC\->R.
DIR
DSIN
\<< DC\->R OVER COS *
SWAP SIN SWAP R\->C
\>>
DASIN
\<< DC\->R SWAP ASIN
SWAP OVER COS / R\->C
\>>
DCOS
\<< DC\->R OVER SIN NEG *
SWAP COS SWAP R\->C
\>>
DACOS
\<< DC\->R SWAP ACOS
SWAP OVER SIN / R\->C
\>>
DTAN
\<< DC\->R SWAP TAN
SWAP OVER SQ 1 + * R\->C
\>>
DATAN
\<< DC\->R OVER ATAN
ROT ROT SWAP SQ 1 + / R\->C
\>>
DSINH
\<< DC\->R OVER SINH
ROT ROT SWAP COSH * R\->C
\>>
DASNH
\<< DC\->R SWAP ASINH
SWAP OVER COSH / R\->C
\>>
DCOSH
\<< DC\->R OVER COSH
ROT ROT SWAP SINH * R\->C
\>>
DACSH
\<< DC\->R SWAP ACOSH
SWAP OVER SINH / R\->C
\>>
DTANH
\<< DC\->R SWAP TANH
SWAP OVER SQ 1 SWAP - * R\->C
\>>
DATNH
\<< DC\->R OVER ATANH
ROT ROT SWAP SQ 1 SWAP - / R\->C
\>>
DARG @ Argument of dual number.
\<< DC\->R SWAP /
\>>
DR\->P @ Rectangular to polar conversion for dual numbers.
\<< DC\->R OVER / R\->C
\>>
DP\->R @ Polar to Rectangular conversion for dual numbers.
\<< DC\->R OVER SWAP * R\->C
\>>
END
Finally, the same directory optimized for the 49/50.
Code:
@ Dual number trig functions. All require DC\->R.
DIR
DSIN
\<< DC\->R OVER COS *
SWAP SIN SWAP R\->C
\>>
DASIN
\<< DC\->R SWAP ASIN
SWAP OVER COS / R\->C
\>>
DCOS
\<< DC\->R OVER SIN NEG *
SWAP COS SWAP R\->C
\>>
DACOS
\<< DC\->R SWAP ACOS
SWAP OVER SIN / R\->C
\>>
DTAN
\<< DC\->R SWAP TAN
SWAP OVER SQ 1. + * R\->C
\>>
DATAN
\<< DC\->R OVER ATAN
UNROT SWAP SQ 1. + / R\->C
\>>
DSINH
\<< DC\->R OVER SINH
UNROT SWAP COSH * R\->C
\>>
DASNH
\<< DC\->R SWAP ASINH
SWAP OVER COSH / R\->C
\>>
DCOSH
\<< DC\->R OVER COSH
UNROT SWAP SINH * R\->C
\>>
DACSH
\<< DC\->R SWAP ACOSH
SWAP OVER SINH / R\->C
\>>
DTANH
\<< DC\->R SWAP TANH
SWAP OVER SQ 1. SWAP - * R\->C
\>>
DATNH
\<< DC\->R OVER ATANH
UNROT SWAP SQ 1. SWAP - / R\->C
\>>
DARG @ Argument of dual number.
\<< DC\->R SWAP /
\>>
DR\->P @ Rectangular to polar conversion for dual numbers.
\<< DC\->R OVER / R\->C
\>>
DP\->R @ Polar to Rectangular conversion for dual numbers.
\<< DC\->R OVER SWAP * R\->C
\>>
END