1 / 42

Lab 2

Lab 2. Lists in Mathematica. Lab Preparation. Create a lab2 folder within your mbac611 folder (located within your private network folder) Copy the fares_120630 file from your lab1 folder to your lab2 folder. Start Mathematica ( )

everly
Download Presentation

Lab 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 2 Lists in Mathematica

  2. Lab Preparation Create a lab2 folder within your mbac611 folder (located within your private network folder) Copy the fares_120630 file from your lab1 folder to your lab2 folder. Start Mathematica ( ) On the intro screen select . This will create a new Notebook.

  3. Mathematica Notebook A Notebook is similar to a document in a traditional word processor (e.g. MS Word). However expressions and commands entered into the notebook are evaluated by Mathematica producing an answer or other appropriate output.

  4. Mathematical Expressions Type the expression 311+80 into the notebook. Notice that when you press the Enter key no answer is returned. To instruct Mathematica to evaluate the expression you need to hold the Shift key and then press the Enter key. Alternatively, you can just press the Enter key on the numeric keypad. Both methods are equivalent and therefore when we refer to the evaluation key we are referring to either key combination.

  5. In and Out Labels Notice that Mathematica automatically adds In and Out labels to the notebook. These labels are sequentially numbered and allow us to refer to previous answers when writing new expressions. The In label identifies what you entered and the Out label identifies Mathematica’s answer.

  6. Lists When using Mathematica our data will be stored in data structures called lists. A data structure is something that holds data. Elements of a list are enclosed in curly braces {} and separated by commas.

  7. Defining Lists In the Notebook enter the list then press the evaluation key. You should see the following response: Note that lists don’t only have to contain numbers. They can also contain text (also known as a string) and even other lists. In the Notebook enter the following list then press the evaluation key. You should see the following response: Text (string) list entries must be placed within quotation marks but numbers are not.

  8. Accessing Parts of a List We can access the elements of a list using the Part function. To access parts of the list we must first be able to refer to a previously defined list. So far the only way to refer to a list is by its Out label number. As you will recall our first list was labeled 2( )

  9. In the Notebook enter the following then press the evaluation key. You should see the following response: Notice that we used the expression %2 to refer to the second Out result (the first list we created). Similarly we could use %3 to refer to the third Out result. Lets take a closer look at the function we just entered: Function name. Note the capital letter - all Mathematica built-in functions begin with a capital letter Arguments. The are the values we wish the function to use. Each argument is separated by a comma. Arguments are enclosed within square brackets.

  10. Functions and Arguments When we entered into the Notebook we asked Mathematica to perform a function call (or function execution) – to apply the function to the arguments. Arguments can also contain function calls. For example, suppose we wanted to extract the second name (“John Doe”) in our previously defined list: We can accomplish this using the following function call:

  11. Enter the following expression then press the evaluation key. You should see the following result: Lets examine this expression more closely: Arguments are always evaluated first so Mathematica first evaluates Part[%3,3]. This returns the list 2) With the inner Part function evaluated Mathematica is essentially evaluating: Which asks for the second element of the list. This second element is

  12. Variables When we wanted to access the lists that we previously defined we had to use the number in the list’s Out label. This is not an ideal way to identify lists as we have to keep track of the various list label numbers and this can become difficult if we create many lists. A better way to keep track of the lists is to name them. This is what variables allow us to do.

  13. Enter the following into the Notebook and then press the evaluation key. With this expression we have bound (or assigned) the variable named to the list . A variable name that you create must begin with a letter and cannot conflict with names that are predefined in Mathematica (such as Part). Mathematica will give you an error message if you try to use a predefined name as a variable. It is a good idea to name all your variables with a starting lowercase letter as all of Mathematica’s predefined names start with a capital letter. Variables are case sensitive – first, fIRST, First and firsT are all different variable names. Once defined, you can use a variable anywhere you would use any other value.

  14. Variables In Action Enter the following examples into the Notebook (remember to hit the evaluation key after each example):

  15. Working Directory By default Mathematica saves your files to, and retrieves files from, a directory called the current working directory. To see your current working directory enter the expression then press the evaluation key. Note that the expression is really a function call with no arguments.

  16. Setting The Working Directory We want to retrieve the NYC subway data set that we utilized in the last lab. As you will recall the file was copied to your private lab2 folder. Lets make this lab2 folder your current working directory. Enter the following into your Notebook then press the evaluation key:

  17. Import Function Mathematica provides the Import to import data into Mathematica. Import takes a filename as an argument. Based on the file extension of this file Mathematica knows how to read the data. Enter the following expression into the Notebook and then press the evaluation key.

  18. The following dialog box should appear: Mathematica is warning us that the expression we just entered will result in a large output. This is expected so just click on . The full file should then be displayed in the form of a list. Use the vertical scroll bar on the right-hand of the screen scroll up to the top of the imported data set.

  19. Notice that the first two comment lines occupy the first two elements of the list. The header (field names) occupies the third element of the list. The data records begin with list element four. 1st Element 2nd Element 3rd Element Since we will be working with this list it would helpful to assign it to a variable – instead of having to remember it’s label number.

  20. Suppressing Output There are times when we wish to prevent the output from from being displayed. This may be necessary when the output is rather large. Placing a semicolon(;) at then end of an expression tells Mathematica to suppress the output. Enter the following expression into the Notebook, replacing %n with the number of label that corresponds to the previously executed Import statement. Note the semicolon at the end of the expression. fares=%n; This expression assigns the list to the fares variable but does not actually display the list (output was suppressed).

  21. Removing the Comments Lets remove the comments. To do this we need to remove the first two elements of the list. We can remove list elements using the Drop function. The syntax of the Drop function is Drop[m,n] where m is the list you want to remove items from. The argument n specifies the number of elements you want to remove from the front of the list.

  22. For the rest of this lab you can assume that you need to press the evaluation key after entering an expression into the Notebook. In the Notebook enter the following: Drop[fares, 2] You should see the following output: Notice that the first two elements (the comments) do not appear. However, you may be surprised to learn that the comments are still present in the fares list. In the Notebook enter the following: fares

  23. Notice that the comments are back. How can this be? The reason for this is that the Drop function, like many functions in Mathematica, do not modify their arguments but rather simply copy the arguments and perform the function on the copy. So the original is unchanged. If we want to use the changed list we can simply assign our variable (fares) to the result of the Drop function. Enter the following expression into the Notebook: fares=Drop[fares,2] Then enter the following expression into the Notebook: fares

  24. Notice that the comments (first two elements) are now removed. Now the first element in the list is the list of headers. Just to verify this enter the following into the Notebook: Part[fares,1] You should see the following output:

  25. Mathematica Comments At time we will want to add comments to the Notebook. Comments are not evaluated by Mathematica. Comments in Mathematica are placed between the characters (* and *). Enter the following into the Notebook: (* this is my first comment *)

  26. Lab Exercise 1 In the Notebook enter the following comment: (* lab exercise 1 *) Write the Mathematica expressions that do the following: Stores the header in a variable named header. Removes the header from the fares list. The fares list should be modified – that is it should no longer contain the header as the first list element.

  27. Exercise 1 Result To check if you successfully completed the exercise enter the expression fares into the Notebook. You should see the following output: Enter the expression header into the Notebook. You should see the following output:

  28. Saving Your Notebook Mathematica allows you to save the Notebook to disk. To save the Notebook select (which is found in the upper left hand corner) and then select from the pull-down menu. In the File name textbox enter lab2_notebook. Make sure you are saving in your private lab2 network folder.

  29. A Closer Look At The Data # of Metrocard swipes arranged by Metrocard type Station ID & Name In this data set the first two fields specify the station (via name and ID). The remainder of the columns specify the number of total number of different metrocard swipes (usage) that occurred at a the station during the specified week.

  30. Adding Elements In A List We would like to calculate the total metrocard swipes for each station. For this we use the Total function. Lets try calculating the total for the first station (first element of the fares list). Enter the following into the Notebook: Total[Part[fares,1]]

  31. Total Function Extract first element of the list (first station information). This argument is evaluated first. So we are essentially asking Mathematica to evaluate the following: Notice the strange output that Mathematica produced:

  32. The first item is the sum of the numbers in the list. The fourth item is the second field of the data (Station Name) The second item seems to be a collection of white space in quotes. This actually comes from the last column of the data. For some reason the MTA added extra space to the end of the record – this may be an error or perhaps it once contained data that the MTA did not wish to share. The third item is the first field of the data (Station ID) Since Mathematica doesn’t know how to interpret these items it simply adds them to the output since we asked for the total of all the fields.

  33. Choosing Elements From A List Clearly we don’t want the Total function to apply to the station id, name and white space. We really want the total of the sublist that consists of the third field through the next to the last field. We can access this sublist by simply adding another argument to the Part function.

  34. The Third Argument Enter the following expression into the notebook: Part[fares,1,1] The output is the first element of the first list stored by the faresvariable. The additional argument allows you to specify which element of the selected list you wish to view. In this case we want the first element of the first list.

  35. Lab Exercise 2 Save your Notebook to disk. Add the comment (* lab exercise 2 *) to the Notebook. 1) Write the expression to display the second element of the first list of fares. The output should be . 2) Write the expression to display the second element of the fourth list of fares. The output should be

  36. Specifying Sublists The Part function also allows us to extract sublists from a list. Enter the following into the Notebook: Part[fares,1,2;;4] The output should be: The expression 2;;4 specifies the sublist containing elements 2 through four (inclusive).

  37. Enter the following into the Notebook: Part[fares,1, 3;;] Notice that no number is specified after the two semicolons. When this number is not specified it is interpreted to mean the end of the list. Therefore this expression first extracts the first element of the fares list – which is itself a list. From this extracted list, the third argument asks Mathematica to extract the sublist starting at the third element and ending at the last element. Lets take a closer look at the output: Note the large space between the last comma and the closing brace. This is extra white space that we mentioned earlier when discussing the Total function.

  38. Length function The Length function allows us to determine the length of the list. Enter the following in the Notebook: Length[fares] This expression tells us the number of elements (in this case lists) within the fares list – remember fares is a list of lists.

  39. Enter the following into the Notebook: Length[Part[fares,1]] The output should be the number 26. Lets take a closer look at this expression: The argument is evaluated first. This extracts a copy of the first element of the fares list. After the argument is evaluated the Length function is executed on the first element of the list.

  40. Lab Exercise 3 Add the comment (* lab exercise 3 *) to the Notebook. We now know that the first list in fares has 26 elements. The last element (26th element) is just white space. 1) Write the expression that displays the 3rd through 25th elements of the first element of the fares list. Hint: this requires the using two semicolons. The output should be: 2) Write the expression to add up all the elements in the list you just generated. Hint: this requires the Total function. The output should be:

  41. Saving Your Expressions In our next lab we will be using the values stores in the variable fares and header. Therefore we want to save them to disk. Enter the following expression into the Notebook: This expression saves just those variable to a file that we can use in our next lab.

  42. Submiting Your Lab Save your Notebook Close Mathematica Submit the file lab2_notebook to Moodle. The file is stored in your private lab2 folder.

More Related