<<<>>>
?
<<<>>>
? diff
<<<>>>
12+5; (-5+2.3^2)*2.3;
<<<>>>
1/5 + 1/2;
<<<>>>
sqrt(1097653086183136645373121);
<<<>>>
2^(2^15);
<<<>>>
fact(1000);
<<<>>>
sqrt(2); sin(5); 2*demo+b*c-sqrt(demo^2);
<<<>>>
float(1/3); float(sqrt(2));
<<<>>>
DIGITS := 500;
float(sqrt(2));
DIGITS := NIL:
<<<>>>
E; PI; cos(PI);
<<<>>>
DIGITS := 20;
float(PI);
DIGITS := NIL:
<<<>>>
igcd(12,18);
<<<>>>
ifactor(2^18-1);
<<<>>>
hold(1*3^3*7^1*19^1*73^1);
<<<>>>
ifactor(1000);
<<<>>>
ifactor(500);
<<<>>>
fact(500);
<<<>>>
ifactor(%);
<<<>>>
isprime(123);
<<<>>>
ifactor(123);
<<<>>>
isprime(2432902008176640029);
<<<>>>
fact(40);
nextprime(%);
<<<>>>
reset():
<<<>>>
(a+b)^100;
<<<>>>
expand(%);
<<<>>>
p := x^2+2*x;
expand(p^2);
<<<>>>
p := x^2+2*x:
x := 4:
p;
<<<>>>
p^5;
<<<>>>
s := "This is an example";
<<<>>>
s1 := substring(s,0,8);
s2 := substring(s,8,10);
s1.s2;
<<<>>>
s := "This is an example of a string":
strmatch(s,"\*exam\*");
strmatch(s,"\*hello\*");
<<<>>>
reset():
<<<>>>
1 < 2;
<<<>>>
bool(%);
<<<>>>
e := a*b*c = a*b*d;
<<<>>>
bool(e);
<<<>>>
c := d;
<<<>>>
bool(e);
<<<>>>
e := 1=1 and not(2 <> 3);
<<<>>>
bool(e);
<<<>>>
reset():
<<<>>>
domtype(123);
<<<>>>
domtype(1.2);
<<<>>>
domtype(a);
<<<>>>
domtype(a*b); type(a*b);
<<<>>>
domtype(a+b); type(a+b);
<<<>>>
domtype( hold((a:=b)) ); type( hold((a:=b)) );
<<<>>>
testtype(2,DOM_INT); testtype(2,DOM_RAT);
<<<>>>
testtype(2,NUMERIC); testtype(2+3*I,NUMERIC);
<<<>>>
reset():
<<<>>>
1, 2, 3, 4, a, b, 1;
<<<>>>
i := NIL:
<<<>>>
i $ 4;
A := i^2 $ i = 1..50;
<<<>>>
A[2];
<<<>>>
i^3 $ i=1..200;
<<<>>>
_plus(%);
<<<>>>
_mult(%2);
<<<>>>
M := {1, 2, 3, 4, a, b, 1, 2};
<<<>>>
N := {3, 4, a, c};
<<<>>>
M union N;
M intersect N;
<<<>>>
M := { 1, 2, 3 };
N := { M, 2, 3/4 };
<<<>>>
M := {1, 2, 3, -3, 0, 2};
contains(M,3);
<<<>>>
L := [a, b, a, b, c];
<<<>>>
L[2]; L[5];
<<<>>>
L[2] := 100: L;
<<<>>>
L[2] := NIL: L;
<<<>>>
contains(L,c); contains(L,100);
<<<>>>
[a, b] . [c, d] . [b,-a,a] . [c];
<<<>>>
A := array(1..2,1..2,[ [11,12],[21,22] ]);
<<<>>>
n := 3: m := 4:
B := array(1..n,1..m);
<<<>>>
A[1,1];
A[1,1] + A[2,2];
<<<>>>
A[1,1] := 0:
A[2,2] := NIL:
A;
<<<>>>
T[a] := 2:
T[3] := 4:
T;
<<<>>>
S := table( 1 = 2, 3 = 4);
<<<>>>
S[{1},1] := 11:
S[[2],2] := 22:
S;
<<<>>>
reset():
<<<>>>
(u+v+w)(22);
<<<>>>
(u*v*w)(22);
<<<>>>
(u @ v @ w)(22);
<<<>>>
(sin^2 + cos^2)(22.0);
<<<>>>
(P[1]-P[2])(22);
<<<>>>
(max^3)(2,3);
<<<>>>
f := NIL: g := NIL:
f(0) := {1,2,3};
g(0) := {-1,-2,-3};
<<<>>>
(f union g)(0);
<<<>>>
_union := funcattr(_union, "operator", op_plusmult):
<<<>>>
(f union g)(0);
<<<>>>
op_range := proc(func_call, operator)
name op_range;
local num, func, args;
begin
   num  := op(operator, 2);
   func := op(operator, 1);
   args := context(op(func_call));
   result := func(args);
   for i from 2 to num do
      result := subsop(func("dummy"), 1 = result);
   end_for;
   result;
end_proc:
<<<>>>
_range := funcattr(_range, "operator", op_range);
<<<>>>
f := proc(n)
begin
   n + 1;
end_proc:
<<<>>>
(f..3)(a);
<<<>>>
reset():
<<<>>>
op( a+b+c );
nops( a+b+c );
<<<>>>
op( {a, b, c} );
<<<>>>
op( T[a, b] );
<<<>>>
T := table( 1=2, 2=3 );
op(T);
<<<>>>
op( a*b*c, 2 );
<<<>>>
op( a*b+c, [2, 1] );
<<<>>>
subsop( a+b+c*d, 2 = e );
<<<>>>
subs( a+b+c, a=1 );
<<<>>>
subs( a+b+c, a+b = 1 );
<<<>>>
subs( tan(x+ln(x)/ln(sin(exp(x)))),x=exp(x) );
<<<>>>
simplify( % );
<<<>>>
subsex( a+b+c, a+b = 1 );
<<<>>>
reset():
<<<>>>
n := 3:
m := 4:
A := array(1..n,1..m):
for i from 1 to n do
    for j from 1 to m do
        A[i,j] := i*j;
    end_for;
end_for:
A;
<<<>>>
L := [1, 2, 3, 4]:
result := []:
for i in L do
   result := result . [i+c];
end_for;
<<<>>>
map( L, _plus, 5 );
<<<>>>
Max := proc(a,b)
       begin
          if a < b then
             b
          else
             a
          end_if;
       end_proc:
<<<>>>
Max( 2,3 );
<<<>>>
Max := proc()
       local m, i;
       begin
           m := 0;
           for i in args() do
               if i > m then
                  m := i;
               end_if;
           end_for;
           return(m);
       end_proc:
<<<>>>
random() $ hold(i)=1..8;
<<<>>>
Max( % );
<<<>>>
time( _plus(1/k $ k=1..1000) );
<<<>>>
time( _plus(1/k $ k=1..10000) );
<<<>>>
time( fact(1000) );
<<<>>>
fib := proc(n)
       begin
         if n<2 then
            1;
         else
            fib(n-1)+fib(n-2);
         end_if;
       end_proc;
<<<>>>
profile( fib(7) );
<<<>>>
fib := proc(n)
       option remember;
       begin
          if n<2 then
             1;
          else
             fib(n-1)+fib(n-2);
          end_if;
       end_proc;
<<<>>>
profile( fib(7) );
<<<>>>
profile( factor(poly(x^2-1,[x])) );
<<<>>>
reset():
<<<>>>
taylor( sin(x), x=PI, 6 );
<<<>>>
taylor( cos(x), x=PI, 6 );
<<<>>>
O(x^6) - O(x^6);
<<<>>>
O(x^3) * O(x^2);
<<<>>>
info( O );
<<<>>>
loadlib("domains"): export(domains);
<<<>>>
Z7 := IntegerMod(7);
<<<>>>
a := Z7(8);
<<<>>>
b := Z7(2)-1/Z7(3);
<<<>>>
a+b;
<<<>>>
type(a);
<<<>>>
op(a);
<<<>>>
type(Field);
<<<>>>
Z7::hasProp(Field);
<<<>>>
Z6 := IntegerMod(6);
<<<>>>
Z6::hasProp(Field);
<<<>>>
reset():
<<<>>>
loadlib("sharelib"): export(sharelib);
<<<>>>
SGML( 2*(a+b)^3-sin(1) );
<<<>>>
SGML(( fac := proc(n)
              begin
                 if n=1 then
                    1;
                 else
                    n*fac(n-1);
                 end_if;
              end_proc));
