Frontend

January 29 2007 by
false

5

The frontend command is a bit tricky. The basic idea is that it replaces expressions in the arguments to a procedure with names, evaluates the procedure with the replaced arguments, then back-substitutes the expressions for the the names in the final result. The first argument passed to frontend is the procedure that is to be evaluated. The second argument is a list containing the arguments to be passed to the procedure. Remaining arguments are optional, we'll get to those in a second. Suppose that we are given the expression

> y := cos(x) + 3*a*x^2:

and we want the coefficient of the term . We might try using coeff:

> coeff(y, x^2);

Error, unable to compute coeff

That fails because y is not a polynomial in x. The frontend command can be used here:

> frontend(coeff, [y,x^2]);

To get a better handle on how this works, I'll create a modified version of frontend, called Frontend:

> Frontend := proc(f)
frontend( proc()
printf("%a\n",'f(args)');
f(args) end proc, _rest)
end proc:

This version prints what the actual call looks like, before evaluating it:

> Frontend(coeff, [y,x^2]);

coeff(O+3*a*x^2,x^2)

Here we see that the actual call to coeff is coeff(O+3*a*x^2,x^2). This is clearly a polynomial in x, so coeff is able to return the desired result. You are probably wondering what the 'O' is doing in the expression. That is a frozen version of the expression cos(x). Let's try a slightly different example:

> Frontend(coeff, [cos(x)+sin(x)+5*x^2,x^2]);

coeff(O+O+5*x^2,x^2)

This time the first argument passed to coeff is O+O+5*x^2. The two O's are frozen expressions for cos(x) and sin(x). While they look the same, they are actually different names to Maple.

Now let's examine the optional third argument to frontend. It must be a list of exactly two sets. By default, this argument has the value [+,*,], which means that sums and products are not frozen. The first set in the list is a set of types. Any subexpressions in the list of arguments that match these types are not frozen. Let's see this in action:

> Frontend(coeff, [a*cos(x)+b*sin(x)+c*x^2, x^2], [+,]);

coeff(O+O+O,x^2)

Unlike the default, subexpressions of type * are frozen. That leads to an error, since the c*x^2 term was frozen. One way to avoid that is to include the term x^2 in the second set of the third argument. Expressions in the second set are not frozen (the first set consists of types not to freeze, the second set consists of expressions not to freeze.

> Frontend(coeff, [a*cos(x)+b*sin(x)+c*x^2, x^2], [+,x^2]);

>

coeff(O+O+c*x^2,x^2)

Now try calling frontend with the coefficient of x^2 set to 1 and without including x^2 in the set of expression not to freeze.

> Frontend(coeff, [a*cos(x)+b*sin(x)+x^2, x^2], [+,]);

coeff(O+O+x^2,x^2)

Notice that the x^2 term was not frozen. frontend never freezes, as a single expression, an exponent of an integer. That includes an expression of the form 1/expr, which is represented internally in Maple as expr^(-1).

With that in mind, we now return to the original problem, which was computing the eigenvectors of a Matrix of functional expressions. First, let's pass use the default third argument:

> Frontend(LinearAlgebra:-Eigenvectors,[<<f(a)|0>,<0|f(b)>>]);

LinearAlgebra:-Eigenvectors(O)

Error, (in LinearAlgebra:-Eigenvectors) invalid input: LinearAlgebra:-Eigenvectors expects its 1st argument, A, to be of type Matrix(square) but received O

That didn't work. We need to prevent frontend from freezing the complete Matrix. To do that, include the type Matrix in the set of types that are not to be frozen.

> Frontend(LinearAlgebra:-Eigenvectors,[<<f(a)|0>,<0|f(b)>>],[Matrix,]);

LinearAlgebra:-Eigenvectors(Matrix(2, 2, [[O,0],[0,O]]))

That works. Consider a slightly more complicated case:

> Frontend(LinearAlgebra:-Eigenvalues,[<<f(a)+f(b)|f(b)>,<f(a)|f(b)>>],[Matrix,]);

LinearAlgebra:-Eigenvalues(Matrix(2, 2, [[O,O],[O,O]]))

That is correct, but the result is more complicated than necessary because frontend froze the polynomials inside the Matrix. To prevent that we can include the sum and product types in the set of types not to freeze, along with Matrix:

> Frontend(LinearAlgebra:-Eigenvalues,[<<f(a)+f(b)|f(b)>,<f(a)|f(b)>>],[Matrix,+,*,]);

LinearAlgebra:-Eigenvalues(Matrix(2, 2, [[O+O,O],[O,O]]))

>

This post was generated using the MaplePrimes File Manager

View file details