常量、操作符和函数
数字
gnuplot 表示数字可分成整数、实数及复数三类:
整数:gnuplot 与 C 语言相同,采用 4 byte 储存整数。故能表示 -2147483647 至 +2147483647 之间的整数。
实数:能表示约 6 或 7 位的有效位数,指数部份为不大于 308 的数字。
复数:以 {<real>,<imag>} 表示复数。其中<real>为复数的实数部分,<imag>为虚数部分,此两部分均以实数型态表示。 如 3 + 2i 即以 {3,2} 表示。
gnuplot 储存数字的原则为,若能以整数方式储存则以整数储存数字,不然以实数方式储存,其次以复数方式储存。例如在 gnuplot 执行
print 1/3*3 print 1./3*3
分别得到 0 和 1.0 的结果。这是因前者使用整数计算,而后者采用实数计算的结果。执行
print 1234.567 print 12345 + 0.56789 print 1.23e300 * 2e6 print 1.23e300 * 2e8
分别得到 1234.57、12345.6、2.46e+304 和 undefined value 的结果。这些例子是受到实数的有效位数和所能表现最大数字的限制。这是我们要注意的。
操作符
gnuplot 的操作符与 C 语言基本相同。 所有的操作均可做用在整数、实数或复数上。
表格 1 Unary Operators
Symbol |
Example |
Explanation |
- |
-a |
unary minus |
~ |
~a |
one's complement |
! |
!a |
logical negation |
! |
a! |
factorial |
表格 2 Binary Operators
Symbol |
Example |
Explanation |
** |
a**b |
exponentiation |
* |
a*b |
multiplication |
/ |
a/b |
division |
% |
a%b |
modulo |
+ |
a+b |
addition |
- |
a-b |
subtraction |
== |
a==b |
equality |
!= |
a!=b |
inequality |
& |
a&b |
bitwise AND |
^ |
a^b |
bitwise exclusive OR |
| |
a|b |
bitwise inclusive OR |
&& |
a&&b |
logical AND |
|| |
a||b |
logical OR |
?: |
a?b:c |
ternary operation |
函数
在 gnuplot 中函数的参数可以是整数,实数或是复数。表格 3是 gnuplot 所提供的函数。
Function |
Auguments |
Returns |
abs(x) |
any |
absolute value of x, |x|; same type |
abs(x) |
complex |
length of x, sqrt( real(x)^2 + imag(x)^2 ) |
acos(x) |
any |
1/cos(x) (inverse cosine) in radians |
Acosh(x) |
any |
cosh−1 x (inverse hyperbolic cosine) in radians |
arg(x) |
complex |
the phase of x in radians |
asin(x) |
any |
1/sin(x) (inverse sin) in radians |
asinh(x) |
any |
sinh−1 x (inverse hyperbolic sin) in radians |
atan(x) |
any |
1/tan(x) (inverse tangent) in radians |
atan2(y,x) |
int or real |
tan−1(y/x) (inverse tangent) |
atanh(x) |
any |
tanh−1 x (inverse hyperbolic tangent) in radians |
besj0(x) |
int or real |
J0 Bessel function of x |
besj1(x) |
int or real |
J1 Bessel function of x |
besy0(x) |
int or real |
Y0 Bessel function of x |
besy1(x) |
int or real |
Y1 Bessel function of x |
ceil(x) |
any |
smallest integer not less than x (real part) |
cos(x) |
radians |
cos x, cosine of x |
cosh(x) |
radians |
cosh x, hyperbolic cosine of x |
erf(x) |
any |
Erf(real(x)), error function of real(x) |
erfc(x) |
any |
Erfc(real(x)), 1.0 - error function of real(x) |
exp(x) |
any |
exponential function of x |
floor(x) |
any |
largest integer not greater than x (real part) |
gamma(x) |
any |
Gamma(real(x)), gamma function of real(x) |
ibeta(p,q,x) |
any |
Ibeta(real(p,q,x)), ibeta function of real(p,q,x) |
inverf(x) |
any |
inverse error function of real(x) |
igamma(a,x) |
any |
Igamma(real(a,x)), igamma function of real(a,x) |
imag(x) |
complex |
imaginary part of x as a real number |
invnorm(x) |
any |
inverse normal distribution function of real(x) |
int(x) |
real |
integer part of x, truncated toward zero |
lambertw(x) |
real |
Lambert W function |
lgamma(x) |
any |
Lgamma(real(x)), lgamma function of real(x) |
log(x) |
any |
ln(x), natural logarithm (base e) of x |
log10(x) |
any |
log(x), logarithm (base 10) of x |
norm(x) |
any |
normal distribution (Gaussian) function of real(x) |
rand(x) |
any |
normal distribution (Gaussian) function of real(x) |
real(x) |
any |
Rand(real(x)), pseudo random number generator |
sgn(x) |
any |
real part of x |
sin(x) |
any |
1 if x>0, -1 if x<0, 0 if x=0. imag(x) ignored |
sinh(x) |
radians |
sin(x), sine of x |
sqrt(x) |
radians |
sinh(x), hyperbolic sine x |
tan(x) |
any |
sqrt(x), square root of x |
tanh(x) |
complex |
tan(x), tangent of x |
column(x) |
int |
column x during datafile manipulation. |
defined(X) |
variable name |
returns 1 if a variable X is defined, 0 otherwise. |
tm hour(x) |
int |
the hour |
tm mday(x) |
int |
the day of the month |
tm min(x) |
int |
the minute |
tm mon(x) |
int |
the month |
tm sec(x) |
int |
the second |
tm wday(x) |
int |
the day of the week |
tm yday(x) |
int |
the day of the year |
tm year(x) |
int |
the year |
valid(x) |
int |
test validity of column(x) during datafile manip. |
下面举一些例子:
plot [0.5:20] besj0(x), besj1(x), besy0(x), besy1(x) plot [0:5] erf(x), erfc(x), inverf(x)
用户自定义函数和常量
在 gnuplot 中,用户可自定函数。函数可有 1 至 5 个自变量。 其定义函数的语法如下:
<function-name> ( <dummy1> {,<dummy2> {, ...}}) = <expression>
而用户定义常数的语法如下:
<variable-name> = <constant-expression>
下面举一些例子:
# 常数 w 为 2。 w = 2 # 常数 q 为小于但最接近 tan(pi/2 - 0.1) 的整数。 q = floor(tan(pi/2 - 0.1)) # 函数 f(x) 为 sin(w*x),其中 w 为常数。 f(x) = sin(w*x) # 函数 sinc(x) 为 sin(pi*x)/(pi*x)。 sinc(x) = sin(pi*x)/(pi*x) # 函数 delta(t) 为脉冲函数。 delta(t) = (t == 0) # 函数 ramp(t) 当其小于零为零,当其大于零为斜率等于 1 的直线。 ramp(t) = (t > 0) ? t : 0 # 函数 min(a,b) 取两者中较小的数。 min(a,b) = (a < b) ? a : b comb(n,k) = n!/(k!*(n-k)!) len3d(x,y,z) = sqrt(x*x+y*y+z*z) plot f(x) = sin(x*a), a = 0.2, f(x), a = 0.4, f(x)
gnuplot 已定义的常数仅有 pi (pi = 3.14159)。