<<<>>>
reset():
<<<>>>
diff( sin(x)*ln(x),x );
<<<>>>
diff( ln(ln(x)),x$2 );
<<<>>>
diff( sin(x)*sin(y),y,x );
<<<>>>
diff( poly(sin(a)*x^3 + 2*x, [x]),a,x );
<<<>>>
diff( f(x)*g(x),x );
<<<>>>
diff( f(g(x)),x );
<<<>>>
diff( f(g(h(x))),x );
<<<>>>
D( sin );
<<<>>>
D( sin+cos )(PI);
<<<>>>
D( f @ (g+h) );
<<<>>>
int( (4-2*t)^5,t );
<<<>>>
int( 1/(x*ln(x)),x );
<<<>>>
F := int( 1/exp(x)+1,x );
<<<>>>
diff( F,x );
<<<>>>
sum( n^2,n );
<<<>>>
sum( 1/(x^2-1),x );
<<<>>>
sum( 1/k/(k+2)^2,k );
<<<>>>
sum( 1/(n^2+21*n),n=1..infinity );
<<<>>>
sum( binomial(n,k)/2^n-binomial(n+1,k)/2^(n+1),k );
<<<>>>
taylor( 1/(x^2-a),x );
<<<>>>
taylor( 1/x + y + x^3,x=0 );
<<<>>>
series( 1/x + y + x^3,x=0 );
<<<>>>
series( sin(x+a),a=0,4 );
<<<>>>
series( ln(x), x=0 );
<<<>>>
s := series( sin(x),x=0,4 );
<<<>>>
1/s+2*s;
<<<>>>
s^3;
<<<>>>
s @ s;
<<<>>>
limit( (sin(x+h)-sin(x))/h,h=0 );
<<<>>>
limit( (1+1/n)^n,n=infinity );
<<<>>>
limit( x^(-sqrt(x)) - 1,x=0 );
<<<>>>
limit( (exp(x*exp(-x)/(exp(-x)+exp(-2*x^2/(x+1))))-exp(x))/x,
   x=infinity );
<<<>>>
limit( exp(exp(n))*(exp(sin(1/n+1/exp(exp(n))))-exp(sin(1/n))),
   n=infinity );
<<<>>>
reset():
<<<>>>
solve( x^4-5*x^2+6*x=2,x );
<<<>>>
solve( a*x^2+b*x+c,x );
<<<>>>
solve( x^3=-1,x );
<<<>>>
solve( {u+v+w=1, 3*u+v=3, u-2*v-w=0},{u,v,w} );
<<<>>>
solve( {u+v+w=1, 3*u+v=3, u^2-2*v-w=0},{u,v,w} );
<<<>>>
Re( sin(2+3*I)+exp(2-I) + (2+3*I)^(1/3) );
<<<>>>
Im( tan(x) );
<<<>>>
rectform( tan(x) );
<<<>>>
rectform( sin(x)*cos(y),{x} );
<<<>>>
Re(%), Im(%);
<<<>>>
a := rectform( exp(x) );
<<<>>>
PI*a, a^2;
<<<>>>
sin(x)*a;
<<<>>>
reset():
<<<>>>
p := 2*x^3*z^2 + 7*x^2*y^2 + 5*y - 3*z;
<<<>>>
coeff( p, [x,y] );
<<<>>>
coeff( p, x, 2 );
<<<>>>
degree( p, x );
<<<>>>
degree( p );
<<<>>>
q:= (x + 1)*(x + y) + 2;
coeff( q, [x] );
coeff( q );
<<<>>>
p := 2*x^3*z^2 + 7*x^2*y^2 + 5*y - 3*z;
lcoeff( p, [x,y] );
<<<>>>
lcoeff( p, [x,y], DegreeOrder );
<<<>>>
lmonomial( p, [x,y] );
<<<>>>
lterm( p, [x,y] );
<<<>>>
degreevec( p , [x,y] );
<<<>>>
tcoeff( p, [x,y] );
<<<>>>
lmonomial( p, [x,y], DegreeOrder );
lterm( p, [x,y], DegreeOrder );
degreevec( p , [x,y], DegreeOrder );
<<<>>>
p := 2*x^3*z^2 + 7*x^2*y^2 + 5*y - 3*z;
nterms( p );
<<<>>>
nthcoeff( p, [x,y], 2 );
nthmonomial( p, [x,y], 2 );
nthterm( p, [x,y], 2 );
<<<>>>
p := 6*x^2 + 4;
q := 2*x + 1;
divide( p, q );
<<<>>>
divide( p, q, Quo );
<<<>>>
divide( p, q, Rem );
<<<>>>
divide( p, q, Exact );
<<<>>>
divide( x^2 - 1, x + 1, Exact );
<<<>>>
pdivide( p, q );
<<<>>>
gcd( x^2 - 1, x^2 + 2*x + 1 );
<<<>>>
gcd( x^2 - y^2, x^3 - y^3 );
<<<>>>
lcm( x^2 - y^2, x^3 - y^3 );
<<<>>>
normal( (x^2 - 1) / (x^2 + 2*x + 1) );
<<<>>>
normal( (x^2 - y^2) / (x^3 - y^3) );
<<<>>>
factor( 6*x^2 + 18*x - 24 );
<<<>>>
factor( 4*x^3 + 4*x^2*y + 3*x*y + 3*y^2 );
<<<>>>
p := x^2*y^2 + 2;
<<<>>>
evalp( p, x=2 ); evalp( p, x=1, y=1 );
<<<>>>
a := 2*x + 2;
norm( a );
norm( a, 2 );
<<<>>>
p:= multcoeffs( 6*x^2 + 4, 7 );
<<<>>>
content( p );
<<<>>>
mapcoeffs( p, sin );
<<<>>>
poly( x^2 + 2*x + 1 );
<<<>>>
poly( x^2*y^2 + x*y + 1 );
<<<>>>
poly( x * (2*x + y + 2)^2 + 2 );
<<<>>>
poly( x^2*y^2 + x*y + 1, [y,x] );
<<<>>>
poly( x^2*y^2 + 6*x*y - 2*x - 5, IntMod(7) );
<<<>>>
a := poly( x^2*y^2 - 2, IntMod(7) );
b := poly( 2*x + 2*y, IntMod(7) );
a + b; a * b; a^3;
<<<>>>
a := poly( x^2*y^2 + 2, [x,y] );
b := poly( 2*x + 2*y, [y,x] );
a + b;
<<<>>>
a := poly( 2*x^3 + 7*x^2*y^2 + 5*y, [x,y] );
coeff( a );
coeff( a, x, 2 );
degreevec( a );
<<<>>>
factor( poly( 6*x^2 + 18*x - 24, IntMod(7) ) );
<<<>>>
factor( 6*x^2 + 18*x - 24 );
<<<>>>
factor( poly( x^3 + 3*x^2 + 5*x + 9, IntMod(13) ) );
<<<>>>
a := poly( 5*x^2*y^2 + 2*y + 3, [x], Poly([y]) );
<<<>>>
coeff( a );
<<<>>>
decompose(x^6+6*x^4+x^3+9*x^2+3*x-5);
<<<>>>
decompose(poly(x^4-3*x^3-x+5,[x],IntMod(5)));
<<<>>>
factor( x^3+1 );
<<<>>>
P := x^8 + 8*x^7 + 28*x^6 + 56*x^5 + 70*x^4 +
56*x^3 + 28*x^2 + 8*x^2 +8*x +1;
<<<>>>
factor( P );
<<<>>>
P := poly( x^26 + x^13 + 1, [x] );
<<<>>>
factor( P );
<<<>>>
factor( poly(x^3+1,[x],IntMod(2)) );
<<<>>>
Factor( x^100-1 );
<<<>>>
reset(): loadlib("domains"): export(domains):
READ_PATH := LIB_PATH.pathname(Up,"demo","domains"):
read("GaussNum.mu"):
<<<>>>
GaussInt := GaussianNumber( Integer );
<<<>>>
a := poly( 2*x^2 + (1-8*I)*x^4, GaussInt );
<<<>>>
b := poly( I*x^2 + ((9-15*I)*x)^3, GaussInt );
<<<>>>
a+b - a*b^3;
<<<>>>
coeff( a^3 );
<<<>>>
reset();
<<<>>>
READ_PATH := LIB_PATH.pathname(Up,"demo","graphics");
<<<>>>
plot2d([Mode = Curve,
             [u, sin(u)], u = [-PI, PI]
       ]);
