gmtmath man page

gmtmath — Reverse Polish Notation (RPN) calculator for data tables


gmtmath [ -At_f(t).d[+e][+s|w] ] [ -Ccols ] [ -Eeigen ] [ -I ] [ -Nn_col[/t_col] ] [ -Q ] [ -S[f|l] ] [ -Tt_min/t_max/t_inc[+]|tfile ] [ -V[level] ] [ -bbinary ] [ -dnodata ] [ -fflags ] [ -ggaps ] [ -hheaders ] [ -iflags ] [ -oflags ] [ -sflags ] operand [ operand ] OPERATOR [ operand ] OPERATOR ... = [ outfile ]

Note: No space is allowed between the option flag and the associated arguments.


gmtmath will 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]. Data operations are element-by-element, not matrix manipulations (except where noted). Some operators only require one operand (see below). If no data tables are used in the expression then options -T, -N can be set (and optionally -bo to indicate the data type for binary tables). If STDIN is given, the standard input 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). Complicated or frequently occurring expressions may be coded as a macro for future use or stored and recalled via named memory locations.

Required Arguments

If operand can 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 that stdin will be read and placed on the stack; STDIN can appear more than once if necessary.
The name of a table data file that will hold the final result. If not given then the output is sent to stdout.

Optional Arguments

Requires -N and will partially initialize a table with values from the given file containing t and f(t) only. The t is placed in column t_col while f(t) goes into column n_col - 1 (see -N). Append +r to only place f(t) and leave the left hand side of the matrix equation alone. If used with operators LSQFIT and SVDFIT you can optionally append the modifier +e which will instead evaluate the solution and write a data set with four columns: t, f(t), the model solution at t, and the the residuals at t, respectively [Default writes one column with model coefficients]. Append +w if t_f(t).d has a third column with weights, or append +s if t_f(t).d has a third column with 1-sigma. In those two cases we find the weighted solution. The weights (or sigmas) will be output as the last column when +e is in effect.
Select 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). -Ca selects all columns, including time column, while -Cr reverses (toggles) the current choices. When -C is in effect it also controls which columns from a file will be placed on the stack.
Sets the minimum eigenvalue used by operators LSQFIT and SVDFIT [1e-7]. Smaller eigenvalues are set to zero and will not be considered in the solution.
Reverses the output row sequence from ascending time to descending [ascending].
Select the number of columns and optionally the column number that contains the "time" variable [0]. Columns are numbered starting at 0 [2/0]. If input files are specified then -N will add any missing columns.
Quick mode for scalar calculation. Shorthand for -Ca -N1/0 -T0/0/1.
Only report the first or last row of the results [Default is all rows]. This is useful if you have computed a statistic (say the MODE) and only want to report a single number instead of numerous records with identical values. Append l to get the last row and f to get the first row only [Default].
Required 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 -T with 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.
-V[level] (more ...)
Select verbosity level [c].
-bi[ncols][t] (more ...)
Select native binary input.
-bo[ncols][type] (more ...)
Select native binary output. [Default is same as input, but see -o]
-d[i|o]nodata (more ...)
Replace input columns that equal nodata with NaN and do the reverse on output.
-f[i|o]colinfo (more ...)
Specify data types of input and/or output columns.
-g[a]x|y|d|X|Y|D|[col]z[+|-]gap[u] (more ...)
Determine data gaps and line breaks.
-h[i|o][n][+c][+d][+rremark][+rtitle] (more ...)
Skip or produce header record(s).
-icols[l][sscale][ooffset][,...] (more ...)
Select input columns (0 is first column).
-ocols[,...] (more ...)
Select output columns (0 is first column).
-s[cols][a|r] (more ...)
Set handling of NaN records.
-^ or just -
Print a short message about the syntax of the command, then exits (NOTE: on Windows use just -).
-+ or just +
Print an extensive usage (help) message, including the explanation of any module-specific option (but not the GMT common options), then exits.
-? or no arguments
Print a complete usage (help) message, including the explanation of options, then exits.


Choose among the following 176 operators. "args" are the number of input and output arguments.

