Next: About this document Up: Programming in Maple: The Previous: Fortran and C

Exercises

  1. Write a Maple procedure called remove1 where remove1(x,L) removes the first occurrence of the value from the list . If is not in the list , return FAIL.

  2. Write a Maple procedure called variance that computes the variance of a list of numbers. I.e. variance(x) computes

    where is the number of elements of the list and is the average of the numbers in list . Your routine should print an error if the list is empty.

  3. Write a Maple procedure that computes the Frobenius norm of an by matrix . The Frobenius norm is .

  4. Write a Maple procedure which sorts an array of numbers faster than the bubblesort algorithm in the section on arrays, e.g. by using shell sort, quick sort, merge sort, or heap sort or some other sorting algorithm. Modify your Maple procedure to accept an optional 2nd argument , a boolean function to be used for comparing two elements of the array.

  5. Write a Maple procedure which computes the Fibonacci polynomials . These polynomials satisfy the linear recurrence , , and . Compute and factor the first 10 Fibonacci polynomials. Can your program compute ?

  6. Write a Maple procedure called structure that when given a Maple expression returns the structure of the expression as a tree, namely

    For example: structure(x^3*sin(x)/cos(x)); should return

    
        [*, [^, x, 3], [function, sin, x], [^, [function, cos, x], -1]]

    Your routine should handle the Maple types integer, fraction, float, `+`, `*`, `^`, string, indexed, function, range, equation, set, list, series and table. Test your function on the following input

    
       Int( exp(-t)*sqrt(t)*ln(t), t=0..infinity ) =
       int( exp(-t)*sqrt(t)*ln(t), t=0..infinity );

  7. Extend the DIFF procedure to differentiate general powers, the functions and to return unevaluated instead of giving an error when it does not know how to differentiate the given input. Extend DIFF to handle repeated derivatives, i.e. how to DIFF the function DIFF. For example, DIFF(DIFF(f(x,y),x),y) should output the same result as DIFF(DIFF(f(x,y),y),x).

  8. Write a routine comb that when given a set of values and a non-negative integer returns a list of all combinations of the values of size . For example

    =-1.00.5plus##1`##1=12=^^M=12 > comb( a,b,c,d, 3 );

    a,b,c, a,b,d, a,c,d, b,c,d

    plusplus -100 plus Modify your routine to work for lists which may contain duplicates, e.g.

    =-1.00.5plus##1`##1=12=^^M=12 > comb( [a,b,b,c], 2 );

    [[a,b], [a,c], [b,b], [b,c]]

    plusplus -100 plus

  9. The Maple function degree computes the total degree of a polynomial in a given variable(s). For example, the polynomial

    =-1.00.5plus##1`##1=12=^^M=12 > p := x^3*y^3 + x^4 + y^5: > degree(p,x); # degree in x

    4

    > degree(p,x,y); # total degree in x and y

    6

    plusplus -100 plus We showed how one could code the degree function in Maple with our DEGREE function. However, the degree function and our DEGREE function only work for integer exponents. Extend the DEGREE so that it computes the degree of a general expression, which may have rational or even symbolic exponents, e.g.

    =-1.00.5plus##1`##1=12=^^M=12 > f := x^(3/2) + x + x^(1/2); 3/2 1/2 f := x + x + x

    > DEGREE(f,x); 3/2

    > h := (x^n + x^(n-1) + x^2) * y^m;

    n (n - 1) 2 m h := (x + x + x ) y

    > DEGREE(h,x); max(n, 2)

    > DEGREE(h,x,y); max(n, 2) + m

    plusplus -100 plus

  10. Write Maple procedures to add and multiply univariate polynomials and which are represented as

    1. a Maple list of coefficients i.e.

    2. a linked list of coefficients i.e. .

  11. The data structures in the above exercise are inefficient for sparse polynomials because zeros are represented explicitly. E.g. consider the polynomial . Represented as a Maple list it would be

    [10,0,0,0,0,0,0,0,1,1]

    and as a linked list it is

    [10,[0,[0,[0,[0,[0,[0,[0,[0,[1,[1,NIL]]]]]]]]]]].

    A more efficient representation for sparse polynomials is to store just the non-zero terms. Let us store the 'th non-zero term as where . Now write procedures to add and multiply sparse univariate polynomials which are represented as

    1. a Maple list of non-zero terms

    2. a linked list of non-zero terms

  12. The Gaussian integers are the set of complex numbers with integer coefficients, i.e. numbers of the form where and . What is most interesting about the Gaussian integers is that they form a Euclidean domain, i.e. they can be factored into primes, and you can use the Euclidean algorithm to compute the GCD of two Gaussian integers. Given a Gaussian integer let be the norm of . Write a Maple procedure REM that computes the remainder of two non-zero Gausian integers such that where . Use your remainder routine to compute the GCD of two Gaussian integers.

  13. Write a Maple n-ary procedure GCD that does integer and symbolic simplification of GCD calculations in . E.g. on input of GCD(b,GCD(b,a),-a), your procedure should return GCD(a,b). For integer inputs, your routine should compute the GCD directly. For symbolic inputs, your GCD procedure should know the following

    1. GCD(a,b) = GCD(b,a) (GCD's are commutative)
    2. GCD(GCD(a,b),c) = GCD(a,GCD(b,c)) = GCD(a,b,c) (GCD's are associative)
    3. GCD(0,a) = GCD(a) (the identity is 0)
    4. GCD(a) = GCD(a) = abs(a)

  14. Write a Maple procedure monomial(v,n) which computes a list of the monomials of total degree in the list of variables . For example, monomial([u,v],3) would return the list . Hint: consider the Taylor series expansion of the product

    in to order - see Maple's taylor command.

  15. Given a sequence of points where the 's are distinct, the Maple library function interp computes a polynomial of degree which interpolates the points using the Newton interpolation algorithm. Write a Maple procedure which computes a natural cubic spline for the points. A natural cubic spline is a piecewise cubic polynomial where each interval is defined by the cubic polynomial

    where the unknown coefficients are uniquely determined by the following conditions

    These conditions mean that the resulting piecewise polynomial is continuous. Write a Maple procedure which on input of the points as a list of 's in the form [x0, y0, x1, y1, ..., xn, yn], and a variable, outputs a list of the segment polynomials [f1, f2, ..., fn]. This requires that you create the segment polynomials with unknown coefficients, use Maple to compute the derivatives and solve the resulting equations. For example

    =-1.00.5plus##1`##1=12=^^M=12 > spline([0,1,2,3],[0,1,1,2],x);

    3 3 2 3 2 [- 1/3 x + 4/3 x, 2/3 x - 3 x + 13/3 x - 1, - 1/3 x + 3 x - 23/3 x + 7]

    plusplus -100 plus

  16. Design a data structure for representing a piecewise function. The representation for the piecewise cubic spline above does not interface with other facilities in Maple. Let us represent a piecewise function as an unevaluated function instead as follows. Let

    mean

    • Write a Maple procedure called IF that simplifies such a piecewise function for conditions which are purely numerical.
    • Write a procedure called `evalf/IF` which evaluates an IF expression in floating point, hence allowing IF expressions to be plotted.
    • Write a procedure called `diff/IF` which differentiates an IF expression.

    E.g. your procedures should result in the following behaviour.

    =-1.00.5plus##1`##1=12=^^M=12 > IF( x<0, sin(x), cos(x) );

    IF(x < 0, sin(x), cos(x)) > diff(",x); IF(x < 0, cos(x), - sin(x))

    > IF( Pi/3<0, sin(Pi/3), cos(Pi/3) ); 1/2 IF(1/3 Pi < 0, 1/2 3 , 1/2) > evalf("); .5000000000

    plusplus -100 plus Modify your IF procedure to simplify nested IF expressions, and to handle constant conditions like the example above. E.g. your IF procedure should result in the following behaviour.

    =-1.00.5plus##1`##1=12=^^M=12 > IF( x<0, 0, IF( x<1, x^2, IF(x>2, 0, 1-x^2) ) );

    2 2 IF(x < 0, 0, x < 1, x , 2 < x, 0, 1 - x )

    > IF( Pi/3<0, sin(Pi/3), cos(Pi/3) );

    1/2

    plusplus -100 plus

  17. The following iteration is known as the Halley iteration

    Program this iteration in Maple and check that it converges cubically. Prove that the convergence is cubic.

  18. The command dsolve solves ordinary differential equations analytically, although not all ODE's can be solved in closed form. But it is always possible to obtain a series solution which may be useful. We are given the ODE

    and the initial condition and we want to find the first few terms in the series

    Write a Maple procedure which on input of and the initial condition constructs a linear system of equations to solve. I.e. let

    substitute this finite sum into the ODE, equate coefficients and solve for the unknowns . Note, you will want to use the taylor command to truncate the result to order . Test your Maple procedure on the following ODE

    You should get the following series

    Compute the solution analytically using Maple's dsolve command and check that your series solution agrees with Maple's analytic solution.

    Develop a Newton iteration to solve for the series which converges quadratically. I.e. the 'th approximation

    is accurate to . Thus the iteration starts with and computes then and so on. Write a Maple procedure to compute the series.

  19. Modify the GaussianElimination procedure so that it does a complete row reduction of a rectangular matrix of rational numbers reducing the matrix to Reduced Row Echelon Form. Now modify your procedure to work with a matrix of formulae. Use the simplify function to simplify the intermediate results.

  20. The use of the Taylor series for computing becomes increasingly inefficient for large because it converges slowly. It also becomes increasingly inaccurate because of cancellation of positive and negative terms. Write a Maple procedure which sums the terms of the asymptotic series for for large until it converges. How large must be before this series converges at Digits decimal digits of precision? Combine the use of both the Taylor series and the asymptotic series to write a procedure that computes for all accurate to Digits decimal digits of precision.



Next: About this document Up: Programming in Maple: The Previous: Fortran and C


Klaus Steinberger
Mi Apr 13 12:51:51 MDT 1994