CHAPTER 1 Getting Started see how long the calculation takes. The Timing function can be wrapped around any other calculation. Here is an example. Example l1l.1 In[51]:=(* Timing can be wrapped around any ca1 culation★ fiming[5^10000000;] u1={1.17752,Nu11} This calculation took 1. 17752 seconds on the authors' laptop The"Null(which means"nothing)is present because we used the semicolon to suppress the output Notice that the semicolon is inside the last bracket. We have simply taken the Timing function and wrapped it completely around the calculation that we want to do. Can you predict what will happen if we enter Timing[5100000000];? Try it! In addition to using Timing, we'll see another way to monitor the speed of our calculations in Chap. 3 1.12 Lists Lists are so important to Mathematica that we need to learn what they are right away. In Mathematica a list is an ordered set of things delimited byi and) and separated by commas. Example 1. 12. 1 gives a few examples Example 1. 12.1 In(17:=(* examples of lists * B={{1,2},{3,4} ou={1,2,3,4,5 Out(18Mon,Tue,Wed,Thur,Fri,Sat,Sun ou1={1,2},{3,4} have defined three lists and named each with a variable name. The name of the first list is a. This list has five elements, namely, the integers I through 5. The first element of the list is the number 1 the second element of the list is 2. and so on
CHAPTER 1 Getting Started 17 see how long the calculation takes. The Timing function can be wrapped around any other calculation. Here is an example. Example 1.11.1 In[51]:= (* Timing can be wrapped around any calculation *) Timing [5 ^ 10 000 000 ;] Out[51]= {1.17752 , Null } This calculation took 1.17752 seconds on the authors’ laptop. The “Null” (which means “nothing”) is present because we used the semicolon to suppress the output. Notice that the semicolon is inside the last bracket. We have simply taken the Timing function and wrapped it completely around the calculation that we want to do. Can you predict what will happen if we enter Timing[5^100000000];? Try it! In addition to using Timing, we’ll see another way to monitor the speed of our calculations in Chap. 3. 1.12 Lists Lists are so important to Mathematica that we need to learn what they are right away. In Mathematica a list is an ordered set of things delimited by { and } and separated by commas. Example 1.12.1 gives a few examples. Example 1.12.1 In[17]:= (* examples of lists *) a = {1, 2, 3, 4, 5} days = {"Mon", "Tue", "Wed", "Thur ", "Fri", "Sat", "Sun"} B = {{1, 2}, {3, 4}} Out[17]= {1, 2, 3, 4, 5} Out[18]= {Mon , Tue , Wed , Thur , Fri , Sat , Sun } Out[19]= {{1, 2}, {3, 4}} We have defined three lists and named each with a variable name. The name of the first list is a. This list has five elements, namely, the integers 1 through 5. The first element of the list is the number 1, the second element of the list is 2, and so on
18 Mathematica Demystified The second list contains the days of the week which are represented as strings. In general a string is any amount of text surrounded by quotation marks. Finally, the last example is a list with two elements, each of which is a list itself! The elements of a list can be anything: numbers, variables, expressions, strings, or even other lists. The elements of a list do not have to all be the same"kind of thing. It would be perfectly OK for a list to contain a number, a string, and perhaps another list Many of the functions in Mathematica use lists as one of their arguments. For example, the Table function, which is used to generate lists, takes two arguments, the second of which must be a list. Suppose we want to create a list of the squares of the first 10 integers. These are 1-= 1, 24=4, 3=9, and so on up to 10-= 100 Example 1. 12.2 shows how to do this Example 1.1 In(55)=(* a table of perfect squares Table[i2,t ] o={1,4,9,16,25,36,49,64,81,100} The Table function takes two arguments. In this case the first argument is the expression i 2. The second argument is the list i, 1, 10]. What Table does is create a list by evaluating the expression i 2 with each value of i running from 1 to 10. The variable i is called the index or counter. It starts at l and is increased by I at a time until it reaches 10. For each value of i the expression is evaluated and the result is placed in the list. The argument, i, 1, 10, is extremely common in Mathematica. We will see arguments of this kind in many of the built-in functions provided by mathematic Sometimes we may not want the counter to go up in steps of 1. We can change this by adding an optional stepsize to the indexing list. Example 1. 12.3 illustrates different values for the stepsize. Can you see what is happening Example 1. 12.3 In(56)=(+ examples of stepsize Table[ i2, t Table[j+3, i 1}] Ou={1,9,25,49,81} 7,6,5,4 QUotation marks from another set of delimiters
18 Mathematica Demystified The second list contains the days of the week which are represented as strings. In general a string is any amount of text surrounded by quotation marks.8 Finally, the last example is a list with two elements, each of which is a list itself! The elements of a list can be anything: numbers, variables, expressions, strings, or even other lists. The elements of a list do not have to all be the same “kind” of thing. It would be perfectly OK for a list to contain a number, a string, and perhaps another list. Many of the functions in Mathematica use lists as one of their arguments. For example, the Table function, which is used to generate lists, takes two arguments, the second of which must be a list. Suppose we want to create a list of the squares of the first 10 integers. These are 12 = 1, 22 = 4, 32 = 9, and so on up to 102 = 100. Example 1.12.2 shows how to do this. Example 1.12.2 In[55]:= (* a table of perfect squares *) Table [ i ^ 2, {i, 1, 10}] Out[55]= {1, 4, 9, 16, 25, 36, 49, 64, 81, 100 } The Table function takes two arguments. In this case the first argument is the expression i∧2. The second argument is the list {i,1,10}. What Table does is create a list by evaluating the expression i∧2 with each value of i running from 1 to 10. The variable i is called the index or counter. It starts at 1 and is increased by 1 at a time until it reaches 10. For each value of i the expression is evaluated and the result is placed in the list. The argument, {i,1,10}, is extremely common in Mathematica. We will see arguments of this kind in many of the built-in functions provided by Mathematica. Sometimes we may not want the counter to go up in steps of 1. We can change this by adding an optional stepsize to the indexing list. Example 1.12.3 illustrates different values for the stepsize. Can you see what is happening? Example 1.12.3 In[56]:= ( * examples of stepsize *) Table [ i ^ 2, {i, 1, 10, 2}] Table [ j + 3, {j, 5, 1, – 1}] Out[56]= {1, 9, 25, 49, 81} Out[57]= {8, 7, 6, 5, 4} 8Quotation marks from another set of delimiters
CHAPTER 1 Getting Started In the first table, the counter i goes up in steps of 2, so that we only list the squares of the odd integers from l to 10. In the second table, the counter j goes down from 5 to I in steps of 1(or up in steps of -1). The entries of the table,or list, are the index plus 3 so we obtain 8, 7, 6, 5, and 4 In general, the indexing argument to the Table command is of the form [index, bwer vahe, upper vale, stepsize) although variations on this are possible. For example, we may omit the stepsize in which case Mathematica will use the default stepsize of 1. The index starts at the lower value, increases each time by an amount equal to stepsize, and ends when it reaches, or surpasses, the upper value. Notice that when we used i,1,10, 2) the last value of i is The next value would be 1 1 which is more than 10. So in this case the index is never actually equal to the upper value, instead it skips over the upper value We'll be learning a lot more about lists in Chap 4, but there are a couple of list related functions that are worth mentioning now. Suppose we want to"access a specific element of a list, say the third element In the days list above, the third element is "Wed The construction is to use the name of the list followed by double square brackets enclosing the number of the element we want. Thus, typing days[ [3]] gives us the the third element of the list days. The list B in Example 1. 12. 1 was actually a list of lists. Hence, entering b[ [1]] is the list (1,2. If we want to access the second element of the first element of B we can type either B[[1]][[2]]orB[[l, 2]]. We illustrate this in the following example Example 1. 12. 4 In(10)=(+ getting elements from a list* days[[ 3] B[[1] B[[1,2] ou1{1,2 o122 Another useful function for dealing with lists is the length function Length. We can use this to find out how many elements are in a list. In Example 1. 12.5 we use the Table command to first build a list and then the length command to see how long it is. (Of course we knew in advance how long it was-this is just to
CHAPTER 1 Getting Started 19 In the first table, the counter i goes up in steps of 2, so that we only list the squares of the odd integers from 1 to 10. In the second table, the counter j goes down from 5 to 1 in steps of 1 (or up in steps of −1). The entries of the table, or list, are the index plus 3 so we obtain 8, 7, 6, 5, and 4. In general, the indexing argument to the Table command is of the form {index, lower value, upper value, stepsize} although variations on this are possible. (For example, we may omit the stepsize in which case Mathematica will use the default stepsize of 1.) The index starts at the lower value, increases each time by an amount equal to stepsize, and ends when it reaches, or surpasses, the upper value. Notice that when we used {i,1,10,2} the last value of i is 9. The next value would be 11 which is more than 10. So in this case the index is never actually equal to the upper value, instead it skips over the upper value. We’ll be learning a lot more about lists in Chap. 4, but there are a couple of list related functions that are worth mentioning now. Suppose we want to “access” a specific element of a list, say the third element. In the days list above, the third element is “Wed.” The construction is to use the name of the list followed by double square brackets enclosing the number of the element we want. Thus, typing days[[3]] gives us the the third element of the list days. The list B in Example 1.12.1 was actually a list of lists. Hence, entering B[[1]] is the list {1,2}. If we want to access the second element of the first element of B we can type either B[[1]][[2]] or B[[1,2]]. We illustrate this in the following example. Example 1.12.4 In[10]:= ( * getting elements from a list *) days [ [ 3] ] B[ [ 1] ] B[ [ 1, 2] ] Out[10]= Wed Out[11]= {1, 2} Out[12]= 2 Another useful function for dealing with lists is the length function Length. We can use this to find out how many elements are in a list. In Example 1.12.5 we use the Table command to first build a list and then the Length command to see how long it is. (Of course we knew in advance how long it was—this is just to
20 Mathematica Demystified illustrate using Length. Also, notice that by following the Table command with a semicolon, Mathematica does not print out the list in an output cell. Example 1. 12.5 In(59]= cubes Table[i3,i, 1, 103]; ( Length will give the number of elements in a1主st★ Out(60]= 10 1.13 Palettes So far we have seen how to enter the square root of 12 in two different ways: as Sqrt[12]or as 12" (1/2). A third way is to enter is as v12 by using the"Basic Mathematics Input Palette If we select Palettes p BasicMathInput from the menu bar, a window will open from which we may then select various forms of algebraic expressions, relational symbols, and Greek letters. Figure 1. 2 shows what the Palette looks like. If we click on the square root expression, which is the first expression in the second row, the square root symbol, will be placed into the input cell and the cursor will be placed under the square root symbol so tha can begin typing there. After typing 12 we may press Shift+Return and obtain the answer. Example 1. 13. 1 shows how it looks Example 1.13.1 In(37 =(* using the square root symbol * ou37=2V3 The BasicMathInput Palette contains a number of templates easily recognized by the little squares that are present, some of which are black. If you click Palette template it will be inserted in your notebook and whatever you type next will be inserted in the template at the location of the black square. Pressing the Tab key will take you to the next square in the template. If you select some expression and then click on a template in the Palette, whatever you selected will be pasted into the template at the location of the black square. The black squares are called selection Placeholders while the white squares are simply Placeholders. After filling in all he place holders, type Ctrl+space( the Control key and the spacebar at the same
20 Mathematica Demystified illustrate using Length.) Also, notice that by following the Table command with a semicolon, Mathematica does not print out the list in an output cell. Example 1.12.5 In[59]:= cubes = Table [ i ^ 3, {i, 1, 10}] ; ( * Length will give the number of elements in a list *) Length [ cubes ] Out[60]= 10 1.13 Palettes So far we have seen how to enter the square root of 12 in two different ways: as Sqrt[12] or as 12ˆ(1/2). A third way is to enter is as √12 by using the “Basic Mathematics Input Palette.” If we select Palettes BasicMathInput from the menu bar, a window will open from which we may then select various forms of algebraic expressions, relational symbols, and Greek letters. Figure 1.2 shows what the Palette looks like. If we click on the square root expression, which is the first expression in the second row, the square root symbol, √ , will be placed into the input cell and the cursor will be placed under the square root symbol so that we can begin typing there. After typing 12 we may press Shift+Return and obtain the answer. Example 1.13.1 shows how it looks. Example 1.13.1 In[37]:= 12 Out[37]= 2 3 ( * using the square root symbol *) The BasicMathInput Palette contains a number of templates easily recognized by the little squares that are present, some of which are black. If you click on a Palette template it will be inserted in your notebook and whatever you type next will be inserted in the template at the location of the black square. Pressing the Tab key will take you to the next square in the template. If you select some expression and then click on a template in the Palette, whatever you selected will be pasted into the template at the location of the black square. The black squares are called Selection Placeholders while the white squares are simply Placeholders. After filling in all the place holders, type Ctrl+space (the Control key and the spacebar at the same
CHAPTER 1 Getting Started 21 O Basic Math v■| ■daa dola ∑· n r△e Figure 1.2 The Basic Math Input Palette
CHAPTER 1 Getting Started 21 Figure 1.2 The Basic Math Input Palette