Provided by: gmt_4.5.11-1build1_amd64

**NAME**

gmtmath - Reverse Polish Notation calculator for data tables

**SYNOPSIS**

gmtmath[-At_f(t).d] [-Ccols] [-Fcols] [-H[i][nrec] ] [-I] [-Nn_col/t_col] [-Q] [-S[f|l] ] [-Tt_min/t_max/t_inc[+]|tfile] [-V] [-b[i|o][s|S|d|D[ncol]|c[var1/...]] ] [-f[i|o]colinfo] [-m[i|o][flag] ]operand[operand]OPERATOR[operand]OPERATOR...=[outfile]

**DESCRIPTION**

gmtmathwill perform operations like add, subtract, multiply, and divide on one or more table data files or constants using Reverse Polish 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,-Ncan be set (and optionally-bto indicate the data domain). If STDIN is given, <stdin> will be read and placed on the stack as if a file with that content had been given on the command line. 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). The special argument STDIN means thatstdinwill be read and placed on the stack; STDIN can appear more than once if necessary.outfileThe name of a table data file that will hold the final result. If not given then the output is sent to stdout.OPERATORSChoose among the following 131 operators. "args" are the number of input and output arguments. Operator args ReturnsABS1 1 abs (A).ACOS1 1 acos (A).ACOSH1 1 acosh (A).ACOT1 1 acot (A).ACSC1 1 acsc (A).ADD2 1 A + B.AND2 1 NaN if A and B == NaN, B if A == NaN, else A.ASEC1 1 asec (A).ASIN1 1 asin (A).ASINH1 1 asinh (A).ATAN1 1 atan (A).ATAN22 1 atan2 (A, B).ATANH1 1 atanh (A).BEI1 1 bei (A).BER1 1 ber (A).CEIL1 1 ceil (A) (smallest integer >= A).CHICRIT2 1 Critical value for chi-squared-distribution, with alpha = A and n = B.CHIDIST2 1 chi-squared-distribution P(chi2,n), with chi2 = A and n = B.COL1 1 Places column A on the stack.CORRCOEFF2 1 Correlation coefficient r(A, B).COS1 1 cos (A) (A in radians).COSD1 1 cos (A) (A in degrees).COSH1 1 cosh (A).COT1 1 cot (A) (A in radians).COTD1 1 cot (A) (A in degrees).CPOISS2 1 Cumulative Poisson distribution F(x,lambda), with x = A and lambda = B.CSC1 1 csc (A) (A in radians).CSCD1 1 csc (A) (A in degrees).D2DT21 1 d^2(A)/dt^2 2nd derivative.D2R1 1 Converts Degrees to Radians.DDT1 1 d(A)/dt Central 1st derivative.DILOG1 1 dilog (A).DIV2 1 A / B.DUP1 2 Places duplicate of A on the stack.EQ2 1 1 if A == B, else 0.ERF1 1 Error function erf (A).ERFC1 1 Complementary Error function erfc (A).ERFINV1 1 Inverse error function of A.EXCH2 2 Exchanges A and B on the stack.EXP1 1 exp (A).FACT1 1 A! (A factorial).FCRIT3 1 Critical value for F-distribution, with alpha = A, n1 = B, and n2 = C.FDIST3 1 F-distribution Q(F,n1,n2), with F = A, n1 = B, and n2 = C.FLIPUD1 1 Reverse order of each column.FLOOR1 1 floor (A) (greatest integer <= A).FMOD2 1 A % B (remainder after truncated division).GE2 1 1 if A >= B, else 0.GT2 1 1 if A > B, else 0.HYPOT2 1 hypot (A, B) = sqrt (A*A + B*B).I01 1 Modified Bessel function of A (1st kind, order 0).I11 1 Modified Bessel function of A (1st kind, order 1).IN2 1 Modified Bessel function of A (1st kind, order B).INRANGE3 1 1 if B <= A <= C, else 0.INT1 1 Numerically integrate A.INV1 1 1 / A.ISNAN1 1 1 if A == NaN, else 0.J01 1 Bessel function of A (1st kind, order 0).J11 1 Bessel function of A (1st kind, order 1).JN2 1 Bessel function of A (1st kind, order B).K01 1 Modified Kelvin function of A (2nd kind, order 0).K11 1 Modified Bessel function of A (2nd kind, order 1).KEI1 1 kei (A).KER1 1 ker (A).KN2 1 Modified Bessel function of A (2nd kind, order B).KURT1 1 Kurtosis of A.LE2 1 1 if A <= B, else 0.LMSSCL1 1 LMS scale estimate (LMS STD) of A.LOG1 1 log (A) (natural log).LOG101 1 log10 (A) (base 10).LOG1P1 1 log (1+A) (accurate for small A).LOG21 1 log2 (A) (base 2).LOWER1 1 The lowest (minimum) value of A.LRAND2 1 Laplace random noise with mean A and std. deviation B.LSQFIT1 0 Let current table be [A | b]; return least squares solution x = A \ b.LT2 1 1 if A < B, else 0.MAD1 1 Median Absolute Deviation (L1 STD) of A.MAX2 1 Maximum of A and B.MEAN1 1 Mean value of A.MED1 1 Median value of A.MIN2 1 Minimum of A and B.MOD2 1 A mod B (remainder after floored division).MODE1 1 Mode value (Least Median of Squares) of A.MUL2 1 A * B.NAN2 1 NaN if A == B, else A.NEG1 1 -A.NEQ2 1 1 if A != B, else 0.NOT1 1 NaN if A == NaN, 1 if A == 0, else 0.NRAND2 1 Normal, random values with mean A and std. deviation B.OR2 1 NaN if A or B == NaN, else A.PLM3 1 Associated Legendre polynomial P(A) degree B order C.PLMg3 1 Normalized associated Legendre polynomial P(A) degree B order C (geophysical convention).POP1 0 Delete top element from the stack.POW2 1 A ^ B.PQUANT2 1 The B'th Quantile (0-100%) of A.PSI1 1 Psi (or Digamma) of A.PV3 1 Legendre function Pv(A) of degree v = real(B) + imag(C).QV3 1 Legendre function Qv(A) of degree v = real(B) + imag(C).R22 1 R2 = A^2 + B^2.R2D1 1 Convert Radians to Degrees.RAND2 1 Uniform random values between A and B.RINT1 1 rint (A) (nearest integer).ROOTS2 1 Treats col A as f(t) = 0 and returns its roots.ROTT2 1 Rotate A by the (constant) shift B in the t-direction.SEC1 1 sec (A) (A in radians).SECD1 1 sec (A) (A in degrees).SIGN1 1 sign (+1 or -1) of A.SIN1 1 sin (A) (A in radians).SINC1 1 sinc (A) (sin (pi*A)/(pi*A)).SIND1 1 sin (A) (A in degrees).SINH1 1 sinh (A).SKEW1 1 Skewness of A.SQR1 1 A^2.SQRT1 1 sqrt (A).STD1 1 Standard deviation of A.STEP1 1 Heaviside step function H(A).STEPT1 1 Heaviside step function H(t-A).SUB2 1 A - B.SUM1 1 Cumulative sum of A.TAN1 1 tan (A) (A in radians).TAND1 1 tan (A) (A in degrees).TANH1 1 tanh (A).TCRIT2 1 Critical value for Student's t-distribution, with alpha = A and n = B.TDIST2 1 Student's t-distribution A(t,n), with t = A, and n = B.TN2 1 Chebyshev polynomial Tn(-1<A<+1) of degree B.UPPER1 1 The highest (maximum) value of A.XOR2 1 B if A == NaN, else A.Y01 1 Bessel function of A (2nd kind, order 0).Y11 1 Bessel function of A (2nd kind, order 1).YN2 1 Bessel function of A (2nd kind, order B).ZCRIT1 1 Critical value for the normal-distribution, with alpha = A.ZDIST1 1 Cumulative normal-distribution C(x), with x = A.SYMBOLSThe following symbols have special meaning:PI3.1415926...E2.7182818...EULER0.5772156...TMINMinimum t valueTMAXMaximum t valueTINCt incrementNThe number of recordsTTable with t-coordinates

