Sponsored links: Algebra eBooks
 

Help Index

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

The Maxima on-line user's manual

Algebra Calculator

Search:

Function Calculator

Function

39.2 Function=============

39.2.1 Ordinary functions-------------------------

To define a function in Maxima you use the := operator. E.g.

f(x) := sin(x)

defines a function f. Anonmyous functions may also be created usinglambda. For example

lambda ([i, j], ...)

can be used instead of f where

f(i,j) := block ([], ...); map (lambda ([i], i+1), l)

would return a list with 1 added to each term.

You may also define a function with a variable number of arguments,by having a final argument which is assigned to a list of the extraarguments:

     (%i1) f ([u]) := u;
     (%o1)                      f([u]) := u
     (%i2) f (1, 2, 3, 4);
     (%o2)                     [1, 2, 3, 4]
     (%i3) f (a, b, [u]) := [a, b, u];
     (%o3)               f(a, b, [u]) := [a, b, u]
     (%i4) f (1, 2, 3, 4, 5, 6);
     (%o4)                 [1, 2, [3, 4, 5, 6]]

The right hand side of a function is an expression. Thus if youwant a sequence of expressions, you do

f(x) := (expr1, expr2, ...., exprn);

and the value of <exprn> is what is returned by the function.

If you wish to make a return from some expression inside thefunction then you must use block and return.

block ([], expr1, ..., if (a > 10) then return(a), ..., exprn)

is itself an expression, and so could take the place of the righthand side of a function definition. Here it may happen that the returnhappens earlier than the last expression.

The first [] in the block, may contain a list of variables andvariable assignments, such as [a: 3, b, c: []], which would cause thethree variables a,b,and c to not refer to their global values,but rather have these special values for as long as the code executesinside the block, or inside functions called from inside the block.This is called dynamic binding, since the variables last from the startof the block to the time it exits. Once you return from the block,or throw out of it, the old values (if any) of the variables will berestored. It is certainly a good idea to protect your variables inthis way. Note that the assignments in the block variables, are donein parallel. This means, that if you had used c: a in the above,the value of c would have been the value of a at the time you justentered the block, but before a was bound. Thus doing something like

block ([a: a], expr1, ... a: a+3, ..., exprn)

will protect the external value of a from being altered, but wouldlet you access what that value was. Thus the right hand side of theassignments, is evaluated in the entering context, before any bindingoccurs. Using just block ([x], ... would cause the x to have itselfas value, just as if it would have if you entered a fresh Maximasession.

The actual arguments to a function are treated in exactly same way asthe variables in a block. Thus in

f(x) := (expr1, ..., exprn);

and

f(1);

we would have a similar context for evaluation of the expressions asif we had done

block ([x: 1], expr1, ..., exprn)

Inside functions, when the right hand side of a definition, may becomputed at runtime, it is useful to use define and possibly buildq.

39.2.2 Array functions----------------------

An array function stores the function value the first time it is calledwith a given argument, and returns the stored value, withoutrecomputing it, when that same argument is given. Such a function isoften called a memoizing function.

Array function names are appended to the global list arrays (notthe global list functions). arrayinfo returns the list ofarguments for which there are stored values, and listarray returnsthe stored values. dispfun and fundef return the array functiondefinition.

arraymake constructs an array function call, analogous tofunmake for ordinary functions. arrayapply applies an arrayfunction to its arguments, analogous to apply for ordinary functions.There is nothing exactly analogous to map for array functions,although map(lambda([<x>], <a>[<x>]), <L>) or makelist(<a>[<x>],<x>, <L>), where <L> is a list, are not too far off the mark.

remarray removes an array function definition (including anystored function values), analogous to remfunction for ordinaryfunctions.

kill(<a>[<x>]) removes the value of the array function <a> storedfor the argument <x>; the next time <a> is called with argument <x>,the function value is recomputed. However, there is no way to removeall of the stored values at once, except for kill(<a>) orremarray(<a>), which also remove the function definition.

There are also some inexact matches for function. Try ?? function to see them.

(%o1)                                true
(%i2) 

Function Example

Related Examples

function-numer-sqrt

f(x):=x^307-7*x^37+3*...

a=(1-%i*sqrt(3))/2;

f(a),numer;

Calculate

function-load-matrix

aa : matrix([a,b],[c,...

load("diag.mac");

mat_function(exp,aa);

Calculate

function-limit

function: limit((ln(1...

Calculate

function-log-taylor

k: 1-eps;

function: -( 1-k^2 + ...

taylor( function, eps...

Calculate

function

value : 3;

equation : a+2=b;

function(x) := x+3;

Calculate

function

value : 3;

equation : a+2 =b;

function(x) := x + 3;

Calculate

function

value : 3;

equation : a+2=b;

function(x) := x+3;

Calculate

function-kill

value : 3;

equation : a+2=b;

function(x) := x+3;

Calculate

function-hessian

-- function: hessian;

Calculate