UP | HOME

free42 Random Math Stuff

Author: Mitch Richling
Updated: 2024-02-12 12:55:50

Copyright 2024 Mitch Richling. All rights reserved.

Table of Contents

1. Metadata

The home for this HTML file is: https://richmit.github.io/hp42/math.html

A PDF version of this file may be found here: https://richmit.github.io/hp42/math.pdf

Files related to this document may be found on github: https://github.com/richmit/hp42

Directory contents:

src - The org-mode file that generated this HTML document
src_42s - Ready to convert source listings for 42s code in this document
docs - This html document and associated PDF
bin - Importable RAW program files

2. Introduction

This org-mode file collects together a handful of mathematical stuff I find useful. Note that in the past I had a collection of simple mathematical functions in this file. That stuff has moved to sfun.html.

3. NLA: Linear Algebra

Menu Lab   Inputs Output
IDEN MXIDN Create an nxn identity matrix X: N X: identity matrix
DIAG MXDIAG Create diagonal matrix with given elements X: VEC X: diagonal matrix
TR MXTR Compute the trace of a matrix X: M X: trace
CPLY MXCPLY Compute matrix Characteristic polynomial X: M X: polynomial
▒▒▒▒        
EDIT LBL 96      
MAT LBL 98 Store/Recall Current CPOLY matrix    
X LBL 97 Store/Recall current value of "X"    
▒▒▒▒        
▒▒▒▒        
▒▒▒▒        
EVCP EQCPLY Evaluate Characteristic polynomial    

3.2. Notes on individual programs

3.2.1. MXCPLY: Characteristic polynomial

MXCPLY uses the Faddeev–LeVerrier algorithm to compute the characteristic polynomial of a matrix. The polynomial is a matrix of coefficients suitable for use by the polynomials tools found later on this page.

One can find the eigenvalues of a matrix by using PR1ST & PRNXT to solve the characteristic polynomial.

3.2.2. EQCPLY: Characteristic polynomial as an MVAR program

The EQCPLY function is an MVAR function that directly computes values of the characteristic polynomial. It is horribly inefficient, but it can be used by the built in solver to find real eigenvalues or to plot the characteristic polynomial (See: pgmforfun.html).

3.3. Code for Menu