<<<>>>
plot2d([Mode = Curve,
             [sin(u), cos(u)], u = [-PI, PI]
       ]);
<<<>>>
my_curve_x := proc(u)
begin
  sin(u)*sin(2*u)*sin(3*u):
end_proc:

my_curve_y := proc(u)
begin
  sin(4*u)*sin(5*u)*sin(6*u):
end_proc:

plot2d(Axes = Box, Ticks = 0,
       Scaling = UnConstrained,
       PointStyle = Circles, PointWidth = 6,
       [Mode = Curve,
             [hold(my_curve_x(u)),
              hold(my_curve_y(u))],
             u = [-PI, PI],
             Grid = [200],
             Style = [LinesPoints],
             Color = [Height, [1, 1, 0],
                              [1, 0, 1]]
       ]);
<<<>>>
pascal_x := proc(a, l, u)
begin
  a*cos(u)^2*l*cos(u):
end_proc:

pascal_y := proc(a, l, u)
begin
  a*cos(u)*sin(u) + l*cos(u)
end_proc:

plot2d([Mode = Curve,
             [hold(pascal_x(1, 1, u)),
              hold(pascal_y(1, 1, u))],
             u = [0, 2*PI], Grid = [100],
             Color = [Height],
             Style = [Points]
       ],
       [Mode = Curve,
             [hold(pascal_x(1.5, 1, u)),
              hold(pascal_y(1.5, 1, u))],
             u = [0, 2*PI], Grid = [100],
             Color = [Height],
             Style = [Lines]
       ],
       [Mode = Curve,
             [hold(pascal_x(2, 1, u)),
              hold(pascal_y(2, 1, u))],
             u = [0, 2*PI], Grid = [100],
             Color = [Height],
             Style = [LinesPoints]
       ]);
<<<>>>
epi_x := proc(a, b, u)
begin
  (a+b)*cos(u) - a*cos((a+b)*u/a):
end_proc:

epi_y := proc(a, b, u)
begin
  (a+b)*sin(u) + a*sin((a+b)*u/a):
end_proc:

plot2d([Mode = Curve,
             [hold(epi_x(1, 1, u)),
              hold(epi_y(1, 1, u))],
             u = [0, 2*PI], Grid = [40],
             Color = [Height]
       ],
       [Mode = Curve,
             [hold(epi_x(1, 2, u)),
              hold(epi_y(1, 2, u))],
             u = [0, 2*PI], Grid = [60],
             Color = [Height]
       ],
       [Mode = Curve,
             [hold(epi_x(1, 4, u)),
              hold(epi_y(1, 4, u))],
             u = [0, 2*PI], Grid = [80],
             Color = [Height]
       ]);
<<<>>>
plot2d(Labels = ["", ""], Labeling = TRUE,
       Title = "Different Curve Styles",
       Axes = Origin, AxesOrigin = [0, 0],
       [Mode = Curve,
             [u, 2*cos(u)], u = [0, PI/2],
             Grid = [15], Style = [Points],
             Color = [Height, [1, 1, 0],
                              [1, 0, 0]],
             Title = "Points",
             TitlePosition = [2, 2.5]
       ],
       [Mode = Curve,
             [u, 2*cos(u)], u = [PI/2, PI],
             Grid = [15], Style = [Lines],
             Color = [Height, [0, 1, 0],
                              [1, 1, 0]],
             Title = "Lines",
             TitlePosition = [3.5, 5.5]
       ],
       [Mode = Curve,
             [u, 2*cos(u)], u = [PI, 3*PI/2],
             Grid = [15], Style = [LinesPoints],
             Color = [Height, [0, 1, 1],
                              [0, 0, 1]],
             Title = "LinesPoints",
             TitlePosition = [6, 7.5]
       ],
       [Mode = Curve,
             [u, 2*cos(u)], u = [3*PI/2, 2*PI],
             Grid = [15], Style = [Impulses],
             Color = [Height, [0, 0, 1],
                              [1, 0, 1]],
             Title = "Impulses",
             TitlePosition = [8, 4]
       ]);
<<<>>>
reset();
READ_PATH := LIB_PATH.pathname(Up,"demo","graphics");
<<<>>>
plot3d(Axes = Box, Ticks = 0,
       Scaling = UnConstrained,
       [Mode = Curve,
             [u, -PI, sin(u)], u = [-PI, PI],
             Grid = [30], Style = [Points],
             Color = [Height]
       ],
       [Mode = Curve,
             [u, -PI/3, sin(u)], u = [-PI, PI],
             Grid = [30], Style = [Lines],
             Color = [Height]
       ],
       [Mode = Curve,
             [u, PI/3, sin(u)], u = [-PI, PI],
             Grid = [30], Style = [LinesPoints],
             Color = [Height]
       ],
       [Mode = Curve,
             [u, PI, sin(u)], u = [-PI, PI],
             Grid = [30], Style = [Impulses],
             Color = [Height]
       ]);
<<<>>>
plot3d(Axes = Box, Ticks = 0, LineWidth = 2,
       CameraPoint = [-0.2, 1.0, -20],
       [Mode = Curve,
             [u/8*sin(u), u/8*cos(u), 3*cos(u/8)],
             u = [0, 8*PI], Grid = [160],
             Color = [Height]
       ]);
<<<>>>
plot3d([Mode = Surface,
             [u, v, u+v], u = [-1, 1], v = [-1, 1]
       ]);
<<<>>>
plot3d([Mode = Surface,
             [u, v, u+v], u = [-1, 1], v = [-1, 1],
             Style = [ColorPatches, AndMesh],
             Color = [Height]
       ]);
<<<>>>
plot3d(Axes = None, Ticks = 0,
       Scaling = UnConstrained,
       CameraPoint = [114, 0, 155],
       BackGround = [1, 1, 1],
       ForeGround = [0, 0, 1],
       [Mode = Surface,
             [4*v*cos(u)-v*cos(4*u),
              4*v*sin(u)+v*sin(4*u),-6*cos(v)],
             u = [0, 2*PI], v = [0.1, 6],
             Grid = [41, 20],
             Style = [ColorPatches, AndMesh],
             Color = [Height, [0.4, 0.4, 0.4],
                              [0, 1, 1]]]);
<<<>>>
plot3d(CameraPoint = [0.05, -0.05, 5],
       Axes = Box, Ticks = 0,
       [Mode = Surface,
             [2*u/(1+u^2+v^2),
              2*v/(1+u^2+v^2),
              (u^2+v^2-1)/(u^2+v^2+1)],
             u = [-PI, PI], v = [-PI, PI],
             Grid = [30, 30],
             Style = [ColorPatches, AndMesh],
             Color = [Height, [1, 1, 0],
                              [1, 0, 1]]
       ]);
<<<>>>
plot3d(Axes = Box, Ticks = 0,
       Title = "Enneper's minimal surface",
       [Mode = Surface,
             [u - 1/3*u^3 + u*v^2,
              -v - u^2*v + 1/3*v^3,
              u^2 - v^2],
             u = [-1.2, 1.2], v = [-1.2, 1.2],
             Style = [ColorPatches, AndMesh],
             Color = [Height]
        ]);
<<<>>>
plot3d(Axes = None, Ticks = 0,
       Title = "Catenoid",
       [Mode = Surface,
             [cosh(u)*cos(v), 3*u, -cosh(u)*sin(v)],
             u = [-PI, PI], v = [-PI, PI],
             Grid = [30, 30],
             Style = [ColorPatches, AndMesh],
             Color = [Height, [1, 0.7, 0],
                              [0, 0.7, 1]]
       ]);
<<<>>>
plot3d(Axes = None, Ticks = 0,
       CameraPoint = [0, -6, 80],
       Title = "Helicoid",
       [Mode = Surface,
             [sinh(u)*sin(v), 3*v, sinh(u)*cos(v)],
             u = [-PI, PI], v = [-PI, PI],
             Grid = [20, 30],
             Style = [ColorPatches, AndMesh],
             Color = [Height, [0.4, 1, 0],
                              [0, 0.4, 1]]
       ]);
