Scientific Programmable Script
Webb - all rights reserved - ©2004
spacer
Author - Bryan Monosmith
Email: bryanmonosmith@comcast.net
Webpage:

Note: This script invented a simple macro capability, and was written before keystroke macro was built into RPN. It is therefore obsolete. I leave it here as an historical curiosity. The remaining useful elements, root extraction of polynomials, and numerical integration and summation, are included elsewhere on this site.

Summary:

Basic scientific functions with,
- 71 step keystroke macro program capability.
- Fundamental physical constants in MKS units.
- Quadratic equation solver.
- Summation and Numerical Integration.
    (Credits to Jan Popelek , whose excellent implementation of the
factorial function I basically lifted for this script.)
! This script is almost 3k bytes in size. Users will need 'ClipHack' or
some other method of getting this into the clipboard


Instructions:
* Basic functions- These are mostly intuitive. The help mode will fill any
gaps.
* "rec" (macro record)-  Brings up 4 options, Begin, End, Load and
Dump.  Begin starts the keystroke recorder. Every key touched is recorded,
except for "EEX", ".", and the script menu key. End terminates  keystroke
recording.  Dump will place the current macro onto the stack as a meta
object, where  the number at tos contains the count for how many additional
stack elements are contained in the macro dump. Load works in reverse.
These functions allow for the creation, saving, and loading of macros.
         NOTE: Numeric entry in macro mode is limited to single digits. In
order to use multi digit numbers in a macro, I have created 10 new storage
registers that can be preloaded with float values in run mode, then
accessed in macro mode. This is somewhat of a kludge and is either an
inability on my part to understand how to process full numeric input, or a
limitation of RPN functionality in this area. If the latter, I don't think
Russ ever anticipated that anyone would write a script to do what I have
attempted.
* "play" - Execution of this function simply plays the recorded macro.
* "rcl" -  Execution with a tos value of 0-9, recalls the number stored in
the macro register. "inv", "rcl" stores the number at the second stack
position into the register number at tos.
* "stk"- Provides a menu of simple stack operations, primarily for use in
macros.
* "f" - Tapping "f" presents 4 options. Upper and lower limits, previously
put onto the stack, are entered by tapping the appropriate "upper" or
"lower" assignment functions. Summation and Integration functions will use
the assigned limits, and accomplish the desired computation using the
function recorded in macro memory. The macro function should be written to
assume, on entry, that the value of 'x' is at tos.
    Numerical integration uses the Romberg algorithm and returns the answer
in tos. Also on the stack is the 'estimated' error and the number of
iterations performed. This is a very small routine! It is optimized for
speed and does not contain the code necessary to watch for pathological or
ill behaved functions which may return erroneous values or cause the
algorithm to thrash. Lacking convergence, the routine will time-out after 9
iterations, which corresponds to a time on the order of a minute or so,
depending on the size of the macro function and the speed of the machine.
The error term is extremely crude and can normally be off by 2 orders of
magnitude and can be off by 4 orders of magnitude for ill behaved
functions. Despite these limitations, the error term is better than no
information at all.
    Summation will return the sum of the macro function, evaluated from the
lower limit to the upper limit, in tos. The value of the next term in the
series is returned in the next stack position and provides an indication of
the rate of convergence.

Example (1)   
Automate the evaluation of the polynomial:
x^3 + 5.127*x^2 - 2*x - 7 , for different values of x.
Put "5.127" on the stack. Now tap "0", "inv", "rcl". This stores 5.127 into
the macro register 0. Tap "rec", then "Begin". Notice the script put a
dummy number on the stack if you hadn't already placed one there. Enter the
function: tap "ENTER", "3", "y^x", "stk", "over", "x^2", "0", "rcl", "*",
"+", "stk", "swap", "2", "*", "-", "7", "-". Tap "rec", then "End". Now
find the value of the function at 4.2 by putting 4.2 on the stack and
tapping "play". The result should be 149.1282.

Example (2)   
Approximate the integral from 0 to infinity of the function
x*exp(-x).
Tap "rec", then "Begin". Enter the function: "ENTER", "+/-", "exp", "*".
Now tap "rec", followed by "End". Put the numbers 0 and 20 (close enough to
infinity) on the stack, tap "f", followed by "upper", "lower", then
"Integ.". In less than a minute the answer appears as ~1, with ~6E-11 and 8
on the stack to indicate a crude estimate of the error and the number of
iterations required in the Romberg algorithm.


Code:

