[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10. Expressions

In general, any mathematical expression accepted by C, FORTRAN, Pascal, or BASIC is valid. The precedence of these operators is determined by the specifications of the C programming language. White space (spaces and tabs) is ignored inside expressions.

Complex constants are expressed as {<real>,<imag>}, where <real> and <imag> must be numerical constants. For example, {3,2} represents 3 + 2i; {0,1} represents 'i' itself. The curly braces are explicitly required here.

Note that gnuplot uses both "real" and "integer" arithmetic, like FORTRAN and C. Integers are entered as "1", "-10", etc; reals as "1.0", "-10.0", "1e1", 3.5e-1, etc. The most important difference between the two forms is in division: division of integers truncates: 5/2 = 2; division of reals does not: 5.0/2.0 = 2.5. In mixed expressions, integers are "promoted" to reals before evaluation: 5/2e0 = 2.5. The result of division of a negative integer by a positive one may vary among compilers. Try a test like "print -5/2" to determine if your system chooses -2 or -3 as the answer.

The integer expression "1/0" may be used to generate an "undefined" flag, which causes a point to ignored; the ternary operator gives an example.

The real and imaginary parts of complex expressions are always real, whatever the form in which they are entered: in {3,2} the "3" and "2" are reals, not integers.

10.1 Functions  
10.2 Operators  
10.3 User-defined  


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1 Functions

The functions in gnuplot are the same as the corresponding functions in the Unix math library, except that all functions accept integer, real, and complex arguments, unless otherwise noted.

For those functions that accept or return angles that may be given in either degrees or radians (sin(x), cos(x), tan(x), asin(x), acos(x), atan(x), atan2(x) and arg(z)), the unit may be selected by set angles, which defaults to radians.

 

10.1.1 abs  
10.1.2 acos  
10.1.3 acosh  
10.1.4 arg  
10.1.5 asin  
10.1.6 asinh  
10.1.7 atan  
10.1.8 atan2  
10.1.9 atanh  
10.1.10 besj0  
10.1.11 besj1  
10.1.12 besy0  
10.1.13 besy1  
10.1.14 ceil  
10.1.15 cos  
10.1.16 cosh  
10.1.17 erf  
10.1.18 erfc  
10.1.19 exp  
10.1.20 floor  
10.1.21 gamma  
10.1.22 ibeta  
10.1.23 inverf  
10.1.24 igamma  
10.1.25 imag  
10.1.26 invnorm  
10.1.27 int  
10.1.28 lgamma  
10.1.29 log  
10.1.30 log10  
10.1.31 norm  
10.1.32 rand  
10.1.33 real  
10.1.34 sgn  
10.1.35 sin  
10.1.36 sinh  
10.1.37 sqrt  
10.1.38 tan  
10.1.39 tanh  
10.1.40 column  
10.1.41 tm_hour  
10.1.42 tm_mday  
10.1.43 tm_min  
10.1.44 tm_mon  
10.1.45 tm_sec  
10.1.46 tm_wday  
10.1.47 tm_yday  
10.1.48 tm_year  
10.1.49 valid  


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.1 abs

The abs function returns the absolute value of its argument. The returned value is of the same type as the argument.

For complex arguments, abs(x) is defined as the length of x in the complex plane [i.e., sqrt(real(x)**2 + imag(x)**2) ].


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.2 acos

The acos function returns the arc cosine (inverse cosine) of its argument. acos returns its argument in radians or degrees, as selected by `set angles`.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.3 acosh

The acosh function returns the inverse hyperbolic cosine of its argument in radians.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.4 arg

The arg function returns the phase of a complex number in radians or degrees, as selected by set angles.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.5 asin

The asin function returns the arc sin (inverse sin) of its argument. asin returns its argument in radians or degrees, as selected by `set angles`.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.6 asinh

The asinh function returns the inverse hyperbolic sin of its argument in radians.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.7 atan

The atan function returns the arc tangent (inverse tangent) of its argument. atan returns its argument in radians or degrees, as selected by set angles.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.8 atan2

The atan2 function returns the arc tangent (inverse tangent) of the ratio of the real parts of its arguments. atan2 returns its argument in radians or degrees, as selected by set angles, in the correct quadrant.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.9 atanh

The atanh function returns the inverse hyperbolic tangent of its argument in radians.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.10 besj0

The besj0 function returns the j0th Bessel function of its argument. besj0 expects its argument to be in radians.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.11 besj1

The besj1 function returns the j1st Bessel function of its argument. besj1 expects its argument to be in radians.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.12 besy0

The besy0 function returns the y0th Bessel function of its argument. besy0 expects its argument to be in radians.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.13 besy1

The besy1 function returns the y1st Bessel function of its argument. besy1 expects its argument to be in radians.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.14 ceil

The ceil function returns the smallest integer that is not less than its argument. For complex numbers, ceil returns the smallest integer not less than the real part of its argument.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.15 cos

The cos function returns the cosine of its argument. cos accepts its argument in radians or degrees, as selected by set angles.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.16 cosh

The cosh function returns the hyperbolic cosine of its argument. cosh expects its argument to be in radians.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.17 erf

The erf function returns the error function of the real part of its argument. If the argument is a complex value, the imaginary component is ignored.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.18 erfc

The erfc function returns 1.0 - the error function of the real part of its argument. If the argument is a complex value, the imaginary component is ignored.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.19 exp

The exp function returns the exponential function of its argument (e raised to the power of its argument). On some implementations (notably suns), exp(-x) returns undefined for very large x. A user-defined function like safe(x) = x<-100 ? 0 : exp(x) might prove useful in these cases.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.20 floor

The floor function returns the largest integer not greater than its argument. For complex numbers, floor returns the largest integer not greater than the real part of its argument.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.21 gamma

The gamma function returns the gamma function of the real part of its argument. For integer n, gamma(n+1) = n!. If the argument is a complex value, the imaginary component is ignored.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.22 ibeta

The ibeta function returns the incomplete beta function of the real parts of its arguments. p, q > 0 and x in [0:1]. If the arguments are complex, the imaginary components are ignored.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.23 inverf

The inverf function returns the inverse error function of the real part of its argument.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.24 igamma

The igamma function returns the incomplete gamma function of the real parts of its arguments. a > 0 and x >= 0. If the arguments are complex, the imaginary components are ignored.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.25 imag

The imag function returns the imaginary part of its argument as a real number.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.26 invnorm

The invnorm function returns the inverse normal distribution function of the real part of its argument.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.27 int

The int function returns the integer part of its argument, truncated toward zero.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.28 lgamma

The lgamma function returns the natural logarithm of the gamma function of the real part of its argument. If the argument is a complex value, the imaginary component is ignored.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.29 log

The log function returns the natural logarithm (base e) of its argument.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.30 log10

The log10 function returns the logarithm (base 10) of its argument.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.31 norm

The norm function returns the normal distribution function (or Gaussian) of the real part of its argument.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.32 rand

The rand function returns a pseudo random number in the interval [0:1] using the real part of its argument as a seed. If seed < 0, the sequence is (re)initialized. If the argument is a complex value, the imaginary component is ignored.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.33 real

The real function returns the real part of its argument.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.34 sgn

The sgn function returns 1 if its argument is positive, -1 if its argument is negative, and 0 if its argument is 0. If the argument is a complex value, the imaginary component is ignored.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.35 sin

The sin function returns the sine of its argument. sin expects its argument to be in radians or degrees, as selected by set angles.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.36 sinh

The sinh function returns the hyperbolic sine of its argument. sinh expects its argument to be in radians.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.37 sqrt

The sqrt function returns the square root of its argument.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.38 tan

The tan function returns the tangent of its argument. tan expects its argument to be in radians or degrees, as selected by set angles.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.39 tanh

The tanh function returns the hyperbolic tangent of its argument. tanh expects its argument to be in radians.

A few additional functions are also available.

 


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.40 column

column(x) may be used only in expressions as part of using manipulations to fits or datafile plots. See plot datafile using.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.41 tm_hour

The tm_hour function interprets its argument as a time, in seconds from 1 Jan 2000. It returns the hour (an integer in the range 0--23) as a real.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.42 tm_mday

The tm_mday function interprets its argument as a time, in seconds from 1 Jan 2000. It returns the day of the month (an integer in the range 1--31) as a real.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.43 tm_min

The tm_min function interprets its argument as a time, in seconds from 1 Jan 2000. It returns the minute (an integer in the range 0--59) as a real.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.44 tm_mon

The tm_mon function interprets its argument as a time, in seconds from 1 Jan 2000. It returns the month (an integer in the range 1--12) as a real.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.45 tm_sec

The tm_sec function interprets its argument as a time, in seconds from 1 Jan 2000. It returns the second (an integer in the range 0--59) as a real.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.46 tm_wday

The tm_wday function interprets its argument as a time, in seconds from 1 Jan 2000. It returns the day of the week (an integer in the range 1--7) as a real.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.47 tm_yday

The tm_yday function interprets its argument as a time, in seconds from 1 Jan 2000. It returns the day of the year (an integer in the range 1--366) as a real.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.48 tm_year

The tm_year function interprets its argument as a time, in seconds from 1 Jan 2000. It returns the year (an integer) as a real.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.1.49 valid

valid(x) may be used only in expressions as part of using manipulations to fits or datafile plots. See plot datafile using.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2 Operators

The operators in gnuplot are the same as the corresponding operators in the C programming language, except that all operators accept integer, real, and complex arguments, unless otherwise noted. The ** operator (exponentiation) is supported, as in FORTRAN.

Parentheses may be used to change order of evaluation.

10.2.1 Unary  
10.2.2 Binary  
10.2.3 Ternary  


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.1 Unary

The following is a list of all the unary operators and their usages:

 
   Symbol      Example    Explanation
     -           -a          unary minus
     +           +a          unary plus (no-operation)
     ~           ~a        * one's complement
     !           !a        * logical negation
     !           a!        * factorial
     $           $3        * call arg/column during using manipulation
(*) Starred explanations indicate that the operator requires an integer argument.

Operator precedence is the same as in Fortran and C. As in those languages, parentheses may be used to change the order of operation. Thus -2**2 = -4, but (-2)**2 = 4.

The factorial operator returns a real number to allow a greater range.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.2 Binary

The following is a list of all the binary operators and their usages:

 
   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           less than
     <=          a<=b          less than or equal to
     >           a>b           greater than
     >=          a>=b          greater than or equal to
     &           a&b         * bitwise AND
     ^           a^b         * bitwise exclusive OR
     |           a|b         * bitwise inclusive OR
     &&          a&&b        * logical AND
     ||          a||b        * logical OR

(*) Starred explanations indicate that the operator requires integer arguments.

Logical AND (&&) and OR (||) short-circuit the way they do in C. That is, the second && operand is not evaluated if the first is false; the second || operand is not evaluated if the first is true.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.3 Ternary

There is a single ternary operator:

 
   Symbol       Example      Explanation
     ?:          a?b:c     ternary operation

The ternary operator behaves as it does in C. The first argument (a), which must be an integer, is evaluated. If it is true (non-zero), the second argument (b) is evaluated and returned; otherwise the third argument (c) is evaluated and returned.

The ternary operator is very useful both in constructing piecewise functions and in plotting points only when certain conditions are met.

Examples:

Plot a function that is to equal sin(x) for 0 <= x < 1, 1/x for 1 <= x < 2, and undefined elsewhere:
 
     f(x) = 0<=x && x<1 ? sin(x) : 1<=x && x<2 ? 1/x : 1/0
     plot f(x)
Note that gnuplot quietly ignores undefined values, so the final branch of the function (1/0) will produce no plottable points. Note also that f(x) will be plotted as a continuous function across the discontinuity if a line style is used. To plot it discontinuously, create separate functions for the two pieces. (Parametric functions are also useful for this purpose.)

For data in a file, plot the average of the data in columns 2 and 3 against the datum in column 1, but only if the datum in column 4 is non-negative:

 
     plot 'file' using 1:( $4<0 ? 1/0 : ($2+$3)/2 )

Please see plot data-file using for an explanation of the using syntax.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.3 User-defined

New user-defined variables and functions of one through five variables may be declared and used anywhere, including on the plot command itself.

User-defined function syntax:
 
     <func-name>( <dummy1> {,<dummy2>} ... {,<dummy5>} ) = <expression>

where <expression> is defined in terms of <dummy1> through <dummy5>.

User-defined variable syntax:
 
     <variable-name> = <constant-expression>

Examples:
 
     w = 2
     q = floor(tan(pi/2 - 0.1))
     f(x) = sin(w*x)
     sinc(x) = sin(pi*x)/(pi*x)
     delta(t) = (t == 0)
     ramp(t) = (t > 0) ? t : 0
     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)

Note that the variable pi is already defined. But it is in no way magic; you may redefine it to be whatever you like.

Valid names are the same as in most programming languages: they must begin with a letter, but subsequent characters may be letters, digits, "$", or "_". Note, however, that the fit mechanism uses several variables with names that begin "FIT_". It is safest to avoid using such names. "FIT_LIMIT", however, is one that you may wish to redefine. See the documentation on fit for details.

See show functions, show variables, and fit.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by root l2-hrz on May, 9 2001 using texi2html