<<<>>>
plot3d(Axes = Corner, Ticks = 0,
       Title = "Different Mesh Styles",
       Scaling = UnConstrained,
       ForeGround = [0, 0, 0],
       BackGround = [0.6, 0.6, 0.6],
       [Mode = Surface,
             [u, v, sin(u^2+v^2)],
             u = [-PI/2, 0], v = [-PI/2, 0],
             Style = [ColorPatches, AndULine],
             Grid = [15, 15],
             Color = [Height, [1, 0, 0],
                              [1, 1, 0]]
       ],
       [Mode = Surface,
             [u, v, sin(u^2+v^2)],
             u = [-PI/2, 0], v = [0, PI/2],
             Style = [ColorPatches, AndMesh],
             Grid = [15, 15],
             Color = [Height, [1, 0, 0],
                              [1, 1, 0]]
       ],
       [Mode = Surface,
             [u, v, sin(u^2+v^2)],
             u = [0, PI/2], v = [-PI/2, 0],
             Style = [ColorPatches, Only],
             Grid = [15, 15],
             Color = [Height, [1, 0, 0],
                              [1, 1, 0]]
       ],
       [Mode = Surface,
             [u, v, + sin(u^2+v^2)],
             u = [0, PI/2], v = [0, PI/2],
             Style = [ColorPatches, AndVLine],
             Grid = [15, 15],
             Color = [Height, [1, 0, 0],
                              [1, 1, 0]]
       ]);
<<<>>>
plot3d(Axes = Box, Ticks = 0,
       Title = "The Difference made by Smoothness",
       [Mode = Surface,
             [1.5+sin(u)*cos(v), sin(u)*sin(v),
              cos(u)],
             u = [0, PI], v = [-PI, PI],
             Grid = [10, 10], Smoothness = [0, 0],
             Style = [ColorPatches, AndMesh],
             Color = [Height, [0.3, 1, 0],
                              [0.3, 1, 1]]
       ],
       [Mode = Surface,
             [-1.5+sin(u)*cos(v), sin(u)*sin(v),
              cos(u)],
             u = [0, PI], v = [-PI, PI],
             Grid = [10, 10], Smoothness = [1, 1],
             Style = [ColorPatches, AndMesh],
             Color = [Height, [0.3, 1, 0],
                              [0.3, 1, 1]]
       ]);
<<<>>>
benjamin_ono_1 := proc(x, t, c)
begin
  10*c/((c*(x-c*t))^2+1):
end_proc:

plot3d(Axes = None, Scaling = UnConstrained,
       Ticks = 0,
       Title = "Single Soliton of the B.O.",
       [Mode = Surface,
             [u, v, hold(benjamin_ono_1(u, v, 1/2))],
             u = [-10, 10], v = [-10, 10],
             Grid = [40, 40],
             Style = [ColorPatches, AndMesh],
             Color = [Height, [0, 0, 1],
                              [1, 1, 1]]
       ]);
<<<>>>
benjamin_ono_2 := proc(x, t, c1, c2)

local e1, e2, f, fx;

begin
  e1 := c1*(x - c1*t):
  e2 := c2*(x - c2*t):

  f  := 1-e1*e2+4*c1*c2/(c1-c2)^2+I*(e1+e2):
  fx := -c1*c2*(2*x-c2*t-c1*t)+I*(c1+c2):

  30*abs(fx/f):
end_proc:

plot3d(Axes = None, Ticks = 0,
       Scaling = UnConstrained,
       Title = "Twosoliton of the B.O.",
       [Mode = Surface,
             [u, v,
              hold(benjamin_ono_2(u, v, 1/4, 3/5))],
             u = [-40, 40], v = [-30, 30],
             Grid = [50, 50],
             Smoothness = [1, 0],
             Style = [ColorPatches, AndMesh],
             Color = [Height, [0, 0, 1],
                              [1, 1, 1]]
       ]):
<<<>>>
col1 := [0.7, 0.7, 0.7]:
col2 := [0.95, 0.95, 0.95]:
col3 := [0.8, 0.8, 0.8]:
col4 := [0, 0, 0]:
col5 := [1, 1, 1]:

plot3d(Axes = None, Scaling = UnConstrained,
       ForeGround = col4, BackGround = col5,
       [Mode = Surface,
             [2, u, v],
             u = [-1, 1], v = [0, 2],
             Grid = [4, 4],
             Style = [ColorPatches, AndMesh],
             Color = [Flat, col1]],
       [Mode = Surface,
             [4, u, v],
             u = [-1, 1], v = [0, 2],
             Grid = [4, 4],
             Style = [ColorPatches, AndMesh],
             Color = [Flat, col1]],
       [Mode = Surface,
             [3 + u, -1, v],
             u = [-1, 1], v = [0, 2],
             Grid = [4, 4],
             Style = [ColorPatches, AndMesh],
             Color = [Flat, col1]],
       [Mode = Surface,
             [3 + u, 1, v],
             u = [-1, 1], v = [0, 2],
             Grid=[4, 4],
             Style = [ColorPatches, AndMesh],
             Color = [Flat, col1]],
       [Mode = Surface,
             [2 + u, -1 + v, 2],
             u = [0, 2], v = [0, 2],
             Grid = [4, 4],
             Style = [ColorPatches, AndMesh],
             Color = [Flat, col1]],
       [Mode = Surface,
             [sin(u)*(4-sin(v))/2,
              cos(u)*(4-sin(v))/2, v],
             u = [-PI, PI], v = [0, PI],
             Grid = [20, 10], Smoothness = [1, 1],
             Style = [ColorPatches, AndMesh],
             Color = [Height, col1, col2]],
       [Mode = Surface,
             [sin(u)/6 + 4, cos(u)/6 + 1.3, v],
             u = [-PI, PI], v = [0, 5],
             Grid = [10, 10], Smoothness = [1, 0],
             Style = [ColorPatches, AndMesh],
             Color = [Height, col1, col2]],
       [Mode = Surface,
             [sin(u)*cos(v)*2 + 6, cos(u)*cos(v)*2,
              sin(v) + 3],
             u = [-PI, PI], v = [0, PI*1/2],
             Grid = [10, 10],
             Style = [ColorPatches, AndMesh],
             Color = [Height, col3, col2]],
       [Mode = Surface,
             [sin(u)*2 + 6, cos(u)*2, v],
             u = [-PI, PI], v = [0, 3],
             Grid = [10, 10],
             Style = [ColorPatches, AndMesh],
             Color = [Height, col1, col3]]);
<<<>>>
reset();
<<<>>>
mid_point := proc(p1, p2)
    local    x, y;
begin
    x := op(p1,1) + (op(p2,1)-op(p1,1))/2.0:
    y := op(p1,2) + (op(p2,2)-op(p1,2))/2.0:
    point(x, y):
end_proc:

sierpinski := proc(p1, p2, p3, n)
local   np1, np2, np3;
begin
    if n = 0 then
        polygon(p1, p2, p3, Closed = TRUE):
    else
        np1 := mid_point(p1, p2):
        np2 := mid_point(p2, p3):
        np3 := mid_point(p3, p1):
        sierpinski(p1, np1, np3, n-1),
        sierpinski(np1, p2, np2, n-1),
        sierpinski(np2, p3, np3, n-1):
    end_if:
end_proc:

P1 := point(-1, 0):
P2 := point( 1, 0):
P3 := point( 0, sqrt(3.0)):

plot2d(Axes = None,
       [Mode = List,
             [sierpinski(P1, P2, P3, 5)],
             Color = [Height,[0,1,1],[1,1,0]]]):
<<<>>>
tetra_hedron := proc(p1, p2, p3, p4)
begin
    polygon(p1, p2, p4, Closed=TRUE, Filled=TRUE),
    polygon(p1, p3, p4, Closed=TRUE, Filled=TRUE),
    polygon(p2, p3, p4, Closed=TRUE, Filled=TRUE)
end_proc:

mid_point := proc(p1, p2)
    local    x, y, z;
begin
    x     := op(p1,1) + (op(p2,1)-op(p1,1))/2.0:
    y     := op(p1,2) + (op(p2,2)-op(p1,2))/2.0:
    z     := op(p1,3) + (op(p2,3)-op(p1,3))/2.0:
    point(x, y, z):
end_proc:

tetra_rec := proc(p1, p2, p3, p4, n)
    local    np1, np2, np3, np4, np5, np6;
begin
    if n = 0 then
        tetra_hedron(p1, p2, p3, p4):
    else
        np1 := mid_point(p1, p2):
        np2 := mid_point(p2, p3):
        np3 := mid_point(p3, p1):
        np4 := mid_point(p1, p4):
        np5 := mid_point(p2, p4):
        np6 := mid_point(p3, p4):
        tetra_rec(p1, np1, np3, np4, n-1),
        tetra_rec(np1, p2, np2, np5, n-1),
        tetra_rec(np2, p3, np3, np6, n-1),
        tetra_rec(np4, np5, np6, p4, n-1):
    end_if:
end_proc:

