常量、操作符和函數 數字
gnuplot 表示數字可分成整數、實數及複數三類:
整數:gnuplot 與 C 語言相同,採用 4 byte 儲存整數。故能表示 -2147483647 至 +2147483647 之間的整數。
實數:能表示約 6 或 7 位的有效位元,指數部份為不大於 308 的數字。
複數:以 {<real>,<imag>} 表示複數。其中<real>為複數的實數部分,<imag>為虛數部分,此兩部分均以實數型態表示。 如 3 + 2i 即以 {3,2} 表示。
gnuplot 儲存數位原則為,若能以整數方式儲存則以整數儲存數字,不然以實數方式儲存,其次以複數方式儲存。例如在 gnuplot 執行
print 1/3*3print 1./3*3
分別得到 0 和 1.0 的結果。這是因前者使用整數計算,而後者採用實數計算的結果。執行
print 1234.567print 12345 + 0.56789print 1.23e300 * 2e6print 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 所提供的函數。
表格 3 gnuplot functions
| 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 : bcomb(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)。