ABS1 1abs (A)
ACOS1 1acos (A)
ACOSH1 1acosh (A)
ACSC1 1acsc (A)
ACOT1 1acot (A)
ADD2 1A + B
AND2 1B if A == NaN, else A
ASEC1 1asec (A)
ASIN1 1asin (A)
ASINH1 1asinh (A)
ATAN1 1atan (A)
ATAN22 1atan2 (A, B)
ATANH1 1atanh (A)
BCDF3 1Binomial cumulative distribution function for p = A, n = B, and x = C
BPDF3 1Binomial probability density function for p = A, n = B, and x = C
BEI1 1bei (A)
BER1 1ber (A)
BITAND2 1A & B (bitwise AND operator)
BITLEFT2 1A << B (bitwise left-shift operator)
BITNOT1 1~A (bitwise NOT operator, i.e., return two's complement)
BITOR2 1A | B (bitwise OR operator)
BITRIGHT2 1A >> B (bitwise right-shift operator)
BITTEST2 11 if bit B of A is set, else 0 (bitwise TEST operator)
BITXOR2 1A ^ B (bitwise XOR operator)
CEIL1 1ceil (A) (smallest integer >= A)
CHICRIT2 1Chi-squared distribution critical value for alpha = A and nu = B
CHICDF2 1Chi-squared cumulative distribution function for chi2 = A and nu = B
CHIPDF2 1Chi-squared probability density function for chi2 = A and nu = B
COL1 1Places column A on the stack
COMB2 1Combinations n_C_r, with n = A and r = B
CORRCOEFF2 1Correlation coefficient r(A, B)
COS1 1cos (A) (A in radians)
COSD1 1cos (A) (A in degrees)
COSH1 1cosh (A)
COT1 1cot (A) (A in radians)
COTD1 1cot (A) (A in degrees)
CSC1 1csc (A) (A in radians)
CSCD1 1csc (A) (A in degrees)
DDT1 1d(A)/dt Central 1st derivative
D2DT21 1d^2(A)/dt^2 2nd derivative
D2R1 1Converts Degrees to Radians
DENAN2 1Replace NaNs in A with values from B
DILOG1 1dilog (A)
DIFF1 1Forward difference between adjacent elements of A (A[1]-A[0], A[2]-A[1], ..., NaN)
DIV2 1A / B
DUP1 2Places duplicate of A on the stack
ECDF2 1Exponential cumulative distribution function for x = A and lambda = B
ECRIT2 1Exponential distribution critical value for alpha = A and lambda = B
EPDF2 1Exponential probability density function for x = A and lambda = B
ERF1 1Error function erf (A)
ERFC1 1Complementary Error function erfc (A)
ERFINV1 1Inverse error function of A
EQ2 11 if A == B, else 0
EXCH2 2Exchanges A and B on the stack
EXP1 1exp (A)
FACT1 1A! (A factorial)
FCDF3 1F cumulative distribution function for F = A, nu1 = B, and nu2 = C
FCRIT3 1F distribution critical value for alpha = A, nu1 = B, and nu2 = C
FLIPUD1 1Reverse order of each column
FLOOR1 1floor (A) (greatest integer <= A)
FMOD2 1A % B (remainder after truncated division)
FPDF3 1F probability density function for F = A, nu1 = B, and nu2 = C
GE2 11 if A >= B, else 0
GT2 11 if A > B, else 0
HYPOT2 1hypot (A, B) = sqrt (A*A + B*B)
I01 1Modified Bessel function of A (1st kind, order 0)
I11 1Modified Bessel function of A (1st kind, order 1)
IFELSE3 1B if A != 0, else C
IN2 1Modified Bessel function of A (1st kind, order B)
INRANGE3 11 if B <= A <= C, else 0
INT1 1Numerically integrate A
INV1 11 / A
ISFINITE1 11 if A is finite, else 0
ISNAN1 11 if A == NaN, else 0
J01 1Bessel function of A (1st kind, order 0)
J11 1Bessel function of A (1st kind, order 1)
JN2 1Bessel function of A (1st kind, order B)
K01 1Modified Kelvin function of A (2nd kind, order 0)
K11 1Modified Bessel function of A (2nd kind, order 1)
KN2 1Modified Bessel function of A (2nd kind, order B)
KEI1 1kei (A)
KER1 1ker (A)
KURT1 1Kurtosis of A
LCDF1 1Laplace cumulative distribution function for z = A
LCRIT1 1Laplace distribution critical value for alpha = A
LE2 11 if A <= B, else 0
LMSSCL1 1LMS scale estimate (LMS STD) of A
LMSSCLW2 1Weighted LMS scale estimate (LMS STD) of A for weights in B
LOG1 1log (A) (natural log)
LOG101 1log10 (A) (base 10)
LOG1P1 1log (1+A) (accurate for small A)
LOG21 1log2 (A) (base 2)
LOWER1 1The lowest (minimum) value of A
LPDF1 1Laplace probability density function for z = A
LRAND2 1Laplace random noise with mean A and std. deviation B
LSQFIT1 0Let current table be [A | b] return least squares solution x = A \ b
LT2 11 if A < B, else 0
MAD1 1Median Absolute Deviation (L1 STD) of A
MADW2 1Weighted Median Absolute Deviation (L1 STD) of A for weights in B
MAX2 1Maximum of A and B
MEAN1 1Mean value of A
MEANW2 1Weighted mean value of A for weights in B
MEDIAN1 1Median value of A
MEDIANW2 1Weighted median value of A for weights in B
MIN2 1Minimum of A and B
MOD2 1A mod B (remainder after floored division)
MODE1 1Mode value (Least Median of Squares) of A
MODEW2 1Weighted mode value (Least Median of Squares) of A for weights in B
MUL2 1A * B
NAN2 1NaN if A == B, else A
NEG1 1-A
NEQ2 11 if A != B, else 0
NORM1 1Normalize (A) so max(A)-min(A) = 1
NOT1 1NaN if A == NaN, 1 if A == 0, else 0
NRAND2 1Normal, random values with mean A and std. deviation B
OR2 1NaN if B == NaN, else A
PCDF2 1Poisson cumulative distribution function for x = A and lambda = B
PERM2 1Permutations n_P_r, with n = A and r = B
PPDF2 1Poisson distribution P(x,lambda), with x = A and lambda = B
PLM3 1Associated Legendre polynomial P(A) degree B order C
PLMg3 1Normalized associated Legendre polynomial P(A) degree B order C (geophysical convention)
POP1 0Delete top element from the stack
POW2 1A ^ B
PQUANT2 1The B'th quantile (0-100%) of A
PQUANTW3 1The C'th weighted quantile (0-100%) of A for weights in B
PSI1 1Psi (or Digamma) of A
PV3 1Legendre function Pv(A) of degree v = real(B) + imag(C)
QV3 1Legendre function Qv(A) of degree v = real(B) + imag(C)
R22 1R2 = A^2 + B^2
R2D1 1Convert radians to degrees
RAND2 1Uniform random values between A and B
RCDF1 1Rayleigh cumulative distribution function for z = A
RCRIT1 1Rayleigh distribution critical value for alpha = A
RINT1 1rint (A) (round to integral value nearest to A)
RMS1 1Root-mean-square of A
RPDF1 1Rayleigh probability density function for z = A
ROLL2 0Cyclicly shifts the top A stack items by an amount B
ROTT2 1Rotate A by the (constant) shift B in the t-direction
SEC1 1sec (A) (A in radians)
SECD1 1sec (A) (A in degrees)
SIGN1 1sign (+1 or -1) of A
SIN1 1sin (A) (A in radians)
SINC1 1sinc (A) (sin (pi*A)/(pi*A))
SIND1 1sin (A) (A in degrees)
SINH1 1sinh (A)
SKEW1 1Skewness of A
SQR1 1A^2
SQRT1 1sqrt (A)
STD1 1Standard deviation of A
STDW2 1Weighted standard deviation of A for weights in B
STEP1 1Heaviside step function H(A)
STEPT1 1Heaviside step function H(t-A)
SUB2 1A - B
SUM1 1Cumulative sum of A
TAN1 1tan (A) (A in radians)
TAND1 1tan (A) (A in degrees)
TANH1 1tanh (A)
TAPER1 1Unit weights cosine-tapered to zero within A of end margins
TN2 1Chebyshev polynomial Tn(-1<A<+1) of degree B
TCRIT2 1Student's t distribution critical value for alpha = A and nu = B
TPDF2 1Student's t probability density function for t = A, and nu = B
TCDF2 1Student's t cumulative distribution function for t = A, and nu = B
UPPER1 1The highest (maximum) value of A
VAR1 1Variance of A
VARW2 1Weighted variance of A for weights in B
WCDF3 1Weibull cumulative distribution function for x = A, scale = B, and shape = C
WCRIT3 1Weibull distribution critical value for alpha = A, scale = B, and shape = C
WPDF3 1Weibull density distribution P(x,scale,shape), with x = A, scale = B, and shape = C
XOR2 1B if A == NaN, else A
Y01 1Bessel function of A (2nd kind, order 0)
Y11 1Bessel function of A (2nd kind, order 1)
YN2 1Bessel function of A (2nd kind, order B)
ZCDF1 1Normal cumulative distribution function for z = A
ZPDF1 1Normal probability density function for z = A
ZCRIT1 1Normal distribution critical value for alpha = A
ROOTS2 1Treats col A as f(t) = 0 and returns its roots


The following symbols have special meaning:

EPS_F1.192092896e-07 (sgl. prec. eps)
EPS_D2.2204460492503131e-16 (dbl. prec. eps)
TMINMinimum t value
TMAXMaximum t value
TRANGERange of t values
TINCt increment
NThe number of records
TTable with t-coordinates
TNORMTable with normalized t-coordinates
TROWTable with row numbers 1, 2, ..., N-1

ASCII Format Precision

The ASCII output formats of numerical data are controlled by parameters in your gmt.conf file. Longitude and latitude are formatted according to FORMAT_GEO_OUT, whereas other values are formatted according to FORMAT_FLOAT_OUT. 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 (-bo if available) or specify more decimals using the FORMAT_FLOAT_OUT setting.

Notes on Operators

1. The operators PLM and PLMg calculate 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. PLM is not normalized and includes the Condon-Shortley phase (-1)^M. PLMg is normalized in the way that is most commonly used in geophysics. The C-S phase can be added by using -M as argument. PLM will overflow at higher degrees, whereas PLMg is 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., ./).

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.

