Function

A function may include variables as parameters, independent variables e.g. $x,y,...$ and other functions.

The \field{...} command determines how the numbers within the function will be interpreted.

Syntax for a function.

\function[<action>]{<variable>}{<expression>}
  • action see table below
  • variable name of the function
  • expression definition of the function

Examples functions

\function[normalize]{f}{x*x+3+4+0*z}            % defines a function f of one variable: f(x)
\function{g}{x^4 - 5*x*y^3 + 4x^2 + 3y + 7}     % defines a function g of two variables: g(x,y)

$$f = x^2 + 7$$
$$g = x^4 - 5xy^3 + 4x^2 + 3y + 7$$

Actions

1
2
3
4
5
\begin{variables}
  \number{a}{1}
  \number{b}{3}
  \function[...]{...}{...}
\end{variables}
Action Description Example
replace Replace all variables in the expression (this is the default action and can be omitted) \function[replace]{f}{a/b}
$f = \frac{1}{3}$
normalize Replaces all variables and normalizes the expression. Normalize applies the following rules. \function[normalize]{f}{x*x+3+4+0*z}
$f = x^2 + 7$
expand Replaces all variables and expands the expression. Expand applies the following rules. \function[expand, normalize]{f}{(x-1)(x-2)}
$f = x^2 - 3x + 2$
sort Replaces all variables and sorts the expression. \function[sort]{f}{c-b+a*c*b}
$f = abc - b + c$
calculate Replaces all variables and calculates the expression. Using this option will always result in a number, in the case where a variable is undefined it takes the value 0. \function[calculate]{f}{a/b}
$f = 0.333333333333333$

amount of digit
substitute Creates a new function g by replacing a free variable x in f by an earlier defined variable (possible again a function) g. \substitute{h}{f}{x}{g}

Amount of digits

Suppose we set the field to real or complex (See \field{...}). Then the functions evaluated with the calculate option will show the result with 15 digits behind the decimal mark.

Quite often this is undesired when you want to output this value on the screen. In order to reduce the amount of digits behind the decimal mark, you can provide a second (optional) parameter. The calculated result will then be rounded to this precision.

Optional Second parameter

  • number Rounds the calculated result to the given number. This number must be higher then the specified corrector precision.
  • display Rounds the calculated result to the specified display precision.
1
2
3
4
5
6
7
8
9
10
% field is real
 
\number{a}{1}
\number{b}{3}
\function[calculate, 3]{f}{a/b}
 
% instead of a number one can use the option display
% the result will then be displayed based on \displayprecision
 
\function[calculate, display]{f}{a/b}

$$f = 0.333$$

More details on precision can be found here
Rounding

Evaluation of a function

The command \substitute is used for the evaluation of a function and for the composition of two functions. For more details about substitute see here. It can be used for functions of serveral variables in the same way.

Here are two examples for the evaluation of a function:

Example evaluation of a function

1
2
3
\number{a}{1}
\function{f}{2*x^2 -1}     % defines a function f of the variables x                        
\substitute[normalize]{f(1)}{f}{x}{a}   % evaluation at x=1.

The function $$f(x) = 2x^2 - 1$$ evaluated at $$x=2$$ yields $$f(1) = 7$$

1
2
3
\number{a}{1}
\function{f}{2*x^2 -y}     % defines a function f of the variables x and y                        
\substitute[normalize]{f(1,y)}{f}{x}{a}   % evaluation at x=1.

The function $$f(x,y) = 2x^2 - y$$ evaluated at $$x=1$$ yields $$f(1,y) = 2-y$$

Composition of two functions

Example composition of functions

1
2
3
4
% field is real
\function{f}{exp(y)}     % defines the exponential function exp(y)
\function{g}{-x^2}       % defines the function g(x) = -x^2.
\substitute[normalize]{gauss}{f}{y}{g}   % defines the function f(g(x)) = exp{-x^2}

The composition of the functions $$f(y) = \exp(y)$$ and $$g(x) = x^2$$ produces the Gauss-function $$f(g(x)) = \exp(-x^2)$$