gmtmath - Reverse Polish Notation calculator for data tables

gmtmath[-At_f(t).d] [-Ccols] [-Hnrec] [-Nn_col/t_col] [-Q] [-S[f|l] ][-Tt_min/t_max/t_inc|tfile] [-V] [-bi[s][n] ] [-bo[s][n] ]operand[operand]OPERATOR[operand]OPERATOR...=[outfile]

gmtmathwill perform operations like add, subtract, multiply, and divide on one or more table data files or constants using Reverse Pol- ish Notation (RPN) syntax (e.g., Hewlett-Packard calculator-style). Arbitrarily complicated expressions may therefore be evaluated; the final result is written to an output file [or standard output]. When two data tables are on the stack, each element in file A is modified by the corresponding element in file B. However, some operators only require one operand (see below). If no data tables are used in the expression then options-T,-Nmust be set (and optionally-b). By default, all columns except the "time" column are operated on, but this can be changed (see-C).operandIfoperandcan be opened as a file it will be read as an ASCII (or binary, see-bi) table data file. If not a file, it is interpreted as a numerical constant or a special symbol (see below).outfileis a table data file that will hold the final result. If not given then the output is sent to stdout.OPERATORSChoose among the following operators: Operator n_args ReturnsABS1 abs (A).ACOS1 acos (A).ACOSH1 acosh (A).ADD(+)2 A + B.AND2 NaN if A and B == NaN, B if A == NaN, else A.ASIN1 asin (A).ASINH1 asinh (A).ATAN1 atan (A).ATAN22 atan2 (A, B).ATANH1 atanh (A).BEI1 bei (A).BER1 ber (A).CEIL1 ceil (A) (smallest integer >= A).CHICRIT2 Critical value for chi-squared-distribution, with alpha = A and n = B.CHIDIST2 chi-squared-distribution P(chi2,n), with chi2 = A and n = B.COL1 Places column A on the stack.COS1 cos (A) (A in radians).COSD1 cos (A) (A in degrees).COSH1 cosh (A).D2DT21 d^2(A)/dt^2 2nd derivative.D2R1 Converts Degrees to Radians.DILOG1 dilog (A).DIV(/)2 A / B.DDT1 d(A)/dt 1st derivative.DUP1 Places duplicate of A on the stack.ERF1 Error function erf (A).ERFC1 Complementary Error function erfc (A).ERFINV1 Inverse error function of A.EQ2 1 if A == B, else 0.EXCH2 Exchanges A and B on the stack.EXP1 exp (A).FCRIT3 Critical value for F-distribution, with alpha = A, n1 = B, and n2 = C.FDIST3 F-distribution Q(F,n1,n2), with F = A, n1 = B, and n2 = C.FLIPUD1 Reverse order of each columnFLOOR1 floor (A) (greatest integer <= A).FMOD2 A % B (remainder).GE2 1 if A >= B, else 0.GT2 1 if A > B, else 0.HYPOT2 hypot (A, B) = sqrt (A*A + B*B).I01 Modified Bessel function of A (1st kind, order 0).I11 Modified Bessel function of A (1st kind, order 1).IN2 Modified Bessel function of A (1st kind, order B).INT1 Numerically integrate A.INV1 1 / A.ISNAN1 1 if A == NaN, else 0.J01 Bessel function of A (1st kind, order 0).J11 Bessel function of A (1st kind, order 1).JN2 Bessel function of A (1st kind, order B).K01 Modified Kelvin function of A (2nd kind, order 0).K11 Modified Bessel function of A (2nd kind, order 1).KN2 Modified Bessel function of A (2nd kind, order B).KEI1 kei (A).KER1 ker (A).LE2 1 if A <= B, else 0.LMSSCL1 LMS scale estimate (LMS STD) of A.LOG1 log (A) (natural log).LOG101 log10 (A) (base 10).LOG1P1 log (1+A) (accurate for small A).LOG21 log2 (A) (base 2).LOWER1 The lowest (minimum) value of A.LRAND2 Laplace random noise with mean A and std. deviation B.LSQFIT1 Let current table be [A | b]; return least squares solution x = A \ b.LT2 1 if A < B, else 0.MAD1 Median Absolute Deviation (L1 STD) of A.MAX2 Maximum of A and B.MEAN1 Mean value of A.MED1 Median value of A.MIN2 Minimum of A and B.MODE1 Mode value (Least Median of Squares) of A.MUL(x)2 A * B.NAN2 NaN if A == B, else A.NEG1 -A.NEQ2 1 if A != B, else 0.NRAND2 Normal, random values with mean A and std. deviation B.OR2 NaN if A or B == NaN, else A.PLM3 Associated Legendre polynomial P(-1<A<+1) degree B order C.POP1 Delete top element from the stack.POW(^)2 A ^ B.R22 R2 = A^2 + B^2.R2D1 Convert Radians to Degrees.RAND2 Uniform random values between A and B.RINT1 rint (A) (nearest integer).ROOTS2 Treats col A as f(t) = 0 and returns its rootsROTT2 Rotate A by the (constant) shift B in the t-direction.SIGN1 sign (+1 or -1) of A.SIN1 sin (A) (A in radians).SINC1 sinc (A) (sin (pi*A)/(pi*A)).SIND1 sin (A) (A in degrees).SINH1 sinh (A).SQRT1 sqrt (A).STD1 Standard deviation of A.STEP1 Heaviside step function H(A).STEPT1 Heaviside step function H(t-A).SUB(-)2 A - B.SUM1 Cumulative sum of ATAN1 tan (A) (A in radians).TAND1 tan (A) (A in degrees).TANH1 tanh (A).TCRIT2 Critical value for Student’s t-distribution, with alpha = A and n = B.TDIST2 Student’s t-distribution A(t,n), with t = A, and n = B.UPPER1 The highest (maximum) value of A.XOR2 B if A == NaN, else A.Y01 Bessel function of A (2nd kind, order 0).Y11 Bessel function of A (2nd kind, order 1).YN2 Bessel function of A (2nd kind, order B).ZCRIT1 Critical value for the normal-distribution, with alpha = A.SYMBOLSThe following symbols have special meaning:PI3.1415926...E2.7182818...TTable with t-coordinates