The DDT and D2DT2 functions only work on regularly spaced data.

6. All derivatives are based on central finite differences, with natural boundary conditions.

ROOTS must be the last operator on the stack, only followed by =.

Store, Recall and Clear

You may store intermediate calculations to a named variable that you may recall and place on the stack at a later time. This is useful if you need access to a computed quantity many times in your expression as it will shorten the overall expression and improve readability. To save a result you use the special operator STO@label, where label is the name you choose to give the quantity. To recall the stored result to the stack at a later time, use [RCL]@label, i.e., RCL is optional. To clear memory you may use CLR@label. Note that STO and CLR leave the stack unchanged.

8. The bitwise operators (BITAND, BITLEFT, BITNOT, BITOR, BITRIGHT, BITTEST, and BITXOR) convert a tables's double precision values to unsigned 64-bit ints to perform the bitwise operations. Consequently, the largest whole integer value that can be stored in a double precision value is 2^53 or 9,007,199,254,740,992. Any higher result will be masked to fit in the lower 54 bits. Thus, bit operations are effectively limited to 54 bits. All bitwise operators return NaN if given NaN arguments or bit-settings <= 0.

9. TAPER will interpret its argument to be a width in the same units as the time-axis, but if no time is provided (i.e., plain data tables) then the width is taken to be given in number of rows.


Users may save their favorite operator combinations as macros via the file gmtmath.macros in their current or user directory. The file may contain any number of macros (one per record); comment lines starting with # are skipped. The format for the macros is name = arg1 arg2 ... arg2 [ : comment] where name is how the macro will be used. When this operator appears on the command line we simply replace it with the listed argument list. No macro may call another macro. As an example, the following macro expects that the time-column contains seafloor ages in Myr and computes the predicted half-space bathymetry:

DEPTH = SQRT 350 MUL 2500 ADD NEG : usage: DEPTH to return half-space seafloor depths

Note: Because geographic or time constants may be present in a macro, it is required that the optional comment flag (:) must be followed by a space. As another example, we show a macro GPSWEEK which determines which GPS week a timestamp belongs to:

GPSWEEK = 1980-01-06T00:00:00 SUB 86400 DIV 7 DIV FLOOR : GPS week without rollover

Active Column Selection

When -Ccols is set then any operation, including loading of data from files, will restrict which columns are affected. To avoid unexpected results, note that if you issue a -Ccols option before you load in the data then only those columns will be updated, hence the unspecified columns will be zero. On the other hand, if you load the file first and then issue -Ccols then the unspecified columns will have been loaded but are then ignored until you undo the effect of -C.


To take the square root of the content of the second data column being piped through gmtmath by process1 and pipe it through a 3rd process, use

