CHAPTER 3

image

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:

  • seq (f (i), i = a...b) creates the sequence f (a), f (a+1),..., f(b-1), f (b). The numbers a and b must be integers.
  • seq(expr,var=a..b) creates the sequence of expressions resulting from the substitution of the variable var into the expression expr where var ranges over the values a, a+1,..., b-1, b. The numbers a and b must be integers.
  • seq (f(i), i = expr) creates the sequence corresponding to the values of f applied to the expression expr.
  • seq (f(i), i = list) creates the sequence corresponding to the values obtained by applying the function f to the elements of the specified list.
  • seq (f(i), i = set) creates the sequence corresponding to the values obtained by applying the function f to elements of the specified set.
  • f (i) $ i = a...b creates the sequence f (a), f (a+1),..., f(b-1), f (b). The numbers a and b need not be integers.
  • $ a..b creates the sequence, a+1, a+2,..., b-1, b. The numbers a and b need not be integers.
  • f $ n creates the sequence f, f,...............f (with n occurrences of f).
  • s [i] returns the ith element of the sequence s.
  • nops ([s]) gives the length of the string s (number of elements).
  • op (list) returns the sequence formed by the elements of the specified list.
  • op (set) returns the sequence formed by the elements of the specified set.
  • [s] creates the list whose elements are those of the sequence s.
  • {s} creates the set whose elements are those of the sequence s.
  • s,s creates a sequence formed by the repetition of the sequence s.
  • s, s,..., s (n occurrences of s) creates a sequence formed by the repetition of the sequence s n times.
  • sequence: = NULL defines the empty sequence.
  • add(expr, var=a..b) sums the sequence of expressions obtained by substituting the values a, a+1,..., b-1, b of the variable var into the expression expr.
  • add (f (i), i = a...b) sums the sequence f (a), f (a+1),..., f (b-1), f (b). The values a and b must be numeric.
  • add (f (i), i = expr) adds the sequence of numerical values obtained by applying f to the expression expr.
  • add (f (i), i = list) adds the sequence of values obtained when applying the function f to the elements from the specified list.
  • add (f (i), i = set) adds the sequence of values obtained by applying the function f to the elements of the given set.
  • mul(expr, var=a..b) finds the product of the sequence of expressions obtained by substituting the values a, a+1,..., b-1, b of the variable var into the expression expr.
  • mul(f (i), i = a...b) finds the product of the sequence f (a), f (a+1),..., f (b–1), f (b). The values a and b must be numeric.
  • mul(f (i), i = expr) finds the product of the sequence of numerical values obtained by applying f to the expression expr.
  • mul(f (i), i = list) finds the product of the sequence of values obtained by applying the function f to the elements of the given list.
  • mul(f (i), i = set) finds the product of the sequence of values obtained by applying the function f to the elements of the given set.
  • sum(expr, var=a..b) finds the sum of the sequence of expressions obtained by substituting the values a, a+1,..., b–1, b of the variable var into the expression expr. The values a and b can be symbolic.
  • sum (f (i), i = a...b) finds the sum of the sequence f (a), f (a+1),...,f (b–1), f (b). That is, it returns the sumimage. The values a and b can be symbolic.
  • product(expr, var=a..b) finds the product of the sequence of expressions obtained by substituting the values a, a+1,..., b–1, b of the variable var into the expression expr.
  • product (f (i), i = a...b) finds the product of the sequence f (a), f (a+1),...,f (b–1), f (b). That is, it returns the productimage. The values a and b can be symbolic.
  • op(a..b, expr) or seq(op(i,expr), i=a..b) extracts the sequence comprising the a-th through b-th operands of the expression expr.

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:

image

>> 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: image, imageand image

