next up previous
Next: A Second Session with AXIOM Up: Foundations of Computer Algebra: Introduction Previous: Conclusions

A First Session with MAPLE

We continue with some illustrative examples of Computer Algebra capabilities using the MAPLE system.

For more information about MAPLE and ORCCA please visit:

Maplesoft
ORCCA

AXIOM SESSIONscale=2.5]

> restart;
Elementary calculations
> 1/3+2;
                               7
                               -
                               3
> 1/3+2.0;
                          2.333333333
> 5.0^(1/3);
                          1.709975947
> 2345/34570;
                              469 
                              ----
                              6914
> 3*x^3-4*x^2+x-7;
                         3      2        
                      3 x  - 4 x  + x - 7
> x^2/25+y^2/36;
                         1   2   1   2
                         -- x  + -- y 
                         25      36   
> sum('i^2', 'i'=1..100);
                             338350
> sum('i^2', 'i'=1..n);
               1        3   1        2   1     1
               - (n + 1)  - - (n + 1)  + - n + -
               3            2            6     6
> sum('1/i^2', 'i'=1..infinity);
                             1   2
                             - Pi 
                             6    
> evalf(Pi, 25);
                   3.141592653589793238462643
> diff(x^3*y^2, y);
                                3  
                             2 x  y
> limit((-x^2+x+1)/(x+4), x=infinity);
                           -infinity
> limit(tan(x), x=Pi/2, left);
                            infinity
> int(1/(exp(x^2)+x), x);
                         /     1        \
                      int|-----------, x|
                         |   / 2\       |
                         \exp\x / + x   /
> int(1/x, x=2..4);
                             ln(2)
> int(int(int(x^2*y^2*z^2, x=1..2), y=1..2), z=1..2);
                              343
                              ---
                              27 
> with(linalg):
Warning, the protected names norm and trace have been redefined and unprotected
> A := array(1..3,1..3,[[1,x,-x],[-x,0,x],[0,1,x]]);
                             [1   x  -x]
                             [         ]
                        A := [-x  0  x ]
                             [         ]
                             [0   1  x ]
> det(A);
                                3    2
                          -x + x  + x 
> inverse(A);
        [       1              x + 1            x     ]
        [- -----------    - -----------    -----------]
        [        2                2              2    ]
        [  -1 + x  + x      -1 + x  + x    -1 + x  + x]
        [                                             ]
        [      x                1             x - 1   ]
        [ -----------      -----------     -----------]
        [       2                2               2    ]
        [ -1 + x  + x      -1 + x  + x     -1 + x  + x]
        [                                             ]
        [       1                1              x     ]
        [- -----------  - ---------------  -----------]
        [        2          /      2    \        2    ]
        [  -1 + x  + x    x \-1 + x  + x/  -1 + x  + x]
> det(array(1..2, 1..2, [[a,b],[c,d]]));
                           a d - b c
> solve(cos(x) + y = 9, x);
                       Pi - arccos(y - 9)
> solve(x^3 - 6*x^2 + 11*x - 6, x);
                            1, 2, 3
> solve({x+y=1, 2*x+y=3}, {x,y});
                        {x = 2, y = -1}
> solve(z^4+z^3-2*z-1,z);
                /  4     3                      \  
          RootOf\_Z  + _Z  - 2 _Z - 1, index = 1/, 

                  /  4     3                      \  
            RootOf\_Z  + _Z  - 2 _Z - 1, index = 2/, 

                  /  4     3                      \  
            RootOf\_Z  + _Z  - 2 _Z - 1, index = 3/, 

                  /  4     3                      \
            RootOf\_Z  + _Z  - 2 _Z - 1, index = 4/
> solve(z^3+z-1,z);
                    (1/3)                            
1 /           (1/2)\                   2             
- \108 + 12 93     /      - -----------------------, 
6                                             (1/3)  
                            /           (1/2)\       
                            \108 + 12 93     /       
                       (1/3)                          
  1  /           (1/2)\                   1           