-ARequires-Nand will partially initialize a table with values from the given file containingtandf(t)only. Thetis placed in columnt_colwhilef(t)goes into columnn_col- 1 (see-N).-CSelect the columns that will be operated on until next occur- rence of-C. List columns separated by commas; ranges like 1,3-5,7 are allowed.-C(no arguments) resets the default action of using all columns except time column (see-N).-Caselects all columns, including time column, while-Crreverses (toggles) the current choices.-HInput file(s) has Header record(s). Number of header records can be changed by editing your .gmtdefaults4 file. If used,GMTdefault is 1 header record. Use-Hiif only input data should have header records [Default will write out header records if the input data have them].-NSelect the number of columns and the column number that contains the "time" variable. Columns are numbered starting at 0 [2/0].-QQuick mode for scalar calculation. Shorthand for-Ca-N1/0-T0/0/1.-SOnly report the first or last row of the results [Default is all rows]. This is useful if you have computed a statistic (say theMODE) and only want to report a single number instead of numer- ous records with identical values. Appendlto get the last row andfto get the first row only [Default].-TRequired when no input files are given. Sets the t-coordinates of the first and last point and the equidistant sampling inter- val for the "time" column (see-N). If there is no time column (only data columns), give-Twith no arguments; this also implies-Ca. Alternatively, give the name of a file whose first column contains the desired t-coordinates which may be irregular.-VSelects verbose mode, which will send progress reports to stderr [Default runs "silently"].-biSelects binary input. Appendsfor single precision [Default is double]. Appendnfor the number of columns in the binary file(s).-boSelects binary output. Appendsfor single precision [Default is double]. Appendnfor the number of columns in the binary file(s).

The operatorPLMcalculates the associated Legendre polynomial of degree L and order M, and its argument is the cosine of the colatitude which must satisfy -1 <= x <= +1.PLMis not normalized. All derivatives are based on central finite differences, with natural boundary conditions.

To take log10 of the average of 2 data files, usegmtmathfile1.d file2.dADD0.5MULLOG10=file3.d Given the file samples.d, which holds seafloor ages in m.y. and seafloor depth in m, use the relation depth(in m) = 2500 + 350 * sqrt (age) to print the depth anomalies:gmtmathsamples.dTSQRT350MUL2500ADDSUB=| lpr To take the average of columns 1 and 4-6 in the three data sets sizes.1, sizes.2, and sizes.3, usegmtmath-C1,4-6 sizes.1 sizes.2ADDsizes.3ADD3DIV=ave.d To take the 1-column data set ages.d and calculate the modal value and assign it to a variable, try set mode_age = ‘gmtmath-S-Tages.dMODE=‘ To evaluate the dilog(x) function for coordinates given in the file t.d:gmtmath-Tt.dTDILOG=dilog.d To use gmtmath as a RPN Hewlett-Packard calculator on scalars (i.e., no input files) and calculate arbitrary expressions, use the-Qoption. As an example, we will calculate the value of Kei (((1 + 1.75)/2.2) + cos (60)) and store the result in the shell variable z: set z = ‘gmtmath-Q1 1.75ADD2.2DIV60COSDADDKEI=‘ To usegmtmathas a general least squares equation solver, imagine that the current table is the augmented matrix [ A | b ] and you want the least squares solution x to the matrix equation A * x = b. The opera- torLSQFITdoes this; it is your job to populate the matrix correctly first. The-Aoption will facilitate this. Suppose you have a 2-column file ty.d withtandb(t)and you would like to fit a the model y(t) = a + b*t + c*H(t-t0), where H is the Heaviside step function for a given t0 = 1.55. Then, you need a 4-column augmented table loaded with t in column 0 and your observed y(t) in column 3. The calculation becomesgmtmath-N4/1-Aty.d-C0 1ADD-C2 1.55STEPTADD-CaLSQFIT=solu- tion.d Note we use the-Coption to select which columns we are working on, then make active all the columns we need (here all of them, with-Ca) before callingLSQFIT. The second and fourth columns are preloaded with t and y(t), respectively, the other columns are zero. If you already have a precalculated table with the augmented matrix [ A | b ] in a file (say lsqsys.d), the least squares solution is simplygmtmath-Tlsqsys.dLSQFIT=solution.d

Files that have the same name as some operators, e.g.,ADD,SIGN,=, etc. cannot be read and must not be present in the current directory. Piping of files is not allowed on input, but the output can be sent to stdout. The stack limit is hard-wired to 50. All functions expecting a positive radius (e.g.,LOG,KEI, etc.) are passed the absolute value of their argument. TheDDTandD2DT2functions only work on regularly spaced data.ROOTSmust be the last operator on the stack, only fol- lowed by=.

Abramowitz, M., and I. A. Stegun, 1964,HandbookofMathematicalFunc-tions, Applied Mathematics Series, vol. 55, Dover, New York. Press, W. H., S. A. Teukolsky, W. T. Vetterling, B. P. Flannery, 1992,NumericalRecipes, 2nd edition, Cambridge Univ., New York.

gmt(l),grd2xyz(l),grdedit(l),grdinfo(l),grdmath(l),xyz2grd(l) GMT4.0 1 Oct 2004 GMTMATH(l)

Man(1) output converted with man2html