>> 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):

  • array(1..n, element_list) creates the array with n elements specified in the list and whose indices vary from 1 to n.
  • array (n..m, item_list) creates the array with the  m-n + 1 elements specified in the list and whose indices vary from n to m.
  • array(1..n) creates an array with n unspecified elements whose indices vary from 1 to n.
  • array (n..m) creates the array with m-n + 1 unspecified elements whose indices vary from n to m.
  • array (item_list) creates an array whose elements are taken from the list (of length n) and whose indices vary from 1 to n.
  • array(1..m, 1..n, [list1,...,listn]) creates a two-dimensional array of n rows and m columns, where the rows of elements are the given lists. The indices are the pairs (i, j) with i = 1... n, j = 1... m.
  • array(1..m, 1..n, 1..p, [[list11,...,list1n],...[listm1,...,listmn]],...,
  • [[listp1,...,listpn],...,[list(p+m)1,...,list(p+m)n]] creates a three-dimensional array whose elements are given by the specified lists. The indices are the triples (i, j, k) with i = 1... n, j = 1.. m, k = 1.. p.
  • array(1..n, option) or array(1..n,1..m, option) creates a one-dimensional or two-dimensional array of the type specified in option. The option values are symmetric, antisymmetric, diagonal, identity or sparse.
  • A [i] returns the element with index i in the one-dimensional array A.
  • A [i, j] returns the element with index (i, j) in the two-dimensional array A.
  • A [i]: = element assigns the element to the array element with index i.
  • A [i, j]: = element assigns the element to the array element with index (i, j).
  • eval (array) or evalm (array) shows the elements of an array in certain non-trivial cases.
  • op (eval (array)) shows the internal structure of the array and allows access to its elements.
  • op (n, eval (array)) extracts the nth operand of the array. The first operand is the index function, the second operand is a sequence of ranges or indices and the third operand is the table of data representing the elements of the array.
  • nops(array) or nops(eval(array)) gives the number of elements in the array.
  • indices(A) shows the indices of the array A.
  • entries(A) shows the elements of the array A.
  • copy(A) creates a copy of the array A.
  • map(function, array) applies the given command or function to all the elements of the array.
  • subs ({old = new}, eval (array)) replaces the old element with the new throughout the array.
  • subs ({old1 = new1, old2 = new2,..., oldn = newn}, eval (array)) replaces the old elements with the new throughout the array.
  • convert(A,list) creates the one-dimensional array corresponding to the given list with the same elements.
  • convert (A, listlist) converts the two-dimensional array A to a list of lists.
  • convert (A, vector) converts the one-dimensional array A to a vector with the same elements.
  • convert(A,matrix) converts the two dimensional array to a matrix.
  • convert (A set) converts the one-dimensional array A to a set with the same elements.
  • type(expression, array) determines if the expression is an array.
  • type(expression, ‘array’ (type)) determines whether the expression is an array with all the elements of the given type (integer, complex,...).
  • typematch (expression, array) returns true if the given expression is of type array.
  • typematch(expr, array, name) in addition, if the match is successful, assigns to name a list of equations representing the variables and their matched values.

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:

  • maple(‘array([v1, ..., vm])’)
  • maple (‘vector(m, item_list)’)
  • maple (‘vector([v1,..., vm])’)
  • maple (‘vector (m, k - > f (k))’)

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:

  • maple (‘matrix([lista1,..., listam])’) (each list has n elements and is a row of the matrix)
  • maple (‘array([lista1,..., listam])’) (each list has n elements and is a row of the matrix)
  • maple (‘matrix (m, n, item_list)’)
  • maple(‘matrix(m, n, (i,j)->f(i,j))’)

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):

  • table ([(index1) = element1,..., (indexn) = elementn]) creates a table with the specified indices and elements (inputs).
  • table([element1,...,elementn]) creates a table with the specified elements which have indices 1, 2,..., n.
  • table (option, [(index1) = element1,..., (indexn) = elementn]) creates a table of the type specified by the option (symmetric, antisymmetric, diagonal, identity or sparse) with the given indices and elements.
  • T [i] returns the element of index i of the table T.
  • T [i]: = element assigns the element to the entry of T with index i.
  • eval (table) or evalm (table) shows the elements of a table in certain non-trivial cases.
  • op (eval (table)) shows the internal structure of the table and allows access to its elements.
  • op (n, eval (table)) extracts the nth operand of the table. The first operand is the index function and the second operand is a list of equations whose left-hand sides are the indices and whose right-hand sides are the elements of the table.
  • nops (T) or nops (eval (T)) gives the number of elements in the table T.
  • indexes(T) shows the indices of the table T.
  • entries (T) shows the elements of the table T.
  • copy (T) creates a copy of the table T.
  • map (function, table) applies the given command or function given to all the elements of the array.
  • subs ({old = new}, eval (table)) replaces the old element with the new throughout the table.
  • subs ({old1 = new1, old2 = new2,..., oldn = newn}, eval (table)) replaces the old items for new ones throughout the table.
  • convert(T,list, ‘=’) converts the table T to a list of equations of the form index T = element.
  • convert(T,multiset) converts the table T to a list of lists such that the first elements are the indices of the table T and the second elements are their corresponding values.
  • convert(T,set) converts the table T to a set containing the elements of the table, ignoring the indices.
  • type(expression, table) determines if the expression is a table.
  • type (expression, ‘table’ (type)) determines whether the expression is a table with elements of the given type (integer, complex...).
  • typematch (expression, table) returns true if the given expression is of type table.
  • typematch (expr, array, name) in addition, if the match is successful, assigns to name a list of equations representing the variables and their matched values.

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):

  • [a1, a2,..., an]creates a list of the given elements.
  • [sequence] create a list whose elements are defined in the specified sequence.
  • list: = [] defines the empty list.
  • L [i] returns the ith element of the list L.
  • L[-i] returns the ith element from the end of the list L.
  • L [a...b] returns the elements of the list that occupy the places a, a+1,..., b-1, b.
  • L [-a...-b] returns the elements of the list that occupy the places a, a+1,..., b-1, b starting from the end of the list.
  • [list, list] create a list of two lists.
  • [list,..., list] creates a list of n lists.
  • member (expression, list) determines whether the expression belongs to the list.
  • member(expr, list, name) assigns the specified name to the first item in the list that matches expr.
  • nops (L) gives the number of elements in the list L.
  • op (L) transforms the list L into a sequence.
  • op (n, L) gives the nth element in the list L.
  • op(a..b, L) gives the a-th through b-th elements of the list L, inclusive.
  • convert (A, list) converts the one-dimensional array A into a list.
  • convert(V,list) converts the vector V into a list.
  • convert(M, list, list) converts the matrix M into a list of lists.
  • convert(expression, list) converts the given expression into a list whose first operands are its elements.
  • convert(T, list, ‘=’) converts the table T into a list of equations of the form index = element.
  • convert({expr1,..., exprn}, list) converts the set of expressions into a list.
  • convert (A, listlist) converts the two-dimensional array A into a list of lists.
  • convert([index1=element1,..., indexn=elementn], listlist) converts the given list of equations into a list that contains only items properly sorted by index, but not the indices themselves.
  • convert(L, set) converts the list L into a set with the same elements, ignoring indices.
  • convert(T, multiset) converts the table T into a list of lists such that the first elements are the indices of the table T and the second elements are their corresponding values.
  • convert([expr1,..., exprn], option) creates a list with the given expressions converted according to the specified option (trig, exp, ln,...).
  • convert(L,vector) converts the list L to a vector.
  • convert([list1,..., listn], matrix) converts the list of lists to a matrix whose rows are the given lists.
  • type (expression, list) determines whether the expression is a list.
  • type(expresion, listlist) determines whether the expression is a list of lists.
  • type(expression, ‘list’ (type)) determines whether the expression is a list with the elements of the given type (integer, complex...).
  • typematch (expression, list) returns true if the given expression is of type list.
  • typematch (expr, list, name) in addition, if the match is successful, assigns to name a list of equations representing the variables and their matched values.

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:

  • select(boolean_function, list) creates a list by selecting from the specified list the items that match the command or given Boolean function.
  • select(boolean_function, list, expr1,..., exprn) applies the given Boolean function with parameters expr1,..., exprn to the elements of the given list to determine which ones are selected.
  • remove(boolean_function, list) creates a list by removing from the specified list the items that match the given command or Boolean function.
  • remove(boolean_function, list, expr1,..., exprn) applies the given Boolean function with parameters expr1,..., exprn to the elements of the given list to determine which ones are removed.
  • zip(function, L1, L2) uses lists or vectors of L1 and L2 to generate a new list whose elements are the result of applying the specified binary function to each pair of corresponding elements of the lists. If the two lists do not have the same length, the process only applies up to the length of the shortest list.
  • zip(function, L1, L2, expr) as above except the length of the generated list is equal to the length of the longest of the two lists L1 and L2 and any empty elements at the tail of the list are filled with the value given by expr.

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):

  • sort (list) or sort (list, lexorder) or sort (list, string): sorts the elements of the given list using lexicographical order.
  • sort (list, numeric) or sort(list, ‘<’) or sort([num1,..., numn]): sorts the elements of the numerical list in numerical order from least to greatest.
  • sort (list, boolean_function): sorts the elements of the list according to the values of the specified Boolean function applied to pairs in the list.
  • sort (list, address): sorts the elements of the given list by internal address.
  • map (function, list): applies the specified command or function to each item in the list.
  • map (function, list, expr1,..., exprn): applies the specified command or function (with the parameters expr1,..., exprn) to each element from the given list.
  • map2 (function, expression, list): applies the specified command or function to each element of the list, so that the function takes as its first argument a constant given by the expression and as second argument each item in the list.
  • map2 (function, expression, list, expr3, expr4,..., exprn): applies the command or function to each element of the list in such a way that the function has as its first argument a constant given by the expression, as the second argument each element of the list, and as constant arguments from the third to the nth arguments the expressions expr3, expr4,..., exprn.

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):

  • add(expression, variable = list) or add (f (i), i = list) gives the sequence of elements obtained by adding the given expression or function to each element of the list.
  • mul(expression, variable = list) or add (f (i), i = list) gives the sequence of elements obtained by multiplying each element of the list by the given expression or function.
  • seq(expression, variable = list) or add (f (i), i = list) gives the sequence of elements obtained by applying the specified expression or function to each element of the list.

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):

  • {a1, a2,..., an} creates the set with the specified elements.
  • {string} creates a set whose elements are defined by the specified string.
  • set: = {} defines the empty set.
  • C[i] returns the ith element of the set C.
  • C[-i] returns the ith element from the end of the set C.
  • C[a...b] returns the ath through bth elements of the set C, inclusive.
  • C[-a...-b] returns the ath through bth elements from the end of the set C.
  • [set1, set2] creates an ordered pair of two sets.
  • [set1,..., setn] creates an ordered n-tuple of sets.
  • member (expression, C) determines whether the expression belongs to the set C.
  • member (expr, C, name) assigns the specified name to the first item in the set that matches expr.
  • nops (C) gives the number of elements of the set C.
  • op (C) converts the sequence C into a set.
  • op (n, C) gives the nth element of the set C.
  • op (a..b, C) gives the ath through bth elements of the set C.
  • convert (A,set) converts a one-dimensional array A to a set.
  • convert (L, set) converts the list L to a set.
  • convert (expression, set) converts the given expression to a set whose elements are its first level operands.
  • convert (T, set) converts the table T to a set containing the elements in the table, ignoring its indices.
  • convert ([expr1,..., exprn], set) converts the list of expressions to a set.
  • convert (C,list) converts the set C to a list with the same elements, ignoring indices.
  • convert ({expr1,..., exprn}, option) creates a set with the given expressions converted according to the specified option (trig, exp, ln,...).
  • type (expression, set) determines whether the expression is a set.
  • type (expression, set (type)) determines whether the expression is a set with elements of the given type (integer, complex...).
  • typematch(expression, set) returns true if the given expression is of type set.
  • typematch (expr, set, name) in addition, if the match is successful, assigns to name a list of equations representing the variables and their matched values.
  • select (boolean_function, set) creates a set by selecting the elements of the given set that match the command or Boolean function.
  • select (boolean_function, set, expr1,..., exprn) applies the Boolean function with the given parameters expr1,..., exprn to the elements in the set to determine which ones are selected.
  • remove (boolean_function, set) creates a set by deleting from the given set the elements that match the given command or Boolean function.
  • remove (boolean_function, set, expr1,..., exprn) applies the Boolean function with the given parameters expr1,..., exprn to the elements in the set to determine which ones are removed.
  • map (function, C) applies the specified command or function to each element of the set C.
  • map (function, C, expr1,..., exprn) applies the specified command or function (with the parameters expr1,..., exprn) to each element of the set C.
  • map2 (function, expression, C) applies the specified binary command or function to the pairs with first argument a constant given by the expression and second element each element in the set C, collecting all such values together to form a set.
  • map2 (function, expression, C, expr3, expr4,..., exprn) applies the specified n-ary command or function with first argument the constant expression, second argument each element of the set C and for third to nth arguments the expressions expr3, expr4,..., exprn, collecting all such values together to form a set.
  • add (expression, variable = set) or add (f(i), i = set) finds the sum of the sequence of elements obtained by applying the specified expression or function to each element in the set.
  • mul (expression, variable = set) or mul(f(i), i = set) finds the product of the sequence of elements obtained by applying the specified expression or function to each element in the set.
  • seq (expression, variable = set) or seq (f i), i = set) constructs the sequence of elements obtained by applying the specified expression or function to each element in the set.
  • C1 union C2 is the set union of the sets C1 and C2.
  • ‘union’ (C1, C2) is the set union of the sets C1 and C2.
  • C1 intersect C2 is the set intersection of the sets C1 and C2.
  • ‘intersect’(C1, C2) is the set intersection of the sets C1 and C2.
  • C1 minus C2 is the set difference of the sets C1 and C2.
  • ‘minus’ (C1, C2) is the set difference of the sets C1 and C2.
  • readlib (symmdiff): symmdiff (C1, C2, ..., Cn) is the symmetric difference of the specified sets.

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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset