(* Content-type: application/vnd.wolfram.mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 11.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 158, 7] NotebookDataLength[ 32645, 919] NotebookOptionsPosition[ 29674, 822] NotebookOutlinePosition[ 30031, 838] CellTagsIndexPosition[ 29988, 835] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["Matrices and Linear Algebra", "Section", CellChangeTimes->{{3.618749138518262*^9, 3.618749147133906*^9}}], Cell[TextData[{ "The ", StyleBox["Mathematica", FontSlant->"Italic"], " front end provides an Insert->Table/Matrix submenu for creating and \ editing arrays with any specified number of rows and columns. Once you have \ such an array, you can edit it to fill in whatever elements you want." }], "Text", CellChangeTimes->{{3.6187492587709017`*^9, 3.618749273529153*^9}, { 3.618749823821348*^9, 3.6187498371461906`*^9}}], Cell[CellGroupData[{ Cell["Some examples of vectors and matrices:", "Subsection", CellChangeTimes->{{3.61875170027941*^9, 3.618751710687172*^9}, { 3.61875183319631*^9, 3.6187518350739613`*^9}, {3.618752888639907*^9, 3.6187528907837*^9}, {3.618753948983429*^9, 3.618753953572925*^9}}], Cell[TextData[{ "Mathematica treats a matrix like a list of lists. To see it in standard \ matrix form, use the command ", StyleBox["MatrixForm[..]", FontWeight->"Bold"] }], "Text", CellChangeTimes->{{3.618750014907218*^9, 3.618750038266217*^9}, { 3.618880378095418*^9, 3.618880403395008*^9}, {3.6496420997533703`*^9, 3.649642117154052*^9}, {3.649642173862206*^9, 3.649642177836809*^9}}], Cell[BoxData[ RowBox[{"A", "=", RowBox[{"(", "\[NoBreak]", GridBox[{ {"1", "0", "0"}, {"1", "1", "1"} }], "\[NoBreak]", ")"}]}]], "Input", CellChangeTimes->{{3.6187499445038223`*^9, 3.618749963982069*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[{ RowBox[{"IdentityMatrix", "[", "3", "]"}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", "%", "]"}]}], "Input", CellChangeTimes->{{3.6187539895034523`*^9, 3.618754030712672*^9}, { 3.6496405318701353`*^9, 3.649640533077649*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell["\<\ A vector can be given as a list. Column vectors and row vectors are \ represented identically.\ \>", "Text", CellChangeTimes->{{3.649642287955686*^9, 3.6496422980729847`*^9}, 3.6496423848725567`*^9, {3.6950336107773457`*^9, 3.6950336112875767`*^9}}], Cell[BoxData[{ RowBox[{"w", "=", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", "w", "]"}]}], "Input", CellChangeTimes->{{3.6496422207367773`*^9, 3.649642237914913*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[TextData[{ "We can also use the Insert->Table/Matrix submenu to create a column vector \ with a given number of elements, as a matrix with n rows and one column", Cell[BoxData[ RowBox[{"(", "\[NoBreak]", GridBox[{ {"\[Placeholder]"}, {"\[Placeholder]"}, {"\[Placeholder]"} }], "\[NoBreak]", ")"}]]], ". If we use this template, the vector is interpreted as a list of lists." }], "Text", CellChangeTimes->{{3.6187518975616302`*^9, 3.618751903999228*^9}, { 3.618751935982326*^9, 3.618751998667486*^9}, 3.6187520387084*^9, { 3.6187521213027973`*^9, 3.618752123614378*^9}, {3.618752857947176*^9, 3.618752867936822*^9}, {3.6188807043141403`*^9, 3.618880726814105*^9}, { 3.6188808359477987`*^9, 3.6188808763599167`*^9}, {3.649642337981893*^9, 3.6496423453169537`*^9}}], Cell[BoxData[ RowBox[{"v", "=", RowBox[{"(", "\[NoBreak]", GridBox[{ {"1"}, {"2"}, {"3"} }], "\[NoBreak]", ")"}]}]], "Input", CellChangeTimes->{{3.618752020694131*^9, 3.618752050554582*^9}}, Background->RGBColor[0.87, 0.94, 1]] }, Open ]], Cell[CellGroupData[{ Cell["The inverse of a matrix", "Subsection", CellChangeTimes->{{3.6889252291403837`*^9, 3.688925249677278*^9}, { 3.689788503520482*^9, 3.689788503936264*^9}, {3.6897910849305058`*^9, 3.68979108549016*^9}}], Cell[TextData[{ StyleBox["Inverse[A]", FontWeight->"Bold"], " computes the inverse of an nxn square matrix A, if the matrix is \ invertible, and displays an error message otherwise. A matrix whose \ determinant is different from 0 is invertible. Its inverse is the unique nxn \ square matrix B wih the property that AB = BA = IdentityMatrix[n]." }], "Text", CellChangeTimes->{{3.688924935972577*^9, 3.688924943442913*^9}, { 3.688925168404255*^9, 3.688925203282329*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"M", "=", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "2"}], "}"}], ",", RowBox[{"{", RowBox[{"3", ",", "4"}], "}"}]}], "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"Inverse", "[", "M", "]"}], "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{ RowBox[{ RowBox[{"MatrixForm", "[", "M", "]"}], RowBox[{"MatrixForm", "[", RowBox[{"Inverse", "[", "M", "]"}], "]"}]}], ",", "\"\<=\>\"", ",", RowBox[{ RowBox[{"MatrixForm", "[", RowBox[{"Inverse", "[", "M", "]"}], "]"}], RowBox[{"MatrixForm", "[", "M", "]"}]}], ",", "\"\<=\>\"", ",", RowBox[{"MatrixForm", "[", RowBox[{"IdentityMatrix", "[", "2", "]"}], "]"}]}], "]"}]}], "Input", CellChangeTimes->{{3.688924966349895*^9, 3.688925150076127*^9}}, Background->RGBColor[0.87, 0.94, 1]] }, Open ]], Cell[CellGroupData[{ Cell["Matrix and Vector Operations", "Subsection", CellChangeTimes->{{3.618751105193527*^9, 3.6187511372141237`*^9}, { 3.618751673706723*^9, 3.618751676336491*^9}}], Cell[CellGroupData[{ Cell[TextData[ButtonBox["https : // \ reference.wolfram.com/language/guide/MatrixOperations.html", BaseStyle->"Hyperlink", ButtonData->{ FrontEnd`FileName[{ "https : ", "", " reference.wolfram.com", "language", "guide"}, "MatrixOperations.html", CharacterEncoding -> "UTF-8"], None}]], "Subsubsection", CellChangeTimes->{{3.649642082462585*^9, 3.6496420824637136`*^9}, { 3.649642427850439*^9, 3.649642444110654*^9}}], Cell["\<\ We can perform matrix addition A+B and scalar multiplication 3A in the usual \ way. However, to do matrix multiplication we cannot use the symbol * , \ instead we have to use a dot! If two matrices A and B cannot be multiplied \ together (because of incompatible sizes), you will receive an error message \ like \[OpenCurlyDoubleQuote]Tensors A and B have incompatible shapes\ \[CloseCurlyDoubleQuote].\ \>", "Text", CellChangeTimes->{{3.618751330382312*^9, 3.61875136816485*^9}, { 3.6187535600369043`*^9, 3.618753581644103*^9}, 3.649642474513891*^9, { 3.695033862144256*^9, 3.6950339044576607`*^9}, {3.695033935149412*^9, 3.695033966478888*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"A", "=", RowBox[{"(", "\[NoBreak]", GridBox[{ {"1", "0", "0"}, {"1", "1", "1"} }], "\[NoBreak]", ")"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"A", "+", "A"}], ";", " ", RowBox[{"MatrixForm", "[", "%", "]"}]}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", RowBox[{"3", "A"}], "]"}], "\[IndentingNewLine]", RowBox[{"A", ".", "A"}], "\[IndentingNewLine]", RowBox[{"A", ".", RowBox[{"Transpose", "[", "A", "]"}]}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", "%", "]"}]}], "Input", CellChangeTimes->{{3.649642521585968*^9, 3.6496425534509363`*^9}, { 3.6496426453350077`*^9, 3.649642655935836*^9}, {3.649642726948922*^9, 3.649642766027976*^9}, {3.649642828247629*^9, 3.649642828925767*^9}, { 3.695033783094488*^9, 3.6950337873349953`*^9}, {3.695033909403459*^9, 3.695033924106576*^9}, {3.6950398768209248`*^9, 3.6950398779545727`*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[{ RowBox[{"A", ".", "w"}], "\[IndentingNewLine]", RowBox[{"A", ".", "v"}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", RowBox[{"A", ".", "w"}], "]"}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", RowBox[{"A", ".", "v"}], "]"}]}], "Input", CellChangeTimes->{{3.649642876244841*^9, 3.649642891581328*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[TextData[{ StyleBox["Transpose[matrix]", FontWeight->"Bold", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" ", FontColor->RGBColor[1, 0.5, 0]], "-- Computes the transpose\n", StyleBox["MatrixPower[matrix, power]", FontWeight->"Bold", FontColor->RGBColor[0.5, 0, 0.5]], " -- For square matrices, we can compute the power of a matrix ", Cell[BoxData[ FormBox[ SuperscriptBox["A", "n"], TraditionalForm]]], " \n", StyleBox["Eigenvalues[matrix]", FontWeight->"Bold", FontColor->RGBColor[0.5, 0, 0.5]], " -- Finds the eigenvalues of a square matrix\n", StyleBox["Eigensystem[matrix]", FontWeight->"Bold", FontColor->RGBColor[0.5, 0, 0.5]], " -- Finds finds both the eigenvalues and a complete linearly independent \ set of eigenvectors for each eigenvalue.\n", StyleBox["Eigenvectors[matrix]", FontWeight->"Bold", FontColor->RGBColor[0.5, 0, 0.5]], " -- Finds the eigenvectors of a square matrix\n", StyleBox["Inverse[matrix]", FontWeight->"Bold", FontColor->RGBColor[0.5, 0, 0.5]], " -- Finds the inverse of a square matrix" }], "Text", CellChangeTimes->{{3.6187536227169323`*^9, 3.61875366160083*^9}, { 3.6187537033656054`*^9, 3.6187537327897778`*^9}, {3.649642925992427*^9, 3.649642932934637*^9}, {3.64964301564824*^9, 3.6496431419929*^9}, { 3.6496432824954576`*^9, 3.649643301077739*^9}, {3.650077961935432*^9, 3.650077979079103*^9}}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Linear Algebra - Eigenvectors and Eigenvalues", "Subsection", CellChangeTimes->{{3.688924476117079*^9, 3.6889244969411507`*^9}, { 3.688925266268701*^9, 3.688925274747867*^9}}], Cell[TextData[{ "The function ", StyleBox["Eigenvalues[A]", FontWeight->"Bold"], " returns a list with the eigenvalues of the square matrix A. Recall that a \ number \[Lambda] is called an eigenvalue of A if there exists a nonzero \ vector v such that Av=\[Lambda]v. The function ", StyleBox["Eigensystem[A]", FontWeight->"Bold"], " finds both the eigenvalues and a complete linearly independent set of \ eigenvectors for each eigenvalue. The output is given in the form ", StyleBox["{list of eigenvalues, list of eigenvectors}", FontWeight->"Bold"], ". If the geometric multiplicity of an eigenvalue is less than its algebraic \ multiplicity, then the list of eigenvectors is completed with 0 vectors. " }], "Text", CellChangeTimes->{{3.688924083925561*^9, 3.688924196719234*^9}, { 3.688924700008912*^9, 3.6889247007756233`*^9}, 3.6889256525488987`*^9, { 3.6898195648444357`*^9, 3.6898195691235733`*^9}, {3.6950369001385593`*^9, 3.6950370153921022`*^9}}, TextJustification->0.75], Cell[TextData[{ "Consider an nxn matrix A. The Jordan Decomposition Theorem says there \ exists an invertible matrix S such that ", Cell[BoxData[ FormBox[ RowBox[{ RowBox[{ SuperscriptBox["S", RowBox[{"-", "1"}]], "A", " ", "S"}], "=", "J"}], TraditionalForm]]], " , where J is called the Jordan canonical form of A (J is an upper \ triangular matrix whose diagonal entries are the eigenvalues of A, and \ immediately above the diagonal some entries are equal to 1). The function ", StyleBox["JordanDecomposition[A]", FontWeight->"Bold"], " returns a list of the form {S, J}, where J is the Jordan canonical form of \ A. S is one choice of matrix satisfying ", Cell[BoxData[ FormBox[ RowBox[{ RowBox[{ SuperscriptBox["S", RowBox[{"-", "1"}]], "A", " ", "S"}], "=", "J"}], TraditionalForm]]], ", although it\[CloseCurlyQuote]s not the usually the one that we would get \ when doing the computations by hand." }], "Text", CellChangeTimes->{{3.688927205253468*^9, 3.6889272836828117`*^9}, { 3.688927383371907*^9, 3.688927502237399*^9}, 3.688927543247259*^9, { 3.688927688499289*^9, 3.688927690849292*^9}, {3.688927927852591*^9, 3.688927962677896*^9}, {3.688927996794561*^9, 3.688928267365918*^9}, { 3.689789922044322*^9, 3.68978995092348*^9}, {3.689790174130645*^9, 3.689790289638935*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"M", " ", "=", " ", TagBox[ RowBox[{"(", GridBox[{ {"2", "0", "1"}, {"0", "1", RowBox[{"-", "1"}]}, { RowBox[{"-", "1"}], "0", "0"} }, GridBoxAlignment->{ "Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}, "Items" -> {}, "ItemsIndexed" -> {}}, GridBoxSpacings->{"Columns" -> { Offset[0.27999999999999997`], { Offset[0.7]}, Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { Offset[0.2], { Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}, "Items" -> {}, "ItemsIndexed" -> {}}], ")"}], Function[BoxForm`e$, MatrixForm[BoxForm`e$]]]}], ";"}], "\[IndentingNewLine]", RowBox[{"Eigenvalues", "[", "M", "]"}], "\[IndentingNewLine]", RowBox[{"Eigensystem", "[", "M", "]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"S", ",", "J"}], "}"}], "=", RowBox[{"JordanDecomposition", "[", "M", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"MatrixForm", "[", "J", "]"}]}], "]"}], "\[IndentingNewLine]", RowBox[{"Print", "[", "\"\\"", "]"}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", RowBox[{ RowBox[{"Inverse", "[", "S", "]"}], ".", "M", ".", "S"}], "]"}]}], "Input",\ CellChangeTimes->{{3.695034271462816*^9, 3.695034501220285*^9}, 3.695034539080099*^9}, Background->RGBColor[0.87, 0.94, 1]], Cell[CellGroupData[{ Cell["\<\ Exercise 1 : Find the eigenvalues and eigenvectors of matrix B.\ \>", "Subsubsection", CellChangeTimes->{{3.649643314664514*^9, 3.649643333031528*^9}, { 3.649643559217512*^9, 3.649643572173437*^9}, {3.695034248518467*^9, 3.695034248685399*^9}}], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"B", "=", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "1"}], "}"}]}], "}"}]}], ";"}], " "}]], "Input", CellChangeTimes->{{3.618753664502377*^9, 3.618753688392681*^9}, { 3.618753744938005*^9, 3.618753768477441*^9}, {3.649642980671962*^9, 3.6496429835018053`*^9}, {3.6496431535647182`*^9, 3.649643153682136*^9}, { 3.650078008474262*^9, 3.650078010490074*^9}}, Background->RGBColor[0.87, 0.94, 1]] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Changing Elements", "Section", CellChangeTimes->{{3.618753863360368*^9, 3.618753874392268*^9}}], Cell[CellGroupData[{ Cell["To access different matrix or vector elements use [[..]]", "Subsection", CellChangeTimes->{{3.6187534570589943`*^9, 3.618753488663953*^9}, { 3.6187550295012913`*^9, 3.61875503161803*^9}, {3.649643677456523*^9, 3.6496436834161873`*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"w", "=", RowBox[{"{", RowBox[{"10", ",", "20", ",", "30"}], "}"}]}], ";", " ", RowBox[{"w", "[", RowBox[{"[", "1", "]"}], "]"}]}]], "Input", CellChangeTimes->{{3.618753503661138*^9, 3.618753531599218*^9}, { 3.618753884281281*^9, 3.618753908055534*^9}, {3.6496437961934967`*^9, 3.649643800785715*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[{ RowBox[{ RowBox[{"B", "=", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"10", ",", "20"}], "}"}], ",", RowBox[{"{", RowBox[{"30", ",", "40"}], "}"}]}], "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"B", "[", RowBox[{"[", "1", "]"}], "]"}]}], "Input", CellChangeTimes->{ 3.649643745573472*^9, {3.649643786897085*^9, 3.649643792985208*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[TextData[{ "To access the element on row i and column j of matrix ", StyleBox["B", FontWeight->"Bold"], ", we use ", StyleBox["B[[i, j]]", FontWeight->"Bold"], " or ", StyleBox["B[[ i ]] [[ j ]]", FontWeight->"Bold"], ". Remember to always use double brackets, as simple brackets are reserved \ for functions." }], "Text", CellChangeTimes->{{3.695034616691209*^9, 3.6950346882542753`*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"B", "[", RowBox[{"[", "1", "]"}], "]"}], "[", RowBox[{"[", "1", "]"}], "]"}], "\[IndentingNewLine]", RowBox[{"B", "[", RowBox[{"[", RowBox[{"1", ",", "1"}], "]"}], "]"}]}], "Input", CellChangeTimes->{{3.618755137003212*^9, 3.6187551439590893`*^9}, { 3.64964375128965*^9, 3.649643781705182*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell["To change elements of a matrix or vector:", "Text", CellChangeTimes->{{3.618755058569146*^9, 3.618755076832057*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{"B", "[", RowBox[{"[", RowBox[{"2", ",", "1"}], "]"}], "]"}], "=", "2017"}], ";"}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", "B", "]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"w", "[", RowBox[{"[", "1", "]"}], "]"}], "=", "2017"}], ";", " ", "w"}]}], "Input",\ CellChangeTimes->{{3.688932501907317*^9, 3.688932521462216*^9}, { 3.6950347799233017`*^9, 3.6950348159608297`*^9}}, Background->RGBColor[0.87, 0.94, 1]] }, Open ]], Cell[CellGroupData[{ Cell["A lot of vector operations are already built-in", "Subsection", CellChangeTimes->{{3.695035103847197*^9, 3.6950351453008432`*^9}}], Cell[TextData[{ StyleBox["Length[..]", FontWeight->"Bold", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" ", FontWeight->"Bold"], "-- returns the number of elements in a list.\n", StyleBox["Sum[..]", FontWeight->"Bold", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" ", FontWeight->"Bold"], "-- returns the sum of elements in a list.\n", StyleBox["Product[..]", FontWeight->"Bold", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" ", FontWeight->"Bold"], "-- returns the product of elements in a list." }], "Text", CellChangeTimes->{{3.6187552336521883`*^9, 3.6187552387535973`*^9}, 3.61875686664825*^9, {3.649643902650455*^9, 3.649643929617958*^9}, { 3.649643979715928*^9, 3.6496439922847013`*^9}, {3.64964406921894*^9, 3.649644076794561*^9}, {3.6950351542853537`*^9, 3.695035181902719*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"w", "=", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"w", "[", RowBox[{"[", "1", "]"}], "]"}], "+", RowBox[{"w", "[", RowBox[{"[", "2", "]"}], "]"}], "+", RowBox[{"w", "[", RowBox[{"[", "3", "]"}], "]"}]}], "\[IndentingNewLine]", RowBox[{"Sum", "[", RowBox[{ RowBox[{"w", "[", RowBox[{"[", "i", "]"}], "]"}], ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", RowBox[{"Length", "[", "w", "]"}]}], "}"}]}], "]"}]}], "Input", CellChangeTimes->{{3.650078067976989*^9, 3.650078085511833*^9}, { 3.6950351977807713`*^9, 3.695035213756147*^9}, {3.6950354670033627`*^9, 3.6950355024511347`*^9}, {3.695037804718491*^9, 3.69503780582067*^9}}, Background->RGBColor[0.87, 0.94, 1]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Loops and Vectors/Matrices", "Section", CellChangeTimes->{{3.650107425736381*^9, 3.650107449599822*^9}, { 3.695037232559629*^9, 3.695037243887607*^9}, 3.695037813964571*^9}], Cell[CellGroupData[{ Cell["Find the sum of the elements of a square matrix.", "Subsubsection", CellChangeTimes->{{3.619165941295232*^9, 3.619165971262051*^9}, { 3.650108155927075*^9, 3.650108157021879*^9}, {3.6950372542629423`*^9, 3.6950372557749033`*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"A", "=", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}], ",", RowBox[{"{", RowBox[{"4", ",", "5", ",", "6"}], "}"}], ",", RowBox[{"{", RowBox[{"7", ",", "8", ",", "9"}], "}"}]}], "}"}]}], ";"}]], "Input", CellChangeTimes->{{3.650078550736923*^9, 3.6500785626292963`*^9}}, FontSize->14, Background->RGBColor[0.87, 0.94, 1]], Cell["We can use the built - in command Sum[ ..]", "ItemNumbered", CellChangeTimes->{{3.695037282136385*^9, 3.695037309032648*^9}}], Cell[BoxData[ RowBox[{"Sum", "[", RowBox[{ RowBox[{ RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}], "[", RowBox[{"[", "j", "]"}], "]"}], ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", RowBox[{"Length", "[", "A", "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", "1", ",", RowBox[{"Length", "[", "A", "]"}]}], "}"}]}], "]"}]], "Input", CellChangeTimes->{{3.650078576034027*^9, 3.650078611693968*^9}}, FontSize->14, Background->RGBColor[0.87, 0.94, 1]], Cell["\<\ We can use the Do[...] loop. The variable sumElements is initially set to 0. \ We then add each (i,j) element of the matrix A to the variable sumElements.\ \>", "ItemNumbered", CellChangeTimes->{{3.695037282136385*^9, 3.6950373389374647`*^9}, { 3.695037512425418*^9, 3.695037588134479*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"sumElements", "=", "0"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Do", "[", RowBox[{ RowBox[{"sumElements", "=", RowBox[{"sumElements", "+", RowBox[{ RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}], "[", RowBox[{"[", "j", "]"}], "]"}]}]}], ",", " ", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"i", ",", "1", ",", RowBox[{"Length", "[", "A", "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", "1", ",", RowBox[{"Length", "[", "A", "]"}]}], "}"}]}], " ", "]"}], ";"}], "\[IndentingNewLine]", RowBox[{"Print", "[", "sumElements", "]"}]}], "Input", CellChangeTimes->{{3.619166523580942*^9, 3.619166594177628*^9}, { 3.61916707369309*^9, 3.6191670971640263`*^9}, {3.650108189617193*^9, 3.650108222476372*^9}, 3.695037595016295*^9}, FontSize->16, Background->RGBColor[0.87, 0.94, 1]], Cell["\<\ We can use two For[...] loops. The second For loop is highlighted in \ LightYellow. \ \>", "ItemNumbered", CellChangeTimes->{{3.695037282136385*^9, 3.6950373389374647`*^9}, { 3.695037369602357*^9, 3.69503740573143*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"sumElements", " ", "=", "0"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "1"}], ",", RowBox[{"i", "\[LessEqual]", RowBox[{"Length", "[", "A", "]"}]}], ",", RowBox[{"i", "++"}], ",", " ", StyleBox[ RowBox[{"For", "[", RowBox[{ RowBox[{"j", "=", "1"}], ",", RowBox[{"j", "\[LessEqual]", RowBox[{"Length", "[", "A", "]"}]}], ",", RowBox[{"j", "++"}], ",", RowBox[{"sumElements", "=", RowBox[{"sumElements", " ", "+", " ", RowBox[{ RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}], "[", RowBox[{"[", "j", "]"}], "]"}]}]}]}], " ", "]"}], FontColor->GrayLevel[0], Background->RGBColor[1, 1, 0.85]]}], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{"Print", "[", "sumElements", "]"}]}], "Input", CellChangeTimes->{{3.619165978182548*^9, 3.619166248174325*^9}, { 3.619167060101962*^9, 3.619167064786715*^9}, 3.619167099521508*^9, { 3.65007856569189*^9, 3.650078569869108*^9}, {3.650083193236937*^9, 3.6500832158036127`*^9}, {3.6501081617990923`*^9, 3.65010817928088*^9}, 3.695037597640835*^9}, FontSize->16, Background->RGBColor[0.87, 0.94, 1]], Cell["\<\ Similarly, we can use two While[...] loops. The second While loop is \ highlighted in LightYellow. \ \>", "ItemNumbered", CellChangeTimes->{{3.695037282136385*^9, 3.6950373389374647`*^9}, { 3.695037369602357*^9, 3.69503744815674*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"sumElements", " ", "=", "0"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"i", "=", "1"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"While", "[", RowBox[{ RowBox[{"i", "\[LessEqual]", RowBox[{"Length", "[", "A", "]"}]}], ",", " ", RowBox[{ RowBox[{"j", "=", "1"}], ";", " ", StyleBox[ RowBox[{"While", "[", RowBox[{ RowBox[{"j", "\[LessEqual]", RowBox[{"Length", "[", "A", "]"}]}], ",", " ", RowBox[{ RowBox[{"sumElements", "=", RowBox[{"sumElements", "+", RowBox[{ RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}], "[", RowBox[{"[", "j", "]"}], "]"}]}]}], ";", RowBox[{"j", "++"}]}]}], " ", "]"}], FontColor->RGBColor[0, 0, 1], Background->RGBColor[1, 1, 0.85]], StyleBox[";", Background->RGBColor[1, 1, 0.85]], StyleBox["\[IndentingNewLine]", Background->RGBColor[1, 1, 0.85]], RowBox[{"i", "++"}]}]}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{"Print", "[", "sumElements", "]"}]}], "Input", CellChangeTimes->{{3.619166650234419*^9, 3.619166851845337*^9}, { 3.619166924443342*^9, 3.6191669360083323`*^9}, {3.6191670395253487`*^9, 3.6191670443872833`*^9}, {3.650083334664687*^9, 3.650083341286989*^9}, { 3.695037467979648*^9, 3.695037480489155*^9}, 3.695037601680251*^9}, FontSize->16, Background->RGBColor[0.87, 0.94, 1]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Other ways of generating a matrix:", "Section", CellChangeTimes->{{3.618756906318632*^9, 3.61875691615844*^9}, { 3.618757000413563*^9, 3.6187570015222816`*^9}}], Cell[CellGroupData[{ Cell[TextData[{ StyleBox["Table[f, {i,m}, {j,n}]", FontColor->RGBColor[0.5, 0, 0.5]], " builds an mxn matrix by evaluating the function f with arguments i and j, \ where i ranges from 1 to m and j ranges from 1 to n. The lower bound is \ implicitly 1, so we only specify the upper bound." }], "Subsubsection", CellChangeTimes->{{3.6187570131620607`*^9, 3.618757099206767*^9}, { 3.618881531407371*^9, 3.6188815921307507`*^9}}], Cell["\<\ The following commands generate random 4x4 matrix with entries between 0 and \ 1:\ \>", "Text", CellChangeTimes->{{3.618757160740077*^9, 3.618757184371043*^9}, { 3.649644681371842*^9, 3.649644707892558*^9}}], Cell[BoxData[ RowBox[{"Rn", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Random", "[", "]"}], ",", RowBox[{"{", RowBox[{"i", ",", "4"}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", "4"}], "}"}]}], "]"}]}]], "Input", CellChangeTimes->{{3.618757130703257*^9, 3.6187571392297688`*^9}, { 3.618757211563464*^9, 3.618757222810998*^9}, {3.695035004316843*^9, 3.695035027004259*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[ RowBox[{"Ri", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"RandomInteger", "[", "]"}], ",", RowBox[{"{", "4", "}"}], ",", RowBox[{"{", "4", "}"}]}], "]"}]}]], "Input", CellChangeTimes->{{3.618757634754353*^9, 3.618757654120116*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell["\<\ We may also want to define our own function f, and use it to define a matrix \ with a given pattern. For example, the code below generates a matrix whose \ (i,j)-entry is equal to i+j.\ \>", "Text", CellChangeTimes->{{3.61875724873071*^9, 3.618757268087577*^9}, { 3.6187573499746037`*^9, 3.6187573503725224`*^9}, {3.6188816757672243`*^9, 3.618881726229151*^9}, 3.6188818137477407`*^9, {3.649645737239935*^9, 3.649645740749114*^9}, 3.6496460108512487`*^9, {3.6950331427002993`*^9, 3.695033205084332*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"f", "[", RowBox[{"i_", ",", "j_"}], "]"}], ":=", RowBox[{"i", "+", "j"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{"NewM", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"f", "[", RowBox[{"i", ",", "j"}], "]"}], ",", RowBox[{"{", RowBox[{"i", ",", "4"}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", "4"}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", "NewM", "]"}]}], "Input", CellChangeTimes->{{3.618757270156877*^9, 3.618757325612507*^9}, { 3.695033211989931*^9, 3.695033212917687*^9}}, Background->RGBColor[0.87, 0.94, 1]] }, Open ]], Cell["\<\ Exercise 2: Generate a 7x7 matrix B with Bij=i if i>=j and Bij=0 otherwise\ \>", "Subsubsection", CellChangeTimes->{{3.618757843280552*^9, 3.6187579287409897`*^9}, 3.618758315183332*^9, {3.618881986290406*^9, 3.6188820109774313`*^9}, { 3.6496443479128017`*^9, 3.649644348175686*^9}, {3.649644664179162*^9, 3.649644664393923*^9}, {3.69503493290042*^9, 3.695034936058178*^9}, { 3.69503528102188*^9, 3.695035281157961*^9}, {3.695037117604076*^9, 3.695037117713925*^9}, {3.695038156592037*^9, 3.695038164967472*^9}}], Cell["\<\ Exercise 3: Find the sum of the elements on the 5th row of the matrix B that \ you have generated in exercise 2. Use your favorite loop (Do, While, For).\ \>", "Subsubsection", CellChangeTimes->{{3.618757843280552*^9, 3.6187579287409897`*^9}, 3.618758315183332*^9, {3.618881986290406*^9, 3.6188820109774313`*^9}, { 3.6496443479128017`*^9, 3.649644348175686*^9}, {3.649644664179162*^9, 3.649644664393923*^9}, {3.69503493290042*^9, 3.695034936058178*^9}, { 3.69503528102188*^9, 3.695035281157961*^9}, {3.695037117604076*^9, 3.695037117713925*^9}, {3.695037651083001*^9, 3.6950377454883347`*^9}, { 3.6950381683828087`*^9, 3.695038169342659*^9}}] }, Open ]] }, WindowSize->{808, 606}, WindowMargins->{{4, Automatic}, {Automatic, 4}}, FrontEndVersion->"11.0 for Mac OS X x86 (32-bit, 64-bit Kernel) (September \ 21, 2016)", StyleDefinitions->"Default.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[580, 22, 112, 1, 64, "Section"], Cell[695, 25, 426, 9, 68, "Text"], Cell[CellGroupData[{ Cell[1146, 38, 268, 3, 44, "Subsection"], Cell[1417, 43, 397, 8, 49, "Text"], Cell[1817, 53, 262, 7, 63, "Input"], Cell[2082, 62, 290, 5, 70, "Input"], Cell[2375, 69, 264, 5, 30, "Text"], Cell[2642, 76, 278, 6, 70, "Input"], Cell[2923, 84, 809, 16, 86, "Text"], Cell[3735, 102, 252, 8, 79, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[4024, 115, 212, 3, 44, "Subsection"], Cell[4239, 120, 476, 9, 68, "Text"], Cell[4718, 131, 866, 23, 112, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[5621, 159, 167, 2, 44, "Subsection"], Cell[CellGroupData[{ Cell[5813, 165, 437, 9, 35, "Subsubsection"], Cell[6253, 176, 667, 11, 87, "Text"], Cell[6923, 189, 979, 21, 170, "Input"], Cell[7905, 212, 379, 8, 112, "Input"], Cell[8287, 222, 1394, 37, 145, "Text"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[9730, 265, 184, 2, 44, "Subsection"], Cell[9917, 269, 1006, 20, 106, "Text"], Cell[10926, 291, 1355, 30, 127, "Text"], Cell[12284, 323, 1727, 45, 234, "Input"], Cell[CellGroupData[{ Cell[14036, 372, 260, 5, 35, "Subsubsection"], Cell[14299, 379, 548, 14, 48, "Input"] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[14908, 400, 102, 1, 64, "Section"], Cell[CellGroupData[{ Cell[15035, 405, 247, 3, 44, "Subsection"], Cell[15285, 410, 395, 10, 48, "Input"], Cell[15683, 422, 438, 14, 70, "Input"], Cell[16124, 438, 409, 13, 49, "Text"], Cell[16536, 453, 387, 10, 70, "Input"], Cell[16926, 465, 123, 1, 30, "Text"], Cell[17052, 468, 513, 15, 91, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[17602, 488, 137, 1, 44, "Subsection"], Cell[17742, 491, 826, 23, 68, "Text"], Cell[18571, 516, 829, 23, 91, "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[19449, 545, 183, 2, 64, "Section"], Cell[CellGroupData[{ Cell[19657, 551, 241, 3, 35, "Subsubsection"], Cell[19901, 556, 436, 13, 50, "Input"], Cell[20340, 571, 132, 1, 30, "ItemNumbered"], Cell[20475, 574, 508, 15, 50, "Input"], Cell[20986, 591, 303, 5, 49, "ItemNumbered"], Cell[21292, 598, 923, 24, 131, "Input"], Cell[22218, 624, 232, 5, 30, "ItemNumbered"], Cell[22453, 631, 1333, 34, 157, "Input"], Cell[23789, 667, 247, 5, 30, "ItemNumbered"], Cell[24039, 674, 1489, 38, 183, "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[25577, 718, 169, 2, 64, "Section"], Cell[CellGroupData[{ Cell[25771, 724, 432, 8, 83, "Subsubsection"], Cell[26206, 734, 220, 5, 30, "Text"], Cell[26429, 741, 452, 12, 48, "Input"], Cell[26884, 755, 307, 8, 48, "Input"], Cell[27194, 765, 529, 9, 49, "Text"], Cell[27726, 776, 657, 18, 91, "Input"] }, Open ]], Cell[28398, 797, 583, 10, 83, "Subsubsection"], Cell[28984, 809, 674, 10, 53, "Subsubsection"] }, Open ]] } ] *)