6.1 Function Splicing

In PyXPlot, as in Gnuplot, user-defined functions may be declared on the command line:

f(x) = x*sin(x)

It is also possible to declare functions which are valid only over certain ranges of argument space. For example, the following function would only be valid within the range $-2<x<2$:1

f(x)[-2:2] = x*sin(x)

The following function would only be valid when all of ${a,b,c}$ were in the range $-1 \to 1$:

f(a,b,c)[-1:1][-1:1][-1:1] = a+b+c

If an attempt is made to evaluate a function outside of its specified range, then an error results. This may be useful, for example, for plotting a function only within some specified range. The following would plot the function ${\rm sinc}(x)$, but only in the range $-2<x<7$:

f(x)[-2:7] = sin(x)/x
plot f(x)
\includegraphics{examples/eps/ex_funcsplice1.eps}
Figure 6.1: A simple example of the use of function splicing to truncate the function ${\rm sinc}(x)$ at $x=-2$ and $x=7$. See details in the text.

The output of this particular example can be seen in Figure 6.1. A similar effect could also have been achieved with the select keyword; see Section 4.3.

It is possible to make multiple declarations of the same function, over different regions of argument space; if there is an overlap in the valid argument space for multiple definitions, then later declarations take precedence. This makes it possible to use different functional forms for functions in different parts of parameter space, and is especially useful when fitting functions to data, if different functional forms are to be spliced together to fit different regimes in the data.

Another application of function splicing is to work with functions which do not have analytic forms, or which are, by definition, discontinuous, such as top-hat functions or Heaviside functions. The following example would define $f(x)$ to be a Heaviside function:

f(x) = 0
f(x)[0:] = 1

The following example would define $f(x)$ to follow the Fibonacci sequence, though it is not at all computationally efficient, and it is inadvisable to evaluate it for $x\gtrsim 8$:

f(x) = 1
f(x)[2:] = f(x-1) + f(x-2)
plot [0:8] f(x)
\includegraphics{examples/eps/ex_funcsplice2.eps}
Figure 6.2: An example of the use of function splicing to define a function which does not have an analytic form – in this case, the Fibonacci sequence. See the text for details.

The output of this example can be seen in Figure 6.2

Footnotes

  1. The syntax [-2:2] can also be written [-2 to 2].