a := point( 1.0,  0.0,                0.0):
b := point(-0.7,  0.5*sqrt(3.0),      0.0):
c := point(-0.7, -0.5*sqrt(3.0),      0.0):
d := point( 0.1,  0.0,           sqrt(3.0)):

plot3d(Axes = None,
       CameraPoint = [0.15, -0.1, 10.0],
       [Mode = List, [tetra_rec(a, b, c, d, 3)],
             Color = [Height]
       ]);
<<<>>>
loadlib("Lsys");

l := Lsys(90, "L", "L" = "L+R+", "R" = "-L-R",
              "L" = line, "R" = line):

Lsys::plot(l, 8):
<<<>>>
loadlib("Lsys");

l := Lsys(22.5, "L", "L" = "LL-[-L+L+L]+[+L-L-L]",
                "L" = line):

Lsys::plot(l, 3):

<<<>>>
reset();
<<<>>>
mandel := proc(x_coord, y_coord)
    local    c, new_z, old_z, i, maxit, eps;
begin
    eps    := 0.01:
    maxit  := 100:
    old_z  := 0.0:
    c      := x_coord + I*y_coord:
    for i from 1 to maxit do
        new_z := old_z^2 - c:
        if abs(old_z - new_z) < eps then
            return([0, 0, 1]):
        elif abs(new_z) > 4.0 then
            return([1, i/maxit, 0]):
        else
            old_z := new_z:
        end_if:
    end_for:
    return([0,0,1]):
end_proc:

pl := []:
for i from -0.75 to 1.75 step 0.1 do
    for j from -1.25 to 1.25 step 0.1 do
        pl := append(pl, point(i, j)):
    end_for:
end_for:

plot2d(Axes = None,
            [Mode = List, pl,
                  Color = [Function, mandel]
            ]);
<<<>>>
complex_surf := proc(re, im)
begin
    1/((re+I*im)^3+1):
end_proc:

phase := proc(x_coord, y_coord, z_coord, u_val, v_val)
    local    erg, real, imag, phi;
begin
    erg := complex_surf(x_coord, y_coord):
    real := op(erg, 1):
    imag := op(erg, 2):
    if abs(real) > EPS then
        phi := atan(imag/abs(real)):
    else
        phi := sign(imag)*PI/2:
    end_if:
    if float(phi) < 0 then
        value := (phi - MIN_PHI)/(0 - MIN_PHI):
        [1-value, value, 0]:
    else
        value := (phi)/MAX_PHI:
        [0, 1-value, value]:
    end_if:
end_proc:

MIN_PHI := float(-PI/2):
MAX_PHI :=  float(PI/2):
EPS     := 10^(-DIGITS):

plot3d(Axes = None, Scaling = UnConstrained,
       [Mode = Surface,
             [u, v, min(abs(complex_surf(u, v)), 2.0)],
             u = [-2, 2], v = [-2,2],
             Style = [ColorPatches, AndMesh],
             Color = [Function, phase],
             Grid = [50, 50]
       ]):
<<<>>>
READ_PATH := LIB_PATH.pathname(Up,"demo","graphics");
<<<>>>
read("kdv_two_film.mu"):
<<<>>>
kdv_two_film();
<<<>>>
read("kdv_fatou_film.mu"):
<<<>>>
kdv_fatou_film();
<<<>>>
read("cat_film.mu"):
<<<>>>
cat_film():
<<<>>>
reset(): loadlib("domains"): export(domains):
<<<>>>
MQ := Matrix( Rational );
<<<>>>
A := MQ( 3,3,1/2 );
<<<>>>
type( A );
<<<>>>
A::hasProp( MatrixCat );
<<<>>>
MZ6 := Matrix( IntegerMod(6) );
<<<>>>
B := MZ6( 2,4,[[3,0,8,1],[1,-5]] );
<<<>>>
MZ := Matrix( Integer );
<<<>>>
f := proc(i,j)
     begin
        if i = j then
           2
        elif i-1 = j or i+1 = j then
           -1
        else
           0
        end_if
     end_proc:
<<<>>>
C := MZ( 6,6,f );
<<<>>>
F := MZ6( C );
<<<>>>
testtype( C,MZ6 );
<<<>>>
F := MZ6::convert( C );
<<<>>>
type( F );
<<<>>>
MExpr := Matrix();
<<<>>>
A := MExpr( [[sin(PI/2*a),I*b],[-I*c,cos(PI*d)]] );
<<<>>>
MZ := Matrix( Integer );
<<<>>>
MZ::allEntries();
<<<>>>
A := MZ( [[1,2,3],[4,5,6],[7,8,9]] );
<<<>>>
A::gaussElim( A );
<<<>>>
A::delRow( A::delCol(A,2),1 );
<<<>>>
A::transpose( A );
<<<>>>
A::col( A,2 );
<<<>>>
A::stackMatrix( A,A::row( A,1 ) );
<<<>>>
A::concatMatrix( A,A::col( A,2 ) );
<<<>>>
A::swapCol( A,1,3 );
<<<>>>
A::setRow( A,3,MZ(1,3,[1,0,1]) );
<<<>>>
map( A,proc(x) begin
           if isprime(x) then 1 else 0 end_if
       end_proc
);
<<<>>>
A := MQ( 2,2,[1,5],Diagonal );
<<<>>>
B := MQ( [[1,2],[3,4]] ): C := MQ( [[0,4],[5]]  ):
<<<>>>
A - B + C*B - 3*(A/C+B);
<<<>>>
1/2*B + A^10 - B^(-2);
<<<>>>
C^0 * 1/(A*C);
<<<>>>
A := MExpr( [[sin(a),I*b],[-I*c,cos(d)]] );
<<<>>>
A^(-1);
<<<>>>
A::gaussElim( A );
<<<>>>
conjugate( A );
<<<>>>
SMQ := SquareMatrix( 3,Rational );
<<<>>>
SMZ := SquareMatrix( 3,Integer );
<<<>>>
SMQ::hasProp( Ring );
<<<>>>
A := SMQ( 5/2,Diagonal );
<<<>>>
B := SMZ( [[1,2,3],[-5,3],[]] );
<<<>>>
A+B;
<<<>>>
testtype( A,SMZ );
<<<>>>
testtype( B,SMQ );
<<<>>>
F := SMQ::convert( B );
<<<>>>
A+F, A*F;
<<<>>>
F^10;
<<<>>>
A := MQ( 3,4,1,Diagonal );
<<<>>>
B := SMQ( [1/2,0,-1/2],Diagonal );
<<<>>>
testtype( A,SMQ ), testtype( B,MQ );
<<<>>>
MQ::convert( B );
<<<>>>
type( % );
<<<>>>
A := SMQ( [1,2,3],Diagonal );
<<<>>>
B := A::delRow( A,2 );
<<<>>>
type( B );
<<<>>>
reset(): loadlib("linalg"): export(linalg):
<<<>>>
MZ := Matrix( Integer ):
MQ := Matrix( Rational ):
MExpr := Matrix():
<<<>>>
info( linalg );
<<<>>>
A := MQ( [[1,2,3,4],[-1,0,1],[3,5,6,9]] );
<<<>>>
gaussElim( A );
<<<>>>
gaussJordan( A );
<<<>>>
A := randomMatrix(
    2,2,DistributedPolynomial([x],IntegerMod(7))
);
<<<>>>
det( A );
<<<>>>
gaussJordan( A );
<<<>>>
-A; 7 * A;
<<<>>>
iszero( % );
<<<>>>
A := MQ( [[1,-2],[3,-8]] );
<<<>>>
linalg::norm( A );
<<<>>>
f := poly( 1+2*x+10*x^2,IntMod(7) );
g := poly( x^2+23*x-5,IntMod(7) );
<<<>>>
sylvester( f,g,x );
<<<>>>
v1 := MQ( [1,0,1,1,0] ):
v2 := MQ( [2,2,6,2,4] ):
v3 := MQ( [1,2,5,3,5] ):
v4 := MQ( [0,-1,-2,2,-1] ):
<<<>>>
freeSet( [v1,v2,v3,v4] );
<<<>>>
bool( nops( freeSet({v1,v2}) ) = 2 );
<<<>>>
A := MExpr( [[2,-3,-1],[1,1,-1],[0,1,-1]] );
<<<>>>
f := factorQR( A ):
Q := op(f,1); R := op(f,2);
<<<>>>
linalg::innerProduct := proc(f,g) local F, R, t;
begin
    R := f::coeffRing;

    F := int( f[1]*g[1], t );
    F := normal( subs(F,t=1) - subs(F,t=0) );

    R(F)