**OPTIONS**

-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 occurrence 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.-FGive a comma-separated list of desired columns or ranges that should be part of the output (0 is first column) [Default outputs all columns].-HInput file(s) has header record(s). If used, the default number of header records isN_HEADER_RECS. Use-Hiif only input data should have header records [Default will write out header records if the input data have them]. Blank lines and lines starting with # are always skipped.-IReverses the output row sequence from ascending time to descending [ascending].-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 numerous 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 interval for the "time" column (see-N). Append+if you are specifying the number of equidistant points instead. 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 isd(double)]. UppercaseSorDwill force byte-swapping. Optionally, appendncol, the number of columns in your binary input file if it exceeds the columns needed by the program. Or appendcif the input file is netCDF. Optionally, appendvar1/var2/...to specify the variables to be read.-boSelects binary output. Appendsfor single precision [Default isd(double)]. UppercaseSorDwill force byte-swapping. Optionally, appendncol, the number of desired columns in your binary output file. [Default is same as input, but see-F]-mMultiple segment file(s). Segments are separated by a special record. For ASCII files the first character must beflag[Default is '>']. For binary files all fields must be NaN and-bmust set the number of output columns explicitly. By default the-msetting applies to both input and output. Use-miand-moto give separate settings to input and output.

**ASCII** **FORMAT** **PRECISION**

