==================================================================== Make Function ==================================================================== 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 3 x 2 4 3 x 5 4 3 2 3 (x (%e ) + (- 2x - 2x )%e + x + 2x + x )sin(x ) + 2 x 2 3 2 x 4 3 2 2 2 (3x (%e ) + (- 6x - 6x )%e + 3x + 6x + 3x )sin(x ) + x 2 2 x 3 2 2 x 2 (3x (%e ) + (- 6x - 6x)%e + 3x + 6x + 3x)sin(x ) + (%e ) + x 2 (- 2x - 2)%e + x + 2x + 1 Type: Expression Integer You could, of course, use the function eval 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 function which does exactly this. Issue this to create the function f(x) given by expr. :: function(expr, f, x) f Type: Symbol 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 function to create a multivariate function f(x1,...,xn). :: e := (x - y + 1)^2 * (x^2 * y + 1)^2 4 4 5 4 2 3 6 5 4 3 2 2 x y + (- 2x - 2x + 2x )y + (x + 2x + x - 4x - 4x + 1)y + 4 3 2 2 (2x + 4x + 2x - 2x - 2)y + x + 2x + 1 Type: Polynomial Integer function(e, g, [x, y]) g Type: Symbol In the case of just two variables, they can be given as arguments without making them into a list. :: function(e, h, x, y) h Type: Symbol Note that the functions created by function 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] ] +1 2+ | | +3 4+ Type: SquareMatrix(2,Integer) m2 := squareMatrix [ [1, 0], [-1, 1] ] + 1 0+ | | +- 1 1+ Type: SquareMatrix(2,Integer) h(m1, m2) +- 7836 8960 + | | +- 17132 19588+ Type: SquareMatrix(2,Integer) See Also: * )show MakeFunction