RPN.2.z\Scientific-P
[o]0Xw;
{o}0XxCo;
[a]c(:7:8:9:/:t::4:5:6:*:P:g1:1:2:3:-:s::0::n:+:2P:d1:CA:CB:CC:CD:CE:CF:CG:CH
:CI:CJ:CiCo:::::CO:CjCo);
[m]7Xz{xz1+x@V8Xy{#'100'vg2/g1wVf*g1(Ca:d10XzB)_xy}_xz};
[w]#'1.0E-9';
[W]xuCmC0xtCmC1+Hxtxu-g1Xi*g12Xv{2xv2-P1-V0{v1H+xi*xu+Cm+_v}xi*g2+Hg1xv2+k@2Xx{4xx1-P*r2-4xx1-P1-/g1xv2+k@xx1+g1Xxxv>(B)}Vvxv2+k@xiHXixv3>(d1xv1-g@v-v/bg1Cw|:0)(XiB:d1xv1+Xv)}
xvXx{d1_xx}
0Xxxvxiv*2*vC^;
[0]g1#'1.0e4'/b#'1.0e63990'>(d1xu#'1.0e-16'+g1XuCm);
[1]g1#'1.0e4'/b#'1.0e63990'>(d1xt#'1.0e-16'-g1XtCm);
[Y]xuV0{vCm+v1+Vvxt>(B)}vCmr2C^;
[b]#'200';
[^]#'1000'#'54'S;
[*]#'300'#'54'S;
[9]#'99'XyC*;
[=]00000000XaXbXcXdXeXfXgXh;
[e]     #'2.71828182845904524';
[p]     #'3.14159265358979324';
[P]     V1       #'0.577191652'v*-       #'0.988205891'v2P*+
                 #'0.897056937'v3P*-     #'0.918206857'v4P*+
                 #'0.756704078'v5P*-     #'0.482199394'v6P*+
                 #'0.193527818'v7P*-     #'0.035868343'v8P*+ ;
[!]     Vvvb<(  vCbn<(v; :vnV 1{_vvn*}vn* vnCPk2/)
         :       vCb >(  v2*Cp*s vCe/vP*
                         1 vm12t+ #'288'v2P*t+ #'139'#'51840'v3P*/+ *
                 :     1v{_v*v}CP*
         )       );
[i]c(#'299792458':#'6.626075540e-34':#'9.1093897e-31':#'1.602177335e-19':#'1.6726231e-27':#'6.6725985e-11':#'6.022136736e23':#'1.380658e-23':Cp:);
[g]D'light,Planck,e mass,charge\p
mass,grav.,Avogad.,Boltz.,pi|c|h|me|e|mp|G|Na|k|pi|\ESC|'
xx(g1c(#'19':#'13':#'14':#'15':7:8:9:1:2:3)xyr2XyCTXy)Ci;
[j]c(r2:g2:r3:g@:);
[s]D'|swap|over|rot|pick|\ESC|' xx(g1c(#'19':#'13':#'14':#'15':7)xyr2XyCTXy)Cj;
[d]D'\Set limits      Run
funct.|upper|lower|Integ.|Sum|\ESC|'c(XtUdCd:XuUdCd:CW:CY:);
[A]xw(0:1)Xw;
[B]xw(I:i)Co;
[C]xw(O:o)Co;
[D]xw(A:a)Co;
[E]xw(l:e)Co;
[F]xw(91+r2P:L)Co;
[G]xw(b:%)Co;
[H]xw(?2r2g1ig3*k3o*:?2g22Pg22P+sVv/Or2v/I0<(nMr(#'6.28318530718':#'360')+)v)Co;
[I]xw(1-)C!Co;
[J]xw(w:f)Co;
[K]?3g22Pr2g4*4*-r32*r3g2n/k32P/g2g2g10<(nsnr2r3nsk4:s+k3s-0k30r2);
[L]Cd;
[M]Cs;
[N]D'   Keystroke
Macro:|Begin|End|Load|Dump|\ESC|'c(1Xx0XzC=h(:2):0Xx:hg2>(C=1-V{8v-X@_v}):7V{v1+x@g10=0(d1B)_v}7v-:);
[O]#'10'+xw(X@:x@)Co;
[X]c(C9:7:8:9:/:t:C9:4:5:6:*:P:g1:1:2:3:-:s::0:C9:n:+:2P:d1:CA:CB:CC:CD:CE:CF:CG:CH:CI:CJ:Cg:C9:C9:CN#'99'Xy:C9:CO:CM);
[T]xy#'99'=0!(xz#'72'=0(C*:8xz9/w-g1x@#'10'xz9%2*Pxy*+r2X@xz1+Xz));
{b}xx(g1#'25'>(#'75'-)g1XyUhCXCTUd:d1);
"Scientific-P"
"inv:\\bm v3b"xw(C*:C^)CA;
"sin"CB;
"cos"CC;
"tan"CD;
~
"exp:\ln (shift)"CE;
"log:\10^x (shift)"CF;
"mod:\|x| (shift)"CG;
" r>p: x,y to r,theta coords.\r,theta to  x,y coords.(shift)"CH;
~
"!: factorial x\  gamma x (shift)"CI;
"fp: fractional part\    integer part (shift)"CJ;
"c: constants"CoCg;
"QE: quadratic solver\in: a,b,c\out: Im,Re,Im,Re"CoCK;
"f:Sum-x: summation value\Integrate-x: integral value\y: est. error    z: #
iterations"CoCL;
~
"rec:Keystroke macro recorder.\71 step memory."CoCN;
"play"CoCmUeUd;
"rcl: 0-9 Rcl\    0-9 Sto (shift)"g10|!(CO:C*);
"stk"CoCM;