- -- \108 + 12 93     /      + -----------------------
  12                                             (1/3)
                               /           (1/2)\     
                               \108 + 12 93     /     

                /                    (1/3)                          \  
     1    (1/2) |1 /           (1/2)\                   2           |  
   + - I 3      |- \108 + 12 93     /      + -----------------------|, 
     2          |6                                             (1/3)|  
                |                            /           (1/2)\     |  
                \                            \108 + 12 93     /     /  
                       (1/3)                          
  1  /           (1/2)\                   1           
- -- \108 + 12 93     /      + -----------------------
  12                                             (1/3)
                               /           (1/2)\     
                               \108 + 12 93     /     

                /                    (1/3)                          \
     1    (1/2) |1 /           (1/2)\                   2           |
   - - I 3      |- \108 + 12 93     /      + -----------------------|
     2          |6                                             (1/3)|
                |                            /           (1/2)\     |
                \                            \108 + 12 93     /     /
> solve({x^3+y-1,x+y^3-1},{x,y});
                                 /          /  3                      \  
{y = 0, x = 1}, {x = 0, y = 1}, { x = RootOf\_Z  + _Z - 1, label = _L3/, 
                                 \                                       

                                       \    /
            /  3                      \ }  { 
  y = RootOf\_Z  + _Z - 1, label = _L3// ,  \

            /  4     3                        \  
  y = RootOf\_Z  + _Z  - 2 _Z - 1, label = _L4/, 

                                                3    \ 
             /  4     3                        \      }
  x = -RootOf\_Z  + _Z  - 2 _Z - 1, label = _L4/  + 1/ 

Sequences, sets and lists
> restart;
> 1,2,3,4,5;
                         1, 2, 3, 4, 5
> a+b,b+c,c+d,e+f,f+g;
               a + b, b + c, c + d, e + f, f + g
> seq(sin(Pi*i/6), i = 0..3);
                          1  1  (1/2)   
                       0, -, - 3     , 1
                          2  2          
> i;
                               i
> seq(i!/i^2, i=1..7);
                       1  2  3  24      720
                    1, -, -, -, --, 20, ---
                       2  3  2  5        7 
> {1, 1, 2, 3, 2};
                           {1, 2, 3}
> {red, white, blue};
                       {white, blue, red}
> L := [seq(i, i = 0..6)];
                   L := [0, 1, 2, 3, 4, 5, 6]
> {seq(i^2 mod 7, i = L)};
                          {0, 1, 2, 4}
> {a, b, c, d} union {d, e, f};
                       {a, e, b, c, f, d}
> {1, 2, 3, 4, 5} intersect {2, 4, 6, 8, 10};
                             {2, 4}


Assignments and equations
> restart;
> i :=1;
> 
                             i := 1
> i;
                               1
> unassign('i');
> i;
                               i
> x = y + 3;
                           x = y + 3
> x - y = 1 ;
                           x - y = 1
> x; y;
                               x
                               y
> sols := solve({x+y=3,x-y=1}, {x,y});
                     sols := {y = 1, x = 2}
> x; y;
                               x
                               y
> assign(sols);
> x; y;
                               2
                               1
> x := x + 3 ;
                             x := 5
> unassign('x');
> x := x + 3 ;
Error, recursive assignment
> x;
                               x

Types and representations
> restart;
> whattype(34/57);
                            fraction
> whattype([1,2,3,4,5]);
                              list
> (x+3)*(y-4); whattype((x+3)*(y-4));
                        (x + 3) (y - 4)
                               *
> whattype(x = y+1);
                               =
> greetings := "hello there";
                   greetings := "hello there"
> type(greetings, integer);
                             false
> type(greetings, string);
                              true
> hastype((x+1/2)*exp(3), fraction);
                              true
> hastype(x^2+3*x+5, `*`);
                              true
> int(exp(-x^2),x); hastype(int(exp(-x^2),x), fraction);
                        1   (1/2)       
                        - Pi      erf(x)
                        2               
                              true
> has(x^2+3*x+5, 3);
                              true
> has(x^2+3*x+5, 2*x);
                             false
> object := 3*x^2 + 2*x - 3;
                                 2          
                    object := 3 x  + 2 x - 3
> nops(object);
                               3
