(* 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[ 33792, 853] NotebookOptionsPosition[ 32564, 811] NotebookOutlinePosition[ 32981, 827] CellTagsIndexPosition[ 32938, 824] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["\<\ Lecture 10 : Exact vs. Inexact Arithmetic, Packages and Namespaces \ \>", "Title", CellChangeTimes->{{3.4369039793112497`*^9, 3.43690403272565*^9}, { 3.4369040716632495`*^9, 3.4369041001488495`*^9}, 3.4369053825594497`*^9, { 3.4369060965558496`*^9, 3.43690611713225*^9}, 3.4371044159088526`*^9}], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " tracks the precision (number of significant digits) of the numerical \ quantities being manipulated. Typically, with each operation, the precision \ of the result is less than that of the input, so when the precision of the \ output is important, you should use as high precision input as possible. One \ exception is that of machine precision-- any calculations done with machine \ precision return results in machine precision, regardless of whether or not \ ", StyleBox["Mathematica", FontSlant->"Italic"], " could prove that the result indeed is of machine precision. That is, for \ computational efficiency, ", StyleBox["Mathematica", FontSlant->"Italic"], " doesn't track the precision of calculations 'contaminated' with machine \ precision quantities. \n\nEnter a number with a given precision either by \ enumerating all the significant digits, or using the numbermark notation. The \ precision of numbers can be checked with the Precision command, and set with \ SetPrecision (zeros are inserted as needed). One can also use N to evaluate \ an expression to a desired precision, and Rationalize, Round, Ceiling, Floor \ and other such functions to convert expressions to infinite precision. \ $MachinePrecision (and MachinePrecision) store the number of digits of \ precision on your computer. MachineNumberQ tests an expression to see if it \ has MachinePrecision. see tutorial/MachinePrecisionNumbers for more \ information on machine precision calculations.\n\nNumerical operators such as \ NSolve, NIntegrate, and NDSolve typically accept the directives \ WorkingPrecision and PrecisionGoal which specify, respectively, the maximum \ precision with which to carry out internal calculations and the precision \ which you desire in your final results. To be effective, the inputs (if \ numerical) have to have precision higher than WorkingPrecision and \ PrecisionGoal. If left unspecified, assume that ", StyleBox["Mathematica", FontSlant->"Italic"], " will work in machine precision (for speed). Note that the higher precision \ the input is, the longer the calculations take. \n\n", StyleBox["Mathematica", FontSlant->"Italic"], " uses $MaxExtraPrecision extra digits of precision in its internal \ numerical calculations. For example, when you call N to evaluate a quantity, \ $MaxExtraPrecision affects the intermediate calculations. There are also some \ nonobvious instances where $MaxExtraPrecison comes into play; for example, \ when comparing equality of symbolic quantities, ", StyleBox["Mathematica", FontSlant->"Italic"], " will sometimes evaluate them numerically before comparison. This can lead \ to problems sometimes, e.g. if you compare two complex expressions that are \ in fact equal, then ", StyleBox["Mathematica", FontSlant->"Italic"], " will compare them numerically, and no amount of precision will allow it to \ determine which is the largest. If you need to, you can use a Block to \ locally change the value of $MaxExtraPrecision.\n\nFor more discussion of \ precision in ", StyleBox["Mathematica", FontSlant->"Italic"], ", see tutorial/ArbitraryPrecisionNumbers\n\nNote there's an associated \ notion of accuracy: while precision is the number of significant digits in a \ quantity, accuracy is the number of significant digits after the decimal \ point. Pretty much the same comments above apply to accuracy: corresponding \ to Precision there's Accuracy, to SetPrecision there's SetAccuracy, there's \ AccuracyGoal, etc. Accuracy is important, but you can usually control it by \ controlling precision, so we'll concentrate on that." }], "Text", CellChangeTimes->{{3.4371010994872527`*^9, 3.437101520999253*^9}, { 3.437101582120053*^9, 3.437101693114053*^9}, {3.437101734034853*^9, 3.437101747918853*^9}, {3.4371018058104525`*^9, 3.437101823469653*^9}, { 3.437101919222453*^9, 3.4371021964812527`*^9}, {3.4371023211096525`*^9, 3.437102591036453*^9}, {3.4371026475864525`*^9, 3.437102700954053*^9}, { 3.4371033030984526`*^9, 3.4371033105240526`*^9}, {3.437103781456853*^9, 3.4371037924860525`*^9}, {3.437103825261653*^9, 3.437103826291253*^9}, { 3.437146623264055*^9, 3.437146645565055*^9}, {3.4371466765690546`*^9, 3.437146677829055*^9}}], Cell[CellGroupData[{ Cell["Basic Examples", "Section", CellChangeTimes->{{3.437103584725253*^9, 3.4371035867688527`*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"1", "/", "3"}], " ", "//", "Precision"}], "\[IndentingNewLine]", "$MachinePrecision", "\ \[IndentingNewLine]", RowBox[{"N", "[", RowBox[{ RowBox[{"1", "/", "3"}], ",", " ", "MachinePrecision"}], "]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"3.33", " ", "//", " ", "Precision"}], " ", RowBox[{"(*", " ", RowBox[{ RowBox[{"defaults", " ", "to", " ", "MachinePrecision"}], ",", " ", RowBox[{"because", " ", "it", " ", "fits"}]}], " ", "*)"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{ "MachineNumberQ", "[", "3.33333333333333333333333333333333333333", "]"}], " ", RowBox[{"(*", " ", RowBox[{ RowBox[{"doesn", "'"}], "t", " ", "fit", " ", "MachinePrecision"}], " ", "*)"}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ "3.33333333333333333333333333333333333333", " ", "//", " ", "Precision"}], " "}], "\[IndentingNewLine]", RowBox[{ RowBox[{"3.33333333333333333333333333333333333333", " ", "//", " ", RowBox[{ RowBox[{"First", "@", RowBox[{"RealDigits", "[", "#", "]"}]}], "&"}]}], " ", "//", " ", "Length"}], "\[IndentingNewLine]", RowBox[{"3.33333333333333333333333333333333333333", " ", "//", " ", "Accuracy"}], "\[IndentingNewLine]", RowBox[{"3.33333333333333333333333333333333333333`20", " ", "//", "Precision"}], "\[IndentingNewLine]", RowBox[{"3.33333333333333333333333333333333333333`50", " ", "//", "Precision"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"3.3`", "\[Infinity]"}], " ", "//", "Precision"}], "\[IndentingNewLine]", "3.333`1", "\[IndentingNewLine]", RowBox[{"3.333`1", " ", "//", "FullForm"}], "\[IndentingNewLine]", RowBox[{"3.333`1", " ", "\[Equal]", " ", RowBox[{"3333", "/", "1000", " ", RowBox[{"(*", " ", RowBox[{ RowBox[{ "even", " ", "though", " ", "Mathematica", " ", "only", " ", "displays", " ", "the", " ", "sig", " ", "digits"}], ",", " ", RowBox[{ "it", " ", "uses", " ", "the", " ", "rest", " ", "in", " ", "the", " ", "comparison"}]}], " ", "*)"}]}]}]}], "Input", CellChangeTimes->{{3.437102739579653*^9, 3.437102983064453*^9}, { 3.4371030220176525`*^9, 3.437103172089653*^9}, {3.4371032067684526`*^9, 3.437103213476453*^9}, {3.4371032446452527`*^9, 3.4371032881848526`*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"NSum", "[", RowBox[{ FractionBox["1", SuperscriptBox["i", "2"]], ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "2000"}], "}"}]}], "]"}], " ", RowBox[{"(*", " ", RowBox[{ "gives", " ", "machine", " ", "precision", " ", "by", " ", "default"}], " ", "*)"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{"NSum", "[", RowBox[{ FractionBox["1", SuperscriptBox["i", "2"]], ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "2000"}], "}"}], ",", " ", RowBox[{"PrecisionGoal", "\[Rule]", "100"}]}], "]"}], " ", RowBox[{"(*", " ", RowBox[{ RowBox[{"fails", "--"}], " ", "look", " ", "at", " ", "the", " ", "poor", " ", "error", " ", RowBox[{"estimate", "--"}], " ", "maybe", " ", "because", " ", "we", " ", RowBox[{"aren", "'"}], "t", " ", "using", " ", "enough", " ", "precision", " ", "on", " ", "intermediate", " ", "steps"}], " ", "*)"}]}], "\[IndentingNewLine]", RowBox[{"NSum", "[", RowBox[{ FractionBox["1", SuperscriptBox["i", "2"]], ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "2000"}], "}"}], ",", " ", RowBox[{"PrecisionGoal", "\[Rule]", "100"}], ",", " ", RowBox[{"WorkingPrecision", "\[Rule]", "1000"}]}], "]"}], "\[IndentingNewLine]", RowBox[{"%", " ", "//", " ", "FullForm", " ", RowBox[{"(*", " ", RowBox[{ "shows", " ", "the", " ", "precision", " ", "with", " ", "the", " ", "numbermark", " ", "formatting"}], " ", "*)"}]}]}], "Input", CellChangeTimes->{{3.4371033247044525`*^9, 3.437103494432453*^9}, { 3.4371035421996527`*^9, 3.437103562479653*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Inheritance of precision", "Section", CellChangeTimes->{{3.4371036509472528`*^9, 3.4371036550500526`*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"(*", " ", RowBox[{ RowBox[{"example", " ", "where", " ", "N", " ", RowBox[{"can", "'"}], "t", " ", "give", " ", "the", " ", "precision", " ", "asked", " ", "for"}], ",", " ", RowBox[{ RowBox[{ "because", " ", "you", " ", "of", " ", "inherited", " ", "machine", " ", "precision"}], ";", " ", RowBox[{ RowBox[{"example", " ", "taken", " ", "from", " ", RowBox[{"http", ":"}]}], "//", RowBox[{ RowBox[{ RowBox[{ RowBox[{"dirac", ".", "org"}], "/", "apps"}], "/", "mathematica"}], "/", RowBox[{"numerics", ".", "php"}]}]}]}]}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"x", "=", RowBox[{"{", RowBox[{"2.0", ",", "3.0", ",", "5.0"}], "}"}]}], ";"}], "\n", RowBox[{ RowBox[{"A", "=", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"6.0", ",", "2.0", ",", "1.0"}], "}"}], ",", RowBox[{"{", RowBox[{"2.0", ",", "3.0", ",", "1.0"}], "}"}], ",", RowBox[{"{", RowBox[{"1.0", ",", "1.0", ",", "1.0"}], "}"}]}], "}"}]}], ";"}], "\n", RowBox[{"For", "[", RowBox[{ RowBox[{"k", "=", "0"}], ",", RowBox[{"k", "<", "15"}], ",", RowBox[{"++", "k"}], ",", RowBox[{ RowBox[{"lambda", "=", RowBox[{ RowBox[{"x", ".", "A", ".", "x"}], "/", RowBox[{"(", RowBox[{"x", ".", "x"}], ")"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"y", "=", RowBox[{"LinearSolve", "[", RowBox[{"A", ",", "x"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"x", "=", RowBox[{"y", "/", RowBox[{"Norm", "[", RowBox[{"y", ",", "Infinity"}], "]"}]}]}], ";"}]}], "]"}], "\n", RowBox[{"N", "[", RowBox[{"lambda", ",", "30"}], "]"}], "\n"}]}]], "Input", CellChangeTimes->{{3.4371002706280527`*^9, 3.437100328722453*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"x", "=", RowBox[{"{", RowBox[{"2", ",", "3", ",", "5"}], "}"}]}], ";"}], "\n", RowBox[{ RowBox[{"A", "=", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"6", ",", "2", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{"2", ",", "3", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "1", ",", "1"}], "}"}]}], "}"}]}], ";"}], "\n", RowBox[{"For", "[", RowBox[{ RowBox[{"k", "=", "0"}], ",", RowBox[{"k", "<", "15"}], ",", RowBox[{"++", "k"}], ",", RowBox[{ RowBox[{"lambda", "=", RowBox[{ RowBox[{"x", ".", "A", ".", "x"}], "/", RowBox[{"(", RowBox[{"x", ".", "x"}], ")"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"y", "=", RowBox[{"LinearSolve", "[", RowBox[{"A", ",", "x"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"x", "=", RowBox[{"y", "/", RowBox[{"Norm", "[", RowBox[{"y", ",", "Infinity"}], "]"}]}]}], ";"}]}], "]"}], "\n", RowBox[{ RowBox[{"N", "[", RowBox[{"lambda", ",", "30"}], "]"}], "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"(*", RowBox[{" ", RowBox[{"or", " ", "equivalently", " ", RowBox[{"(", RowBox[{ RowBox[{"could", " ", "also", " ", "use", " ", "Round"}], ",", " ", "Ceiling", ",", " ", RowBox[{ "Floor", " ", "to", " ", "do", " ", "the", " ", "same", " ", "thing"}]}], ")"}]}], ")"}], " ", "*)"}]}], "\n", RowBox[{ RowBox[{"x", "=", RowBox[{"Rationalize", "@", RowBox[{"{", RowBox[{"2.0", ",", "3.0", ",", "5.0"}], "}"}]}]}], ";"}], "\n", RowBox[{ RowBox[{"A", "=", RowBox[{"Rationalize", "@", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"6.0", ",", "2.0", ",", "1.0"}], "}"}], ",", RowBox[{"{", RowBox[{"2.0", ",", "3.0", ",", "1.0"}], "}"}], ",", RowBox[{"{", RowBox[{"1.0", ",", "1.0", ",", "1.0"}], "}"}]}], "}"}]}]}], ";"}], "\n", RowBox[{"For", "[", RowBox[{ RowBox[{"k", "=", "0"}], ",", RowBox[{"k", "<", "15"}], ",", RowBox[{"++", "k"}], ",", RowBox[{ RowBox[{"lambda", "=", RowBox[{ RowBox[{"x", ".", "A", ".", "x"}], "/", RowBox[{"(", RowBox[{"x", ".", "x"}], ")"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"y", "=", RowBox[{"LinearSolve", "[", RowBox[{"A", ",", "x"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"x", "=", RowBox[{"y", "/", RowBox[{"Norm", "[", RowBox[{"y", ",", "Infinity"}], "]"}]}]}], ";"}]}], "]"}], "\n", RowBox[{ RowBox[{"N", "[", RowBox[{"lambda", ",", "30"}], "]"}], "\[IndentingNewLine]", "\n", RowBox[{"(*", " ", RowBox[{ RowBox[{ "you", " ", "could", " ", "start", " ", "off", " ", "with", " ", "higher"}], ",", " ", RowBox[{"but", " ", "finite"}], ",", " ", RowBox[{"precision", " ", "numbers"}]}], " ", "*)"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{"x", "=", RowBox[{"SetPrecision", "[", RowBox[{ RowBox[{"{", RowBox[{"2.0", ",", "3.0", ",", "5.0"}], "}"}], ",", " ", "5000"}], "]"}]}], ";"}], "\n", RowBox[{ RowBox[{"A", "=", RowBox[{"SetPrecision", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"6.0", ",", "2.0", ",", "1.0"}], "}"}], ",", RowBox[{"{", RowBox[{"2.0", ",", "3.0", ",", "1.0"}], "}"}], ",", RowBox[{"{", RowBox[{"1.0", ",", "1.0", ",", "1.0"}], "}"}]}], "}"}], ",", " ", "5000"}], "]"}]}], ";"}], "\n", RowBox[{"For", "[", RowBox[{ RowBox[{"k", "=", "0"}], ",", RowBox[{"k", "<", "15"}], ",", RowBox[{"++", "k"}], ",", RowBox[{ RowBox[{"lambda", "=", RowBox[{ RowBox[{"x", ".", "A", ".", "x"}], "/", RowBox[{"(", RowBox[{"x", ".", "x"}], ")"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"y", "=", RowBox[{"LinearSolve", "[", RowBox[{"A", ",", "x"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"x", "=", RowBox[{"y", "/", RowBox[{"Norm", "[", RowBox[{"y", ",", "Infinity"}], "]"}]}]}], ";"}]}], "]"}], "\n", RowBox[{"N", "[", RowBox[{"lambda", ",", "30"}], "]"}], "\n"}], "Input", CellChangeTimes->{{3.437100352216053*^9, 3.437100489090453*^9}, { 3.437103688246853*^9, 3.4371037131756525`*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Cost of Extra Precision", "Section", CellChangeTimes->{{3.4371036020100527`*^9, 3.437103606830453*^9}}], Cell[BoxData[ RowBox[{"\[IndentingNewLine]", RowBox[{"(*", " ", RowBox[{ RowBox[{"on", " ", "average"}], ",", " ", RowBox[{ "exact", " ", "operations", " ", "are", " ", "the", " ", "most", " ", "expensive"}], ",", " ", RowBox[{"then", " ", "arbitrary", " ", "precision", " ", "operations"}], ",", " ", RowBox[{"then", " ", "machine", " ", "precision", " ", "operations"}]}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"exacttimes", " ", "=", " ", RowBox[{"{", "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"inexacttimes", " ", "=", " ", RowBox[{"{", "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"precisiontimes", " ", "=", " ", RowBox[{"{", "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"For", "[", " ", RowBox[{ RowBox[{"n", "=", "1"}], ",", " ", RowBox[{"n", "<", " ", "100"}], ",", " ", RowBox[{"n", "++"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"Remove", "[", "\"\\"", "]"}], ";", "\[IndentingNewLine]", RowBox[{"A", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ FractionBox["1", RowBox[{"j", "+", "k", "-", "1"}]], ",", RowBox[{"{", RowBox[{"j", ",", "1", ",", "10"}], "}"}], ",", " ", RowBox[{"{", RowBox[{"k", ",", "1", ",", "10"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"(*", " ", RowBox[{"exact", " ", "numbers"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"exacttimes", " ", "=", " ", RowBox[{"Append", "[", RowBox[{"exacttimes", ",", " ", RowBox[{"First", "@", RowBox[{"AbsoluteTiming", "[", " ", RowBox[{"A", " ", "//", "Eigenvalues"}], "]"}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"(*", " ", RowBox[{"inexact", " ", RowBox[{"numbers", ":", " ", RowBox[{ "machine", " ", "precision", " ", "by", " ", "default"}]}]}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"inexacttimes", " ", "=", " ", RowBox[{"Append", "[", RowBox[{"inexacttimes", ",", " ", RowBox[{"First", "@", RowBox[{"AbsoluteTiming", "[", RowBox[{ RowBox[{"A", " ", "//", "N"}], "//", "Eigenvalues"}], "]"}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"(*", " ", RowBox[{"inexact", " ", RowBox[{"numbers", ":", " ", RowBox[{"specify", " ", "a", " ", "precision"}]}]}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"precisiontimes", " ", "=", " ", RowBox[{"Append", "[", RowBox[{"precisiontimes", ",", " ", RowBox[{"First", "@", RowBox[{"AbsoluteTiming", "[", RowBox[{ RowBox[{"A", " ", "//", " ", RowBox[{ RowBox[{"N", "[", RowBox[{"#", ",", " ", "40"}], "]"}], " ", "&"}]}], " ", "//", " ", "Eigenvalues"}], "]"}]}]}], "]"}]}], ";"}]}], "\[IndentingNewLine]", "]"}], ";"}], "\[IndentingNewLine]", RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"{", RowBox[{ "exacttimes", ",", " ", "inexacttimes", ",", " ", "precisiontimes"}], "}"}], ",", " ", RowBox[{"Filling", "\[Rule]", "Axis"}], ",", " ", RowBox[{"PlotStyle", "\[Rule]", RowBox[{"{", RowBox[{"Red", ",", " ", "Green", ",", " ", "Yellow"}], "}"}]}]}], "]"}], "\[IndentingNewLine]", "\[IndentingNewLine]"}]}]], "Input", CellChangeTimes->{{3.4369044872316494`*^9, 3.4369045517532496`*^9}, { 3.4369047261612496`*^9, 3.43690476651845*^9}, {3.4369048797744493`*^9, 3.43690488498485*^9}, {3.43690527936545*^9, 3.4369052871342497`*^9}, { 3.4369053610158496`*^9, 3.4369055788854494`*^9}, {3.4369056090558496`*^9, 3.43690561799465*^9}, {3.4369056776022496`*^9, 3.43690568892785*^9}, { 3.4369057375062494`*^9, 3.4369058496858497`*^9}, {3.4369058806050496`*^9, 3.4369059911934495`*^9}, {3.437103628686053*^9, 3.437103632601653*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"(*", " ", RowBox[{"inexact", " ", RowBox[{"numbers", ":", " ", RowBox[{"compare", " ", "to", " ", "Matlab"}]}]}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"b", " ", "=", " ", RowBox[{"RandomReal", "[", RowBox[{ RowBox[{"{", RowBox[{"0", ",", "1"}], "}"}], ",", " ", "5000"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"A", " ", "=", " ", RowBox[{"RandomReal", "[", RowBox[{ RowBox[{"{", RowBox[{"0", ",", "1"}], "}"}], ",", " ", RowBox[{"{", RowBox[{"5000", ",", "5000"}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"First", "@", RowBox[{"Timing", "[", RowBox[{"x", " ", "=", " ", RowBox[{"LinearSolve", "[", RowBox[{"A", ",", "b"}], "]"}]}], "]"}]}], "\[IndentingNewLine]", RowBox[{"Norm", "[", RowBox[{ RowBox[{ RowBox[{"A", " ", ".", " ", "x"}], " ", "-", " ", "b"}], ",", "\[Infinity]"}], "]"}], "\[IndentingNewLine]"}]}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"(*", " ", RowBox[{ "some", " ", "computations", " ", "should", " ", "only", " ", "be", " ", "done", " ", "on", " ", "numerical", " ", RowBox[{"data", "!"}]}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"A", " ", "=", " ", RowBox[{"HilbertMatrix", "[", "50", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"(*", " ", RowBox[{"Timing", "@", RowBox[{"Short", "@", RowBox[{"Eigenvalues", "@", "A"}]}]}], " ", RowBox[{"(*", " ", RowBox[{ RowBox[{ "a", " ", "list", " ", "of", " ", "50", " ", "Root", " ", "objects"}], ",", " ", RowBox[{"takes", " ", "32", " ", "seconds", " ", "to", " ", RowBox[{"compute", " ", "--"}], " ", "Mathematica", " ", "calculated", " ", "the", " ", "characteristic", " ", "poly", " ", "of", " ", RowBox[{"A", "!"}]}]}], "*)"}], " ", "*)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{ RowBox[{"A", " ", "=", " ", RowBox[{"N", "[", RowBox[{"HilbertMatrix", "[", "50", "]"}], "]"}]}], ";"}], " ", RowBox[{"(*", " ", RowBox[{ "usually", " ", "you", " ", "only", " ", "want", " ", "approximations", " ", "to", " ", "the", " ", "eigenvalues"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"A", " ", "//", " ", "Eigenvalues"}], " ", RowBox[{"(*", " ", RowBox[{"almost", " ", "immediate"}], " ", "*)"}], "\[IndentingNewLine]"}]}]], "Input", CellChangeTimes->{{3.437103878317253*^9, 3.437103933541253*^9}, { 3.437103974881253*^9, 3.4371039962688527`*^9}, {3.4371040400892525`*^9, 3.4371040459236526`*^9}, {3.437104123518053*^9, 3.4371043007184525`*^9}, 3.437104348953653*^9}], Cell[BoxData[ RowBox[{ RowBox[{"(*", " ", RowBox[{ "the", " ", "advantage", " ", "over", " ", "Matlab", " ", "is", " ", "I", " ", "can", " ", "ask", " ", "for", " ", "more", " ", "than", " ", "MachinePrecision", " ", "in", " ", "the", " ", "eigenvalues"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"A", " ", "=", " ", RowBox[{"N", "[", RowBox[{ RowBox[{"HilbertMatrix", "[", "50", "]"}], ",", " ", "100"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"A", " ", "//", " ", "Eigenvalues"}], " ", "//", "Short"}], "\[IndentingNewLine]", RowBox[{"%", " ", "//", " ", "Precision"}]}]}]], "Input", CellChangeTimes->{{3.437103878317253*^9, 3.437103933541253*^9}, { 3.437103974881253*^9, 3.4371039962688527`*^9}, {3.4371040400892525`*^9, 3.4371040459236526`*^9}, {3.437104123518053*^9, 3.4371043334472528`*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Packages and Contexts", "Subtitle", CellChangeTimes->{{3.4371044226168528`*^9, 3.437104442054453*^9}}], Cell[TextData[{ "Contexts are used in ", StyleBox["Mathematica", FontSlant->"Italic"], " to help organize collections of symbols. They are analogous to namespaces \ in C++, etc. Packages are coherent collections of code, which usually define \ useful symbols and functions. For instance, ", StyleBox["Mathematica", FontSlant->"Italic"], " ships with a Histograms package which has useful functions for graphing \ histograms, and a VectorAnalysis package which defines the Grad, Curl, Div, \ Laplacian, etc. operations in several different coordinates. There are tons \ more packages; ", StyleBox["Combinatorica ", FontSlant->"Italic"], "is a pretty neat one, which provides many convenience codes for dealing \ with discrete mathematics and graph theory.\n\nEach symbol has a full name, \ which consists of a context and a short name. Contexts are hierarchial, so a \ symbol could for instance have the full name context`subcontext`shortname \ (the backquote separates contexts and short names). Generally, you refer to \ symbols by their short name and ", StyleBox["Mathematica", FontSlant->"Italic"], " uses the current $Context and then $ContextPath to determine the full name \ of the variable you're referring to. If you consider contexts to be like the \ folders in a file system, then $Context would be the current directory and \ $ContextPath would be like your path. If ", StyleBox["Mathematica", FontSlant->"Italic"], " can't find the symbol you referenced, it creates it in the current \ context. By default the current context is Global`.\n\nYou import a package \ before calling the functions it provides. Do so with the Needs command, \ Needs[\"context`\"]. This will load the package if it has not yet been \ loaded. Alternatively, you can use the Get command, abbreviated <<, e.g. <<", StyleBox["Combinatorica`", FontSlant->"Italic"], " or <"Italic"], " that you're loading a context by using the context mark. What ", StyleBox["Mathematica", FontSlant->"Italic"], " then does is determine the filename corresponding to that context, and \ executes the commands in the file. (Needs calls Get)\n\nBe careful that you \ don't call the commands in a package before loading it, because that will \ generate symbols in the current context that will shadow the package \ definitions after you load it; this can sometimes cause a problem. If you do \ this by accident, use Remove to remove the shadowing symbol.\n\n$Packages \ gives a list of the currently loaded packages (really, their contexts). \ Contexts gives a list of currently defined contexts, and Names gives a list \ of symbols matching a string pattern. ", StyleBox["Mathematica", FontSlant->"Italic"], " built in objects are located in the System` context, and user defined \ variables are by default in Global`, so you can delete all your variables by \ e.g. Remove[\"Global`*\"]. You can use Context to tell the context a symbol \ is in. There doesn't seem to be a formal command for unloading a package, but \ you could try e.g. Remove[\"PackageContext`*\"]\n\nsee tutorial/Contexts and \ tutorial/ContextsAndPackages for more details." }], "Text", CellChangeTimes->{{3.437104501209653*^9, 3.4371046958664527`*^9}, { 3.437104808779253*^9, 3.4371048591672525`*^9}, {3.437104895640053*^9, 3.4371053223000526`*^9}, {3.437105375230853*^9, 3.4371055314960527`*^9}, { 3.437105576548853*^9, 3.437105583256853*^9}, {3.437105618419253*^9, 3.437105665827653*^9}, {3.437105710880453*^9, 3.437105791189253*^9}, { 3.437105825540453*^9, 3.437105871092453*^9}, {3.437106110755253*^9, 3.437106174153653*^9}, {3.4371062238708525`*^9, 3.4371062396580524`*^9}, { 3.4371065417052526`*^9, 3.437106589831253*^9}, {3.437106912907253*^9, 3.4371069354180527`*^9}, {3.4371435404980545`*^9, 3.437143540740055*^9}}], Cell[BoxData[{"$Packages", "\[IndentingNewLine]", RowBox[{"Contexts", "[", "\"\\"", "]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Names", "[", "\"\\"", "]"}], " "}]}], "Input", CellChangeTimes->{{3.437104701482453*^9, 3.4371047027460527`*^9}, { 3.437105536753253*^9, 3.437105536924853*^9}, 3.4371057036420527`*^9, { 3.4371057770556526`*^9, 3.437105779052453*^9}, {3.437105876692853*^9, 3.437105914460453*^9}, {3.437105973085253*^9, 3.437106071427653*^9}, { 3.437106188115653*^9, 3.4371061995504527`*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"Grad", "[", "f", "]"}], " ", RowBox[{"(*", " ", RowBox[{ RowBox[{"grad", " ", "not", " ", "defined", " ", "yet"}], ",", " ", RowBox[{"so", " ", "this", " ", "created", " ", "Global`Grad"}]}], " ", "*)"}]}], "\[IndentingNewLine]", RowBox[{"Names", "[", "\"\\"", "]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Needs", "[", "\"\\"", "]"}], " ", RowBox[{"(*", " ", RowBox[{"I", " ", "get", " ", "a", " ", "warning"}], " ", "*)"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"Remove", "[", "\"\\"", "]"}], ";"}], " ", RowBox[{"(*", " ", RowBox[{"erase", " ", "the", " ", "problem", " ", "def"}], " ", "*)"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Grad", "[", RowBox[{"Xx", "+", "Yy", "+", SuperscriptBox["Zz", "3"]}], "]"}], " ", RowBox[{"(*", " ", RowBox[{ "uses", " ", "the", " ", "correct", " ", "definition", " ", "of", " ", "Grad"}], " ", "*)"}]}]}], "Input", CellChangeTimes->{{3.4371062143548527`*^9, 3.437106217069253*^9}, { 3.437106255554453*^9, 3.437106344412053*^9}, {3.4371063834432526`*^9, 3.4371063854556527`*^9}, {3.4371064295724525`*^9, 3.4371064302744527`*^9}, { 3.4371064665444527`*^9, 3.4371064670592527`*^9}, {3.4371069554952526`*^9, 3.4371070293456526`*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"Needs", "[", "\"\\"", "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"ShowGraph", "[", RowBox[{"RankedEmbedding", "[", RowBox[{ RowBox[{"GridGraph", "[", RowBox[{"5", ",", "5"}], "]"}], ",", RowBox[{"{", "13", "}"}]}], "]"}], "]"}], " ", RowBox[{"(*", " ", RowBox[{"example", " ", "taken", " ", "from", " ", RowBox[{ RowBox[{"Combinatorica", "/", "tutorial"}], "/", "Combinatorica"}]}], " ", "*)"}]}], "\[IndentingNewLine]"}], "Input", CellChangeTimes->{{3.437143574184055*^9, 3.437143575941055*^9}, { 3.4371436335430546`*^9, 3.4371436586130548`*^9}, {3.437143694841055*^9, 3.4371437757720547`*^9}, {3.437143889950055*^9, 3.437143896457055*^9}, { 3.437143930889055*^9, 3.437143938662055*^9}, {3.437144387376055*^9, 3.4371444286960545`*^9}, {3.437144492769055*^9, 3.4371445320570545`*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"Needs", "[", "\"\\"", "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"ExpressionTreePlot", "[", RowBox[{"Hold", "@", RowBox[{"Table", "[", RowBox[{ RowBox[{ FractionBox["1", SqrtBox["6"]], SuperscriptBox["\[ExponentialE]", RowBox[{"\[ImaginaryI]", " ", "\[Pi]", " ", "j", " ", RowBox[{"k", "/", "3"}]}]]}], ",", RowBox[{"{", RowBox[{"j", ",", "1", ",", "6"}], "}"}], ",", " ", RowBox[{"{", RowBox[{"k", ",", "1", ",", "6"}], "}"}]}], "]"}]}], "]"}], " ", RowBox[{"(*", " ", RowBox[{ "check", " ", "out", " ", "the", " ", "Tooltips", " ", "that", " ", "show", " ", "the", " ", "subexpressions", " ", "under", " ", "each", " ", RowBox[{"node", "!"}]}], " ", "*)"}]}], "\[IndentingNewLine]", RowBox[{"ExpressionTreePlot", "[", RowBox[{"Table", "[", RowBox[{ RowBox[{ FractionBox["1", SqrtBox["6"]], SuperscriptBox["\[ExponentialE]", RowBox[{"\[ImaginaryI]", " ", "\[Pi]", " ", "j", " ", RowBox[{"k", "/", "3"}]}]]}], ",", RowBox[{"{", RowBox[{"j", ",", "1", ",", "6"}], "}"}], ",", " ", RowBox[{"{", RowBox[{"k", ",", "1", ",", "6"}], "}"}]}], "]"}], "]"}]}], "Input", CellChangeTimes->{{3.4371439496940546`*^9, 3.437143974329055*^9}, { 3.437144011341055*^9, 3.437144012464055*^9}, {3.4371440940610547`*^9, 3.4371440978380547`*^9}, {3.4371441339700546`*^9, 3.4371441849560547`*^9}, 3.437144241921055*^9, {3.4371442941460547`*^9, 3.437144360938055*^9}, { 3.437144539327055*^9, 3.437144554775055*^9}}] }, Open ]] }, Open ]] }, WindowSize->{1006, 700}, WindowMargins->{{1, Automatic}, {1, Automatic}}, FrontEndVersion->"6.0 for Microsoft Windows (32-bit) (March 13, 2008)", StyleDefinitions->FrontEnd`FileName[{"Creative"}, "PastelColor.nb", CharacterEncoding -> "WindowsANSI"] ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[590, 23, 312, 5, 73, "Title"], Cell[905, 30, 4300, 73, 389, "Text"], Cell[CellGroupData[{ Cell[5230, 107, 101, 1, 75, "Section"], Cell[5334, 110, 2340, 56, 322, "Input"], Cell[7677, 168, 1655, 44, 170, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[9369, 217, 113, 1, 75, "Section"], Cell[9485, 220, 1970, 57, 202, "Input"], Cell[11458, 279, 4404, 134, 482, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[15899, 418, 110, 1, 75, "Section"], Cell[16012, 421, 4201, 99, 434, "Input"], Cell[20216, 522, 1094, 33, 142, "Input"], Cell[21313, 557, 1751, 42, 182, "Input"], Cell[23067, 601, 917, 21, 102, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[24021, 627, 109, 1, 36, "Subtitle"], Cell[24133, 630, 3911, 68, 371, "Text"], Cell[28047, 700, 544, 8, 82, "Input"], Cell[28594, 710, 1361, 32, 124, "Input"], Cell[29958, 744, 914, 20, 82, "Input"], Cell[30875, 766, 1661, 41, 145, "Input"] }, Open ]] }, Open ]] } ] *) (* End of internal cache information *)