next up previous index
Next: Differentiating a Function Up: Functions Previous: Functions

Defining a Function

The following mathematical expression:

\begin{displaymath}f: x \mapsto x^2 + \frac{1}{2}
\end{displaymath}

is implemented in Maxima as 
\begin{figure}
\begin{tex2html_preform}\begin{verbatim}(C26) f(x) := x^2 + 1/2;
2 1
(D26) F(X) := X + -
2
(C27)\end{verbatim}\end{tex2html_preform}\end{figure}

There is a system call functions, which can be used to list all user defined functions:
\begin{figure}
\begin{tex2html_preform}\begin{verbatim}(C27) functions;
(D27) [F(X)]
(C28)\end{verbatim}\end{tex2html_preform}\end{figure}

Function F(x) can be now used like any other function, e.g, $\sin$, or $\cos$:
\begin{figure}
\begin{tex2html_preform}\begin{verbatim}(C35) f(2);
9
(D35) -
2...
...
2 1
(D36) (D + C) + -
2
(C37)\end{verbatim}\end{tex2html_preform}\end{figure}

In Maple, symbol := is used for an assignment. In order to define a function Maple uses notation that is somewhat closer to the mathematical original with a $\mapsto$ arrow:

\begin{figure}
\begin{tex2html_preform}\begin{verbatim}> f := x -> x^2 + 1/2;
2...
...> f(c + d);
2
(c + d) + 1/2>\end{verbatim}\end{tex2html_preform}\end{figure}

Maxima's := notation is a shorthand for calling a function define whose purpose is to define functions:

\begin{figure}
\begin{tex2html_preform}\begin{verbatim}(C37) define(g(x), x^2 + ...
...
(C38) g(2);
9
(D38) -
2
(C39)\end{verbatim}\end{tex2html_preform}\end{figure}

Maple, similarly, has a special form, unapply, which has the same effect as the combination of := and ->:

\begin{figure}
\begin{tex2html_preform}\begin{verbatim}> g := unapply(x^2 + 1/2,...
...:= x -> x + 1/2> g(2);
9/2>\end{verbatim}\end{tex2html_preform}\end{figure}

In Mathematica functions  are defined more like in Maxima. Here is an example:

\begin{figure}
\begin{tex2html_preform}\begin{verbatim}In[26]:= f[x_] = x^2 + 1/...
...ut[28]= - + (a + b)
2In[29]:=\end{verbatim}\end{tex2html_preform}\end{figure}

Observe the underscore that follows x in F[x_]. The underscore tells Mathematica that x in the expression on the right is to be understood as a formal parameter, as in $x\mapsto x^2 + 1/2$.

We can also use the delayed  Set, denoted by :=, while defining a function, although, in this case, it wouldn't make much difference, because the expression x^2 + 1/2 cannot be evaluated any further.

As in Maxima and in Maple, there is a system utility in Mathematica too, called Function, which can be used to define a function. Here is an example of how it works.

\begin{figure}
\begin{tex2html_preform}\begin{verbatim}In[34]:= g = Function[x, ...
...ut[38]= - + (a + b)
2In[39]:=\end{verbatim}\end{tex2html_preform}\end{figure}

Observe the ?g trick. By placing the question mark in front of a symbol, you can always ask Mathematica how it understands the symbol. We have used it already before when we asked Mathematica about delayed expressions. In this case function g is not stored in the same way as function f, even though they have the same effect on their arguments. It is stored as a lambda-expression instead. If you are familiar with Lisp or lambda calculus, you will find it quite natural to think about functions this way.

The definitions of a function in Maxima, Maple and Mathematica suffer from imprecision. In real mathematics we would usually specify precisely a set that x belongs to, as well as a set that the values of F belong to, e.g.,

\begin{displaymath}F: \mathbb{R}\owns x \mapsto x^2 + \frac{1}{2} \in \mathbb{R}\end{displaymath}

Not being able to convey this to symbol manipulation environments discussed in this tutorial may sometimes lead to incorrect results. Having a slave that does all the algebra for you does not release you from having to think about what you do.

We will see later on that there are ways to restrict x to certain ranges in this context.


next up previous index
Next: Differentiating a Function Up: Functions Previous: Functions
Zdzislaw Meglicki
2001-02-26