You are on page 1of 20

Cu trc d liu

I. Dy biu thc ( Expression Sequence)


- Dy l mt cu trc d liu gm:
- Cc phn t ngn cch nhau bi du ,
- Cc phn t ca dy c th l bt c g.
- Maple p dng s nh gi y khi nh gi mt dy.

V d 1
>

>

- Xem kiu ca stuff bng whattype(stuff)


>

- Tm s phn t ca stuff bng hm nops(...)


>

- Truy cp mt hay dy phn t ca stuff


>

>

>

>

- Chp nhn ch s m
>

>

- NULL l dy rng

V d 2
>

- To ra mt dy cha cc s nguyn t t th 10 n th 19
>

- So snh hai dy dng > evalb(day1 = day2);

V d 3
>

>

- Ghp ni hai dy

V d 4
>

- S tr v ca hm solve

V d 5
>

>

- Khi tr v mt danh sch rng Maple dng nh khng ng tnh g

V d 6

>

- Dng lnh seq to ra dy


- C php : > seq(f(i), i=m..n);
> seq(f(i), i = x);
> seq(f(i), i in x);

V d 7
>

>

Danh sch (List)


- Danh sch l dy c bao trong cp du ngoc vung.
- Cc phn t ca danh sch co th l bt c g.

V d 1

>

>

- Tm s phn t ca list bng nops(L);


>

- Truy cp cc phn t ca list


>

>

>

- Thay i cc phn t ca list

>

>

- Hm op(L) tr v dy tng ng vi danh sch => dng ni hai danh sch

V d 2

>

>

- o ngc cc phn t ca danh sch

V d 3

>

>

Tp hp (Set)
- Tp hp l dy c bao trong cp du ngoc nhn.
- Trong tp hp th t cc phn t khng quan trng.
- Cac php tan trn tp hp nh union(hp),intersect(giao),minus(tr).

V d 1

>

>

>

>

>

>
Error, invalid left hand side in assignment

>
Error, invalid quotes and

2 additional errors.

- Cc php tan trn tp hp

V d 2

>

Mng (Array)
- Mng l cu trc d liu bao gm.
- Mt hay nhiu chiu.
- Min ch mc ca cc chiu phi l khang s nguyn.
- Cc phn t ca mng c th l mi th.
- C php : > array(indexfcn , bounds ,list);
- indexfcn (ty chn) : hm ch s.

- bounds (ty chn) : dy ca cc khang.


- list (ty chn) : danh sch cc gi tr khi u.

V d
> v := array(1..4):
for i to 3 do v[i] := i^2 end do:
print(v);

>
> v[2];

Trying to refer to something out of bounds returns an error:


> v[0];
Error, 1st index, 0, smaller than lower array bound 1
> A := array(1..2,1..2):
A[1,2] := x:
A[1,1];

> A[1,2];

> print(A);

> A := array( symmetric, 1..2,1..2, [ [1,x], [x,x^2] ] ):


op(1,eval(A));

> op(2,eval(A));

> op(3,eval(A));

> map(diff,A,x);

V d :
In ra dy Fibonaci t 10 n 20

>

Bng (Table)
- Bng l cu trc d liu trong cc ch mc c th l bt c g.
- C php: > table (F,L);
* F (ty chn): hm ch s
* L (ty chn): dy hay tp hp gm cc phn t ca bng.

- Nu L c dng danh sch hay tp hp cc ng thc th cc v tri s l cc ch s ca


mng ; cc phn t v phi s l cc phn t ca bng.
- Tri li, cc ch s l cc s nguyn 1,2,3...
- indices(bang) : tr v dy ch s.
- entries(bang) : tra v dy cc phn t ca bng.

Cc v d
> table();

> T:=table([22,42]);
T[2];

> S := table([(2)=45,(4)=61]);

> S[1], S[2];

> T := table(symmetric,[(c,b)=x]);

> T[c,b];

> T[b,c];

> op(T);
> op(op(T));
> F := table([sin=cos,cos=-sin]):
op(op(F));
indices(F);
entries(F);

> F[cos](Pi/2);

>

G b phn t c ch s 2
>

>

Thm phn t c ch s 7 vi ga tr x^2


>

>

>

Mt s hm thng dng trn cu trc d liu


Hm pht sinh s ngu nhin
- rand(m..n)s tr v th tc nu gi c th sinh s nguyn ngu nhin trong khang m..n
- rand(m..n)( ): sinh s nguyn ngu nhin trong khang m..n.
- rand( ): sinh s nguyn ngu nhin khng m c 12 ch s.
- randomize( ): gieo ging li b sinh s.
>

>

>

>

- M phng tr chi gieo ng xu , s gieo 2000 ln.

>

Xac suat ngua: 0.494500


>

Hm tm max,min mt dy
- C php: > max( day );
> min( day );

V d
>

>

Hm map
V d
>
>
>
>
>
>
>

map(f, x + y*z);
map(f, y*z);
map(f, {a,b,c});
map(x -> x^2, x + y);
map(proc(x,y) x^2+y end proc, [1,2,3,4], 2);
map2(f, g, {a,b,c});
map2(op, 1, [a+b,c+d,e+f])

Hm add,mul

- add dng cng dy cc gi tr.


- mul dng nhn dy cc gi tr.
- C php:
add(f, i = m..n)
mul(f, i = m..n)

mul(f, i = x)

V d
> mul( i, i=1..5 );

> add( i^2, i=1..5 );

> L := [seq(i, i=1..5)];

> add( i^2, i=L );

> mul( x-i, i=L );

> add( a[i]*x^i, i=0..5 );

The add command works only for numeric ranges.


> add( i^2, i=0..n );
Error, unable to execute add
> sum( i^2, i=0..n );

add(f, i = x)
mul(f, i in x)

add(f, i in x)

> add( i, i=infinity..0 );

> mul( i, i=0..-infinity );

Hm subsop
- Hm subsop dng thay th mt lc ti nhiu v tr mt biu thc hoc mt cu trc
d liu.
- C php: > subsop(vt1=bt1,vt2=bt2,...,vtn=btn,bt);

V d
> p := x^7+8*x^6+x^2-9;

> op(2,p);

> subsop( 2=y, p );

> subsop( 2=-op(2,p), p );

> subsop( 1=0, p );

> subsop( 1=1, x*y*z );

> subsop( 1=NULL, 2=z, 3=y, [x,y,z] );

> subsop( 0=g, f[a,b,c] );

> p := f(x,g(x,y,z),x);

> subsop( [2,3]=w, p );

> subsop( [2,0]=h, [2,3]=w, 3=a, p );

> subsop( p );