Think of a list as a group of items in which you

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: hat is, lists or sets that have no elements. > empty_set := {}; empty _set := {} > empty_list := ; empty _list := You can create a new set from other sets by using, for example, the union command. Delete items from sets by using the minus command. > old_set := {2, 3, 4} union {}; old _set := {2, 3, 4} > new_set := old_set union {2, 5}; new _set := {2, 3, 4, 5} 2.5 Basic Types of Maple Objects > third_set := old_set minus {2, 5}; • 27 third _set := {3, 4} Arrays Arrays are an extension of the concept of the list data structure. Think of a list as a group of items in which you associate each item with a positive integer, its index, that represents its position in the list. The Maple array data structure is a generalization of this idea. Each element is still associated with an index, but an array is not restricted to one dimension. In addition, indices can also be zero or negative. Furthermore, you can define or change the array’s individual elements without redefining it entirely. Declare the array to indicate dimensions. > squares := array(1..3); squares := array(1..3, ) Assign the array elements. Multiple commands can be entered at one command prompt provided each ends with a colon or semicolon. > squares[1] := 1; squares[2] := 2^2; squares[3] := 3^2; squares 1 := 1 squares 2 := 4 squares 3 := 9 Or do both simultaneously. > cubes := array( 1..3, [1,8,27] ); cubes := [1, 8, 27] You can select a single element using the same notation applied to lists. > squares[2]; 28 • Chapter 2: Mathematics with Maple: The Basics 4 You must declare arrays in advance. To see the array’s contents, you must use a command such as print. > squares; squares > print(squares); [1, 4, 9] The preceding array has only one dimension, but arrays can have more than one dimension. Define a 3 × 3 array. > pwrs := array(1..3,1..3); pwrs := array(1..3, 1..3, ) This array has dimension two (two sets of indices). To begin, assign the array elements of the first row. > pwrs[1,1] := 1; pwrs[1,2] := 1; pwrs[1,3] := 1; pwrs 1, 1 := 1 pwrs 1, 2 := 1 pwrs 1, 3 := 1 Continue for the rest of the array. If you prefer, you can end each command with a colon (:), instead of the usual semicolon (;), to suppress the output. Both the colon and semicolon are statement separators. > pwrs[2,1] := 2: > pwrs[3,1] := 3: > print(pwrs); pwrs[2,2] := 4: pwrs[3,2] := 9: pwrs[2,3] := 8: pwrs[3,3] := 27: 11 1 2 4 8 3 9 27 2.5 Basic Types of Maple Objects • 29 You can select an element by specifying both the row and column. > pwrs[2,3]; 8 You can define a two-dimensional array and its elements simultaneously by using a similar method employed for the one-dimensional example shown earlier. To do so, use lists within lists. That is, make a list where each element is a list that contains the elements of one row of the array. Thus, you could define the pwrs array as follows. > pwrs2 := array( 1..3, 1..3, [[1,1,1], [2,4,8], [3,9,27]] ); 11 1 pwrs2 := 2 4 8 3 9 2...
View Full Document

This note was uploaded on 08/27/2012 for the course MATH 1100 taught by Professor Nil during the Spring '12 term at National University of Singapore.

Ask a homework question - tutors are online