返回介绍

GMT 入门

GMT 实例

GMT 进阶

附录

其他

gmtmath

发布于 2023-08-10 21:46:56 字数 29913 浏览 0 评论 0 收藏 0

贡献者

周茂

官方文档

gmtmath

简介

表数据的逆波兰表示法(RPN)计算

gmtmath 使用逆波兰表示法(Reverse Polish Notation)对一个或多个表数据文件或者常量进行加减乘除等操作。逆波兰表示法是一种后缀表示法,即将运算符写在操作数之后。该模块可以计算任意复杂的运算。模块默认逐元素进行计算操作。其中某些运算符仅需要一个参数。如果输入中不含表文件,则可以使用

gmt math [

operand

如果 operand 是一个文件,GMT 将试着以表文件的形式读取该文件。如果非文件,将被解析为

-At_f(t)[+e][+r][+s|w]

建立并求解线性方程。该选项需要 -Nn_col[/t_col] 选项,用来从给定的 t_f(t) 文件中进行初始化解方程所需的增广矩阵,其中 t_f(t) 文件只包含 tf(t) 两列。在增广矩阵中,t 变量于编号为 t_col 指定的列, f(t) 位于编号为 n_col - 1 的列。

  • +e 使用 LSQFITSVDFIT 操作符时,使用该选项,可以求解拟合系数。结果包括 4 列:t,f(t),t 位置的计算值,t 位置的残差 [默认只输出系数]

  • +r 选项只输出 f(t)

  • +s 指定 t_f(t) 的第三列为 1 倍中误差

  • +w 指定 t_f(t) 的第三列为权重

具体使用方法见倒数第二个示例。

-Ccols

选择要运算的列 cols 。多列以逗号分隔,类似 1,3-5,7 的表达形式也是允许的;此外,-Cx 等效于 -C0,-Cy 等效于 -C1。使用不带任何参数的

下面将展示如何使用

Operator 为运算符名称; args 分别表示输入参数和输出参数个数; Returns 中的 A B C … 等字符表示输入参数。所有的三角函数运算符默认输入为弧度,若运算符最后一个字母为 D 表示使用角度计算。

Operator

args

Returns

ABS

1 1

abs (A)

ACOS

1 1

acos (A)

ACOSH

1 1

acosh (A)

ACSC

1 1

acsc (A)

ACOT

1 1

acot (A)

ADD

2 1

A + B

AND

2 1

B if A == NaN, else A

ASEC

1 1

asec (A)

ASIN

1 1

asin (A)

ASINH

1 1

asinh (A)

ATAN

1 1

atan (A)

ATAN2

2 1

atan2 (A, B)

ATANH

1 1

atanh (A)

BCDF

3 1

Binomial cumulative distribution function for p = A, n = B, and x = C

BPDF

3 1

Binomial probability density function for p = A, n = B, and x = C

BEI

1 1

Kelvin function bei (A)

BER

1 1

Kelvin function ber (A)

BITAND

2 1

A & B (bitwise AND operator)

BITLEFT

2 1

A << B (bitwise left-shift operator)

BITNOT

1 1

~A (bitwise NOT operator, i.e., return two’s complement)

BITOR

2 1

A | B (bitwise OR operator)

BITRIGHT

2 1

A >> B (bitwise right-shift operator)

BITTEST

2 1

1 if bit B of A is set, else 0 (bitwise TEST operator)

BITXOR

2 1

A ^ B (bitwise XOR operator)

CEIL

1 1

ceil (A) (smallest integer >= A)

CHICRIT

2 1

Chi-squared distribution critical value for alpha = A and nu = B

CHICDF

2 1

Chi-squared cumulative distribution function for chi2 = A and nu = B

CHIPDF

2 1

Chi-squared probability density function for chi2 = A and nu = B

COL

1 1

Places column A on the stack

COMB

2 1

Combinations n_C_r, with n = A and r = B

CORRCOEFF

2 1

Correlation coefficient r(A, B)

COS

1 1

cos (A) (A in radians)

COSD

1 1

cos (A) (A in degrees)

COSH

1 1

cosh (A)

COT

1 1

cot (A) (A in radians)

COTD

1 1

cot (A) (A in degrees)

CSC

1 1

csc (A) (A in radians)

CSCD

1 1

csc (A) (A in degrees)

DDT

1 1

d(A)/dt Central 1st derivative

D2DT2

1 1

d^2(A)/dt^2 2nd derivative

D2R

1 1

Converts degrees to radians

DEG2KM

1 1

Converts spherical degrees to kilometers

DENAN

2 1

Replace NaNs in A with values from B

DILOG

1 1

dilog (A)

DIFF

1 1

Forward difference between adjacent elements of A (A[1]-A[0], A[2]-A[1], …, NaN)

DIV

2 1

A / B

DUP

1 2

Places duplicate of A on the stack

ECDF

2 1

Exponential cumulative distribution function for x = A and lambda = B

ECRIT

2 1

Exponential distribution critical value for alpha = A and lambda = B

EPDF

2 1

Exponential probability density function for x = A and lambda = B

ERF

1 1

Error function erf (A)

ERFC

1 1

Complementary Error function erfc (A)

ERFINV

1 1

Inverse error function of A

EQ

2 1

1 if A == B, else 0

EXCH

2 2

Exchanges A and B on the stack

EXP

1 1

exp (A)

FACT

1 1

A! (A factorial)

FCDF

3 1

F cumulative distribution function for F = A, nu1 = B, and nu2 = C

FCRIT

3 1

F distribution critical value for alpha = A, nu1 = B, and nu2 = C

FLIPUD

1 1

Reverse order of each column

FLOOR

1 1

floor (A) (greatest integer <= A)

FMOD

2 1

A % B (remainder after truncated division)

FPDF

3 1

F probability density function for F = A, nu1 = B, and nu2 = C

GE

2 1

1 if A >= B, else 0

GT

2 1

1 if A > B, else 0

HSV2LAB

3 3

Convert h,s,v triplets to l,a,b triplets, with h = A (0-360), s = B and v = C (0-1)

HSV2RGB

3 3

Convert h,s,v triplets to r,g,b triplets, with h = A (0-360), s = B and v = C (0-1)

HSV2XYZ

3 3

Convert h,s,v triplets to x,t,z triplets, with h = A (0-360), s = B and v = C (0-1)

HYPOT

2 1

hypot (A, B) = sqrt (A*A + B*B)

I0

1 1

Modified Bessel function of A (1st kind, order 0)

I1

1 1

Modified Bessel function of A (1st kind, order 1)

IFELSE

3 1

B if A != 0, else C

IN

2 1

Modified Bessel function of A (1st kind, order B)

INRANGE

3 1

1 if B <= A <= C, else 0

INT

1 1

Numerically integrate A

INV

1 1

1 / A

ISFINITE

1 1

1 if A is finite, else 0

ISNAN

1 1

1 if A == NaN, else 0

J0

1 1

Bessel function of A (1st kind, order 0)

J1

1 1

Bessel function of A (1st kind, order 1)

JN

2 1

Bessel function of A (1st kind, order B)

K0

1 1

Modified Kelvin function of A (2nd kind, order 0)

K1

1 1

Modified Bessel function of A (2nd kind, order 1)

KM2DEG

1 1

Converts kilometers to spherical degrees

KN

2 1

Modified Bessel function of A (2nd kind, order B)

KEI

1 1

Kelvin function kei (A)

KER

1 1

Kelvin function ker (A)

KURT

1 1

Kurtosis of A

LAB2HSV

3 3

Convert l,a,b triplets to h,s,v triplets

LAB2RGB

3 3

Convert l,a,b triplets to r,g,b triplets

LAB2XYZ

3 3

Convert l,a,b triplets to x,y,z triplets

LCDF

1 1

Laplace cumulative distribution function for z = A

LCRIT

1 1

Laplace distribution critical value for alpha = A

LE

2 1

1 if A <= B, else 0

LMSSCL

1 1

LMS (Least Median of Squares) scale estimate (LMS STD) of A

LMSSCLW

2 1

Weighted LMS scale estimate (LMS STD) of A for weights in B

LOG

1 1

log (A) (natural log)

LOG10

1 1

log10 (A) (base 10)

LOG1P

1 1

log (1+A) (accurate for small A)

LOG2

1 1

log2 (A) (base 2)

LOWER

1 1

The lowest (minimum) value of A

LPDF

1 1

Laplace probability density function for z = A

LRAND

2 1

Laplace random noise with mean A and std. deviation B

LSQFIT

1 0

Let current table be [A | b] return least squares solution x = A \ b

LT

2 1

1 if A < B, else 0

MAD

1 1

Median Absolute Deviation (L1 STD) of A

MADW

2 1

Weighted Median Absolute Deviation (L1 STD) of A for weights in B

MAX

2 1

Maximum of A and B

MEAN

1 1

Mean value of A

MEANW

2 1

Weighted mean value of A for weights in B

MEDIAN

1 1

Median value of A

MEDIANW

2 1

Weighted median value of A for weights in B

MIN

2 1

Minimum of A and B

MOD

2 1

A mod B (remainder after floored division)

MODE

1 1

Mode value (Least Median of Squares) of A

MODEW

2 1

Weighted mode value (Least Median of Squares) of A for weights in B

MUL

2 1

A * B

NAN

2 1

NaN if A == B, else A

NEG

1 1

-A

NEQ

2 1

1 if A != B, else 0

NORM

1 1

Normalize (A) so max(A)-min(A) = 1

NOT

1 1

NaN if A == NaN, 1 if A == 0, else 0

NRAND

2 1

Normal, random values with mean A and std. deviation B

OR

2 1

NaN if B == NaN, else A

PCDF

2 1

Poisson cumulative distribution function for x = A and lambda = B

PERM

2 1

Permutations n_P_r, with n = A and r = B

PPDF

2 1

Poisson distribution P(x,lambda), with x = A and lambda = B

PLM

3 1

Associated Legendre polynomial P(A) degree B order C

PLMg

3 1

Normalized associated Legendre polynomial P(A) degree B order C (geophysical convention)

POP

1 0

Delete top element from the stack

POW

2 1

A ^ B

PQUANT

2 1

The B’th quantile (0-100%) of A

PQUANTW

3 1

The C’th weighted quantile (0-100%) of A for weights in B

PSI

1 1

Psi (or Digamma) of A

PV

3 1

Legendre function Pv(A) of degree v = real(B) + imag(C)

QV

3 1

Legendre function Qv(A) of degree v = real(B) + imag(C)

R2

2 1

R2 = A^2 + B^2

R2D

1 1

Convert radians to degrees

RAND

2 1

Uniform random values between A and B

RCDF

1 1

Rayleigh cumulative distribution function for z = A

RCRIT

1 1

Rayleigh distribution critical value for alpha = A

RGB2HSV

3 3

Convert r,g,b triplets to h,s,v triplets, with r = A, g = B, and b = C (in 0-255 range)

RGB2LAB

3 3

Convert r,g,b triplets to l,a,b triplets, with r = A, g = B, and b = C (in 0-255 range)

RGB2XYZ

3 3

Convert r,g,b triplets to x,y,x triplets, with r = A, g = B, and b = C (in 0-255 range)

RINT

1 1

rint (A) (round to integral value nearest to A)

RMS

1 1

Root-mean-square of A

RMSW

1 1

Weighted root-mean-square of A for weights in B

RPDF

1 1

Rayleigh probability density function for z = A

ROLL

2 0

Cyclicly shifts the top A stack items by an amount B

ROTT

2 1

Rotate A by the (constant) shift B in the t-direction

SEC

1 1

sec (A) (A in radians)

SECD

1 1

sec (A) (A in degrees)

SIGN

1 1

sign (+1 or -1) of A

SIN

1 1

sin (A) (A in radians)

SINC

1 1

sinc (A) (sin (pi*A)/(pi*A))

SIND

1 1

sin (A) (A in degrees)

SINH

1 1

sinh (A)

SKEW

1 1

Skewness of A

SQR

1 1

A^2

SQRT

1 1

sqrt (A)

STD

1 1

Standard deviation of A

STDW

2 1

Weighted standard deviation of A for weights in B

STEP

1 1

Heaviside step function H(A)

STEPT

1 1

Heaviside step function H(t-A)

SUB

2 1

A - B

SUM

1 1

Cumulative sum of A

TAN

1 1

tan (A) (A in radians)

TAND

1 1

tan (A) (A in degrees)

TANH

1 1

tanh (A)

TAPER

1 1

Unit weights cosine-tapered to zero within A of end margins

TN

2 1

Chebyshev polynomial Tn(-1<A<+1) of degree B

TCRIT

2 1

Student’s t distribution critical value for alpha = A and nu = B

TPDF

2 1

Student’s t probability density function for t = A, and nu = B

TCDF

2 1

Student’s t cumulative distribution function for t = A, and nu = B

UPPER

1 1

The highest (maximum) value of A

VAR

1 1

Variance of A

VARW

2 1

Weighted variance of A for weights in B

VPDF

3 1

Von Mises density distribution V(x,mu,kappa), with angles = A, mu = B, and kappa = C

WCDF

3 1

Weibull cumulative distribution function for x = A, scale = B, and shape = C

WCRIT

3 1

Weibull distribution critical value for alpha = A, scale = B, and shape = C

WPDF

3 1

Weibull density distribution P(x,scale,shape), with x = A, scale = B, and shape = C

XOR

2 1

B if A == NaN, else A

XYZ2HSV

3 3

Convert x,y,z triplets to h,s,v triplets

XYZ2LAB

3 3

Convert x,y,z triplets to l,a,b triplets

XYZ2RGB

3 3

Convert x,y,z triplets to r,g,b triplets

Y0

1 1

Bessel function of A (2nd kind, order 0)

Y1

1 1

Bessel function of A (2nd kind, order 1)

YN

2 1

Bessel function of A (2nd kind, order B)

ZCDF

1 1

Normal cumulative distribution function for z = A

ZPDF

1 1

Normal probability density function for z = A

ZCRIT

1 1

Normal distribution critical value for alpha = A

ROOTS

2 1

Treats col A as f(t) = 0 and returns its roots

其他符号

以下符号具有特殊的含义:

PI

3.1415926…

E

2.7182818…

EULER

0.5772156…

PHI

1.6180339… (golden ratio)

EPS_F

1.192092896e-07 (sgl. prec. eps)

EPS_D

2.2204460492503131e-16 (dbl. prec. eps)

TMIN

Minimum t value

TMAX

Maximum t value

TRANGE

Range of t values

TINC

t increment

N

The number of records

T

Table with t-coordinates

TNORM

Table with normalized t-coordinates

TROW

Table with row numbers 1, 2, …, N-1

上述符号均可以作为变量使用,当其为多个数时,逐元素操作。以

  1. PLMPLMg 运算符用来计算 L 阶 M 次 x 的缔合勒让德函数;各参数的范围应该满足 -1 <= x <= +1 、0 <= M <= L。 PLM 运算符没有经过标准化,并且乘以 phase (-1)^M。 PLMg 使用大地测量/地球物理常见的标准化。使用

    用户可以将中间计算结果储存到一个变量中,并在后续计算中调用该变量。这在需要对某部分进行多次重复计算时可以提高效率和可读性。保存结果需要使用特殊的运算符 STO@label , 其中 label 是变量的名称。调用该变量时,使用 [RCL]@labelRCL 是可选的。使用后要清除该变量,可以使用 CLR@labelSTOCLR 均不影响计算中的堆栈。

    用户可以将特定的运算符组合保存为宏文件 gmtmath.macros 。文件中可以包含任意数量的宏, # 开头的行为注释。宏的格式为

    name = arg1 arg2 ... arg2[ : comment]
    

    其中,name 是宏名,当此运算符出现在命令中时,则将其简单替换为参数列表。宏不可以互相调用。下面将给出一个宏的例子:宏 DEPTH 将在时间列以 Myr 为单位给出海底构造的年龄,并计算预测的 half-space 海深

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

    由于在宏中可能使用地理或时间常数,因此可使用 : 后加一个空格的形式作为注释的开端。下面是一个 GPSWEEK 宏,用于确定给定的时间处于的 GPS 周数(相对于某个参考时刻):

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

    参与运算列的选择

    -Ccols 设置后,任何操作,包括从文件中加载数据都会受到影响。因此,当 -Ccols 放在数据加载前时,只有设定的列会更新,其他的列会置为 0,若不想其他列置为 0,则需把文件放在 -Ccols 前。

    绝对时间列

    如果输入数据有多列并且包含绝对时间列(通过

    使用

    将两种不同单位的长度相减,并赋值给 length 变量

    length=`gmt math -Q 15c 2i SUB =`
    

    计算不同单位的长度之间的比,输出无量纲

    ratio=`gmt math -Qn 15c 2i DIV =`
    

    对 process1 产生的数据进行开方,然后使用管道传递给 process3

    process1 | gmt math STDIN SQRT = | process3
    

    对两个数据文件进行平均,并对结果取 log10

    gmt math file1.txt file2.txt ADD 0.5 MUL LOG10 = file3.txt
    

    对包含海底地形年代(以 m.y. 为单位)和深度(以 m 为单位)的数据文件 samples.txt ,使用公式 depth(单位为 m) = 2500 + 350 * sqrt (age) 计算正常深度,并最终获得深度异常

    gmt math samples.txt T SQRT 350 MUL 2500 ADD SUB =
    

    取三个文件对应列(第 1 列,第 4 到 6列)的平均值

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

    从含有一列数据的 ages.txt 文件中计算模值,并赋值给变量

    mode_age=`gmt math -S -T ages.txt MODE =`
    

    计算 t.txt 中坐标的 dilog(x) 函数值

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

    下面展示一个使用存储变量的例子。将 (2*pi*T/360) 作为变量储存,对该变量分别乘以 2 和 3 ,并对上述结果求余弦,然后加和

    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.txt
    

    使用 gmtmath 实现标量计算(不含输入文件)可以使用

    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.

    相关模块

    grdmath

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文