end_proc:
<<<>>>
p1 := MExpr( [1] ): p2 := MExpr( [t] ):
p3 := MExpr( [t^2] ): p4 := MExpr( [t^3] ):
<<<>>>
S := linalg::ogSystem( [p1,p2,p3,p4] );
<<<>>>
linalg::innerProduct( op(S,2),op(S,4) );
<<<>>>
normalize( p3 );
<<<>>>
A := MQ( [[1,2,3],[5,7,9],[2,4,6]] );
<<<>>>
nullSpace( A );
<<<>>>
reset(): loadlib("linalg"): export(linalg):
<<<>>>
MQ := Matrix( Rational ):
<<<>>>
M := MQ( [[1,3,-4,3],[3,9,-2,-11],
         [4,12,-6,-8],[2,6,2,-14]] );
<<<>>>
v := MQ( [9,-3,6,-12] );
<<<>>>
linearSolve( M,v );
<<<>>>
linearSolve( M,v,c );
<<<>>>
linearSolve( M,v,[2,-5] );
<<<>>>
linearSolve( M,v,Special );
<<<>>>
A := MQ( [[1,2],[1,3]] );
<<<>>>
B := MQ( [[1,1],[-2,1]] );
<<<>>>
linearSolve( A,B );
<<<>>>
expr2Matrix(
   [x+2*y-2=-5*z+3*x,x+z+10=-8*z],[x,y,z],Rational
);
<<<>>>
linearSolve( op(%) );
<<<>>>
reset(): loadlib("linalg"): export(linalg):
READ_PATH := LIB_PATH.pathname(Up,"demo","domains"):
read ("GaussNum.mu"):
<<<>>>
GaussInt := GaussianNumber( Integer );
<<<>>>
GaussInt::hasProp( IntegralDomain );
<<<>>>
MGaussInt := Matrix( GaussInt );
<<<>>>
f := proc(i,j) begin i+I*j end_proc:
A := MGaussInt( 4,4,f );
<<<>>>
gaussJordan( A );
<<<>>>
B := MGaussInt( [[1+I,4-2*I],[3-2*I,1-I]] );
<<<>>>
det( B );
<<<>>>
C := gaussJordan( B );
<<<>>>
F := stackMatrix( B,C );
<<<>>>
A*F;
<<<>>>
l:=expr2Matrix(
      [(4+2*I)*u-(2*I)*v=-4*I,2*u+(2-2*I)*v=-8-22*I],GaussInt
   ):
A := op(l,1); y := op(l,2);
<<<>>>
x := linearSolve( A,y );
<<<>>>
A*x;
<<<>>>
reset():
READ_PATH := LIB_PATH.pathname(Up,"demo","domains"):
<<<>>>
Z7 := domain():
<<<>>>
Z7::name := "Z7":
<<<>>>
Z7::new := proc(x)
           begin
              new(Z7, x mod 7);
           end_proc:
<<<>>>
Z7::print := proc(x)
             begin
               subs( hold(q mod 7), hold(q)=extop(x,1) )
             end_proc:
<<<>>>
a := Z7::new(9);
<<<>>>
b := Z7(9);
<<<>>>
Z7::_plus := proc()
             local x, i;
             begin
                x := extop(args(1), 1);
                for i from 2 to args(0) do
                   x := (x + extop(args(i), 1)) mod 7;
                end_for;
                Z7::new(x);
             end_proc:
<<<>>>
Z7::_mult := proc()
             local x, i;
             begin
                x := extop(args(1), 1);
                for i from 2 to args(0) do
                   x := (x * extop(args(i), 1)) mod 7;
                end_for;
                Z7::new(x);
             end_proc:
<<<>>>
b := Z7(1);
c := Z7(11);
<<<>>>
a+a+Z7(3)*b;
a*b*b;
<<<>>>
Z7::type := proc(x)
            begin
               "Z7"
            end_proc:
<<<>>>
Z7::testtype := proc(x,y)
                begin
                   bool(extop(x,0) = y);
                end_proc:
<<<>>>
Z7::domtype := proc(x)
               begin
                 Z7
               end_proc:
<<<>>>
domtype(a);
type(a);
testtype(a, DOM_STRING);
<<<>>>
Z7::zero := Z7::new(0):
<<<>>>
Z7::one := Z7::new(1):
<<<>>>
Z7::divide := proc(x,y)
              begin
                 Z7::new((extop(x,1)
                          / extop(x,2)) mod 7);
              end_proc:
<<<>>>
Z7::negate := proc(x)
              begin
                 Z7::new( - extop(x,1) mod 7)
              end_proc:
<<<>>>
Z7::invert := proc(x)
              begin
                Z7::new((1/extop(x,1)) mod 7)
              end_proc:
<<<>>>
Z7::_power := proc(x,y)
              local help, i;
              begin
                 if y=0 then return (Z7::one) end_if;
                 if y>0 then
                    help := x;
                 else
                    help := Z7::invert(x);
                    x := help;
                 end_if;
                 for i from 2 to abs(y) do
                    help := Z7::_mult(help, x);
                 end_for;
              end_proc:
<<<>>>
a^5-b/c; 1/Z7(100);
<<<>>>
reset():
<<<>>>
NUMERIC := domain();
<<<>>>
NUMERIC::name := "NUMERIC";
<<<>>>
NUMERIC::testtype := proc(x, y)
begin
     if contains({DOM_INT, DOM_FLOAT, DOM_COMPLEX,
        DOM_RAT}, domtype(x))
     then
        TRUE
     else
        FAIL
     end_if
end_proc;
<<<>>>
testtype(123, NUMERIC);
<<<>>>
testtype(1/2, NUMERIC);
<<<>>>
testtype(2*a, NUMERIC);
<<<>>>
reset(): loadlib("domains"): export(domains):
<<<>>>
loadlib("domains"): export(domains):
<<<>>>
Z7 := IntegerMod(7);
<<<>>>
a := Z7(-100); b := Z7(57); c := Z7(2);
<<<>>>
a-b^2; a*b/c+a^3;
<<<>>>
Z7::hasProp(Field);
<<<>>>
Z8 := IntegerMod(8);
<<<>>>
d := Z8(22);
<<<>>>
b+d;
<<<>>>
Z8::hasProp(Field); Z8::hasProp(Ring);
<<<>>>
Integer::negate;
<<<>>>
Integer::whichEntry("negate");
<<<>>>
reset(): loadlib("domains"): export(domains):
<<<>>>
#++

GaussianNumber(R)  -  Gaussian numbers over R

R : ring

A domain created by GaussianNumber defines
the ring of gaussian numbers with coefficients
of R. This ring is an integral domain iff R is it.

++#

