Sequences, Arrays, Tables, Lists and Sets
This chapter covers such essential mathematical concepts as strings, arrays, and tables, which allow for fluid work in vector and matrix analysis, and also the treatment of all kinds of sums and products, both finite and infinite. MATLAB also implements sets and lists. For all of these concepts, we first present the definitions and then give a summary of the commands that can be used to implement them in MATLAB.
3.1 Sequences
A sequence is simply an ordered list of items separated by commas. As well as presenting a sequence explicitly, separating the elements with commas, one can also use the $ operator to create sequences. In particular, lists and sets are represented externally by sequences of elements between square brackets and braces respectively. The string of length zero is valid in MATLAB and is assigned the special name NULL. Among the commands that enable MATLAB to handle sequences (all of which must be preceded by the maple command) we have the following:
Here are some examples:
>> pretty (sym (maple('mul(k, k=1..5)')))
120
>> pretty(sym(maple('add( k^2, k=1..5 )')))
55
>> pretty(sym(maple('L:= [seq(k, k=1..5)] '))),pretty(sym(maple('L')))
[1 2 3 4 5]
>> pretty(sym(maple('add( k^2, k=L)')))
55
>> pretty(sym(maple('mul( x-k, k=L ) ')))
(x - 1) (x - 2) (x - 3) (x - 4) (x - 5)
>> pretty(sym(maple('add( a[k]*x^k, k=0..5 ) ')))
2 3 4 5
a[0] + a[1] x + a[2] x + a[3] x + a[4] x + a[5] x
>> pretty(sym(maple('add( k^2, k=0..n ) ')))
Error, unable to execute add
This example failed because n is a symbolic value. You will need to use the command sum.
>> pretty(sym(maple('sum( k^2, k=0..n ) ')))
3 2
1/3 (n + 1) - 1/2 (n + 1) + 1/6 n + 1/6
>> pretty(sym(maple('product( k^2, k=1..4 ) ')))
576
>> pretty(sym(maple('mul( k^2, k=1..4 ) ')))
576
>> pretty(sym(maple('product( k^2, k=1..n ) ')))
2
gamma(n + 1)
>> pretty(sym(maple('product( k^2, k ) ')))
2
gamma(k)
>> pretty(sym(maple('product( a[k], k=0..4 ) ')))
a[0] a[1] a[2] a[3] a[4]
>> pretty(sym(maple('seq( k^2, k=1..5 ) ')))
1, 4, 9, 16, 25
>> pretty(sym(maple('seq( sin(Pi*k/6), k=0..6 ) ')))
1/2 1/2
0, 1/2, 1/2 3 , 1, 1/2 3 , 1/2, 0
>> pretty(sym(maple('seq( x[k], k=1..5 ) ')))
x[1], x[2], x[3], x[4], x[5]
>> pretty(sym(maple('product( a[k], k=0..n ) ')))
n
--------
| |
| | a[k]
| |
| |
k = 0
>> pretty(sym(maple('sum(a[k]*x^k,k=0..n) ')))
n
-----
k
) a[k] x
/
-----
k = 0
>> pretty(sym(maple('sum(k/(k+1), k=0..n)')))
n + 1 - Psi(n + 2) - eulergamma
>> pretty(sym(maple('sum(k/(k+1), k)')))
k - Psi(k + 1)
>> pretty(sym(maple('$ 2..5')))
2, 3, 4, 5
>> pretty(sym(maple('k^2 $ k = 2/3 .. 8/3')))
4/9, 25/9, 64/9
>> pretty(sym(maple('a[k] $ k = 1..3')))
a[1], a[2], a[3]
>> pretty(sym(maple('x$4')))
x, x, x, x
>> pretty(sym(maple('u:= [1,4,9]:')));
>> pretty(sym(maple('nops(u)')))
3
>> pretty(sym(maple('op(2,u)')))
4
>> pretty(sym(maple('op(2..3,u)')))
4, 9
EXERCISE 3-1
Generate the list containing the sequence of integers from zero to six. Generate the list containing the sequence of its squares and the set containing the sequence of its squares modulo seven. Also generate the list whose elements are pairs of values where the first element of the pairs run through the integers from zero to six, and the second element is the square of the first.
>> maple('X:= [seq( k, k=0..6 )] ')
ans =
X: = [0, 1, 2, 3, 4, 5, 6]
>> maple('Y:= [seq(k^2, k=X)] ')
ans =
Y: = [0, 1, 4, 9, 16, 25, 36]
>> maple('{seq(k^2 mod 7, k=X)} ')
ans =
{0, 1, 2, 4}
>> maple ('[seq ([X [k], Y [k]], k = 1.. nops (X))] ')
ans =
[[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36]]
EXERCISE 3-2
Generate the sequence of 1st through 10th dervivatives of the function ln (x). Generate another sequence of 15 random numbers between 5 and 50.
>> pretty (sym (maple ('seq (diff (ln (x), x$ n), n = 1.. 10)')))
1 2 6 24 120 720 5040 40320 362880
1/x, - ----, ----, - ----, ----, - ---, ---, - ----, -----, - ------
2 3 4 5 6 7 8 9 10
x x x x x x x x x
>> pretty(sym(maple('f:=rand(5..50):seq(f( ),k=1..15)')))
30, 5, 42, 24, 17, 39, 48, 9, 28, 42, 18, 8, 50, 29, 7
EXERCISE 3-3
Calculate the following sums and product:
>> pretty(sym(maple('sum(n*E^(-n),n=1..infinity) ')))
E
---------
2
(-1 + E)
>> pretty(sym(maple('evalf(sum((1+i)^k/(Pi+k)^5,k=0..23)) ')))
.003599764471 +.001081437031 I
This value, with numerical summation limits, can also be found with the command add as follows:
>> pretty(sym(maple('evalf(add((1+I)^k/(Pi+k)^5,k=0..23))')))
.003599764471 + .001081437031 i
>> pretty(sym(maple('evalf(product(n^2*2^n/exp(n),n=1..10))')))
616565.6460
With numerical limits, this product can also be found using the command mul as follows:
>> pretty(sym(maple('evalf(mul(n^2*2^n/exp(n),n=1..10)) ')))
616565.6460
EXERCISE 3-4
Calculate the following sums: , and
>> pretty (sym (maple('sum(k^3, k=0..n) ')))
4 3 2
1/4 (n + 1) - 1/2 (n + 1) + 1/4 (n + 1)
>> pretty(sym(maple('sum( k^4, k=0..n ) ')))
5 4 3
1/5 (n + 1) - 1/2 (n + 1) + 1/3 (n + 1) - 1/30 n - 1/30
>> pretty(sym(maple('sum( k^5, k=0..n ) ')))
6 5 4 2
1/6 (n + 1)-1/2 (n + 1) + 5/12 (n + 1)-1/12 (n + 1)
3.2 Arrays
An array is a well-ordered collection of individual elements each of which is associated with a (possible multi-) index which indicates its position in the array. It is essential that each element is associated with a unique index, which may also be zero or negative, so that, in order to make changes to any element of the array, one simply refers to its index.
Arrays can be of one or more dimensions, so an array may have one or more sets of indices that identify its elements. The most important commands that enables MATLAB to work with arrays are as follows (all of them must be preceded by the maple command):
Here are some examples:
>> pretty (sym (maple ('array(1..3, 1..3, [[a,b,c],[d,e,f],[g,h,i]])')))
[a b c]
[ ]
[d e f]
[ ]
[g h i]
>> pretty(sym(maple('array(3..6,[a,b,c,d]) ')))
array(3 .. 6, [
3 = a
4 = b
5 = c
6 = d
])
>> pretty(sym(maple('array(1..2,1..2,identity) ')))
[1 0]
[0 1]
>> pretty(sym(maple('array(1..2, 1..6, [(1,3)=p, (2,4)=r], sparse) ')))
[0 0 p 0 0 0]
[ ]
[0 0 0 r 0 0]
>> pretty(sym(maple('M:=array(1..2, 1..3,[[2,8,32], [45,-1,0]]) ')));
>> pretty(sym(maple('M')))
[ 2 8 32]
[ ]
[45 -1 0]
>> pretty(sym(maple('M[1,3] ')))
32
>> pretty(sym(maple('M[2,3]:=x-3 ')));
>> pretty(sym(maple('op(M) ')))
[ 2 8 32 ]
[ ]
[45 -1 x - 3]
>> pretty(sym(maple(' v:= array(1..4): ')));
>> pretty(sym(maple('for k to 3 do v[k]:= k^2 od: ')))
>> pretty(sym(maple('print(v) ')))
[1, 4, 9, v[4]]
>> pretty(sym(maple('v[2] ')))
4
>> pretty(sym(maple('v[0] ')))
Error, 1st index, 0, smaller than lower array bound 1
>> pretty(sym(maple('A:= array(1..2,1..2): ')));
>> pretty(sym(maple('A[1,2]:= x ')));
>> pretty(sym(maple('print(A) ')))
[A[1, 1] x ]
[ ]
[A[2, 1] A[2, 2]]
>> pretty(sym(maple('A:= array( symmetric, 1..2,1..2, [ [1,x], [x,x^2] ] ) ')))
>> pretty(sym(maple('A')))
[1 x ]
[ ]
[ 2]
[x x ]
>> pretty(sym(maple('op(A)')))
[1 x ]
[ ]
[ 2]
[x x ]
>> pretty(sym(maple('op(1,eval(A)) ')))
symmetric
>> pretty(sym(maple('op(2,eval(A)) ')))
1 .. 2, 1 .. 2
>> pretty(sym(maple('op(3,eval(A)) ')))
2
[(1, 2) = x, (1, 1) = 1, (2, 2) = x ]
>> pretty(sym(maple('map(diff,A,x) ')))
[0 1 ]
[ ]
[1 2 x]
EXERCISE 3-5
Generate a one-dimensional array with three elements whose indices range from 1 to 3 and fill the array with the squares of the first three natural numbers. Also generate a two-dimensional array whose two indices vary from 1 to 3 and fill the array with ones in the first row, the squares of the first three positive integers in the second row and the cubes of the first three positive integers in the third row. Finally, replace all ones in the two-dimensional array by fives and check the result. Convert the first array to a set and the second to a list.
>> pretty (sym (maple (' squ: = array(1..3) ')));
>> maple('squ[1]:=1^2;squ[2]:=2^2;squ[3]:=3^2 '),
>> pretty(sym(maple('print(squ) ')))
[1, 4, 9]
>> pretty(sym(maple('bidimen:=array(1..3,1..3) ')));
>> maple('bidimen[1,1]:=1;bidimen[1,2]:=1;bidimen[1,3]:=1;
bidimen[2,1]:=1^2;bidimen[2,2]:=2^2;bidimen[2,3]:=3^2;
bidimen[3,1]:=1^3;bidimen[3,2]:=2^3;bidimen[3,3]:=3^3 '),
>> pretty(sym(maple('print(bidimen)')))
[1 1 1]
[ ]
[1 4 9]
[ ]
[1 8 27]
>> pretty(sym(maple('subs({1=5}, evalm(bidimen)) ')))
[5 5 5]
[ ]
[5 4 9]
[ ]
[5 8 27]
A more elegant way to define the two arrays of this problem is as follows:
>> pretty(sym(maple('squ:=array(1..3,[seq(k^2,k=1..3)]) ')))
[1 4 9]
>> pretty(sym(maple('bidimen:=array(1..3,1..3,[[seq(k^0,k=1..3)],
[seq(k^2,k=1..3)],[seq(k^3,k=1..3)]]) ')))
>> pretty(sym(maple('bidimen')))
[1 1 1]
[ ]
[1 4 9]
[ ]
[1 8 27]
>> pretty(sym(maple('convert(squ,set) ')))
{1, 4, 9}
>> pretty(sym(maple('convert(bidimen,listlist) ')))
[[1, 1, 1], [1, 4, 9], [1, 9 , 27]]
EXERCISE 3-6
Generate a three-dimensional array containing the first eight positive natural numbers and whose three indices vary from 1 to 2. Access its operands and display its internal structure.
>> pretty(sym(maple('tridimen:=array(1..2,1..2,1..2,[[[1,2],[3,4]],[[5,6],[7,8]]])')))
>> pretty(sym(maple('tridimen')))
array(1 .. 2, 1 .. 2, 1 .. 2, [
(1, 1, 1) = 1
(1, 1, 2) = 2
(1, 2, 1) = 3
(1, 2, 2) = 4
(2, 1, 1) = 5
(2, 1, 2) = 6
(2, 2, 1) = 7
(2, 2, 2) = 8
])
>> pretty(sym(maple('op(eval(tridimen)) ')))
[1 .. 2, 1 .. 2, 1 .. 2], [(1, 1, 1) = 1, (1, 1, 2)= 2, (1, 2, 1)= 3,
(1, 2, 2)=4, (2, 1, 1)=5, (2, 1, 2) = 6, (2, 2, 1) = 7, (2, 2, 2) = 8]
>> pretty(sym(maple('op(1,eval(tridimen)) ')))
[1 .. 2 1 .. 2 1 .. 2]
>> pretty(sym(maple('op(2,eval(tridimen)) ')))
[(1, 1, 1) = 1 (1, 1, 2) = 2, (1, 2, 1) = 3, (1, 2, 2) = 4,]
(2, 1, 1) = 5, (2, 1, 2) = 6, (2, 2, 1) = 7 (2, 2, 2) = 8]
3.3 Relationships Between Vectors, Matrices and Arrays
A vector is equivalent to a one-dimensional array whose index begins with 1. Therefore, a vector is a special case of an array. As we know, MATLAB defines a vector with m row elements in the following ways:
Here are some examples:
>> pretty(sym(maple('vector(4,[1,x,x^2,x^3]) ')))
2 3
[1, x, x , x ]
>> pretty(sym(maple('vector([1,x,x^2,x^3]) ')))
2 3
[1, x, x , x ]
>> pretty(sym(maple('array([1,x,x^2,x^3]) ')))
2 3
[1, x, x , x ]
>> pretty(sym(maple('vector(3,k->k^2)')))
[1 4 9]
A matrix is equivalent to a two-dimensional array whose indices both begin at 1. Therefore, a matrix is also a special case of a two-dimensional array. As we know, MATLAB allows you to define an array of dimension (m×n) in the following ways:
Here are some examples:
>> pretty (sym (maple ('matrix ([[sin(x), x^2 + x + 3], [exp (x), cos(x^2)]])')))
[ 2 ]
[sin (x) x + x + 3 ]
[ ]
[ 2 ]
[exp (x) cos (x) ]
>> pretty(sym(maple('array([[sin(x), x^2+x+3], [exp(x), cos(x^2)]])')))
[ 2 ]
[sin (x) x + x + 3]
[ ]
[ 2 ]
[exp (x) cos (x) ]
>> pretty (sym (maple ('matrix (2,2, [sin (x), x ^ 2 + x + 3, exp (x), cos(x^2)])')))
[ 2 ]
[sin (x) x + x + 3 ]
[ ]
[ 2 ]
[exp(x) cos(x ) ]
>> pretty(sym(maple('matrix(3,4,(p,q)->a^p+b^q)')))
[ 2 3 4 ]
[a + b a + b a + b a + b ]
[ ]
[ 2 2 2 2 3 2 4]
[a + b a + b a + b a + b ]
[ ]
[ 3 3 2 3 3 3 4]
[a + b a + b a + b a + b ]
On the other hand, we know that by using the command convert we can transform any one-dimensional array to a vector and any two-dimensional array to a matrix.
3.3.1 Tables
The concept of a table is an extension of the concept of an array. The difference between an array and a table is that the indices of a table need not vary over integers. For example, you can build a table with algebraic formulas as indices and their derivatives as values in the table. Therefore, the table structure is much more powerful that the array structure. Let’s look at the most common MATLAB commands that enable you to work with tables (all of them must be preceded by the maple command):
Here are some examples:
>> pretty (sym (maple ('table ()')))
table([
])
>> pretty(sym(maple('table([22,42]) ')))
table([
1 = 22
2 = 42
])
>> pretty(sym(maple('S:= table([(2)=45,(4)=61]) ')))
>> pretty(sym(maple('S')))
S:= table([
2 = 45
4 = 61
])
>> pretty(sym(maple('T:= table(symmetric,[(c,b)=x]) ')))
>> pretty(sym(maple('T')))
T:= table(symmetric, [
(c, b) = x
])
>> pretty(sym(maple('op(T) ')))
table(symmetric, [
(c, b) = x
])
>> pretty(sym(maple('F:= table([sin=cos,cos=-sin]): ')))
>> pretty(sym(maple('op(F) ')))
table([)
cos = - sin
sin = cos
])
>> pretty(sym(maple('op(2,eval(F)) ')))
[cos = - sin, sin = cos]
>> pretty (sym (maple ('op (op (F))')))
[cos = - sin, sin = cos]
>> pretty (sym (maple ('F [cos](Pi/2) ')))
-1
>> pretty(sym(maple('convert(F,set) ')))
{cos, -sin}
>> pretty(sym(maple('convert(F,multiset) ')))
[[cos,-sin], [sin, cos]]
3.5 Lists
As we know, a list is a collection of objects separated by commas and enclosed within square brackets. It is a sequence enclosed within square brackets. The order of the elements in a list is important, and elements can be repeated, i.e. the lists [a, b, c] and [b, c, a] are different, and the lists [a, a, b, c, c] and [a, b, c] are also different. The following commands can be used to create and manipulate lists (all must be preceded by the command maple):
Here are some examples:
>> pretty (sym (maple ('L: = [seq(x[k],k=1..4)] ')))
>> pretty(sym(maple('L')))
[x[1], x[2], x[3], x[4]]
>> pretty(sym(maple('L[2]')))
x[2]
>> pretty(sym(maple(' L:= [op(L),x[5]] ')))
>> pretty(sym(maple(' L')))
[x[1], x[2], x[3], x[4], x[5]]
>> pretty(sym(maple('L, L[-3..-2]) ')))
[x[3], x[4]]
>> pretty (sym (maple ('member(x*y, [x*y, w+u, y])')))
true
>> pretty(sym(maple('member(w, [x, y, w, u], k) ')))
true
>> pretty(sym(maple('convert( [1,2,3,4], `+` ) ')))
10
>> pretty(sym(maple('u:= [1,4,9] ')))
>> pretty(sym(maple('u')))
[1 4 9]
>> pretty(sym(maple('nops(u) ')))
3
>> pretty(sym(maple('op(2,u) ')))
4
>> pretty(sym(maple('op(2..3,u) ')))
4, 9
>> pretty(sym(maple('op(u) ')))
1, 4, 9
>> pretty(sym(maple('op(-1,u) ')))
9
>> pretty(sym(maple('op(0,u) ')))
list
>> pretty(sym(maple('[op(u),16] ')))
[1, 4, 9, 16]
>> pretty(sym(maple('X:= [seq( k, k=0..6 )] ')))
>> pretty(sym(maple('Y:= [seq( k^2, k=X )] ')))
>> pretty(sym(maple('X,Y')))
[0 1 2 3 4 5 6]
[0 1 4 9 16 25 36]
>> pretty(sym(maple('[seq( [X[k],Y[k]], k=1..nops(X) )] ')))
[[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36]]
3.5.1 Selecting and Manipulating Elements From Lists
Among the multiple possibilities for structural handling of lists offered by MATLAB, the following group of commands allows the selection and manipulation of elements from lists:
Here are some examples:
>> pretty (sym (maple ('L: = [8, 2.95, Pi, sin(9)] ')))
>> pretty(sym(maple('L')))
[8 2.95 pi sin(9)]
>> pretty(sym(maple('select(type, L, numeric) ')))
[8, 2.95]
>> pretty(sym(maple('f:=x->is(x>3) ')))
>> pretty(sym(maple('select(f,L) ')))
[8, pi]
>> pretty(sym(maple('remove(f,L) ')))
[2.95, sin(9)]
>> pretty (sym (maple ('zip ((x,y) - > x + y, [1,2,3], [4,5,6]) ')))
[5, 7, 9]
>> pretty(sym(maple('zip(gcd,[0,14,8],[2,6,12]) ')))
[2, 2, 4]
>> pretty(sym(maple('zip((x,y)->x+y,[1,2,3],[4,5],0) ')))
[5, 7, 3]
>> pretty(sym(maple('zip((x,y)->x^2+y^2,[a,b,c,d,e],[1,2,3],k) ')))
2 2 2 2 2 2 2
[a + 1, b + 4, c + 9 d + k, e + k]
EXERCISE 3-7
Generate the list containing the integers between 10 and 30, selecting from it the prime numbers. Obtain a new list by selecting only the even numbers.
>> pretty(sym(maple('integers:= [$10..30] ')))
>> pretty(sym(maple('integers')))
[10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24
, 25 , 26 , 27 , 28 , 29 , 30]
>> pretty(sym(maple('select(isprime,integers) ')))
[11, 13, 17, 19, 23, 29]
>> pretty(sym(maple('select(type,integers, even) ')))
[10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
EXERCISE 3-8
Let X be the list of the first six primes and let Y be the list of binomial coefficients, 6 choose k, for k from 1 to 6. Construct a list P consisting of ordered pairs such that the first element of P[i] is X[i] and the second element of P[i] is Y[i].
>> pretty(sym(maple('X:=[seq(ithprime(k), k=1..6)] ')))
>> pretty(sym(maple('X')))
[2 3 5 7 11 13]
>> pretty(sym(maple('Y:=[seq(binomial(6,k), k=1..6)] ')))
>> pretty(sym(maple('Y')))
[6 15 20 15 6 1]
>> pretty(sym(maple('pairs:=(x,y)->[x,y] ')))
>> pretty(sym(maple('P:=zip(pairs,X,Y) ')))
>> pretty (sym (maple ('P')))
[[2, 6], [3, 15], [5, 20], [7, 15], [11, 6], [13, 1]]
3.5.2 Ordering and Applying Functions to Lists
MATLAB includes a group of commands whose purpose is the management of elements of lists. One can also apply functions to items in lists. Commands to this effect are summarized below (all require the prior use of the maple command):
Here are some examples:
>> pretty (sym (maple ('sort ([3,2,1])')))
[1, 2, 3]
>> pretty(sym(maple('sort([c,a,d],lexorder)')))
[a, c, d]
>> pretty(sym(maple('sort([3.12, 1, 1/2], (x,y)->evalb(x>y))')))
[3.12, 1, 1/2]
>> pretty(sym(maple('sort([3.12, 1, 1/2], (x,y)->is(x<y))')))
[1/2, 1, 3.12]
>> pretty(sym(maple('sort([Jonas,Jeremias,Jose,Roberto,Andres],
(x,y)->evalb(length(x)<length(y))) ')))
[Jose Jonas Andres Roberto Jeremias]
>> pretty(sym(maple('map(f, [a,b,c])')))
[f(a), f(b), f(c)]
>> pretty(sym(maple('map2(f, g, [a,b,c])')))
[f(g, a), f(g, b), f(g, c)]
>> pretty(sym(maple('map2(op, 1, [a+b,c+d,e+f])')))
[a, c, e]
>> pretty (sym (maple ('map2(diff,x^y/z,[x,y,z])')))
y y y
x y x ln (x) x
[----, --------, - ----]
x z z 2
z
>> pretty(sym(maple('map(x->x^2,[-1,-2,-3,3,2,1])')))
[1, 4, 9, 9, 4, 1]
3.5.3 Performing Operations With Elements of Lists
There are MATLAB commands that allow you to perform sums and products on elements of lists. Among them are the following (all require the prior use of the command maple):
Here are some examples:
>> pretty (sym (maple ('L: = [seq(k, k=1..5)] ')))
>> pretty(sym(maple('L')))
[1 2 3 4 5]
>> pretty(sym(maple('add( k^2, k=L ) ')))
55
>> pretty(sym(maple('mul( x-k, k=L ) ')))
(x - 1) (x - 2) (x - 3) (x - 4) (x - 5)
>> pretty(sym(maple('A:=x^3+3*x^2+3+x+1 ')))
>> pretty(sym(maple('seq(degree(k,x),k=A) ')))
3, 2, 0, 1
EXERCISE 3-9
Construct Pascal’s triangle of binomial coefficients up to its tenth row.
>> pretty(sym(maple('L:=[seq(k, k=0..9)] ')))
>> pretty(sym(maple('L')))
[0 1 2 3 4 5 6 7 8 9]
>> pretty(sym(maple('[seq(select(type,[seq(binomial(n,m),m=L)],posint),n=L)] ')))
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1],
[1, 5, 10, 10, 5, 1], [1, 6, 15, 20, 15, 6, 1],
[1, 7, 21, 35, 35, 21, 7, 1], [1, 8, 28, 56, 70, 56, 28, 8, 1],
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]]
>> pretty(sym(maple('map(print,") ')))
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
3.5.4 Sets
A set is a collection of objects separated by commas and enclosed in braces. It is a sequence enclosed between braces. The order of the elements in a set is not important and repeating elements will not change the identity of a set, i.e., the sets {a, b, c} and {b, c, a} are equal, and the sets {a, a, b, c, c} and {a, b, c} are also equal. Among the commands relating to sets we have the following (all must be preceded by the command maple):
Here are some examples:
>> pretty(sym (maple(' {x,y,y} ')))
{x, y}
>> pretty(sym(maple(' {y,x,y} ')))
{x, y}
>> pretty(sym (maple('[x,y,y] ')))
[x, y, y]
>> pretty(sym (maple('[y,x,y] ')))
[y, x, y]
>> pretty(sym(maple(' S:= {a,b,c} '))),pretty(sym(maple('S')))
{a, b, c}
>> pretty(sym(maple(' S[1] ')))
a
>> pretty(sym(maple(' S[1..2] ')))
{a, b}
>> pretty(sym(maple(' S[-2..-1] ')))
{b, c}
>> pretty(sym(maple(' u:= {1,4,9} '))),pretty(sym(maple(' u')))
{1, 4, 9}
>> pretty(sym(maple(' nops(u) ')))
3
>> pretty(sym(maple(' op(2,u) ')))
4
>> pretty(sym(maple(' op(2..3,u) ')))
4, 9
>> pretty(sym(maple(' op(u) ')))
1, 4, 9
>> pretty(sym(maple(' op(-1,u) ')))
9
>> pretty(sym(maple(' op(0,u) ')))
set
>> pretty(sym(maple('[op(u),16] ')))
[1, 4, 9, 16]
>> pretty(sym(maple(' member(y, {x, y, z}) ')))
true
>> pretty(sym(maple(' member(y, {x*y, y*z}) ')))
false
>> pretty(sym(maple(' {a,b} union {b,c} ')))
{c, b, a}
>> pretty(sym(maple(' {a,b} intersect {b,c} ')))
{b}
>> pretty(sym(maple(' {a,b} minus {b,c} ')))
{a}
>> pretty(sym(maple(' a union b union a ')))
a union b
>> pretty(sym(maple(' {3,4} union a union {3,7} ')))
a union {3, 4, 7}
>> pretty(sym(maple(' 'union' ({3,4},a,{3,7}) ')))
a union {3, 4, 7}
>> pretty(sym(maple(' integers:= {$10..20} '))),pretty(sym(maple(' integers')))
{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
>> pretty(sym(maple(' select(isprime, integers) ')))
{11, 13, 17, 19}
>> pretty(sym(maple(' remove(isprime, integers) ')))
{10, 12, 14, 15, 16, 18, 20}
>> pretty(sym(maple(' map(f, {a,b,c}) ')))
{f(a), f(b), f(c)}
>> pretty(sym(maple(' map2(f, g, {a,b,c}) ')))
{f(g, a), f(g, b), f(g, c)}
>> pretty(sym(maple(' map(proc(x,y) x^2+y end, {1,2,3,4}, 2) ')))
{3, 6, 11, 18}
>> pretty(sym(maple(' map2(op, 1, {a+b,c+d,e+f}) ')))
{a, e, c}
EXERCISE 3-10
Z1 is the set of integers from 2 to 15. Z2 is the set of primes contained in Z1. Z3 is the set of odd integers contained in Z2 and Z4 is the set of even integers contained in Z2. Calculate the sets: A = Z2 ∩ Z3, B = Z2 ∩ Z4, C = A ∪ B, E = (A ∪ B)-C, F = A Δ B, G = A Δ B Δ C, H = Z2 Δ K and Z3 = Z1 Δ Z2 Δ Z3.
>> pretty(sym(maple('Z1:={seq(k,k=2..15)} '))),pretty(sym(maple('Z1')))
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
>> pretty(sym(maple('Z2:=select(isprime,Z1) '))),pretty(sym(maple('Z2')))
{2, 3, 5, 7, 11, 13}
>> pretty(sym(maple('Z3:=select(type,Z2,odd) '))), pretty(sym(maple('Z3')))
{3, 5, 7, 11, 13}
>> pretty(sym(maple('Z4:=select(type,Z2,even)'))),pretty(sym(maple('Z4')))
{2}
>> pretty(sym(maple(' A:= Z2 intersect Z3 '))),pretty(sym(maple(' A')))
{3, 5, 7, 11, 13}
>> pretty(sym(maple('B:=Z2 intersect Z4 '))),pretty(sym(maple('B')))
{2}
>> pretty(sym(maple('C:=A union B '))),pretty(sym(maple('C')))
{2, 3, 5, 7, 11, 13}
>> pretty(sym(maple('E:=(A union B) minus C '))),pretty(sym(maple('E')))
[ ]
>> pretty(sym(maple( 'readlib(symmdiff): F:=symmdiff(A,B) '))),pretty(sym(maple( 'F')))
{2, 3, 5, 7, 11, 13}
>> pretty(sym(maple('G:=symmdiff(A,B,C) '))),pretty(sym(maple('G')))
[ ]
>> pretty(sym(maple('H:=symmdiff(Z2,Z3) '))),pretty(sym(maple('H')))
{2}
>> pretty(sym(maple('K:=symmdiff(Z1,Z2,Z3) '))),pretty(sym(maple('K')))
{3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
EXERCISE 3-11
Define an average function means(C) which returns the geometric mean, the quadratic mean and the harmonic mean of a set of elements C. As an application, find the geometric, harmonic and quadratic means of the sets {1,5,6,7,9} and {a, b, c, d, e, f, g, h}.
To define means(C), we create the following procedure:
>> maple('means:=proc(C::set) local g,c,h,i,j,k; g:=(mul(i,i=C)^(1/nops(C))); c:=sqrt(add(j^2,j=C)/nops(C));h:=nops(C)/add(1/k,k=C); 'geomean='.g, 'quadmean=' .c, 'harmean=' .h;end;')')))
Now we can calculate the quadratic, geometric and harmonic means of the proposed sets.
>> pretty(sym(maple('means({1,5,6,7,9}) ')))
1/5 1/2 3150
geomean =.(1890), quadmean =.(8/5 15), harmean =. ----
1021
>> pretty (sym (maple ('means({a,b,c,d,e,f,g,h})')))
1/8
geomean =.((g c h b e f a d)), quadmean =.
2 2 2 2 2 2 2 2 1/2
(1/4 (2 g + 2 c + 2 h + 2 b + 2 e + 2 f + 2 a + 2 d) ),
8
harmean =. ---------------------------------------------
1/g + 1/c + 1/h + 1/b + 1/e + 1/f + 1/a + 1/d