(* Content-type: application/mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 6.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 145, 7] NotebookDataLength[ 35114, 1323] NotebookOptionsPosition[ 28961, 1141] NotebookOutlinePosition[ 29302, 1156] CellTagsIndexPosition[ 29259, 1153] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["Lesson 3 - Lists", "Section"], Cell[TextData[{ "One of the most powerful organizing tools in ", StyleBox["Mathematica", FontSlant->"Italic"], " is a list. Lists can represent sequences of numbers, matrices, \ polynomials, vectors, optional arguments for commands, ranges of numbers, \ collections of graphic elements, and many more. But basically a list looks \ like:" }], "Text"], Cell[BoxData[ RowBox[{"{", RowBox[{"1", ",", "3", ",", "5", ",", "6", ",", "7"}], "}"}]], "Input"], Cell["\<\ in this case, a list of 5 integers, separated by commas, within braces. \ Evaluating the above cell does nothing since there is nothing to do with the \ list.\ \>", "Text"], Cell["Here are some other examples:", "Text"], Cell[BoxData[{ RowBox[{"{", RowBox[{"a", ",", "b", ",", "c", ",", "d", ",", "e", ",", "f"}], "}"}], "\[IndentingNewLine]", RowBox[{"{", RowBox[{ RowBox[{"a", "+", "b"}], ",", RowBox[{"c", "+", "d"}], ",", RowBox[{"e", "+", "f"}]}], "}"}], "\[IndentingNewLine]", RowBox[{"{", RowBox[{"Sin", ",", " ", "Cos", ",", "Tan"}], "}"}], "\[IndentingNewLine]", RowBox[{"{", "}"}], "\[IndentingNewLine]", RowBox[{"l", "=", RowBox[{"{", RowBox[{ RowBox[{"1", "/", "2"}], ",", RowBox[{"2", "/", "3"}], ",", RowBox[{"3", "/", "4"}], ",", RowBox[{"4", "/", "5"}]}], "}"}]}]}], "Input"], Cell["\<\ The last example is reminder that lists can be given names, just like \ numbers. The entries in a list can themselves be lists:\ \>", "Text"], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "2"}], "}"}], ",", "S", ",", RowBox[{"{", RowBox[{ RowBox[{"3", "z"}], ",", RowBox[{"5", "g"}]}], "}"}]}], "}"}]], "Input"], Cell["\<\ Unlike sets, order matters in a list and duplicate entries are allowed, so \ the following are distinct lists:\ \>", "Text"], Cell[BoxData[{ RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}], "\[IndentingNewLine]", RowBox[{"{", RowBox[{"2", ",", "3", ",", "1"}], "}"}], "\[IndentingNewLine]", RowBox[{"{", RowBox[{"1", ",", "1", ",", "2", ",", "3"}], "}"}]}], "Input"], Cell[CellGroupData[{ Cell["Basic Operations on Lists", "Subsection"], Cell["\<\ Once you have some lists, you can add, subtract, multiply and divide them, as \ well as square them, take logarithms and many other things.\ \>", "Text"], Cell[BoxData[{ RowBox[{"u", "=", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], "\[IndentingNewLine]", RowBox[{"v", "=", RowBox[{"{", RowBox[{"a", ",", "b", ",", "c"}], "}"}]}]}], "Input"], Cell["Evaluate the following to see what they do:", "Text"], Cell[BoxData[ RowBox[{"u", "+", "v"}]], "Input"], Cell[BoxData[ RowBox[{"u", "-", "v"}]], "Input"], Cell[BoxData[ RowBox[{"u", " ", "v"}]], "Input"], Cell[BoxData[ RowBox[{"u", "/", "v"}]], "Input"], Cell[BoxData[ RowBox[{"3", "v"}]], "Input"], Cell[BoxData[ RowBox[{"3", "+", "v"}]], "Input"], Cell[BoxData[ RowBox[{"v", "/", "3"}]], "Input"], Cell[BoxData[ RowBox[{"x", "^", "u"}]], "Input"], Cell[BoxData[ RowBox[{"x", "^", "v"}]], "Input"], Cell[BoxData[ RowBox[{"u", "^", "2"}]], "Input"], Cell[BoxData[ RowBox[{"v", "^", "2"}]], "Input"], Cell[BoxData[ RowBox[{"Sin", "[", "v", "]"}]], "Input"], Cell["The number of entries in a list is called its length:", "Text"], Cell[BoxData[ RowBox[{"Length", "[", "u", "]"}]], "Input"], Cell[BoxData[ RowBox[{"Length", "[", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4", ",", "5", ",", "6", ",", "7"}], "}"}], "]"}]], "Input"], Cell[BoxData[ RowBox[{"Length", "[", RowBox[{"{", RowBox[{"1", ",", RowBox[{"{", RowBox[{"1", ",", "2"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], "}"}], "]"}]], "Input"], Cell[TextData[{ "The sum of the entries of a list can be obtained using ", StyleBox["Total", "Input"], " (The ", StyleBox["Mathematica", FontSlant->"Italic"], " function ", StyleBox["Sum", "Input"], " has another meaning)." }], "Text"], Cell[BoxData[ RowBox[{"Total", "[", "u", "]"}]], "Input"], Cell[BoxData[ RowBox[{"Total", "[", RowBox[{"{", RowBox[{"10", ",", "11", ",", "x", ",", "y"}], "}"}], "]"}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Vectors and Matrices", "Subsection"], Cell[TextData[{ "The natural way to represent vectors in ", StyleBox["Mathematica", FontSlant->"Italic"], " is as a list of numbers. For example, ", StyleBox["u = {1,2,3}", "Input"], " represents a vector in ", Cell[BoxData[ FormBox[ SuperscriptBox["R", "3"], TraditionalForm]]], " whose components are ", StyleBox["1", "Input"], ", ", StyleBox["2", "Input"], ", and ", StyleBox["3", "Input"], ". This is convenient because if ", StyleBox["u", "Input"], " and ", StyleBox["v", "Input"], " are vectors in ", Cell[BoxData[ FormBox[ SuperscriptBox["R", "3"], TraditionalForm]]], ", then ", StyleBox["u+v", "Input"], " is the sum of the two vectors, and ", StyleBox["3v", "Input"], " is the scalar multiple of the vector ", StyleBox["v", "Input"], " by the scalar ", StyleBox["3", "Input"], ". However, ", StyleBox["uv", "Input"], " is not any product of these vectors that you know from your matrix theory \ class. Fortunately ", StyleBox["Mathematica", FontSlant->"Italic"], " does have a dot-product built-in." }], "Text"], Cell[BoxData[{ RowBox[{"u", "=", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], "\[IndentingNewLine]", RowBox[{"v", "=", RowBox[{"{", RowBox[{"a", ",", "b", ",", "c"}], "}"}]}], "\[IndentingNewLine]", RowBox[{"u", ".", "v"}]}], "Input"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{ RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4", ",", "5"}], "}"}], ".", RowBox[{"{", RowBox[{"6", ",", "5", ",", "4", ",", "3", ",", "2"}], "}"}]}]], "Input"], Cell[BoxData["50"], "Output"] }, Open ]], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " also has a built-in function for the absolute value (length, norm) of \ vectors:" }], "Text"], Cell[BoxData[ RowBox[{"Norm", "[", "u", "]"}]], "Input"], Cell[BoxData[ RowBox[{"Norm", "[", RowBox[{"{", RowBox[{"1", ",", "2", ",", "aa"}], "}"}], "]"}]], "Input"], Cell[TextData[{ "In ", StyleBox["Mathematica", FontSlant->"Italic"], ", a matrix is just a list of lists. For example," }], "Text"], Cell[BoxData[ RowBox[{"M", "=", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "2"}], "}"}], ",", RowBox[{"{", RowBox[{"3", ",", "4"}], "}"}]}], "}"}]}]], "Input"], Cell[TextData[{ "is a 2\[Cross]2 matrix. This is more apparent if the list is displayed \ using the function ", StyleBox["MatrixForm", "Input"], ":" }], "Text"], Cell[BoxData[ RowBox[{"MatrixForm", "[", "M", "]"}]], "Input"], Cell[TextData[{ "Notice that the first entry in the list ", StyleBox["M", "Input"], " is the top row of the matrix it represents. This same output form can be \ obtained by putting the function name at the end:" }], "Text"], Cell[BoxData[ RowBox[{"M", "//", "MatrixForm"}]], "Input"], Cell[TextData[{ "Another useful function is ", StyleBox["TableForm", "Input"], ":" }], "Text"], Cell[BoxData[ RowBox[{"M", "//", "TableForm"}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Practice", "Subsection", CellFrame->{{0, 0}, {0, 2}}], Cell[TextData[{ "1) Is ", StyleBox["2M", "Input"], " the scalar multiplication of ", StyleBox["M", "Input"], " by the scalar ", StyleBox["2", "Input"], "? What is ", StyleBox["M+1", "Input"], "?" }], "Text", CellFrame->{{0, 0}, {2, 0}}] }, Open ]], Cell[CellGroupData[{ Cell["Range", "Subsection"], Cell[TextData[{ "There are many useful ", StyleBox["Mathematica", FontSlant->"Italic"], " functions for generating lists. Here is the simplest:" }], "Text"], Cell[BoxData[ RowBox[{"Range", "[", "10", "]"}]], "Input"], Cell[BoxData[ RowBox[{"Range", "[", RowBox[{"3", ",", "10"}], "]"}]], "Input"], Cell[BoxData[ RowBox[{"Range", "[", RowBox[{"3", ",", "10", ",", "2"}], "]"}]], "Input"], Cell[BoxData[ RowBox[{"Range", "[", RowBox[{"0", ",", RowBox[{"10", "Pi"}], ",", RowBox[{"Pi", "/", "2"}]}], "]"}]], "Input"], Cell[TextData[{ "The ", StyleBox["Range", "Input"], " function generates a list of evenly spaced numbers - or, perhaps, evenly \ spaced symbols representing a sequence of numbers as below:" }], "Text"], Cell[BoxData[ RowBox[{"Range", "[", RowBox[{"x", ",", RowBox[{"x", "+", RowBox[{"4", "a"}]}], ",", RowBox[{"a", "/", "2"}]}], "]"}]], "Input"], Cell["Here are some cute applications :", "Text"], Cell["1) The sum of the first 100 natural numbers:", "Text"], Cell[BoxData[ RowBox[{"Total", "[", RowBox[{"Range", "[", "100", "]"}], "]"}]], "Input"], Cell["\<\ 2) A list of the values of the sine function at 0, 0.1, 0.2, ... ,1.0.\ \>", "Text"], Cell[BoxData[ RowBox[{"Sin", "[", RowBox[{"Range", "[", RowBox[{"0", ",", "1", ",", ".1"}], "]"}], "]"}]], "Input"], Cell[TextData[{ "3) A list of powers of the variable ", StyleBox["x", "Input"], ":" }], "Text"], Cell[BoxData[ RowBox[{"x", "^", RowBox[{"Range", "[", "7", "]"}]}]], "Input"], Cell[TextData[{ "4) A list of approximations to \[ExponentialE] of the form ", Cell[BoxData[ FormBox[ SuperscriptBox[ RowBox[{"(", RowBox[{"1", "+", FractionBox["1", "n"]}], ")"}], "n"], TraditionalForm]]], " with n=1,2,3,...,10." }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"(", RowBox[{"1", "+", RowBox[{"1", "/", RowBox[{"Range", "[", "10", "]"}]}]}], ")"}], "^", RowBox[{"Range", "[", "10", "]"}]}]], "Input"], Cell[BoxData[ RowBox[{"%", "//", "N"}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Practice", "Subsection", CellFrame->{{0, 0}, {0, 2}}], Cell[TextData[{ "1) Calculate ", Cell[BoxData[ FormBox[ RowBox[{ RowBox[{ RowBox[{"1", "+", "1.1", "+", SuperscriptBox["1.1", "2"], "+", SuperscriptBox["1.1", "3"], "+"}], "..."}], "+", SuperscriptBox["1.1", "100"]}], TraditionalForm]]], " and show that this sum is (", Cell[BoxData[ FormBox[ RowBox[{ SuperscriptBox["1.1", "101"], "-", "1"}], TraditionalForm]]], ")/.1 exactly." }], "Text", CellFrame->{{0, 0}, {2, 0}}] }, Open ]], Cell[CellGroupData[{ Cell["Table", "Subsection"], Cell[TextData[{ "The most flexible method for generating a list is the ", StyleBox["Table", "Input"], " function. Here's an easy example:" }], "Text"], Cell[BoxData[ RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"x", "^", "2"}], "+", "x", "+", "1"}], ",", RowBox[{"{", RowBox[{"x", ",", "3", ",", "10"}], "}"}]}], "]"}]], "Input"], Cell[TextData[{ "It generates a list containing the values of the polynomial ", StyleBox["x^2+x+1", "Input"], " at ", StyleBox["x=3,4,...,10", "Input"], ". The iterator, ", StyleBox["{x,3,10}", "Input"], " in the above, can have various forms:" }], "Text"], Cell[TextData[{ "For ", StyleBox["x=1,2,3,...,10", "Input"], ", use" }], "Text"], Cell[BoxData[ RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"x", "^", "2"}], "+", "x", "+", "1"}], ",", RowBox[{"{", RowBox[{"x", ",", "10"}], "}"}]}], "]"}]], "Input"], Cell[TextData[{ "For ", StyleBox["x=1,3,5,7,9", "Input"], ", use" }], "Text"], Cell[BoxData[ RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"x", "^", "2"}], "+", "x", "+", "1"}], ",", RowBox[{"{", RowBox[{"x", ",", "1", ",", "10", ",", " ", "2"}], "}"}]}], "]"}]], "Input"], Cell["Sometimes the index is not needed.", "Text"], Cell[BoxData[ RowBox[{"Table", "[", RowBox[{"Pi", ",", RowBox[{"{", "10", "}"}]}], "]"}]], "Input"], Cell["\<\ The entries in the list do not need to be numbers. They could be lists \ themselves:\ \>", "Text"], Cell[BoxData[ RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{"x", ",", RowBox[{ RowBox[{"x", "^", "2"}], "+", "x", "+", "1"}]}], "}"}], ",", RowBox[{"{", RowBox[{"x", ",", "10"}], "}"}]}], "]"}]], "Input"], Cell[BoxData[ RowBox[{"%", "//", "TableForm"}]], "Input"], Cell["\<\ Sometimes two indices are needed. Here is a list of all possible sums \ obtained by rolling two dice.\ \>", "Text"], Cell[BoxData[ RowBox[{"Table", "[", RowBox[{ RowBox[{"i", "+", "j"}], ",", RowBox[{"{", RowBox[{"i", ",", "6"}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", "6"}], "}"}]}], "]"}]], "Input"], Cell["Notice that the output is a matrix - a list of lists.", "Text"], Cell[BoxData[ RowBox[{"%", "//", "TableForm"}]], "Input"], Cell[TextData[{ "The order of evaluation is as follows: The first index, ", StyleBox["i", "Input"], ", is set to its first value, and then the second index, ", StyleBox["j", "Input"], ", moves through all its values. Next, ", StyleBox[" i", "Input"], " is increased by ", StyleBox["1", "Input"], ", and ", StyleBox["j", "Input"], " moves through all of its values again... . " }], "Text"], Cell["\<\ Here is the list of all possible combinations of two dice rolls. (In fact, \ this is a list of lists of lists of integers.)\ \>", "Text"], Cell[BoxData[ RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{"i", ",", "j"}], "}"}], ",", RowBox[{"{", RowBox[{"i", ",", "6"}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", "6"}], "}"}]}], "]"}]], "Input"], Cell[TextData[{ "The lists created by ", StyleBox["Table", "Input", FontWeight->"Bold"], " do not have to be square, and the second index can depend on the first \ index. Here is an example:" }], "Text"], Cell[BoxData[ RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"j", "^", "2"}], "+", "i"}], ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "5"}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", "1", ",", "i"}], "}"}]}], "]"}]], "Input"], Cell[TextData[{ "It is hard to see the structure from the output. We can use ", StyleBox["//", "Input", FontWeight->"Bold"], StyleBox["TableForm", "Input", FontWeight->"Bold"], " to make the output more readable." }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"j", "^", "2"}], "+", "i"}], ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "5"}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", "1", ",", "i"}], "}"}]}], "]"}], "//", "TableForm"}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Practice", "Subsection", CellFrame->{{0, 0}, {0, 2}}], Cell[TextData[{ "1) Generate a sequence of 10 numbers, starting with ", Cell[BoxData[ FormBox[ RowBox[{ FractionBox[ RowBox[{"sin", " ", RowBox[{"1", "/", "10"}]}], RowBox[{"1", "/", "10"}]], ","}], TraditionalForm]]], Cell[BoxData[ FormBox[ RowBox[{ FractionBox[ RowBox[{"sin", " ", RowBox[{"1", "/", "100"}]}], RowBox[{"1", "/", "100"}]], ","}], TraditionalForm]]], Cell[BoxData[ FormBox[ RowBox[{ FractionBox[ RowBox[{"sin", " ", RowBox[{"1", "/", "1000"}]}], RowBox[{"1", "/", "1000"}]], ","}], TraditionalForm]]], Cell[BoxData[ FormBox[ RowBox[{ FractionBox[ RowBox[{"sin", " ", RowBox[{"1", "/", "10000"}]}], RowBox[{"1", "/", "10000"}]], ","}], TraditionalForm]]], " etc., illustrating that ", Cell[BoxData[ FormBox[ RowBox[{ RowBox[{ SubscriptBox["lim", RowBox[{"x", "\[Rule]", "0"}]], FractionBox[ RowBox[{"sin", " ", "x"}], "x"]}], "=", "1"}], TraditionalForm]]], "." }], "Text", CellFrame->{{0, 0}, {2, 0}}], Cell[TextData[{ "If all the entries are the same, the index is not needed. Here's how to \ make a list all of whose entries are ", StyleBox["a", "Input"], "." }], "Text"], Cell[BoxData[ RowBox[{"Table", "[", RowBox[{"a", ",", RowBox[{"{", "10", "}"}]}], "]"}]], "Input"], Cell[TextData[{ " ", StyleBox["Table", "Input"], " can also be used to do someting to each entry in an existing list. Here's \ an example:" }], "Text"], Cell[BoxData[ RowBox[{"L", "=", RowBox[{"{", RowBox[{"1", ",", "3", ",", "a", ",", "b"}], "}"}]}]], "Input"], Cell[BoxData[ RowBox[{"Table", "[", RowBox[{ RowBox[{"i", "^", "2"}], ",", RowBox[{"{", RowBox[{"i", ",", "L"}], "}"}]}], "]"}]], "Input"], Cell[TextData[{ "In the ", StyleBox["Table", "Input"], " function, instead of ", StyleBox["i", "Input"], " taking a sequence of integer values, it takes its values directly from the \ list ", StyleBox["L", "Input"], ". So the effect of this ", StyleBox["Table", "Input"], " command is to square each entry in ", StyleBox["L", "Input"], ". (This particular simple calculation could also be done by ", StyleBox[Cell[BoxData[ FormBox[ SuperscriptBox["L", "2"], TraditionalForm]], "Input"], "Input"], ".)" }], "Text"] }, Open ]], Cell[CellGroupData[{ Cell["Practice", "Subsection", CellFrame->{{0, 0}, {0, 2}}], Cell[TextData[{ "1) Suppose that ", StyleBox["L", "Input"], " is a list of positive integers. What ", StyleBox["Mathematica", FontSlant->"Italic"], " function could be used create a zero matrix whose row lengths are the \ number in ", StyleBox["L", "Input"], ". For example, if ", StyleBox["L={1,3,2}", "Input"], ", then the function should produce ", StyleBox["{{0},{0,0,0},{0,0}}", "Input"], "." }], "Text", CellFrame->{{0, 0}, {2, 0}}], Cell[TextData[{ "If you did the homework from Lesson 2, you will know that ", StyleBox["RandomInteger", "Input"], " and ", StyleBox["RandomReal", "Input"], " can be used to generate lists and matrices of random numbers. " }], "Text"], Cell[TextData[{ "Here is a list of 20 random integers between ", StyleBox["-4", "Input"], " and ", StyleBox["4", "Input"], ":" }], "Text"], Cell[BoxData[ RowBox[{"RandomInteger", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", "4"}], ",", "4"}], "}"}], ",", "20"}], "]"}]], "Input"], Cell[TextData[{ "Here is a 3\[Cross]3 matrix of random real numbers between ", StyleBox["-4", "Input"], " and ", StyleBox["4", "Input"], ":" }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"RandomReal", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", "4"}], ",", "4"}], "}"}], ",", RowBox[{"{", RowBox[{"3", ",", "3"}], "}"}]}], "]"}], "//", "MatrixForm"}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Accessing List Elements", "Subsection"], Cell[TextData[{ "To access an element in a list or a matrix, you need to specify its \ position. For a list, that is simply the position within the list, starting \ with ", StyleBox["1", "Input"], ". Here is an example:" }], "Text"], Cell[BoxData[ RowBox[{"v", "=", " ", RowBox[{"{", RowBox[{"1", ",", "3", ",", RowBox[{"-", "2"}]}], "}"}]}]], "Input"], Cell[TextData[{ "To get the element ", StyleBox["3", "Input"], " (at position ", StyleBox["2", "Input"], " in the list), we use the name of the list and the position within ", StyleBox["double square brackets", FontVariations->{"Underline"->True}], ". " }], "Text"], Cell[BoxData[ RowBox[{"v", "[", RowBox[{"[", "2", "]"}], "]"}]], "Input"], Cell["\<\ For a matrix, we need to specify the row and then the position within the \ row, very much like the indexing (i,j) for matrices.\ \>", "Text"], Cell[BoxData[{ RowBox[{ RowBox[{"A", "=", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "4", ",", "5"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"-", "2"}], ",", "8", ",", "3"}], "}"}]}], "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"A", "//", "MatrixForm"}]}], "Input"], Cell[TextData[{ "The element ", StyleBox["5", "Input"], " is at position ", StyleBox["1,3", "Input"], " (row 1, column 3)" }], "Text"], Cell[BoxData[ RowBox[{"A", "[", RowBox[{"[", RowBox[{"1", ",", "3"}], "]"}], "]"}]], "Input"], Cell["What happens if you specify only one index for a matrix? ", "Text"], Cell[BoxData[ RowBox[{"A", "[", RowBox[{"[", "1", "]"}], "]"}]], "Input"], Cell[TextData[{ "In this case, the first element in the list ", StyleBox["A", "Input"], " is selected - we get the first row. To get a specific column, we specify \ that we want the elements in ", StyleBox["All", "Input", FontWeight->"Bold"], " rows, and indicate the column. For example, if we want the second column, \ we would write" }], "Text"], Cell[BoxData[ RowBox[{"A", "[", RowBox[{"[", RowBox[{"All", ",", "2"}], "]"}], "]"}]], "Input"], Cell[TextData[{ "Note that the display of the answer is a list in row format, not in column \ format. The elements of the column are listed from top to bottom. \ Furthermore, the specification ", StyleBox["All", "Input", FontWeight->"Bold"], " needs to be capitalized." }], "Text"], Cell[BoxData[ RowBox[{"A", "[", RowBox[{"[", RowBox[{"all", ",", "2"}], "]"}], "]"}]], "Input"], Cell[TextData[{ "Suppose that ", StyleBox["L", "Input"], " is a list of numbers of unknown length, and we want to make a new list ", StyleBox["K", "Input"], " containing the sums of adjacent pairs of numbers in ", StyleBox["L", "Input"], ". That is, if ", StyleBox["L={1,2,3,4}", "Input"], ", then ", StyleBox["K={3,5,7}", "Input"], ". Here is an expression that does the job:" }], "Text"], Cell[BoxData[{ RowBox[{"L", "=", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}]}], "\[IndentingNewLine]", RowBox[{"K", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"L", "[", RowBox[{"[", "i", "]"}], "]"}], "+", RowBox[{"L", "[", RowBox[{"[", RowBox[{"i", "+", "1"}], "]"}], "]"}]}], ",", RowBox[{"{", RowBox[{"i", ",", RowBox[{ RowBox[{"Length", "[", "L", "]"}], "-", "1"}]}], "}"}]}], "]"}]}]}], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Practice", "Subsection", CellFrame->{{0, 0}, {0, 2}}], Cell[TextData[{ "1) Try the above expression for ", StyleBox["K", "Input"], " on ", StyleBox["L=Range[20]", "Input"], "." }], "Text"], Cell[TextData[{ "2) Suppose that ", StyleBox["L", "Input"], " is a list of unknown length. How could we make a new list ", StyleBox["K", "Input"], " reversing the order of the entries in ", StyleBox["L", "Input"], ". (The ", StyleBox["Mathematica", FontSlant->"Italic"], " function ", StyleBox["Reverse", "Input"], " does this, but we don't know that yet!) Try out your expression on various \ lists ", StyleBox["L", "Input"], "." }], "Text"], Cell[TextData[{ "3) Suppose that ", StyleBox["L", "Input"], " is a list of unknown length. How could we make a new list ", StyleBox["K", "Input"], " containing all odd indexed entries of ", StyleBox["L", "Input"], "?" }], "Text", CellFrame->{{0, 0}, {2, 0}}] }, Open ]], Cell[CellGroupData[{ Cell["Homework", "Subsection"], Cell["1) Find a slick way to generate the following expression:", "Text"], Cell[BoxData[ RowBox[{"1", "+", "x", "+", SuperscriptBox["x", "2"], "+", SuperscriptBox["x", "3"], "+", SuperscriptBox["x", "4"], "+", SuperscriptBox["x", "5"], "+", SuperscriptBox["x", "6"], "+", SuperscriptBox["x", "7"], "+", SuperscriptBox["x", "8"], "+", SuperscriptBox["x", "9"], "+", SuperscriptBox["x", "10"]}]], "Output"], Cell[TextData[{ "2) Calculate ", Cell[BoxData[ FormBox[ RowBox[{ RowBox[{ RowBox[{"1", "+", FractionBox["1", "2"], "+", FractionBox["1", "3"], "+"}], "..."}], "+", FractionBox["1", "100"]}], TraditionalForm]]], "exactly by applying ", StyleBox["Total", "Input"], " to a list. (You can also calculate this using ", StyleBox["Sum", "Input"], ". See Lesson 7)" }], "Text"], Cell["\<\ 3) Generate a table of the values of the sine function at 0, 0.1, 0.2, ... \ ,1.0 as below.\ \>", "Text"], Cell[BoxData[ TagBox[GridBox[{ {"0.`", "0.`"}, {"0.1`", "0.09983341664682815`"}, {"0.2`", "0.19866933079506124`"}, {"0.30000000000000004`", "0.2955202066613396`"}, {"0.4`", "0.3894183423086505`"}, {"0.5`", "0.479425538604203`"}, {"0.6000000000000001`", "0.5646424733950355`"}, {"0.7000000000000001`", "0.6442176872376911`"}, {"0.8`", "0.7173560908995228`"}, {"0.9`", "0.7833269096274834`"}, {"1.`", "0.8414709848078965`"} }, GridBoxAlignment->{ "Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}}, GridBoxSpacings->{"Columns" -> { Offset[0.27999999999999997`], { Offset[0.7]}, Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { Offset[0.2], { Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}}], Function[BoxForm`e$, TableForm[BoxForm`e$]]]], "Output"], Cell[TextData[{ "4) Does ", StyleBox["Mathematica", FontSlant->"Italic"], " have a built-in function for the cross product of two vectors in ", Cell[BoxData[ FormBox[ SuperscriptBox["R", "3"], TraditionalForm]]], "?" }], "Text"], Cell[TextData[{ "5) If ", StyleBox["M", "Input"], " and ", StyleBox["L", "Input"], " are matrices, is ", StyleBox["M+L", "Input"], " their sum and ", StyleBox["M.L", "Input"], " their product? " }], "Text"], Cell[TextData[{ "6) Create the first 11 rows of Pascal's triangle, i.e, a table that \ contains in its nth row the binomial coefficients ", Cell[BoxData[ FormBox[ RowBox[{"(", GridBox[{ {"n"}, {"k"} }], ")"}], TraditionalForm]]], ", k=0,...,n, where n= 0,...,10. ", StyleBox[" ", FontWeight->"Bold"], "Display the result in tabular format, as shown below. (Hint: ", StyleBox["Mathematica", FontSlant->"Italic"], " has a function that computes binomial coefficients. Look it up!)" }], "Text"], Cell[BoxData[ InterpretationBox[GridBox[{ {"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", "\<\"\"\>"}, {"1", "10", "45", "120", "210", "252", "210", "120", "45", "10", "1"} }, GridBoxAlignment->{ "Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}}, GridBoxSpacings->{"Columns" -> { Offset[0.27999999999999997`], { Offset[2.0999999999999996`]}, Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { Offset[0.2], { Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}}], TableForm[{{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}, {1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1}}]]], "Output"], Cell[TextData[{ "7) Can ", StyleBox["Total", "Input"], " be used to sum up a list of vectors?" }], "Text"], Cell[TextData[{ "8) Suppose that ", StyleBox["M", "Input"], " is a matrix of unknown size. What expression would produce a list of the \ row sums of ", StyleBox["M", "Input"], "? A row sum is just the sum of the entries in a row of the matrix, and \ there is one row sum for each row. What expression would produce a list of \ the column sums of ", StyleBox["M", "Input"], "?" }], "Text"], Cell["\<\ 9) Calculate the sum of 10000 random integers between 0 and 10. How close is \ your result to 50000?\ \>", "Text"] }, Open ]] }, Open ]] }, WindowSize->{735, 862}, WindowMargins->{{100, Automatic}, {Automatic, 0}}, FrontEndVersion->"6.0 for Mac OS X PowerPC (32-bit) (April 20, 2007)", StyleDefinitions->"Default.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[590, 23, 35, 0, 67, "Section"], Cell[628, 25, 354, 8, 56, "Text"], Cell[985, 35, 102, 2, 27, "Input"], Cell[1090, 39, 182, 4, 41, "Text"], Cell[1275, 45, 45, 0, 26, "Text"], Cell[1323, 47, 631, 19, 88, "Input"], Cell[1957, 68, 151, 3, 26, "Text"], Cell[2111, 73, 222, 8, 27, "Input"], Cell[2336, 83, 134, 3, 26, "Text"], Cell[2473, 88, 260, 6, 58, "Input"], Cell[CellGroupData[{ Cell[2758, 98, 47, 0, 34, "Subsection"], Cell[2808, 100, 163, 3, 41, "Text"], Cell[2974, 105, 215, 6, 43, "Input"], Cell[3192, 113, 59, 0, 26, "Text"], Cell[3254, 115, 49, 1, 27, "Input"], Cell[3306, 118, 49, 1, 27, "Input"], Cell[3358, 121, 49, 1, 27, "Input"], Cell[3410, 124, 49, 1, 27, "Input"], Cell[3462, 127, 44, 1, 27, "Input"], Cell[3509, 130, 49, 1, 27, "Input"], Cell[3561, 133, 49, 1, 27, "Input"], Cell[3613, 136, 49, 1, 27, "Input"], Cell[3665, 139, 49, 1, 27, "Input"], Cell[3717, 142, 49, 1, 27, "Input"], Cell[3769, 145, 49, 1, 27, "Input"], Cell[3821, 148, 56, 1, 27, "Input"], Cell[3880, 151, 69, 0, 26, "Text"], Cell[3952, 153, 59, 1, 27, "Input"], Cell[4014, 156, 160, 4, 27, "Input"], Cell[4177, 162, 228, 7, 27, "Input"], Cell[4408, 171, 244, 9, 26, "Text"], Cell[4655, 182, 58, 1, 27, "Input"], Cell[4716, 185, 127, 3, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[4880, 193, 42, 0, 34, "Subsection"], Cell[4925, 195, 1068, 39, 74, "Text"], Cell[5996, 236, 265, 7, 58, "Input"], Cell[CellGroupData[{ Cell[6286, 247, 203, 5, 27, "Input"], Cell[6492, 254, 29, 0, 27, "Output"] }, Open ]], Cell[6536, 257, 161, 5, 26, "Text"], Cell[6700, 264, 57, 1, 27, "Input"], Cell[6760, 267, 115, 3, 27, "Input"], Cell[6878, 272, 136, 5, 26, "Text"], Cell[7017, 279, 198, 7, 27, "Input"], Cell[7218, 288, 163, 5, 26, "Text"], Cell[7384, 295, 63, 1, 27, "Input"], Cell[7450, 298, 226, 5, 41, "Text"], Cell[7679, 305, 59, 1, 27, "Input"], Cell[7741, 308, 97, 4, 26, "Text"], Cell[7841, 314, 58, 1, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[7936, 320, 60, 1, 44, "Subsection"], Cell[7999, 323, 246, 11, 36, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[8282, 339, 27, 0, 34, "Subsection"], Cell[8312, 341, 161, 5, 26, "Text"], Cell[8476, 348, 59, 1, 27, "Input"], Cell[8538, 351, 82, 2, 27, "Input"], Cell[8623, 355, 92, 2, 27, "Input"], Cell[8718, 359, 137, 4, 27, "Input"], Cell[8858, 365, 204, 5, 41, "Text"], Cell[9065, 372, 159, 5, 27, "Input"], Cell[9227, 379, 49, 0, 26, "Text"], Cell[9279, 381, 60, 0, 26, "Text"], Cell[9342, 383, 92, 2, 27, "Input"], Cell[9437, 387, 95, 2, 26, "Text"], Cell[9535, 391, 123, 3, 27, "Input"], Cell[9661, 396, 98, 4, 26, "Text"], Cell[9762, 402, 81, 2, 27, "Input"], Cell[9846, 406, 267, 9, 38, "Text"], Cell[10116, 417, 190, 6, 27, "Input"], Cell[10309, 425, 50, 1, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[10396, 431, 60, 1, 44, "Subsection"], Cell[10459, 434, 468, 17, 39, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[10964, 456, 27, 0, 34, "Subsection"], Cell[10994, 458, 153, 4, 26, "Text"], Cell[11150, 464, 200, 6, 27, "Input"], Cell[11353, 472, 264, 8, 41, "Text"], Cell[11620, 482, 83, 4, 26, "Text"], Cell[11706, 488, 190, 6, 27, "Input"], Cell[11899, 496, 80, 4, 26, "Text"], Cell[11982, 502, 218, 7, 27, "Input"], Cell[12203, 511, 50, 0, 26, "Text"], Cell[12256, 513, 107, 3, 27, "Input"], Cell[12366, 518, 108, 3, 26, "Text"], Cell[12477, 523, 243, 8, 27, "Input"], Cell[12723, 533, 58, 1, 27, "Input"], Cell[12784, 536, 125, 3, 26, "Text"], Cell[12912, 541, 213, 7, 27, "Input"], Cell[13128, 550, 69, 0, 26, "Text"], Cell[13200, 552, 58, 1, 27, "Input"], Cell[13261, 555, 400, 12, 41, "Text"], Cell[13664, 569, 147, 3, 26, "Text"], Cell[13814, 574, 238, 8, 27, "Input"], Cell[14055, 584, 209, 6, 26, "Text"], Cell[14267, 592, 258, 8, 27, "Input"], Cell[14528, 602, 233, 7, 26, "Text"], Cell[14764, 611, 300, 10, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[15101, 626, 60, 1, 44, "Subsection"], Cell[15164, 629, 1068, 41, 48, "Text"], Cell[16235, 672, 173, 5, 26, "Text"], Cell[16411, 679, 106, 3, 27, "Input"], Cell[16520, 684, 154, 5, 26, "Text"], Cell[16677, 691, 116, 3, 27, "Input"], Cell[16796, 696, 154, 5, 27, "Input"], Cell[16953, 703, 531, 17, 44, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[17521, 725, 60, 1, 44, "Subsection"], Cell[17584, 728, 453, 15, 51, "Text"], Cell[18040, 745, 238, 6, 41, "Text"], Cell[18281, 753, 143, 6, 26, "Text"], Cell[18427, 761, 160, 5, 27, "Input"], Cell[18590, 768, 158, 6, 26, "Text"], Cell[18751, 776, 244, 8, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[19032, 789, 45, 0, 34, "Subsection"], Cell[19080, 791, 235, 6, 41, "Text"], Cell[19318, 799, 131, 4, 27, "Input"], Cell[19452, 805, 274, 9, 26, "Text"], Cell[19729, 816, 77, 2, 27, "Input"], Cell[19809, 820, 152, 3, 26, "Text"], Cell[19964, 825, 330, 11, 43, "Input"], Cell[20297, 838, 139, 6, 26, "Text"], Cell[20439, 846, 101, 3, 27, "Input"], Cell[20543, 851, 73, 0, 26, "Text"], Cell[20619, 853, 77, 2, 27, "Input"], Cell[20699, 857, 355, 9, 41, "Text"], Cell[21057, 868, 103, 3, 27, "Input"], Cell[21163, 873, 286, 7, 41, "Text"], Cell[21452, 882, 103, 3, 27, "Input"], Cell[21558, 887, 401, 12, 41, "Text"], Cell[21962, 901, 517, 18, 43, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[22516, 924, 60, 1, 44, "Subsection"], Cell[22579, 927, 138, 6, 26, "Text"], Cell[22720, 935, 458, 16, 41, "Text"], Cell[23181, 953, 266, 9, 36, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[23484, 967, 30, 0, 34, "Subsection"], Cell[23517, 969, 73, 0, 26, "Text"], Cell[23593, 971, 357, 10, 30, "Output"], Cell[23953, 983, 409, 15, 36, "Text"], Cell[24365, 1000, 116, 3, 26, "Text"], Cell[24484, 1005, 922, 25, 189, "Output"], Cell[25409, 1032, 241, 9, 29, "Text"], Cell[25653, 1043, 215, 10, 26, "Text"], Cell[25871, 1055, 524, 16, 75, "Text"], Cell[26398, 1073, 1898, 40, 189, "Output"], Cell[28299, 1115, 109, 4, 26, "Text"], Cell[28411, 1121, 395, 11, 56, "Text"], Cell[28809, 1134, 124, 3, 26, "Text"] }, Open ]] }, Open ]] } ] *) (* End of internal cache information *)