GaussianNumber := DomainConstructor::new(
# #
# name #
    GaussianNumber,
# one argument: the coefficient ring R #
    [ R ],
# no local variables #
    [ ],
# initialisation #
    (if R::hasProp(Ring) <> TRUE then
        error("expecting a Ring as argument")
     end_if),
# BaseDomain is super domain of any domain constructor #
    BaseDomain,
# one categorie #
    [ (if R::hasProp(IntegralDomain) then
           IntegralDomain
       else
           Ring
       end_if)
    ],
# axioms:
  a gaussian number has of course a normal representation #
    [ (if R::hasProp(canonicalRep) then
           canonicalRep, normalRep
       else
           normalRep
       end_if)
    ],
# #
# methods #
    "new" = proc(a,b)
        local aR, bR;
    begin
        if testargs() then
            case args(0)
            of 1 do
                if domtype(a) = DOM_COMPLEX then
                    if not testtype( op(a,1),R )
                    or not testtype( op(a,2),R )
                    then
                        error("invalid argument")
                    end_if
                elif not testtype( a,R ) then
                    error("invalid argument")
                end_if;
                break
            of 2 do
                if not testtype(a,R) or not testtype(b,R) then
                    error("invalid arguments")
                end_if;
                break
            otherwise
                error("wrong no of args")
            end_case
        end_if;

        case args(0)
        of 1 do
            if domtype(a) = DOM_COMPLEX then
                aR := R::convert( op(a,1) );
                bR := R::convert( op(a,2) );

                return( new( this,aR,bR ) )
            end_if;
            aR := R::convert( a );
            if aR = FAIL then aR := a::convert_to( a,R ) end_if;

            return( new( this,aR,R::zero ) )
        of 2 do
            aR := R::convert( a ); bR := R::convert( b );
            if aR = FAIL then aR := a::convert_to( a,R ) end_if;
            if bR = FAIL then bR := b::convert_to( b,R ) end_if;

            new( this,aR,bR )
        end_case
    end_proc,

    "print" = proc(x)
    begin
        if iszero(extop(x,1)) then
            expr2text( extop(x,2) )
        elif iszero(extop(x,2)) then
            expr2text( extop(x,1) )
        else
            "(".expr2text( extop(x,1)+I*extop(x,2) ).")"
        end_if
    end_proc,

    "one" = new( this,R::one,R::zero ),

    "zero" = new( this,R::zero,R::zero ),

    "iszero" = proc(x)
     begin
        bool( R::iszero( extop(x,1) ) and R::iszero( extop(x,2) ) )
    end_proc,

    "_plus" = proc(a,b)
        local t;
    begin
        if args(0) > 2 then
            return( this::_plus(
        a,this::_plus(args(i) $ hold(i)=2..args(0))
            ) )
        end_if;

        if domtype(a) = this then
            if domtype(b) = this then
                t := (extop(a,1)+I*extop(a,2))
                     + (extop(b,1)+I*extop(b,2));
                return( new(this,Re(t),Im(t)) )
            elif testtype(b,R) then
                t := (extop(a,1)+I*extop(a,2)) + b;
                return( new(this,Re(t),Im(t)) )
            else
                FAIL
            end_if
        else
            return( this::_mult(b,a) )
        end_if
    end_proc,

    "_mult" = proc(a,b)
        local t;
    begin
        if args(0) > 2 then
            return( this::_mult(
         a,this::_mult(args(i) $ hold(i)=2..args(0))
            ) )
        end_if;

        if domtype(a) = this then
            if domtype(b) = this then
                t := expand(
                      (extop(a,1)+I*extop(a,2)) *
                      (extop(b,1)+I*extop(b,2))
                     );
                return( new(this,Re(t),Im(t)) )
            elif testtype(b,R) then
                t := expand( (extop(a,1)+I*extop(a,2)) * b );
                return( new(this,Re(t),Im(t)) )
            else
                FAIL
            end_if
        else
            return( this::_mult(b,a) )
        end_if
    end_proc,

    "negate" = proc(x)
    begin
        new( this,R::negate(extop(x,1)),R::negate(extop(x,2)) )
    end_proc,

    "testtype" = proc(e,D)
        local d;
    begin
        d := domtype(e);
        if domtype(D) <> DOM_DOMAIN then D := domtype(D) end_if;

        if d = D then
            return( TRUE )
        elif d = this then
            if D = DOM_COMPLEX then
                if testtype( extop(e,1),Type::RealNum ) then
                    return( TRUE )
                end_if;
                 return( FALSE )
            elif R::iszero(extop(e,2)) then
                return( testtype( extop(e,1),D ) )
            end_if
        elif D = this then
            if d = DOM_COMPLEX then
                return(testtype(op(e,1),R) and testtype(op(e,2),R))
            end_if;
            return( testtype( e,R ) )
        end_if;

        FAIL
    end_proc,

    "convert" = proc(e)
        local d, x1, x2;
    begin
        d := domtype(e);
        if d = this then return( e ) end_if;
        if d = DOM_COMPLEX then
            if testtype( op(e,1),R ) and testtype( op(e,2),R ) then
                d := op(e,1);
                x1 := R::convert(d);
                if x1 = FAIL then x1 := d::convert_to(d,R) end_if;
                d := op(e,2);
                x2 := R::convert(d);
                if x2 = FAIL then x2 := d::convert_to(d,R) end_if;

                return( new( this,x1,x2 ) );
            end_if;
            return( FAIL )
        elif testtype( e,R ) then
            x1 := R::convert(e);
            if x1 = FAIL then x1 := d::convert_to(e,R) end_if;

            return( new( this,x1,R::zero ) )
        end_if;
        FAIL
    end_proc,

    "convert_to" = proc(e,D)
        local t;
    begin
        if domtype(e) = D then return(e) end_if;
        if domtype(D) <> DOM_DOMAIN then D := domtype(D) end_if;
        if domtype(e) = this then
            if D = DOM_COMPLEX then
                if testtype( extop(e,1),Type::RealNum ) then
                    return( extop(e,1)+I*extop(e,2) )
                end_if
            elif R::iszero(extop(e,2)) then
                if testtype( extop(e,1),D ) then
                    t := D::convert(extop(e,1));
                    if t = FAIL then t := R::convert_to(extop(e,1),D) end_if;

                    return( t )
                end_if
            end_if
        end_if;

        FAIL
    end_proc,

    "expr" = this::print,

    "norm" =
        (if not testtype(R::one,Type::RealNum) then
            FAIL
        else
            proc(x)
            begin
                sqrt( R::_plus( R::_mult(extop(x,1),extop(x,1)),
                      R::_mult(extop(x,2),extop(x,2)) ) )
            end_proc
        end_if),

    "divex" = (
        if not R::hasProp( IntegralDomain ) then
            FAIL
        else
            proc(x,y)
                 local x1, x2, y1, y2, d, d1, d2, n;
            begin
                x1 := extop(x,1); x2 := extop(x,2);
                y1 := extop(y,1); y2 := extop(y,2);
                d := R::_plus( R::_mult( y1,y1 ),R::_mult( y2,y2 ) );
                n := R::_plus( R::_mult( x1,y1 ),R::_mult( x2,y2 ) );
                d1 := R::divex( n,d );
                if d1 = FAIL then
                    return( FAIL )
                end_if;

                n := R::_plus( R::negate(R::_mult( x1,y2 )),R::_mult( x2,y1 ) );
                d2 := R::divex( n,d );
                if d2 = FAIL then
                    return( FAIL )
                end_if;

                new( this,d1,d2 )
            end_proc
        end_if),

    "invert" = proc(x)
    begin
        this::divex( this::one,x )
    end_proc,

    "diff" = this::zero
):
<<<>>>
GaussInt := GaussianNumber( Integer );
<<<>>>
a := GaussInt(4,2); b := GaussInt(2,1);
<<<>>>
a+b; a-b; a*b; a^10; 4*a;
<<<>>>
4*a;
<<<>>>
GaussInt::hasProp(CommutativeRing);
<<<>>>
GaussInt::allEntries();
<<<>>>
GaussInt::whichEntry("intmult");
<<<>>>
GaussInt::whichEntry("_power");
<<<>>>
GaussInt::getCategories();
<<<>>>
GaussInt::allAxioms();
<<<>>>
reset():
READ_PATH:= LIB_PATH.pathname(Up,"demo","mmg"):
read("mdm.mu"):
<<<>>>
loadmod( "stdmod" ):
info( stdmod ):
<<<>>>
stdmod::which( "stdmod" );
stdmod::which( "unknown module" );
<<<>>>
export( stdmod ):
which( "stdmod" );
<<<>>>
stdmod::max(2);
stdmod::age(300);
age();
<<<>>>
date:= external( "date", "util" ):
print( Unquoted, date() );
<<<>>>
stat();
<<<>>>
loadmod( "slave" ):
stat()["entry"];
<<<>>>
unloadmod( "slave" ):
stat()["entry"];
<<<>>>
print( Unquoted, slave::infomod() ):
stat()["entry"];
<<<>>>
export( module(util) ):
user();
<<<>>>
module::stat():
<<<>>>
module::help( "help", "module" ):
<<<>>>
module::help( "stdmod" ):
<<<>>>
module::help( "slave" ):
<<<>>>
module::help( "string" ):
<<<>>>
module::help( "util" ):
<<<>>>
module::help( "stack" ):
<<<>>>
module::unload():
<<<>>>
module( "slave" ):
<<<>>>
Calculator:= slave::open( "bc" );
<<<>>>
if( (mesg:= slave::get(Calculator)) <> "" ) then
print( Unquoted, mesg );
else
print( Unquoted, "No startup message" );
end_if;
<<<>>>
NumOf:= slave::put( Calculator, "2*20+2\n quit\n" );
<<<>>>
Available := not( slave::timeout( Calculator, 5 ) );
<<<>>>
TheAnswer := slave::get( Calculator ):
print( Unquoted, "The answer is: ".TheAnswer );
slave::close( Calculator ):
<<<>>>
Lower:= "xxx":
Upper = slave::exec( "echo \"".Lower."\" | tr x X" );
Path  = slave::exec( "echo $MuPAD_ROOT_PATH" );
<<<>>>
module::help( "fetch", "slave" ):
slave::fetch("",13,"");
<<<>>>
slave::mupad_news();
<<<>>>
unloadmod();
<<<>>>
reset():
READ_PATH:= LIB_PATH.pathname(Up,"demo","mmg"):
read("mdm.mu"):
<<<>>>
! man mmg
<<<>>>
system( "more ".DEMO_1 ):
<<<>>>
system("mmg -v ".DEMO_1." -V demo"):
  