The ASCII output formats of numerical data are controlled by parameters in your .gmtdefaults4 file. Longitude and latitude are formatted according toOUTPUT_DEGREE_FORMAT, whereas other values are formatted according toD_FORMAT. Be aware that the format in effect can lead to loss of precision in the output, which can lead to various problems downstream. If you find the output is not written with enough precision, consider switching to binary output (-boif available) or specify more decimals using theD_FORMATsetting.

**NOTES** **ON** **OPERATORS**

(1) The operatorsPLMandPLMgcalculate the associated Legendre polynomial of degree L and order M in x which must satisfy -1 <= x <= +1 and 0 <= M <= L. x, L, and M are the three arguments preceding the operator.PLMis not normalized and includes the Condon- Shortley phase (-1)^M.PLMgis normalized in the way that is most commonly used in geophysics. The C-S phase can be added by using -M as argument.PLMwill overflow at higher degrees, whereasPLMgis stable until ultra high degrees (at least 3000). (2) Files that have the same names as some operators, e.g.,ADD,SIGN,=, etc. should be identified by prepending the current directory (i.e., ./LOG). (3) The stack depth limit is hard-wired to 100. (4) All functions expecting a positive radius (e.g.,LOG,KEI, etc.) are passed the absolute value of their argument. (5) TheDDTandD2DT2functions only work on regularly spaced data. (6) All derivatives are based on central finite differences, with natural boundary conditions. (7)ROOTSmust be the last operator on the stack, only followed by=.

**EXAMPLES**

To take the square root of the content of the second data column being piped throughgmtmathby process1 and pipe it through a 3rd process, use process1 |gmtmathSTDINSQRT=| process3 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 operatorLSQFITdoes 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 1 and your observed y(t) in column 3. The calculation becomesgmtmath-N4/1-Aty.d-C0 1ADD-C2 1.55STEPTADD-CaLSQFIT=solution.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 (col numbers 1 and 3) 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

**REFERENCES**

Abramowitz, M., and I. A. Stegun, 1964,HandbookofMathematicalFunctions, Applied Mathematics Series, vol. 55, Dover, New York. Holmes, S. A., and W. E. Featherstone, 2002, A unified approach to the Clenshaw summation and the recursive computation of very high degree and order normalised associated Legendre functions.JournalofGeodesy, 76, 279-299. Press, W. H., S. A. Teukolsky, W. T. Vetterling, and B. P. Flannery, 1992,NumericalRecipes, 2nd edition, Cambridge Univ., New York. Spanier, J., and K. B. Oldman, 1987,AnAtlasofFunctions, Hemisphere Publishing Corp.

**SEE** **ALSO**

GMT(1),grdmath(1)