(MJR-generate-42-menu-code "NLA" 0 tbl 0 1 'stay 'up 'auto #'MJR-custom-gen-lab #'MJR-custom-gen-sub)

3.4. Functions

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (MXCPLY)
@@@@ DSC: Compute matrix Characteristic polynomial
@@@@ IN:  X: Matrix
@@@@ OUT: X: Characteristic polynomial
@@@@ LBL: 28
@@@@ FAQ: Uses INDEX
@@@@ UPD: 2021-04-27
@@@@ TC:  [[1,2,3][4,5,6][7,8,10]] => [1, -16, -12, 3]
LBL "MXCPLY"
FUNC 11         @@## REQ:free42>=2.5.24 
L4STK           @@## REQ:free42>=3.0    
LSTO "_A"
DIM?
XEQ "MXIDN"
LSTO "_M"
LSTO "_I"
R↓
1
+
1
X<>Y
NEWMAT
LSTO "_P"
INDEX "_P"
R↓
-1
STOEL
J+
+/-
LSTO "_CTR"
0               @@@@ p_{n-1}
LBL 28
RCL "_A"        @@@@ A               p_{n-1}
RCL "_M"        @@@@ M               A        p_{n-1}
RCL "_I"        @@@@ I               M        A         p_{n-1}
RCL× ST T       @@@@ I*p_{n-1}       M        A         p_{n-1}
-               @@@@ M-I*p_{n-1}     A        p_{n-1}
×               @@@@ A*(M-I*p_{n-1}) p_{n-1}
LSTO "_M"
XEQ "MXTR"      @@@@ A*(M-I*p_{n-1}) p_{n-1}
RCL "_CTR"
÷               @@@@ p_n             p_{n-1}
STOEL
ISG "_CTR"
NOP
J+
FC? 77
GTO 28
RCL "_P"
+/-
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (MXTR)
@@@@ DSC: Compute matrix trace (sum of the diagonal elements)
@@@@ IN:  X: Matrix
@@@@ OUT: X: trace
@@@@ FAQ: Dosen't use INDEX
@@@@ UPD: 2021-04-27
@@@@ TC:  [[1,2,3][4,5,6][7,8,10]] => 16
LBL "MXTR"
FUNC 11         @@## REQ:free42>=2.5.24 
L4STK           @@## REQ:free42>=3.0    
LSTO "_M"       @@@@ M           -- M is an nxn matrix
DIM?            @@@@ n n M
1               @@@@ 1 n n M
+               @@@@ 1+n n M
DIM "_M"        @@@@ 1+n n M     -- M is now an nx(n+1) matrix with original diag in first column
1               @@@@ 1 1+n n M
1               @@@@ 1 1 1+n n M
NEWMAT          @@@@ P 1+n n M   -- P is a 1x1 zero matrix
SIGN            @@@@ P 1+n n M   -- P is a 1x1 identity matrix
LSTO "_P"
R↓              @@@@ 1+n n M     -- P is a 1x1 matrix e_1
1               @@@@ 1   1+n n   -- P is a 1x1 matrix e_1
X<>Y            @@@@ 1+n 1   n   -- P is a 1x1 matrix e_1
DIM "_P"        @@@@ 1+n 1       -- P is now an 1x(1+n) e_1 row matrix
RCL "_P"        @@@@ P   1+n 1
RCL "_M"        @@@@ M   P   1+n 
TRANS           @@@@ X   P   1+n -- X is now an (n+1)xn matrix with original diag in first row
×               @@@@ X   1+n 1   -- X is now an 1xn matrix with original diag in first row
RSUM            @@@@ X   1+n 1   -- X is now a 1x1 matrix with the sum of the diag
DET             @@@@ TR          -- DET of a 1x1 matrix is matrix element
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (MXIDN)
@@@@ DSC: Create an XxX identity matrix
@@@@ IN:  X: Size of matrix to make
@@@@ OUT: X: Identity matrix of size X
@@@@ FAQ: Dosen't use INDEX
@@@@ UPD: 2021-04-27
@@@@ REF: https://forum.swissmicros.com/viewtopic.php?f=19&t=2958
@@@@ FAQ: This code is longer, but easier to understand -- for me anyhow.
LBL "MXIDN"
FUNC 11         @@## REQ:free42>=2.5.24 
L4STK           @@## REQ:free42>=3.0    
1
NEWMAT          @@@@ X is an nx1 zero matrix 
SIGN            @@@@ X is now a constant matrix filled with 1s
XEQ "MXDIAG"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (MXDIAG)
@@@@ DSC: Create diagonal matrix with given elements
@@@@ IN:  X: matrix
@@@@ IN:  X: diagonal matrix
@@@@ FAQ: Dosen't use INDEX
@@@@ FAQ: Uses all elements of X -- even if it is not 1xn or nx1
@@@@ UPD: 2021-04-27
@@@@ REF: https://forum.swissmicros.com/viewtopic.php?f=19&t=2958
@@@@ FAQ: This code is longer, but easier to understand -- for me anyhow.
LBL "MXDIAG"
FUNC 11         @@## REQ:free42>=2.5.24 
L4STK           @@## REQ:free42>=3.0    
LSTO "_M"       @@@@ D
DIM?            @@@@ n m
×               @@@@ N
1               @@@@ 1 N
X=Y?
GTO 23
                @@@@ non 1x1 case
RCL+ ST Y       @@@@ N+1 N
X<>Y            @@@@ N N+1
DIM "_M"        @@@@ N N+1      -- M is now an (N+1)xN matrix with D on first row
RCL "_M"        @@@@ M N N+1
TRANS           @@@@ M N N+1    -- M is now an Nx(N+1) matrix with D on first column
STO "_M"
R↓              @@@@ 1 N+1  N
ENTER
DIM "_M"        @@@@ 1 N+1  N  -- M is now an NxN matrix with D on the diagonal
LBL 23          @@@@ 1 N+1  N  -- due to the resize reshuffle
                @@@@ All done.  Return
RCL "_M"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (EQCPLY)
@@@@ DSC: Evaluate Chararstic Polynomial of a Matrix: DET(M-X*I)
@@@@ I/O: N/A MVAR program
@@@@ VAR: CPM a square matrix
@@@@ VAR: X a real or complex number
@@@@ LAB: 24-25
@@@@ FAQ: Can be used 
@@@@ FAQ: Dosen't use INDEX
@@@@ UPD: 2021-04-27
LBL "EQCPLY"
MVAR "CPM"
MVAR "X"
RCL "CPM"
RCL "X"
RCL "CPM"
DIM?
R↓
XEQ "MXIDN"
×
-
DET
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Store/Recall variable "CPM"
LBL 98 
FS? 64
RCL "CPM"
STO "CPM"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Store/Recall variable "X"
LBL 97
FS? 64
RCL "X"
STO "X"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Edit matrix
LBL 96
FUNC 11
EDIT
"Enter data; R/S"
" to end"
PROMPT
EXITALL
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
END

4. POLY: A collection of polynomial tools

Menu LBL Description Inputs Output
NEW NEWPLY Create a polynomial of degree X X: N X: P
INTRP PINTRP Create interpolateing polynomial Y: XDAT X: YDAT X: P
▒▒▒▒        
▒▒▒▒        
EDIT LBL 78 Edit a polynomial X: P X:P
VIEW VPOLY View a polynomial X: P N/A
SLV2 P2SLV Solve quadratic polynomial X: P Y: root_1 X: root_2
SLV1 P1SLV Solve linear polynomial X: P X: root
R1ST PR1ST Find a root X: P Z: OPoly Y: DPoly X: root
RNXT PRNXT Find next root Z: OPoly Y: DPoly X: GUESS Z: OPoly Y: DPoly X: root
▒▒▒▒        
VIEW VPOLY View the coeffients of a polynomial X: P N/A
DFALT PDEFLT Deflate polynomial Y: P X: R Y: Remainder X: P/(X-R)
EVAL PEVAL Evaluate polynomial P @ X Y: P X: X X: P(X)
EVAL1 PEVL1 Evaluate polynomial P & P' @ X Y: P X: X Y: P(X) X: P'(x)
EVAL2 PEVL2 Evaluate polynomial P, P', & P'' @ X Y: P X: X Z: P(X) Y: P''(x) Y: P'(x)
LGRR PLGRR Root search (Laguerre's Method) T: P Z: GUESS Y: ACC X: MAXITR Z: Status y: P_Val X: root
VIEW VPOLY View the coeffients of a polynomial X: P N/A
POLY LBL 98 Store/Recall Current Polynomial    
X LBL 97 Store/Recall current value of "X"    
▒▒▒▒        
▒▒▒▒        
▒▒▒▒        
EVAL PWRP Evaluate wrapped polynomial at X N/A X: P("X")

A polynomial is represented as 1xn matrix of coefficients. The first element of the matrix is the coefficient on the highest degree.

4.2. Notes for individual programs

4.2.1. PEVAL, PEVL1, & PEVL2: Evaluating Polynomials

These functions efficiently evaluate a polynomial (and its first and/or second derivative). They are handy for simply evaluating a polynomial repeatedly; however, they are more tuned for use as subroutines in other programs – ex: solvers. Note that the last page of the main menu provides a more efficient way to repeatedly evaluate a polynomial.

4.2.2. PWRP: Wrapping a polynomial matrix in an MVAR function

Simply store the polynomial into the global variable "WRPP", and then feed PWRP to things like the built in solver/integrator and similar tools (See: pgmforfun.html).

4.2.3. PR1ST & PRNXT: Finding the roots of a polynomial

These two programs provide a way to find all the roots of a polynomial. They work on real or complex polynomials, and finds both real and complex roots.

These functions use the global variable ACC to specify how close to zero the polynomial must be to accept a root. If ACC is not set, then 1e-15 is used.

The first function, PR1ST, is used to find an initial root of a polynomial. It only takes a polynomial. It will almost always find a root; however, it is possible for it to fail and return an error. When it fails, I suggest running the function again to see if it will find a root – it uses a random initial guess each time it runs. When it finds a root, it returns the original polynomial, the polynomial with the located root removed (deflated), and a root. This output is precisely what is needed to find more roots.

The second function, PRNXT, finds the next root of the polynomial. It requires three arguments (original polynomial, deflated polynomial, and a guess). This is precisely what the PR1ST function returns. PRNXT also returns the original polynomial, the polynomial with the located root removed, and a new root. So you can feed PRNXT the return of PR1ST or PRNXT.

A common question: Why is the original polynomial required by PRNXT, and not just the deflated one? A series of polynomial deflations leads to a deflated polynomial with some round off error. So wen PRNXT finds a root of the deflated polynomial, it then uses that root as the initial guess to PLGRR on the original polynomial. This significantly reduces round-off error, and almost always works – it is possible that it may converge to a root we already found. Currently PRNXT dosen't check for this case – that is on my todo list.

In summary, to find all the roots of a polynomial: Put the polynomial on the stack, and press PR1ST to get the first root. Then hit PRNXT until you have found all the roots.

Alternately, with a bit more round off error, you can just repeatedly use PR1ST on the deflated polynomial that PR1ST returns.

4.2.4. PLGRR: Search for a polynomial root

This is designed to be used by other programs. It takes a polynomial, a guess, a tolerance, and a maximum number of iterations. If the tolerance is negative, then the function will always preform the maximum number of iterations. This is useful for "refining" a root.

4.3. Code for Menu

(MJR-generate-42-menu-code "POLY" 0 tbl 0 1 'stay 'up 'auto #'MJR-custom-gen-lab #'MJR-custom-gen-sub)

4.4. Local functions

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (PINTRP)
@@@@ DSC: Create an interpolateing polynomial
@@@@ IN:  Y: X data matrix
@@@@      X: Y data matrix
@@@@ OUT: X: interpolateing polynomial
@@@@ TST: free42_3.0.2.2
@@@@ UPD: 2021-04-28
@@@@ FAQ: X & Y must have the same number of elements, but dimintions may differ.
@@@@ FAQ: Explicitly constructs the vandermonde matrix, and solves the system
@@@@ FAQ: Uses INDEX
@@@@ TC:  xdat:[ 1, 2, 3, 4] ydat:[1, -1, 1, -1] => [-4/3 10 -68/3 15] = [-1.33.. 10 -22.66.. 15]
@@@@ TC:  xdat:[-1, 0, 1, 2] ydat:[-2, 3, -24, -77] => [1, -16, -12, 3]  
LBL "PINTRP"
FUNC 21         @@## REQ:free42>=2.5.24 
L4STK           @@## REQ:free42>=3.0    
LSTO "_YDAT"    @@@@ YDAT XDAT
DIM?
×
1
DIM "_YDAT"     @@@@ 1 N XDAT -- YDAT is now an Nx1 matrix
R↓              @@@@ N XDAT
R↓              @@@@ XDAT
LSTO "_XDAT"    @@@@ XDAT
XEQ "MXDIAG"    @@@@ MUL      -- nxn diag matrix
LSTO "_MUL"     @@@@ MUL
DIM?            @@@@ N N
R↓
1               @@@@ 1 N
NEWMAT          @@@@ TPL      -- TPL is an nx1 zero matrix
SIGN            @@@@ TPL      -- TPL is now an NX1 1 matrix
LSTO "_TPL"     @@@@ TPL
DIM?            @@@@ 1 N
R↓              @@@@ N
ENTER           @@@@ N N
NEWMAT          @@@@ VM       -- VM is an NXN zero matrix
LSTO "_VM"      @@@@ VM
DIM?            @@@@ N N
R↓              @@@@ N
INDEX "_VM"
1               @@@@ 1 N
X<>Y            @@@@ N 1
STOIJ
LBL 79
RCL "_TPL"
PUTM
RCL "_MUL"
X<>Y
×
STO "_TPL"
J-
FC? 77
GTO 79
RCL "_YDAT"     @@@@ YDAT
RCL÷ "_VM"      @@@@ POLY
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (NEWPLY)
@@@@ DSC: Create a new polynomial of degree X
@@@@ IN:  X: degree
@@@@ OUT: X: polynomial
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-26
LBL "NEWPLY"
FUNC 11         @@## REQ:free42>=2.5.24 
L4STK           @@## REQ:free42>=3.0    
1
X<>Y
1
+
NEWMAT
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Edit a polynomial in the matrix editor
@@@@ IN:  X: polynomial
@@@@ OUT: X: polynomial
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-26
LBL 78 
FUNC 11         @@## REQ:free42>=2.5.24 
L4STK           @@## REQ:free42>=3.0    
EDIT
"Enter data; R/S"
" to end"
PROMPT
EXITALL
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (P2SLV)
@@@@ DSC: Solve quadratic polynomial
@@@@ IN:  X: Polynomial matrix
@@@@ OUT: Y: root_1
@@@@      X: root_2
@@@@ TST: free42_3.0.2
@@@@ FAQ: Uses INDEX
@@@@ UPD: 2021-04-26
LBL "P2SLV"
FUNC 12         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
LSTO "_M"  
R↓
INDEX "_M"  
WRAP
LBL 77
RCLEL
J+
FC? 77
GTO 77
RCL ST Z        @@@@  a                     c                     b                     a
ABS             @@@@  |a|                   c                     b                     a
X=0?
RTNERR 3
R↓              @@@@  a                     c                     b                     a
2               @@@@  2                     c                     b                      a
RCL× ST T       @@@@  2a                    c                     b                      a
R↓              @@@@  c                     b                     a                     2a                    
RCL÷ ST T       @@@@  c/(2a)                b                     a                     2a                    
X<>Y            @@@@  b                     c/(2a)                a                     2a                    
RCL÷ ST T       @@@@  b/(2a)                c/(2a)                a                     2a                    
+/-             @@@@  -B                    C                     a                     2a                    
ENTER           @@@@  -B                   -B                     C                      a
X↑2             @@@@  B^2                  -B                     C                      a
RCL- ST Z       @@@@  B^2-C                -B                     C                      a
RCL- ST Z       @@@@  B^2-2C               -B                     C                      a
SQRT            @@@@  √(B^2-2C)            -B                     C                      a
RCL ST Y        @@@@  -B                    √(B^2-2C)            -B                      C
RCL- ST Y       @@@@  -B-√(B^2-2C)          √(B^2-2C)            -B                      C
RCL ST Z        @@@@  -B                   -B-√(B^2-2C)          √(B^2-2C)              -B                     
RCL+ ST Z       @@@@  -B+√(B^2-2C)         -B-√(B^2-2C)          √(B^2-2C)              -B                     
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (P1SLV)
@@@@ DSC: Solve linear polynomial
@@@@ IN:  X: Polynomial matrix
@@@@ OUT: X: root
@@@@ TST: free42_3.0.2
@@@@ FAQ: Uses INDEX
@@@@ UPD: 2021-04-26
LBL "P1SLV"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
LSTO "_M"  
R↓
INDEX "_M"  
WRAP
RCLEL           @@@@ a_1
J+
RCLEL           @@@@ a_0
÷
+/-
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (PEVAL)
@@@@ DSC: Evaluate a polynomial
@@@@ IN:  Y: Matrix with polynomial coefficients.  DIM of 1xn, nx1, whatever...
@@@@      X: Value at which polynomial should be evaluated
@@@@ OUT: X: value of polynomial evaluated at X
@@@@ LBL: 91
@@@@ FAQ: Uses INDEX
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-03
LBL "PEVAL"
FUNC 21         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
X<>Y            @@@@ P X
LSTO "_M"  
INDEX "_M"  
WRAP
0               @@@@ PV P X
LBL 91
RCL× ST Z       @@@@ PV*X P X
RCLEL           @@@@ Coef PV*X P X
+               @@@@ PV=Coef+PV*X P X
J+
FC? 77
GTO 91
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (PEVL1)
@@@@ DSC: Evaluate a polynomial and it's first derivative
@@@@ IN:  Y: Matrix with polynomial coefficients.  DIM of 1xn, nx1, whatever...
@@@@      X: Value at which polynomial & derivative should be evaluated
@@@@ OUT: Y: value of polynomial evaluated at X
@@@@      X: value of polynomial's derivative evaluated at X
@@@@ LBL: 91
@@@@ FAQ: Uses INDEX
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-03
LBL "PEVL1"
FUNC 22         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
X<>Y            @@@@ P       X
LSTO "_M"  
INDEX "_M"  
R↓              @@@@ X
WRAP
0               @@@@ PV      X
0               @@@@ DV      PV       X
LBL 92
RCL× ST Z       @@@@ DV*X    PV       X
RCL+ ST Y       @@@@ DV*X+PV PV       X
X<>Y            @@@@ PV      DV*X+PV  X
RCL× ST Z       @@@@ PV*X    DV*X+PV  X
RCLEL           @@@@ C       PV*X     DV*X+PV  X
+               @@@@ C+PV*X  DV*X+PV  X
X<>Y            @@@@ DV*X+PV C+PV*X   X
J+
FC? 77
GTO 92
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (PEVL2)
@@@@ DSC: Evaluate a polynomial and it's first two derivatives
@@@@ IN:  Y: Matrix with polynomial coefficients.  DIM of 1xn, nx1, whatever...
@@@@      X: Value at which polynomial & derivative should be evaluated
@@@@ OUT: Z: value of polynomial evaluated at X
@@@@      Y: value of polynomial's first derivative evaluated at X
@@@@      X: value of polynomial's second derivative evaluated at X
@@@@ LBL: 91
@@@@ FAQ: Uses INDEX
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-03
LBL "PEVL2"
FUNC 23         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
LSTO "_X"       @@@@ X       P
R↓              @@@@ P
LSTO "_M"  
INDEX "_M"  
R↓              @@@@ 
WRAP
0               @@@@ PV      
0               @@@@ DV       PV       
0               @@@@ DDV      DV       PV       
LBL 93
RCL× "_X"       @@@@ DDV*X    DV       PV       
RCL+ ST Y       @@@@ DDV*X+DV DV       PV       
X<>Y            @@@@ DV       DDV*X+DV PV       
RCL× "_X"       @@@@ DV*X     DDV*X+DV PV       
RCL+ ST Z       @@@@ DV*X+PV  DDV*X+DV PV       
X<>Y            @@@@ DDV*X+DV DV*X+PV  PV       
RCL ST Z        @@@@ PV       DDV*X+DV DV*X+PV  PV       
RCL× "_X"       @@@@ PV*X     DDV*X+DV DV*X+PV  PV       
RCLEL           @@@@ C        PV*X     DDV*X+DV DV*X+PV
+               @@@@ C+PV*X   DDV*X+DV DV*X+PV
STO ST T        @@@@ C+PV*X   DDV*X+DV DV*X+PV  C+PV*X   
R↓              @@@@ DDV*X+DV DV*X+PV  C+PV*X   
J+
FC? 77
GTO 93
2
×
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (PDEFLT)
@@@@ DSC: Deflate polynomial
@@@@ IN:  Y: Matrix with polynomial coefficients.  DIM of 1xn, nx1, whatever...
@@@@      X: Root
@@@@ OUT: Y: Remainder (a number)
@@@@      X: Deflated polynomial
@@@@ LBL: 96
@@@@ FAQ: Uses INDEX
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-03
LBL "PDEFLT"
FUNC 22         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
REAL?
GTO 88
X<>Y
XEQ 89          @@@@ MAT2C
X<>Y
LBL 88
X<>Y            @@@@ P R
LSTO "_M"  
INDEX "_M"  
WRAP
R↓              @@@@ R
+/-
0               @@@@ LC R
LBL 96
RCL× ST Y       @@@@ LC*R R
RCLEL           @@@@ C LC*R R
X<>Y            @@@@ LC*R C R
-               @@@@ C-LC*R  R
STOEL         
J+
FC? 77
GTO 96
RCL "_M"        @@@@ REM
DIM?            @@@@ m n REM
×               @@@@ N REM
1               @@@@ 1 N REM
-               @@@@ N-1 REM
1               @@@@ 1 N-1 REM
X<>Y            @@@@ N-1 1 REM
DIM "_M"  
R↓
R↓              @@@@ REM
RCL "_M"        @@@@ P REM
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Make matrix complex
@@@@ NAM: MAT2C 89
@@@@ IN:  X: Matrix
@@@@ OUT: X: Matrix
@@@@ FAQ: Uses INDEX
@@@@ LBL: MAT2C
LBL 89
FUNC 11
L4STK
LSTO "_M"  
INDEX "_M"  
RCLEL
REAL?
GTO 87
R↓
RTN
LBL 87
R↓
ENTER
DIM?
NEWMAT
COMPLEX
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (VPOLY)
@@@@ DSC: View elements of polynomial
@@@@ IN:  X: polynomial matrix
@@@@ OUT: N/A
@@@@ TST: free42_3.0.2
@@@@ FAQ: Uses INDEX
@@@@ UPD: 2021-04-03
LBL "VPOLY"
FUNC 00
LSTO "_M"  
INDEX "_M"  
WRAP
DIM?
×
LBL 90
"X^"
AIP
":"
RCLEL
ARCL ST X
R↓
AVIEW
STOP
1
-
J+
FC? 77
GTO 90
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (PR1ST)
@@@@ DSC: Find a root
@@@@ IN:  X: Polynomial
@@@@ OUT: Z: Origional Polynomial
@@@@      Y: Deflated Polynomial
@@@@      X: Root
@@@@ FAQ: If set, the global variable ACC is used to set accuracy
@@@@ TC:  [1, -16, -12, 3] => -0.90574, 0.1982, 16.70749
LBL "PR1ST"
FUNC 13
L4STK
XEQ 81          @@@@ PLYBAD
RTNERR 5
LSTO "_P"       @@@@ P
RAN
RAN
COMPLEX         @@@@ Guss Poly
SF 25
RCL "ACC"
FC?C 25
1e-15           @@@@ Tol  Guss Poly
50              @@@@ Itr  Tol  Guss  Poly
XEQ "PLGRR"     @@@@ Root Pval Stat
0≠? ST Z
RTNERR 6
RCL "_P"        @@@@ Poly Root Pval Stat
RCL "_P"        @@@@ Poly Poly Root Pval
RCL ST Z        @@@@ Root Poly Poly Root
XEQ "PDEFLT"    @@@@ DPly Rem  Poly Root
X<>Y            @@@@ Rem  DPly Poly Root
R↓              @@@@ DPly Poly Root
RCL ST Z        @@@@ Root DPly Poly Root
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (PRNXT)
@@@@ DSC: Find a another/next root
@@@@ in:  Z: Origional Polynomial
@@@@      Y: Deflated Polynomial
@@@@      X: Guess
@@@@ OUT: Z: Origional Polynomial
@@@@      Y: Deflated Polynomial or 0 if fully deflated
@@@@      X: Root
@@@@ FAQ: If set, the global variable ACC is used to set accuracy
LBL "PRNXT"
FUNC 33
L4STK
RCL ST Z        @@@@ Poly Gues DPly Poly
LSTO "_P"  
R↓              @@@@ Gues DPly Poly
RCL ST Y        @@@@ DPLY Gues DPly Poly
LSTO "_DP"  
XEQ 81          @@@@ PLYBAD
RTNERR 5
R↓              @@@@ Gues DPly Poly
SF 25
RCL "ACC"
FC?C 25
1e-15           @@@@ Tol  Gues DPly Poly
50              @@@@ Itr  Tol  Gues DPly
XEQ "PLGRR"     @@@@ Root Pval Stat
0≠? ST Z
RTNERR 6
RCL "_P"        @@@@ Poly Root Pval Stat
X<>Y            @@@@ Root Poly Pval Stat
-1              @@@@ -1   Root Poly Pval
5               @@@@ 5    -1   Root Poly
XEQ "PLGRR"     @@@@ Root Pval Stat
RCL ST X        @@@@ Root Root Pval Stat
RCL "_DP"       @@@@ DPly Root Root Pval
X<>Y            @@@@ Root DPly Root Pval
XEQ "PDEFLT"    @@@@ DPly Rem  Root Pval
X<>Y
R↓              @@@@ DPly Root Pval
@@@@ TODO: Should check if |Rem| is near zero.  If it is not, then we probably converged to a previously found root and removed from DPly. It is also possible
@@@@ TODO: that we we might have diverged, but that is super unlikely.  In fact, both cases are quite unlikely.  Still good software should check.
RCL "_P"        @@@@ Poly DPly Root Pval
X<>Y            @@@@ DPly Poly Root Pval
RCL ST Z        @@@@ Root DPly Poly Root
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: RTNNO if X is not a polynomial of degree>0
@@@@ NAM: PLYBAD 81
@@@@ IN:  X: Polynomial
@@@@ OUT: N/A
LBL 81
FUNC 00
MAT?
GTO 82
RTNYES
LBL 82
DIM?
×
2
X>Y?
RTNYES
RTNNO

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (PLGRR)
@@@@ DSC: Use Laguerre's method to find a polynomial root
@@@@ IN:  T: Polynomial
@@@@      Z: Guess
@@@@      Y: Tolerance
@@@@      X: Maximum Iteration
@@@@ OUT: Z: Reason for exit
@@@@         0 = A solution has been found.
@@@@         3 = Bad guess was used.
@@@@      Y: P(X)
@@@@      X: Root
LBL "PLGRR"
FUNC 43
L4STK
LSTO "_I"       @@@@ ITR   TOL   GUESS POLY
R↓              @@@@ TOL   GUESS POLY
LSTO "_T"  
R↓              @@@@ GUESS POLY
LSTO "_G"        
R↓              @@@@ POLY
LSTO "_P"  
DIM?                       
×               @@@@ N
LSTO "_N"  
@@@@ TODO: Check N>1.  Another status: 4 = polynomial is constant
1
-               @@@@ N-1
LSTO "_NM1"  
R↓
LBL 94
RCL "_P"  
RCL "_G"  
XEQ "PEVL2"     @@@@ P''         P'     P        ?
RCL ST Z
ABS             @@@@ |P|         P''    P'       P
X<? "_T"
GTO 95
R↓              @@@@ P''         P'     P        ?
@@@@ TODO: Check P' for /0.  Another status: 5 = Iteration failed due to division by zero
RCL÷ ST Y       @@@@ P''/P'      P'     P        ?
R↓              @@@@ P'          P      ?        P''/P'  
÷               @@@@ -N=P/P'     ?      P''/P'   P''/P' 
X<>Y            @@@@ ?           -N     P''/P'   P''/P' 
R↓              @@@@ -N          P''/P' P''/P'   ?
+/-             @@@@ N           P''/P' P''/P'   ?
X<>Y            @@@@ P''/P'      N      P''/P'   ?
RCL× ST Y       @@@@ -L          N      P''/P'   ?
RCL× "_N"       @@@@ -L*n        N      P''/P'   ?
RCL÷ "_NM1"     @@@@ -L*n/(n-1)  N      P''/P'   ?
1
+               @@@@ 1-L*n/(n-1) N      P''/P'   P''/P'  
SQRT
RCL× "_NM1"  
RCL÷ "_N"  
RCL "_N"  
1/X
+  
1/X
×               @@@@ LD          P''/P'   P''/P'  
STO+ "_G"  
DSE "_I"  
GTO 94
@@@@ EXIT: Max iter
3               @@@@ 3      LD       P''/P'   ?
RCL "_P"        @@@@ POLY   3        LD       ?
RCL "_G"        @@@@ G      POLY     3        ?
XEQ "PEVAL"     @@@@ P      3        ?        ?
RCL "_G"        @@@@ G      P        3        ?
RTN
LBL 95          @@@@ |P|    P''      P'       P   
@@@@ EXIT: Found root
R↓              @@@@ P''    P'       P   
R↓              @@@@ P'     P   
R↓              @@@@ P   
0
X<>Y            @@@@ P      0
RCL "_G"        @@@@ Root   Pval     0
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (PWRP)
@@@@ DSC: Make a polynomial stored in a matrix into a =MVAR= function
@@@@ IN:  X: N/A
@@@@ OUT: X: N/A
@@@@ GLB: WRPP -- Store a polynomial matrix in this variable
@@@@ TST: free42_3.0.2
@@@@ FAQ: Allows one to use SOLVER & INTEG on polynomials
@@@@ USE: PEVAL
@@@@ UPD: 2021-04-03
LBL "PWRP"
MVAR "X"
RCL "WRPP"
RCL "X"
XEQ "PEVAL"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Store/Recall variable "WRPP"
LBL 98 
FS? 64
RCL "WRPP"
STO "WRPP"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ Store/Recall variable "X"
LBL 97
FS? 64
RCL "X"
STO "X"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Is a number very close to zero
@@@@ NAM: ZEROISH 80
LBL 80
FUNC 11
L4STK
ABS
1e-10
X>Y?
RTNYES
RTNNO

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
END

5. VEC3: 3D Real Vector Application

This is a simple little application that makes working with 3D vectors less painfull.

Menu Target  
→V LBL 99 Put stack elements X, Y, & Z into a vector: [Z, Y, X]
V→ LBL 98 Vector contents to stack. [A, B, C] => X: C, Y: B, Z: A
DOT   Dot product
CROSS   Cross product
MAG FNRM Euculidian magnitude
VVIEW LBL 96 View a vector one element at a time – press R/S for next element

5.2. Code for Menu

(MJR-generate-42-menu-code "VEC3" 0 tbl 0 1 'stay 'up 'auto #'MJR-custom-gen-lab #'MJR-custom-gen-sub)

5.3. Local functions

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Create a vector from stack contents
@@@@ NAM: →V 99
@@@@ IN:  Z: real number
@@@@      Y: real number
@@@@      X: real number
@@@@ OUT: X: 1x3 matrix
@@@@ LBL: Used: 51
@@@@ FAQ: Uses INDEX
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-03
LBL 99
FUNC 31
XEQ 95
LSTO "_M"  
R↓
INDEX "_M"  
WRAP
J-
LBL 51
STOEL
R↓
J-
FC? 77
GTO 51
RCL "_M"  
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Put vector elements on stack
@@@@ NAM: V→ 98
@@@@ IN:  X: 1x3 matrix V
@@@@ OUT: Z: First component of V
@@@@      Y: Second component of V
@@@@      X: Third component of V
@@@@ LBL: Used: 52
@@@@ FAQ: Uses INDEX
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-03
LBL 98
FUNC 13
LSTO "_M"  
R↓
INDEX "_M"  
WRAP
LBL 52
RCLEL
J+
FC? 77
GTO 52
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: View elements of vector
@@@@ NAM: VVIEW 96
@@@@ IN:  X: 1x3 matrix V
@@@@ OUT: N/A
@@@@ LBL: Used: 53
@@@@ FAQ: Uses INDEX
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-03
LBL 96
FUNC 00
LSTO "_M"  
INDEX "_M"  
WRAP
1
LBL 54
CLA
AIP
":"
RCLEL
ARCL ST X
R↓
AVIEW
STOP
1
+
J+
FC? 77
GTO 54
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Make a 3D vector full of zeros
@@@@ NAM: VVIEW 95
@@@@ IN:  N/A
@@@@ OUT: X: 1x3 Matrix
@@@@ LBL: Used: 53
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-03
LBL 95
FUNC 01
1
3
NEWMAT
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
END

6. STATR: Statistics Registers

Menu Code
Σx FUNC 01; L4STK; ΣREG?; 0; +; RCL IND ST X; "Σx="; ARCL ST X; AVIEW
Σx↑2 FUNC 01; L4STK; ΣREG?; 1; +; RCL IND ST X; "Σx^2="; ARCL ST X; AVIEW
Σy FUNC 01; L4STK; ΣREG?; 2; +; RCL IND ST X; "Σy="; ARCL ST X; AVIEW
Σy↑2 FUNC 01; L4STK; ΣREG?; 3; +; RCL IND ST X; "Σy^2="; ARCL ST X; AVIEW
Σxy FUNC 01; L4STK; ΣREG?; 4; +; RCL IND ST X; "Σxy="; ARCL ST X; AVIEW
n FUNC 01; L4STK; ΣREG?; 5; +; RCL IND ST X; "n="; ARCL ST X; AVIEW
Σlnx FUNC 01; L4STK; ΣREG?; 6; +; RCL IND ST X; "Σlnx="; ARCL ST X; AVIEW
Σlnx↑2 FUNC 01; L4STK; ΣREG?; 7; +; RCL IND ST X; "Σ(lnx)^2="; ARCL ST X; AVIEW
Σlny FUNC 01; L4STK; ΣREG?; 8; +; RCL IND ST X; "Σlny="; ARCL ST X; AVIEW
Σlny↑2 FUNC 01; L4STK; ΣREG?; 9; +; RCL IND ST X; "Σ(lny)^2="; ARCL ST X; AVIEW
Σlnxlny FUNC 01; L4STK; ΣREG?; 10; +; RCL IND ST X; "Σlnxlny="; ARCL ST X; AVIEW
Σxlny FUNC 01; L4STK; ΣREG?; 11; +; RCL IND ST X; "Σxlny="; ARCL ST X; AVIEW
Σylnx FUNC 01; L4STK; ΣREG?; 12; +; RCL IND ST X; "Σylnx="; ARCL ST X; AVIEW

6.2. Code

(MJR-generate-42-menu-code "STATR" 0 tbl 0 nil 'stay 'up 'auto
                           #'MJR-local-only-gen-lab 
                           (lambda (atrg target row) 
                             (cl-destructuring-bind (menu prog) row
                               (mapconcat #'string-trim-left 
                                          (split-string prog ";") "\n"))))

7. EOF