> op(object);
                            2         
                         3 x , 2 x, -3
> op(1, object);
                                 2
                              3 x 
> op(1, op(1, object));
                               3
> whattype(op(2, x^2+exp(1)-3));
                            function

Evaluation and substitution
> restart;
> subs(y=ln(x),exp(y));
> 
                           exp(ln(x))
> simplify(subs(y=ln(x),exp(y)));
                               x
> 4^(1/2)+3; simplify(4^(1/2)+3);
                            (1/2)    
                           4      + 3
                               5
The computations below show that one can replace a variable by a value
and,,also, a term by a value.
> f := x^2 + 3*x + 1; simplify(f, {x^2=1}); simplify(f, {x=1});
                             2          
                       f := x  + 3 x + 1
                            3 x + 2
                               5
> f := x^2 + x*y + y^2; simplify(f, {x^2=1, y^2=6});
                             2          2
                       f := x  + x y + y 
                            x y + 7
More generally, one can simplify an expression by working modulo a relation.
To make clear sense, the precise specifications of "simplify" requires mathematical
notions that are outisde the scope of this introduction.
> f := x^2 + 2*x*y + y^2; simplify(f, {x+y=4});
                            2            2
                      f := x  + 2 x y + y 
                               16
> restart; simplify(x^2*y, {x^2=x*y}); 
                                 2
                              x y 
In particular, when working modulo several relations, things become more tricky.
Indeed, one has to decide which simplication rule to use first.  On the following
example it is easy to see using the first rules twice and then the second once
leads to the returned result. 
> g := simplify(x^5-x^2-x, {x^3 = x*y, y^2 = x+1});
                             g := 0
Solving equations
> restart;
>  solve({x^2 - 3 * x + 2 = 0},{x}) ;
                        {x = 2}, {x = 1}
>  solve(a*x^2 + b*x+ c,x);
                         (1/2)                    (1/2)
             / 2        \             / 2        \     
        -b + \b  - 4 a c/         b + \b  - 4 a c/     
        ----------------------, - ---------------------
                 2 a                       2 a         
> solve(a*x^3 + b*x^2 + c*x + d,x):
> solve(a*x^4 + b*x^3 + c*x^2 + b*x + e,x);
                  /    4       3       2           \
            RootOf\a _Z  + b _Z  + c _Z  + b _Z + e/
> d := (m -1)*x -2*(m+1)*y-m;
                d := (m - 1) x - 2 (m + 1) y - m
>  solve({d = 0},{x});
                     /    2 y m + 2 y + m\ 
                    { x = --------------- }
                     \         m - 1     / 
>  solve({abs(x^2 -4*x -3) = 3},{x});
    /          (1/2)\    /          (1/2)\                   
   { x = 2 + 10      }, { x = 2 - 10      }, {x = 0}, {x = 4}
    \               /    \               /                   
>  solve({-2*x-3*y=2,4*x+6*y=5},{x,y});
> solve({x -2*y -3,-2*x+4*y+6},{x,y});
                      {x = 3 + 2 y, y = y}
> solve({x-y+z-1,x+y-z-2,x+y+z-3},{x,y,z});
                     /           1      3\ 
                    { y = 1, z = -, x = - }
                     \           2      2/ 
>  restart; solve({a*x + b*y - e, c*x + d * y - f},{x,y}) ;
               /    -c e + f a        -d e + b f\ 
              { y = ----------, x = - ---------- }
               \    d a - b c         d a - b c / 

Programming
> f := proc(x,y) x + y; end;
                f := proc(x, y) x + y end proc;
> f(2,3);
                               5
> print(f);
proc(x, y) x + y end proc;
> f;
                               f
Please, refer to the Maple documentation. 
Representations of polynomials: expression trees
The default representation for polynomials in Maple is by expression trees.
This has the advantage that arithmetic operations have O(1) complexity.
However, a quatity which is null may be represented by something else than 0.
In addition,  the complexity of the equality test is not linear in the size of the input 
polynomials. Also finding the degree or the leading term requires to traverse the
representation of the input polynomials.
> restart;
> p1 := x * (x + 1) * (x + 2);
                    p1 := x (x + 1) (x + 2)
