It is sometimes useful to be able to define a function given by the result of a calculation.
Suppose that you have obtained the following expression after several computations and that you now want to tabulate the numerical values of f for x between -1 and +1 with increment 0.1.
expr := (x - exp x + 1)^2 * (sin(x^2) * x + 1)^3
Type: Expression Integer
You could, of course, use the function evalevalExpression within a loop and evaluate expr twenty-one times, but this would be quite slow. A better way is to create a numerical function f such that f(x) is defined by the expression expr above, but without retyping expr! The package MakeFunction provides the operation functionfunctionMakeFunction which does exactly this.
Issue this to create the function f(x) given by expr.
function(expr, f, x)
To tabulate expr, we can now quickly evaluate f 21 times.
tbl := [f(0.1 * i - 1) for i in 0..20];
Type: List Float
Use the list [x1,...,xn] as the third argument to functionfunctionMakeFunction to create a multivariate function f(x1,...,xn).
e := (x - y + 1)^2 * (x^2 * y + 1)^2
Type: Polynomial Integer
function(e, g, [x, y])
In the case of just two variables, they can be given as arguments without making them into a list.
function(e, h, x, y)
Note that the functions created by functionfunctionMakeFunction are not limited to floating point numbers, but can be applied to any type for which they are defined.
m1 := squareMatrix [ [1, 2], [3, 4] ]
m2 := squareMatrix [ [1, 0], [-1, 1] ]