process1 | gmt math STDIN SQRT = | process3

To take log10 of the average of 2 data files, use

gmt math file1.d file2.d ADD 0.5 MUL LOG10 = 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:

gmt math samples.d T SQRT 350 MUL 2500 ADD SUB = | lpr

To take the average of columns 1 and 4-6 in the three data sets sizes.1, sizes.2, and sizes.3, use

gmt math -C1,4-6 sizes.1 sizes.2 ADD sizes.3 ADD 3 DIV = ave.d

To take the 1-column data set ages.d and calculate the modal value and assign it to a variable, try

gmt set mode_age = `gmt math -S -T ages.d MODE =`

To evaluate the dilog(x) function for coordinates given in the file t.d:

gmt math -Tt.d T DILOG = dilog.d

To demonstrate the use of stored variables, consider this sum of the first 3 cosine harmonics where we store and repeatedly recall the trigonometric argument (2*pi*T/360):

gmt math -T0/360/1 2 PI MUL 360 DIV T MUL STO@kT COS @kT 2 MUL COS ADD \
            @kT 3 MUL COS ADD = harmonics.d

To use gmtmath as a RPN Hewlett-Packard calculator on scalars (i.e., no input files) and calculate arbitrary expressions, use the -Q option. 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 = `gmt math -Q 1 1.75 ADD 2.2 DIV 60 COSD ADD KEI =`

To use gmtmath as 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 operator LSQFIT does this; it is your job to populate the matrix correctly first. The -A option will facilitate this. Suppose you have a 2-column file ty.d with t and b(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 becomes

gmt math -N4/1 -Aty.d -C0 1 ADD -C2 1.55 STEPT ADD -Ca LSQFIT = solution.d

Note we use the -C option to select which columns we are working on, then make active all the columns we need (here all of them, with -Ca) before calling LSQFIT. 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 pre-calculated table with the augmented matrix [ A | b ] in a file (say lsqsys.d), the least squares solution is simply

gmt math -T lsqsys.d LSQFIT = solution.d

Users must be aware that when -C controls which columns are to be active the control extends to placing columns from files as well. Contrast the different result obtained by these very similar commands:

echo 1 2 3 4 | gmt math STDIN -C3 1 ADD =
1    2    3    5


echo 1 2 3 4 | gmt math -C3 STDIN 1 ADD =
0    0    0    5


Abramowitz, M., and I. A. Stegun, 1964, Handbook of Mathematical Functions, 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 normalized associated Legendre functions. Journal of Geodesy, 76, 279-299.

Press, W. H., S. A. Teukolsky, W. T. Vetterling, and B. P. Flannery, 1992, Numerical Recipes, 2nd edition, Cambridge Univ., New York.

Spanier, J., and K. B. Oldman, 1987, An Atlas of Functions, Hemisphere Publishing Corp.

See Also

gmt, grdmath


October 20, 2016 5.3.1 GMT