Session 05
Sets, Lists, Sequences and Matrices
We begin by distinguishing between sequences, sets and lists.
A sequence is a data type consisting of objects separated by commas.
> s1:=1,2,3,4,5,6,7;
> s2:=I,am,you,r,n,class; whattype(s2); # objects can be quite general
> s:=you,`and`,I,r,n,class; whattype(s2); # note: can't use and, a boolean operator
Sequences can be concatenated (put together).
> s1,s2;
Here is one way to build a sequence of repeated objects.
> x$5;a$7;
A useful device for building sequences is the seq command.
> s3:=seq(i^2,i=1..5);
> s4:=seq(ithprime(i),i=1..10); # note ithprime is a Maple command
> v:={1,3,5,7};
> s5:=seq(sin(i),i=v); # the range of indices can be a set
> s6:=seq(i^2,i={x,y,z});
Elements of a sequence can be selected in various ways.
> s5[2];s5[2..3];
> s7:=seq(s4[i],i=[1,3,5,8]); s4;
Try this: s7:=seq(s4[i],i=(1,3,5,8));
and this: s7:=seq(s4[i],i={1,3,5,8});
This is one way you could generate numbers to play the Texas Lottery.
> s:=rand(0..50): seq(s(i),i=1..6);
rand(a..b) : returns a procedure which, when called, generates random integers in the range a..b
Here is a different type of sequence declaration.
> op(1..5); # op : applied to any Maple expression (except a sequence itself) returns a sequence of the operands of that expression
> op(x+y+z);
> seq(p.i, i=1..5);
A set is a sequence between braces, e.g., {a,b,c}
> se1:={s1};
> whattype(se1);
> s8:=a$5,b$3,c$2; # a sequence
> se2:={s8}; # a set
> s:=rand(0..50):{seq(s(i),i=1..6)};
We can perform the operations of union, intersection and minus with sets.
> se3:={1,2,3,4,5}; se4:={3,4,6,7,8,9};
> se5:=se3 union se4;
> se6:=se3 intersect se4;
> se7:=se3 minus se4;
> se6 intersect se7;
The empty set is {}
To decide if a certain object is an element of a set use member.
> member(2,se7,'pos');
> pos; # note: pos is the position in the set
> member(3,se7);
A list is a sequence in square brackets, e.g., [a,b,c].
You can construct a list by enclosing a sequence in square brackets.
> s1:=c$3,b$2,a$3;
> se:={s1}; # recall that a set does not duplicate elements
> ls1:=[s1]; # note that a list keeps all the elements in the same order as given in the sequence
> ls2:=sort(ls1); # list elements can be sorted using the sort command
> ls3:=[red,blue,green,white,yellow,black];
> member(indigo,ls3);
> member(green,ls3,'pos');
> pos; # the position of green in the list
> ls3[1]; ls3[2..4]; # listing a particular element or elements of the list
> nops(se3); # number of elements in the list
> subsop(5=indigo,ls3); # change the value of an element
> ls4:=[op(ls3), crimson];
> [ls3,crimson]; # note that this is not correct. It gives a list of lists. That's why we use op above.
Arrays are multidimensional rectangular tables of objects with each component indexed by some segment of integers. One dimensional arrays are called vectors. Two dimensional arrays are called matrices.
In order to do very much with special arrays like matrices and vectors you must first load the linalg package. (in Maple 11- The linalg package has been superseded by the Student[LinearAlgebra] and Student[VectorCalculus] packages)
> with(linalg);
> v:=vector([1+a,2+b,3+c]);
> M:=matrix([[1-p,2-q],[1-r,2-s]]); 
> vv:=vector(20); # you can declare a matrix or vector without specifying the entries
> vv[1];vv[5];vv[20];
> vv[1]:=7; # You can then assign values as you like
> print(vv);
You can use functions to define matrices.
> h:=(i,j)->1/(i+j-1); # this is a definition of hilbert matrix
> hilb:=matrix(5,6,h); # define a matrix 5x6 and populate elements with h as function of its indices.
> xmatrix:=matrix(3,5,0); # a zero matrix
> v; # note that if you just type the name of a vector or matrix it won't display it. You must force it to display (use eval).
> eval(v); eval(M);
A useful command for constructing matrices from lists is convert.
> xlist:=[a,b,c,d];
> xvec:=convert(xlist,array);
> op(4,xlist); xlist[4]; xvec[4];
Note the following!
> op(4,xvec);
Here is another way to solve systems of equations.
> e1:=x1+2*x2-x3=3;
> e2:=3*x1-x2+2*x3=1;
> e3:=2*x1-2*x2+3*x3=2;
> e4:=x1-x2+x3=-1;
> s:=[e1,e2,e3,e4]; # make a list of linear system of equations
The command genmatrix generates the coefficient matrix associated with the linear system of equations in the equation list, in terms of the variables in the variable list. (you need to load linalg package to use genmatrix)
> A:=genmatrix(s,[x1,x2,x3]);
> A:=genmatrix(s,[x1,x2,x3],flag);
> gaussjord(A);
You can also solve system of equations using the linsolve command.
> A:=genmatrix(s,[x1,x2,x3],a);
> eval(a);
> linsolve(A,[3,1,2,-1]);
or
> linsolve(A,a);
Exercises
1. Let U be the set of the first prime numbers. Let V be the set of the first twenty numbers of the form 2^n -1.
Generate the sets U and V. Find U intersection V and the union of U and V.
2. Generate a list of 100 integers between -10 and 10.
(a) Remove all duplicate entries from this list. (b) Select from the list obtained in (a) all numbers which can be divided by 2 or 3.
(c) Pick out from the list in (a) all numbers greater than 5.
3. Generate the 5 x 4 matrix with entries c(i,j)=i/j.
4. For the system
e1:=-x1+2*x2-3*x3=3;
e2:=2*x1-x2+2*x3=1;
e3:=2*x1-2*x2+3*x3=2;
generate the coefficient matrix. Use the gaussjord command to put the matrix in row echelon form. Solve the system.
<< Back - Next >>
|