next up previous contents index
Next: Common data structures Up: Simple exercises in MAGMA Previous: Input-output   Contents   Index

Polynomials and other functions

Another (major) difference with MAPLE is that MAGMA does not like undefined variables. It is fair to say that MAGMA can't stand the sight of an undefined variable. For example, in MAPLE, you can type (without declaring $ x$) >f:=x^2; then >diff(f,x); (resp, >int(f,x);) and you will get the derivative (resp., anti-derivavtive) of $ x^2$. However, MAGMA will complain:

> f:=x^2;

>> f:=x^2;
      ^
User error: Identifier 'x' has not been declared or assigned

Instead, you must declare $ x$ first, by typing something like

> P<x>:=PolynomialRing(Integers());
> f:=x^2;
> Derivative(f);
2*x
> Integral(f) ;                    

>> Integral(f) ;
           ^
Runtime error in 'Integral': Coefficient ring of argument 1 is not a field
This says, MAGMA will differentiate $ f$ but it will not integrate it since the result, $ x^3/3$, will not belong to the ring $ P=\mathbb{Z}[x]$ you declared. To integrate, type:

> P<x>:=PolynomialRing(Rationals()); 
>  f:=x^2;                          
> Evaluate(f,3/2);
9/4
> Integral(f) ;
1/3*x^3

You can also do numerical integration of an improper integral:

> R := RealField();
> f := map< R -> R | x :-> x^2 >;      
> Integral(f, 0, 1); 
0.3333333333333333333333333331
> h:= map< R -> R | x :-> Sin(x)/x >;
> h(1/2);
0.9588510772084060005465758704
> h(0);

???(
    x: 0
)
>> h:= map< R -> R | x :-> Sin(x)/x >;
                                 ^
Runtime error in '/': Division by zero
> Integral(h, 0, 1: Al := "Open");
0.9460830703671830149413533089
Here ``Open'' chooses a certain subroutine to compute $ \int_0^1 \frac{\sin(x)}{x} dx$ since the integrand is undefined at $ x=0$.

There are also routines for infinite sums but I have not gotten some of them to work:

> g := map< Integers() -> R | x :-> 1/x^2 >;
> InfiniteSum(g,1);   
   
[Interrupted]
> PositiveSum(g,1); 
1.644934066848226436472415163
> g2 := map< Integers() -> R | m :-> (-1)^m/(m^2+1) >;
> AlternatingSum(g2,1);                               
-0.3639854725089334185248817081
> g3 := map< Integers() -> R | m :-> (-1)^m/Exp(m) >;    
> InfiniteSum(g3,1);                                 
-0.268941421369995120748840758179095161053748348253

The interuption was after about 1 minute (on a 350Mhz pentium). This is probably because the series is slowly converging and MAGMA will compute the result to the default precision. However, MAGMA returned the PositiveSum(g,1); and AlternatingSum(g2,1); commands instantly.

Exercise 7.2.1   Differentiate and integrate $ g(t)=t^2+t/2+a$, where $ a$ is an arbitrary but fixed constant 7.2.

Exercise 7.2.2   Compute $ \int_0^1 \cos(t)\ dt$ to at least 5 decimal places.

Exercise 7.2.3   Compute $ \sum_{n=1}^\infty 2^{-n}$ to at least 5 decimal places.


next up previous contents index
Next: Common data structures Up: Simple exercises in MAGMA Previous: Input-output   Contents   Index
David Joyner 2002-08-23