<<<>>>
system("mmg -v ".DEMO_1." -V demo -gcc"):
  
<<<>>>
! ls -l demo1.mdm
<<<>>>
info( module("demo1") ):
<<<>>>
Today = demo1::mydate();
module::unload(demo1):
<<<>>>
reset():
READ_PATH:= LIB_PATH.pathname(Up,"demo","mmg"):
read("mdm.mu"):
<<<>>>
loadlib( "mmc" );
f1; smile;
<<<>>>
f1(), -f1(-XX), -f1(3);
smile():
<<<>>>
mmc( "demo2", {"f1","smile"}, "-v -V demo" );
  
<<<>>>
mmc( "demo2", {"f1","smile"}, "-v -V demo -gcc" );
  
<<<>>>
info( module("demo2") ):
<<<>>>
demo2::f1(), -demo2::f1(-XX), -demo2::f1(3);
demo2::smile();
module::unload(demo2):
<<<>>>
reset():
READ_PATH:= LIB_PATH.pathname(Up,"demo","mmg"):
read("mdm.mu"):
<<<>>>
loadlib( "maple"):
maple::open():
<<<>>>
maple::put( "int( sin(x)*cos(x), x );\n" ):
<<<>>>
maple::data();
<<<>>>
maple::get();
<<<>>>
answer:= maple( int( sin(x)*cos(x), x ) );
<<<>>>
diff( answer, x );
<<<>>>
maple::close();
<<<>>>
reset():
READ_PATH:= LIB_PATH.pathname(Up,"demo","mmg"):
read("mdm.mu"):
check_system("sun4","solaris","ibmrs6000");
<<<>>>
system( "more ".CMATH ):
<<<>>>
system( "mmg ".CMATH.
" -v -V \"Faster Sinus\" -L".
        MOD_PATH." -l".LIBM):
  
<<<>>>
system( "mmg ".CMATH.
" -v -V \"Faster Sinus\" -gcc -L".
        MOD_PATH." -l".LIBM ):
  
<<<>>>
export( module("cmath") ):
sin(1.3) - csin(1.3);
<<<>>>
DIGITS:=13:
time((for i from 1 to 1000 do sin (1.3) end_for));
time((for i from 1 to 1000 do csin(1.3) end_for));
DIGITS:=NIL:
<<<>>>
time( plot3d(Axes = Box, Ticks = 0, LineWidth = 2,
       CameraPoint = [-0.2, 1.0, -20],
       [Mode = Curve,
             [u/8*sin(u), u/8*cos(u), 3*cos(u/8)],
             u = [0, 8*PI], Grid = [160],
             Color = [Height]
       ]) );
<<<>>>
time( plot3d(Axes = Box, Ticks = 0, LineWidth = 2,
       CameraPoint = [-0.2, 1.0, -20],
       [Mode = Curve,
             [u/8*csin(u), u/8*ccos(u), 3*ccos(u/8)],
             u = [0, 8*PI], Grid = [160],
             Color = [Height]
       ]) );
<<<>>>
module::unload( cmath );
<<<>>>
reset():
READ_PATH:= LIB_PATH.pathname(Up,"demo","mmg"):
read("mdm.mu"):
check_system("sun4","solaris");
<<<>>>
info( (sound := module(SOUNDM)) ):
list:= external("exec","slave")( "cd ".SND_PATH."; ls *.au"):
print( Unquoted, list );
<<<>>>
snd:= sound::new( SND_PATH.pathname("")."perfectly" );
sound::play( snd ):
<<<>>>
sound::play( sound( SND_PATH.pathname("")."answers" ) ):
<<<>>>
system( "more ".SOUND ):
<<<>>>
reset():
<<<>>>
info(groebner);
<<<>>>
export(groebner);
<<<>>>
p1 := poly(x^2*y + 5*x^2 + y^2, [y,x]);
<<<>>>
p2 := poly(7*x*y^2 - 2*y^3 + 1, [y,x]);
<<<>>>
spoly(p1, p2);
<<<>>>
spoly(p1, p2, LexOrder);
<<<>>>
q := poly(3*x^3*y + 2*x^2*y^2 - 3*x*y + 5*x, [y,x]);
<<<>>>
normalf(q, [p1, p2]);
<<<>>>
gbasis([p1, p2]);
<<<>>>
gbasis([p1, p2], LexOrder, Reorder);
<<<>>>
reset(): export(groebner):
<<<>>>
p1 := poly( x  + y*z - 2, [x, y, z] );
<<<>>>
p2 := poly( x*z + y  - 3, [x, y, z] );
<<<>>>
p3 := poly( x*y + z  - 5, [x, y, z] );
<<<>>>
B := gbasis( [p1, p2, p3] ):
map( B, expr );
<<<>>>
map( B, lterm, DegreeOrder ):
map( %, expr );
<<<>>>
p1 := poly( x^2*y + 4*y^2 - 17, [x,y]);
<<<>>>
p2 := poly( 2*x*y - 3*y^3 + 8, [x,y]);
<<<>>>
p3 := poly( x*y^2 - 5*x*y + 1, [x,y]);
<<<>>>
gbasis( [ p1, p2, p3 ] ):
map( %, expr );
<<<>>>
gbasis( [ p1, p2, p3 ], LexOrder ):
map( %, expr );
<<<>>>
reset(): loadlib("orthpoly"):
export(orthpoly): info(orthpoly);
<<<>>>
chebyshev1(3,x);
<<<>>>
chebyshev2(4,x);
<<<>>>
gegenbauer(5,2,x);
<<<>>>
hermite(3,x);
<<<>>>
jacobi(3,1,2,x);
<<<>>>
laguerre(3,1/2,x);
<<<>>>
legendre(4,x);
<<<>>>
reset():
READ_PATH:=LIB_PATH.pathname(Up,"demo","debugger");
<<<>>>
read("example1.mu");
<<<>>>
lremove([a, b, c, d, e, f, e], e);
<<<>>>
reset():
READ_PATH:=LIB_PATH.pathname(Up,"demo","debugger"):
<<<>>>
read("example2.mu");
<<<>>>
resultant(x+1,x^2+2*x+1,x);
<<<>>>
reset():
<<<>>>
a := contfrac( PI,5 );
<<<>>>
b := contfrac( sqrt(5),4 );
<<<>>>
CF::approx( a );
<<<>>>
CF::linear( b,1,1,0,2 );
<<<>>>
a+b;
<<<>>>
a*b;
<<<>>>
a^2/b;
<<<>>>
reset():
loadlib("linalg"): export(linalg):
<<<>>>
l := expr2Matrix( [x+5*y=-2, 2*x+y=4 ],[x,y],Rational ):
A := l[1]; b := l[2];
<<<>>>
linearSolve( A,b );
<<<>>>
READ_PATH := LIB_PATH.pathname(Up,"demo","domains"):
read("IA.mu"):
<<<>>>
MIA := Matrix( IA ):
l := expr2Matrix(
         [IA(0.99,1.01)*x + IA(4.99,5.01)*y = IA(-2.01,-1.99),
          IA(1.99,2.01)*x + IA(0.99,1.01)*y = IA(3.99,4.01)]
     ,IA ):
AI := l[1]; bI := l[2];
<<<>>>
linearSolve( AI,bI );
<<<>>>
reset(): loadlib("linalg"): export(linalg):
<<<>>>
sqmat := SquareMatrix( 2,Rational );
<<<>>>
a := sqmat( [[2,3],[1,0]] ):
b := sqmat( [[1,0],[-2,3]] ):
c := sqmat( [[0,-4],[2,1]] ):
d := sqmat( [[0,1],[2,-1]] ):
<<<>>>
MS := Matrix( sqmat ):
<<<>>>
A := MS( [[a,b],[c,d]] );
<<<>>>
f := fun( sqmat( randomMatrix( 2,2,Integer ) ) ):
B := MS( 2,2,f );
C := MS( 2,2,f,Diagonal );
<<<>>>
A*B + B - 2*C;
<<<>>>
A^5;
<<<>>>
B*C - C*B;
<<<>>>
det( a );
<<<>>>
det( d-c*(1/a)*b );
<<<>>>
f := d-c*(1/a)*b:
iA := MS( [[1/a+1/a*b*1/f*c*1/a, -1/a*b*1/f],
           [    -1/f*c*1/a     ,    1/f   ]]);
<<<>>>
A * iA;
iA * A;
<<<>>>