> op(p1);
                        x, x + 1, x + 2
> p2 := x^3 + 3 * x^2 + 2 * x;
                            3      2      
                     p2 := x  + 3 x  + 2 x
> op(p2);
                          3     2     
                         x , 3 x , 2 x
> p3 := ((x + 3) * x + 2) * x;
                    p3 := ((x + 3) x + 2) x
> op(p3);
                        (x + 3) x + 2, x
> p1 - p2;
                                   3      2      
              x (x + 1) (x + 2) - x  - 3 x  - 2 x
> expand(p1 - p2);
                               0

Representations of polynomials: canonical representations I
> restart; p := 4*x^3 *y + 5*x*y^2 -7 *x^2 -13 *y + 1;
                     3          2      2           
             p := 4 x  y + 5 x y  - 7 x  - 13 y + 1
> vars := [x,y]; p := expand(p,[x,y]); 
                         vars := [x, y]
                     3          2      2           
             p := 4 x  y + 5 x y  - 7 x  - 13 y + 1
> with(Groebner);
[MulMatrix, SetBasis, ToricIdealBasis, fglm_algo, gbasis, gsolve, hilbertdim, 

  hilbertpoly, hilbertseries, inter_reduce, is_finite, is_solvable, leadcoeff, 

  leadmon, leadterm, normalf, pretend_gbasis, reduce, spoly, termorder, 

  testorder, univpoly]
> ord := plex(op(vars)); lmp :=  leadterm(p,ord); lcp := leadcoeff(p,ord);
                       ord := plex(x, y)
                                  3  
                          lmp := x  y
                            lcp := 4
> [[degree(lmp,vars[1]), degree(lmp,vars[2])], lcp];
                          [[3, 1], 4]
> p := p - lmp  * lcp;
                           2      2           
                 p := 5 x y  - 7 x  - 13 y + 1
Representations of polynomials: canonical representations II
> restart; read(`polynomials.input`):
> vars := [x,y]; 
                         vars := [x, y]
> p := x^2 + 2*x*y + 3*x + 4*y^2 + 5*y +6;
                   2                    2          
             p := x  + 2 x y + 3 x + 4 y  + 5 y + 6
> q := x^2 -2*x*y +3*x -4*y^2 +5*y;
                     2                    2      
               q := x  - 2 x y + 3 x - 4 y  + 5 y
> mapleToPol(1, vars); mapleToPol(x, vars); mapleToPol(y, vars);
                         [[[0, 0], 1]]
                         [[[1, 0], 1]]
                         [[[0, 1], 1]]
> tp := mapleToPol(p,vars);
tp := [[[2, 0], 1], [[1, 1], 2], [[1, 0], 3], [[0, 2], 4], [[0, 1], 5], 

  [[0, 0], 6]]
> tq := mapleToPol(q, vars);
tq := [[[2, 0], 1], [[1, 1], -2], [[1, 0], 3], [[0, 2], -4], [[0, 1], 5]]
> pp := polToMaple(tp,vars); pp -p ;
                   2                    2          
            pp := x  + 2 x y + 3 x + 4 y  + 5 y + 6
                               0
> qq := polToMaple(tq,vars); qq -q ;
                     2                    2      
              qq := x  - 2 x y + 3 x - 4 y  + 5 y
                               0
> ts := addPol(tp, tq); polToMaple(ts, vars) - (p + q);
  ts := [[[2, 0], 2], [[1, 0], 6], [[0, 1], 10], [[0, 0], 6]]
                               0
> tr := polMul(tp, tq); expand(polToMaple(tr, vars) - p * q);
tr := [[[4, 0], 1], [[3, 0], 6], [[2, 2], -4], [[2, 1], 10], [[2, 0], 15], 

  [[1, 3], -16], [[1, 1], 18], [[1, 0], 18], [[0, 4], -16], [[0, 2], 1], 

  [[0, 1], 30]]
                               0


next up previous
Next: A Second Session with AXIOM Up: Foundations of Computer Algebra: Introduction Previous: Conclusions
Marc Moreno Maza
2007-01-10