(* 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[     29886,        984]
NotebookOptionsPosition[     25522,        837]
NotebookOutlinePosition[     25965,        856]
CellTagsIndexPosition[     25922,        853]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{

Cell[CellGroupData[{
Cell["PwM`RandomWalks`", "Section",
 InitializationGroup->True,
 CounterAssignments->{{"Chapter", 0}}],

Cell[TextData[{
 StyleBox["From",
  FontWeight->"Bold"],
 ": ",
 StyleBox[ButtonBox["Programming with Mathematica: An Introduction",
  BaseStyle->"Hyperlink",
  ButtonData->{
    URL["http://www.cambridge.org/wellin"], None},
  ButtonNote->"http://www.cambridge.org/wellin"],
  FontSlant->"Italic",
  FontVariations->{"Underline"->True}]
}], "Text",
 CellMargins->{{28, 10}, {7, 7}},
 FontSize->14],

Cell[CellGroupData[{

Cell["Metadata", "Subsection"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Name", ":", " ", "PwM`RandomWalks`"}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Book", ":", " ", 
    RowBox[{"Programming", " ", "with", " ", 
     RowBox[{"Mathematica", ":", " ", 
      RowBox[{"An", " ", "Introduction"}]}]}]}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Author", ":", " ", 
    RowBox[{"Paul", " ", "Wellin"}]}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{
   RowBox[{":", 
    RowBox[{"Summary", ":", " ", 
     RowBox[{
     "This", " ", "package", " ", "contains", " ", "the", " ", "random", " ", 
      "walk", " ", "functions", " ", "defined", " ", "throughout", " ", "the",
       " ", "book", " ", "but", " ", "particularly", " ", "in", " ", 
      "Chapter", " ", "13"}]}]}], ",", " ", 
   RowBox[{"Applications", " ", "and", " ", 
    RowBox[{"packages", "."}]}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Context", ":", " ", "PwM`RandomWalks`"}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Package", " ", 
    RowBox[{"Version", ":", " ", "1.0"}]}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{
   RowBox[{":", 
    RowBox[{"Copyright", ":", " ", 
     RowBox[{"Copyright", " ", "2012"}]}]}], ",", " ", 
   RowBox[{"Cambridge", " ", "University", " ", "Press"}]}], " ", 
  "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{
   RowBox[{":", 
    RowBox[{"History", ":", " ", 
     RowBox[{
     "Version", " ", "1.0", " ", "by", " ", "Paul", " ", "Wellin"}]}]}], ",", 
   " ", 
   RowBox[{"Dec", " ", "2012"}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{
   RowBox[{":", 
    RowBox[{"Keywords", ":", " ", 
     RowBox[{"random", " ", "walks"}]}]}], ",", " ", 
   RowBox[{"lattice", " ", "walks"}], ",", " ", 
   RowBox[{"off", "-", 
    RowBox[{"lattice", " ", "walks"}]}], ",", " ", 
   RowBox[{"mean", "-", 
    RowBox[{"square", " ", "distance"}]}], ",", " ", 
   RowBox[{"radius", " ", "of", " ", "gyration"}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Source", ":", " ", 
    RowBox[{"RandomWalks", ".", "nb"}]}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Mathematica", " ", 
    RowBox[{"Version", ":", " ", 
     RowBox[{"8.0", ".4"}]}]}]}], " ", "*)"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["BeginPackage", "Subsection"],

Cell[BoxData[
 RowBox[{"Remove", "[", 
  RowBox[{"RandomWalk", ",", "ShowWalk", ",", "RadiusOfGyrationTensor"}], 
  "]"}]], "Input"],

Cell[BoxData[
 RowBox[{"BeginPackage", "[", "\"\<PwM`RandomWalks`\>\"", "]"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["Usage statements", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"AnimateWalk", "::", "usage"}], "=", 
   "\"\<AnimateWalk[walk, opts] creates an animation of a two-dimensional \
random walk. A red ball will be seen to move to the current position in the \
walk to aid in visualizing the animation.\>\""}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"Dimension", "::", "usage"}], "=", 
   "\"\<Dimension is an option to RandomWalk that determines whether the \
random walk will be a one-, two-, or three-dimensional walk. Possible values \
are 1, 2, or 3.\>\""}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"LatticeWalk", "::", "usage"}], "=", 
   "\"\<LatticeWalk is an option to RandomWalk that determines whether the \
random walk will be a lattice walk or an off-lattice walk. Possible values \
are True and False.\>\""}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"MeanSquareDistance", "::", "usage"}], "=", 
   "\"\<MeanSquareDistance[t, m] computes the mean square end-to-end distance \
of m t-step lattice walks.\>\""}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"MeanSquareRadiusGyration", "::", "usage"}], "=", 
   "\"\<MeanSquareRadiusGyration[t, m] computes the mean square radius of \
gyration of a random walk. This is the sum of the squares of the distances of \
the step locations from the center of mass, divided by the number of step \
locations, where the center of mass is the sum of the step locations divided \
by the number of step locations.\>\""}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"RadiusOfGyrationTensor", "::", "usage"}], "=", 
   "\"\<RadiusOfGyrationTensor[walk] computes the radius of gyration tensor \
for the random walk walk.\>\""}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"RandomWalk", "::", "usage"}], "=", 
   "\"\<RandomWalk[t] generates a t-step random walk. The default behavior \
gives a two-dimensional lattice walk with steps in one of the four compass \
directions. The option LatticeWalk takes values True or False. The value of \
the option Dimension can be any of 1, 2, or 3.\>\""}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"ShowWalk", "::", "usage"}], "=", 
   "\"\<ShowWalk[walk] displays a one, two, or three-dimensional random walk \
connecting each site with a line. Graphics options can be passed to ShowWalk. \
E.g., ShowWalk[walk, Background\[Rule]GrayLevel[0]] to produce a black \
background.\>\""}], ";"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["Warning messages", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"RandomWalk", "::", "rwn"}], "=", 
   "\"\<Argument `1` is not a positive integer.\>\""}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"RandomWalk", "::", "baddim"}], "=", 
   "\"\<The value `1` of the option Dimension is not an integer between 1 and \
3.\>\""}], ";"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["Options", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"Options", "[", "RandomWalk", "]"}], "=", 
   RowBox[{"{", 
    RowBox[{
     RowBox[{"LatticeWalk", "\[Rule]", "True"}], ",", 
     RowBox[{"Dimension", "\[Rule]", "2"}]}], "}"}]}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"Options", "[", "ShowWalk", "]"}], "=", 
   RowBox[{"Join", "[", 
    RowBox[{
     RowBox[{"Options", "[", "ListLinePlot", "]"}], ",", 
     RowBox[{"Options", "[", "Graphics3D", "]"}]}], "]"}]}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"Options", "[", "AnimateWalk", "]"}], "=", 
   RowBox[{"Options", "[", "Animate", "]"}]}], ";"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["Begin private context", "Subsection"],

Cell[BoxData[
 RowBox[{"Begin", "[", "\"\<`Private`\>\"", "]"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["The function definitions", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{"latticeWalk", "[", 
   RowBox[{"steps_", ",", "dim_"}], "]"}], ":=", " ", 
  RowBox[{"Module", "[", 
   RowBox[{
    RowBox[{"{", "w", "}"}], ",", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"w", "=", 
      RowBox[{"Accumulate", "[", 
       RowBox[{"RandomChoice", "[", 
        RowBox[{
         RowBox[{"Join", "[", 
          RowBox[{
           RowBox[{"IdentityMatrix", "[", "dim", "]"}], ",", 
           RowBox[{"-", 
            RowBox[{"IdentityMatrix", "[", "dim", "]"}]}]}], "]"}], ",", 
         "steps"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", 
     RowBox[{"If", "[", 
      RowBox[{
       RowBox[{"dim", "\[Equal]", "1"}], ",", 
       RowBox[{"Flatten", "[", "w", "]"}], ",", "w"}], "]"}]}]}], 
   "\[IndentingNewLine]", "]"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"offLatticeWalk", "[", 
   RowBox[{"t_", ",", "dim_"}], "]"}], ":=", 
  RowBox[{"Module", "[", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"f1", ",", "f2", ",", "f3"}], "}"}], ",", "\[IndentingNewLine]", 
    
    RowBox[{
     RowBox[{"f1", "=", 
      RowBox[{"RandomReal", "[", 
       RowBox[{
        RowBox[{"{", 
         RowBox[{
          RowBox[{"-", "1"}], ",", "1"}], "}"}], ",", "t"}], "]"}]}], ";", 
     "\[IndentingNewLine]", 
     RowBox[{"f2", "=", 
      RowBox[{"Map", "[", 
       RowBox[{
        RowBox[{
         RowBox[{"{", 
          RowBox[{
           RowBox[{"Cos", "[", "#", "]"}], ",", 
           RowBox[{"Sin", "[", "#", "]"}]}], "}"}], "&"}], ",", 
        RowBox[{"RandomReal", "[", 
         RowBox[{
          RowBox[{"{", 
           RowBox[{"0", ",", 
            RowBox[{"2", " ", "\[Pi]"}]}], "}"}], ",", "t"}], "]"}]}], 
       "]"}]}], ";", "\[IndentingNewLine]", 
     RowBox[{"f3", "=", 
      RowBox[{"Table", "[", 
       RowBox[{
        RowBox[{
         RowBox[{"Function", "[", 
          RowBox[{
           RowBox[{"{", 
            RowBox[{"\[Theta]", ",", "u"}], "}"}], ",", 
           RowBox[{"{", 
            RowBox[{
             RowBox[{
              RowBox[{"Cos", "[", "\[Theta]", "]"}], 
              RowBox[{"Sqrt", "[", 
               RowBox[{"1", "-", 
                RowBox[{"u", "^", "2"}]}], "]"}]}], ",", 
             RowBox[{
              RowBox[{"Sin", "[", "\[Theta]", "]"}], 
              RowBox[{"Sqrt", "[", 
               RowBox[{"1", "-", 
                RowBox[{"u", "^", "2"}]}], "]"}]}], ",", "u"}], "}"}]}], 
          "]"}], "@@", 
         RowBox[{"{", 
          RowBox[{
           RowBox[{"RandomReal", "[", 
            RowBox[{"{", 
             RowBox[{"0", ",", 
              RowBox[{"2", "Pi"}]}], "}"}], "]"}], ",", 
           RowBox[{"RandomReal", "[", 
            RowBox[{"{", 
             RowBox[{
              RowBox[{"-", "1"}], ",", "1"}], "}"}], "]"}]}], "}"}]}], ",", 
        RowBox[{"{", "t", "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", 
     RowBox[{"Which", "[", "\[IndentingNewLine]", 
      RowBox[{
       RowBox[{"dim", "\[Equal]", "1"}], ",", 
       RowBox[{"Accumulate", "[", "f1", "]"}], ",", "\[IndentingNewLine]", 
       RowBox[{"dim", "\[Equal]", "2"}], ",", 
       RowBox[{"Accumulate", "[", "f2", "]"}], ",", "\[IndentingNewLine]", 
       RowBox[{"dim", "\[Equal]", "3"}], ",", 
       RowBox[{"Accumulate", "[", "f3", "]"}]}], "\[IndentingNewLine]", 
      "]"}]}]}], "]"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"RandomWalk", "[", 
   RowBox[{"t_", ",", 
    RowBox[{"OptionsPattern", "[", "]"}]}], "]"}], ":=", 
  RowBox[{"Module", "[", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"dim", ",", "latticeQ"}], "}"}], ",", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{
      RowBox[{"{", 
       RowBox[{"latticeQ", ",", "dim"}], "}"}], "=", 
      RowBox[{"OptionValue", "[", 
       RowBox[{"{", 
        RowBox[{"LatticeWalk", ",", "Dimension"}], "}"}], "]"}]}], ";", 
     "\[IndentingNewLine]", 
     RowBox[{"Which", "[", "\[IndentingNewLine]", 
      RowBox[{
       RowBox[{"!", 
        RowBox[{"(", 
         RowBox[{
          RowBox[{"IntegerQ", "[", "t", "]"}], "&&", 
          RowBox[{"t", ">", "0"}]}], ")"}]}], ",", 
       RowBox[{"Message", "[", 
        RowBox[{
         RowBox[{"RandomWalk", "::", "\"\<rwn\>\""}], ",", "t"}], "]"}], ",", 
       "\[IndentingNewLine]", 
       RowBox[{"!", 
        RowBox[{"(", 
         RowBox[{
          RowBox[{"IntegerQ", "[", "dim", "]"}], "&&", 
          RowBox[{"1", "\[LessEqual]", "dim", "\[LessEqual]", "3"}]}], 
         ")"}]}], ",", 
       RowBox[{"Message", "[", 
        RowBox[{
         RowBox[{"RandomWalk", "::", "baddim"}], ",", "dim"}], "]"}], ",", 
       "\[IndentingNewLine]", "True", ",", 
       RowBox[{"If", "[", 
        RowBox[{"latticeQ", ",", 
         RowBox[{"latticeWalk", "[", 
          RowBox[{"t", ",", "dim"}], "]"}], ",", 
         RowBox[{"offLatticeWalk", "[", 
          RowBox[{"t", ",", "dim"}], "]"}]}], "]"}]}], "\[IndentingNewLine]", 
      "]"}]}]}], "\[IndentingNewLine]", "]"}]}]], "Input"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"RandomWalk", "[", "x__", "]"}], "/;", 
   RowBox[{"Message", "[", 
    RowBox[{
     RowBox[{"RandomWalk", "::", "rwn"}], ",", "x"}], "]"}]}], ":=", 
  "Null"}], "\n", 
 RowBox[{
  RowBox[{
   RowBox[{
    RowBox[{"RandomWalk", "[", "]"}], "/;", 
    RowBox[{"Message", "[", 
     RowBox[{
      RowBox[{"General", "::", "argx"}], ",", "RandomWalk", ",", "0"}], 
     "]"}]}], ":=", "Null"}], ";"}]}], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["Numerical analysis functions", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{"MeanSquareDistance", "[", 
   RowBox[{"t_Integer", ",", "trials_Integer"}], "]"}], ":=", 
  "\[IndentingNewLine]", 
  RowBox[{"Mean", "[", 
   RowBox[{"Table", "[", 
    RowBox[{
     RowBox[{
      RowBox[{
       RowBox[{"SquaredEuclideanDistance", "[", 
        RowBox[{
         RowBox[{"First", "[", "#", "]"}], ",", 
         RowBox[{"Last", "[", "#", "]"}]}], "]"}], "&"}], " ", "[", " ", 
      RowBox[{"RandomWalk", "[", "t", "]"}], "]"}], ",", 
     RowBox[{"{", "trials", "}"}]}], "]"}], "]"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"squareRadiusGyration", "[", "t_Integer", "]"}], ":=", 
  RowBox[{"Module", "[", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"locs", "=", 
      RowBox[{"RandomWalk", "[", "t", "]"}]}], "}"}], ",", 
    "\[IndentingNewLine]", 
    FractionBox[
     RowBox[{"Total", "[", 
      RowBox[{"Map", "[", 
       RowBox[{
        RowBox[{
         RowBox[{"SquaredEuclideanDistance", "[", 
          RowBox[{"#", ",", 
           RowBox[{"N", "[", 
            RowBox[{"Mean", "[", "locs", "]"}], "]"}]}], "]"}], "&"}], ",", 
        "locs"}], "]"}], "]"}], 
     RowBox[{"t", "+", "1"}]]}], "]"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"MeanSquareRadiusGyration", "[", 
   RowBox[{"t_Integer", ",", "trials_Integer"}], "]"}], ":=", 
  RowBox[{"Mean", "[", 
   RowBox[{"Table", "[", 
    RowBox[{
     RowBox[{"squareRadiusGyration", "[", "t", "]"}], ",", 
     RowBox[{"{", "trials", "}"}]}], "]"}], "]"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"RadiusOfGyrationTensor", "[", "lis_", "]"}], ":=", 
  RowBox[{"Module", "[", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"cmx", ",", "cmy", ",", "xcoords", ",", "ycoords", ",", "xy"}], 
     "}"}], ",", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{
      RowBox[{"{", 
       RowBox[{"cmx", ",", "cmy"}], "}"}], "=", 
      RowBox[{"Mean", "[", "lis", "]"}]}], ";", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"{", 
       RowBox[{"xcoords", ",", "ycoords"}], "}"}], "=", 
      RowBox[{"Transpose", "[", "lis", "]"}]}], ";", "\[IndentingNewLine]", 
     RowBox[{"xy", "=", 
      RowBox[{
       RowBox[{
        RowBox[{"(", 
         RowBox[{"xcoords", "-", "cmx"}], ")"}], ".", 
        RowBox[{"(", 
         RowBox[{"ycoords", "-", "cmy"}], ")"}]}], "/", 
       RowBox[{"Length", "[", "lis", "]"}]}]}], ";", "\[IndentingNewLine]", 
     RowBox[{"{", 
      RowBox[{
       RowBox[{"{", 
        RowBox[{
         RowBox[{"Mean", "[", 
          SuperscriptBox[
           RowBox[{"(", 
            RowBox[{"xcoords", "-", "cmx"}], ")"}], "2"], "]"}], ",", "xy"}], 
        "}"}], ",", 
       RowBox[{"{", 
        RowBox[{"xy", ",", 
         RowBox[{"Mean", "[", 
          SuperscriptBox[
           RowBox[{"(", 
            RowBox[{"ycoords", "-", "cmy"}], ")"}], "2"], "]"}]}], "}"}]}], 
      "}"}]}]}], "\[IndentingNewLine]", "]"}]}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["Visualization functions", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{"ShowWalk", "[", 
   RowBox[{
    RowBox[{"coords_", "?", "VectorQ"}], ",", 
    RowBox[{"opts", ":", 
     RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], " ", ":=", 
  "\[IndentingNewLine]", "\t", 
  RowBox[{"ListLinePlot", "[", 
   RowBox[{"coords", ",", " ", 
    RowBox[{"FilterRules", "[", 
     RowBox[{
      RowBox[{"{", "opts", "}"}], ",", 
      RowBox[{"Options", "[", "ListLinePlot", "]"}]}], "]"}]}], 
   "]"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"ShowWalk", "[", 
   RowBox[{
    RowBox[{"coords", ":", 
     RowBox[{"{", 
      RowBox[{
       RowBox[{"{", 
        RowBox[{"_", ",", "_"}], "}"}], ".."}], "}"}]}], ",", 
    RowBox[{"opts", ":", 
     RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], " ", ":=", 
  "\[IndentingNewLine]", "\t", 
  RowBox[{"ListLinePlot", "[", 
   RowBox[{"coords", ",", " ", 
    RowBox[{"Append", "[", 
     RowBox[{
      RowBox[{"FilterRules", "[", 
       RowBox[{
        RowBox[{"{", "opts", "}"}], ",", 
        RowBox[{"Options", "[", "ListLinePlot", "]"}]}], "]"}], ",", " ", 
      RowBox[{"AspectRatio", "\[Rule]", "Automatic"}]}], "]"}]}], 
   "]"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"ShowWalk", "[", 
   RowBox[{
    RowBox[{"coords", ":", 
     RowBox[{"{", 
      RowBox[{
       RowBox[{"{", 
        RowBox[{"_", ",", "_", ",", "_"}], "}"}], ".."}], "}"}]}], ",", 
    RowBox[{"opts", ":", 
     RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], " ", ":=", 
  "\[IndentingNewLine]", "\t", 
  RowBox[{"Graphics3D", "[", 
   RowBox[{
    RowBox[{"Line", "[", "coords", "]"}], ",", " ", 
    RowBox[{"FilterRules", "[", 
     RowBox[{
      RowBox[{"{", "opts", "}"}], ",", 
      RowBox[{"Options", "[", "Graphics3D", "]"}]}], "]"}]}], 
   "]"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"AnimateWalk", "[", 
   RowBox[{"rw_", ",", 
    RowBox[{"opts", ":", 
     RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
  RowBox[{"Module", "[", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"range", ",", 
      RowBox[{"len", "=", 
       RowBox[{"Length", "[", "rw", "]"}]}], ",", "steps", ",", "gr"}], "}"}],
     ",", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"range", "=", 
      RowBox[{"Map", "[", 
       RowBox[{
        RowBox[{
         RowBox[{"{", 
          RowBox[{
           RowBox[{"Min", "[", "#1", "]"}], ",", 
           RowBox[{"Max", "[", "#1", "]"}]}], "}"}], "&"}], ",", 
        RowBox[{"Transpose", "[", "rw", "]"}]}], "]"}]}], ";", 
     "\[IndentingNewLine]", 
     RowBox[{"gr", "=", 
      RowBox[{"If", "[", 
       RowBox[{
        RowBox[{
         RowBox[{
          RowBox[{"Dimensions", "[", "rw", "]"}], "[", 
          RowBox[{"[", "2", "]"}], "]"}], "\[Equal]", "2"}], ",", "Graphics", 
        ",", "Graphics3D"}], "]"}]}], ";", "\[IndentingNewLine]", 
     RowBox[{"Animate", "[", 
      RowBox[{
       RowBox[{"gr", "[", 
        RowBox[{
         RowBox[{"Line", "[", 
          RowBox[{"Take", "[", 
           RowBox[{"rw", ",", "steps"}], "]"}], "]"}], ",", 
         RowBox[{"PlotRange", "\[Rule]", "range"}], ",", 
         RowBox[{"PlotLabel", "\[Rule]", 
          RowBox[{"StringForm", "[", 
           RowBox[{"\"\<Walk length: ``\>\"", ",", "steps"}], "]"}]}]}], 
        "]"}], ",", "\[IndentingNewLine]", 
       RowBox[{"{", 
        RowBox[{
         RowBox[{"{", 
          RowBox[{"steps", ",", "2", ",", "\"\<Steps\>\""}], "}"}], ",", "2", 
         ",", "len", ",", "1"}], "}"}], ",", "opts"}], "]"}]}]}], 
   "]"}]}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["End private context", "Subsection"],

Cell[BoxData[
 RowBox[{"End", "[", "]"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["EndPackage", "Subsection"],

Cell[BoxData[
 RowBox[{"EndPackage", "[", "]"}]], "Input"]
}, Closed]]
}, Open  ]],

Cell[CellGroupData[{

Cell["Examples", "Section"],

Cell[CellGroupData[{

Cell["Load package", "Subsection"],

Cell[BoxData["Quit"], "Input"],

Cell[TextData[{
 "Load package (do ",
 StyleBox["not",
  FontSlant->"Italic"],
 " say \[OpenCurlyDoubleQuote]Yes\[CloseCurlyDoubleQuote] to evaluate init \
cells dialog)."
}], "Text"],

Cell[BoxData[
 RowBox[{"<<", "PWM`RandomWalks`"}]], "Input"]
}, Open  ]],

Cell[CellGroupData[{

Cell["Test usage messages", "Subsection"],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"?", "AnimateWalk"}]], "Input"],

Cell[BoxData[
 StyleBox["\<\"AnimateWalk[\\!\\(\\*\\nStyleBox[\\\"walk\\\", \\\"TI\\\"]\\), \
\\!\\(\\*\\nStyleBox[\\\"opts\\\", \\\"TI\\\"]\\)] creates an animation of a \
two-dimensional random walk. A red ball will be seen to move to the current \
position in the walk to aid in visualizing the animation.\"\>", 
  "MSG"]], "Print", "PrintUsage"]
}, Closed]],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"?", "Dimension"}]], "Input"],

Cell[BoxData[
 StyleBox["\<\"Dimension is an option to RandomWalk that determines whether \
the random walk will be a one-, two-, or three-dimensional walk. Possible \
values are 1, 2, or 3.\"\>", "MSG"]], "Print", "PrintUsage"]
}, Closed]],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"?", "LatticeWalk"}]], "Input"],

Cell[BoxData[
 StyleBox["\<\"LatticeWalk is an option to RandomWalk that determines whether \
the random walk will be a lattice walk or an off-lattice walk. Possible \
values are True and False.\"\>", "MSG"]], "Print", "PrintUsage"]
}, Closed]],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"?", "MeanSquareDistance"}]], "Input"],

Cell[BoxData[
 StyleBox["\<\"MeanSquareDistance[\\!\\(\\*\\nStyleBox[\\\"t\\\", \\\"TI\\\"]\
\\), \\!\\(\\*\\nStyleBox[\\\"m\\\", \\\"TI\\\"]\\)] computes the mean square \
end-to-end distance of \\!\\(\\*\\nStyleBox[\\\"m\\\", \\\"TI\\\"]\\) \
\\!\\(\\*\\nStyleBox[\\\"t\\\", \\\"TI\\\"]\\)-step lattice walks.\"\>", 
  "MSG"]], "Print", "PrintUsage"]
}, Closed]],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"?", "MeanSquareRadiusGyration"}]], "Input"],

Cell[BoxData[
 StyleBox["\<\"MeanSquareRadiusGyration[\\!\\(\\*\\nStyleBox[\\\"t\\\", \
\\\"TI\\\"]\\), \\!\\(\\*\\nStyleBox[\\\"m\\\", \\\"TI\\\"]\\)] computes the \
mean square radius of gyration of a random walk. This is the sum of the \
squares of the distances of the step locations from the center of mass, \
divided by the number of step locations, where the center of mass is the sum \
of the step locations divided by the number of step locations.\"\>", 
  "MSG"]], "Print", "PrintUsage"]
}, Closed]],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"?", "RadiusOfGyrationTensor"}]], "Input"],

Cell[BoxData[
 StyleBox["\<\"RadiusOfGyrationTensor[\\!\\(\\*\\nStyleBox[\\\"walk\\\", \
\\\"TI\\\"]\\)] computes the radius of gyration tensor for the random walk \
\\!\\(\\*\\nStyleBox[\\\"walk\\\", \\\"TI\\\"]\\).\"\>", "MSG"]], "Print", \
"PrintUsage"]
}, Closed]],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"?", "RandomWalk"}]], "Input"],

Cell[BoxData[
 StyleBox["\<\"RandomWalk[\\!\\(\\*\\nStyleBox[\\\"t\\\", \\\"TI\\\"]\\)] \
generates a \\!\\(\\*\\nStyleBox[\\\"t\\\", \\\"TI\\\"]\\)-step random walk. \
The default behavior gives a two-dimensional lattice walk with steps in one \
of the four compass directions. The option LatticeWalk takes values True or \
False. The value of the option Dimension can be any of 1, 2, or 3.\"\>", 
  "MSG"]], "Print", "PrintUsage"]
}, Closed]],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"?", "ShowWalk"}]], "Input"],

Cell[BoxData[
 StyleBox["\<\"ShowWalk[\\!\\(\\*\\nStyleBox[\\\"walk\\\", \\\"TI\\\"]\\)] \
displays a one, two, or three-dimensional random walk connecting each site \
with a line. Graphics options can be passed to ShowWalk. E.g., \
ShowWalk[\\!\\(\\*\\nStyleBox[\\\"walk\\\", \\\"TI\\\"]\\), \
Background\[Rule]GrayLevel[0]] to produce a black background.\"\>", 
  "MSG"]], "Print", "PrintUsage"]
}, Closed]]
}, Closed]],

Cell[CellGroupData[{

Cell["Exercise the functions and options", "Subsection"],

Cell[BoxData[
 RowBox[{"ShowWalk", "[", 
  RowBox[{"RandomWalk", "[", 
   RowBox[{
    SuperscriptBox["10", "5"], ",", 
    RowBox[{"Dimension", "\[Rule]", "1"}]}], "]"}], "]"}]], "Input"],

Cell[BoxData[
 RowBox[{"ShowWalk", "[", 
  RowBox[{"RandomWalk", "[", 
   RowBox[{
    SuperscriptBox["10", "5"], ",", 
    RowBox[{"Dimension", "\[Rule]", "2"}], ",", 
    RowBox[{"LatticeWalk", "\[Rule]", "False"}]}], "]"}], "]"}]], "Input"],

Cell[BoxData[
 RowBox[{"ShowWalk", "[", 
  RowBox[{"RandomWalk", "[", 
   RowBox[{
    SuperscriptBox["10", "4"], ",", 
    RowBox[{"Dimension", "\[Rule]", "3"}], ",", 
    RowBox[{"LatticeWalk", "\[Rule]", "True"}]}], "]"}], "]"}]], "Input"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"rw", "=", 
   RowBox[{"RandomWalk", "[", 
    RowBox[{"500", ",", 
     RowBox[{"Dimension", "\[Rule]", "3"}], ",", 
     RowBox[{"LatticeWalk", "\[Rule]", "False"}]}], "]"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{"AnimateWalk", "[", 
  RowBox[{"rw", ",", 
   RowBox[{"DisplayAllSteps", "\[Rule]", "True"}]}], "]"}]}], "Input"]
}, Closed]]
}, Open  ]]
},
AutoGeneratedPackage->Automatic,
ScreenStyleEnvironment->"Working",
WindowSize->{843, 715},
WindowMargins->{{56, Automatic}, {Automatic, 0}},
ShowSelection->True,
FrontEndVersion->"10.1 for Mac OS X x86 (32-bit, 64-bit Kernel) (March 23, \
2015)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)

(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[CellGroupData[{
Cell[567, 22, 102, 2, 60, "Section",
 InitializationGroup->True,
 CounterAssignments->{{"Chapter", 0}}],
Cell[672, 26, 398, 13, 29, "Text"],
Cell[CellGroupData[{
Cell[1095, 43, 30, 0, 41, "Subsection"],
Cell[1128, 45, 126, 3, 51, "Input"],
Cell[1257, 50, 239, 6, 51, "Input"],
Cell[1499, 58, 146, 4, 51, "Input"],
Cell[1648, 64, 486, 11, 112, "Input"],
Cell[2137, 77, 129, 3, 51, "Input"],
Cell[2269, 82, 147, 4, 51, "Input"],
Cell[2419, 88, 241, 7, 51, "Input"],
Cell[2663, 97, 256, 8, 51, "Input"],
Cell[2922, 107, 431, 11, 112, "Input"],
Cell[3356, 120, 149, 4, 51, "Input"],
Cell[3508, 126, 173, 5, 51, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[3718, 136, 34, 0, 33, "Subsection"],
Cell[3755, 138, 132, 3, 28, "Input"],
Cell[3890, 143, 88, 1, 28, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[4015, 149, 38, 0, 33, "Subsection"],
Cell[4056, 151, 304, 6, 80, "Input"],
Cell[4363, 159, 276, 6, 63, "Input"],
Cell[4642, 167, 282, 6, 80, "Input"],
Cell[4927, 175, 222, 5, 63, "Input"],
Cell[5152, 182, 465, 8, 114, "Input"],
Cell[5620, 192, 223, 5, 63, "Input"],
Cell[5846, 199, 386, 7, 97, "Input"],
Cell[6235, 208, 349, 7, 80, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[6621, 220, 38, 0, 33, "Subsection"],
Cell[6662, 222, 153, 4, 97, "Input"],
Cell[6818, 228, 191, 5, 158, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[7046, 238, 29, 0, 33, "Subsection"],
Cell[7078, 240, 245, 7, 97, "Input"],
Cell[7326, 249, 260, 7, 97, "Input"],
Cell[7589, 258, 153, 4, 97, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[7779, 267, 43, 0, 33, "Subsection"],
Cell[7825, 269, 74, 1, 97, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[7936, 275, 46, 0, 33, "Subsection"],
Cell[7985, 277, 805, 22, 110, "Input"],
Cell[8793, 301, 2560, 71, 205, "Input"],
Cell[11356, 374, 1626, 44, 167, "Input"],
Cell[12985, 420, 458, 15, 53, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[13480, 440, 50, 0, 33, "Subsection"],
Cell[13533, 442, 552, 15, 46, "Input"],
Cell[14088, 459, 637, 19, 77, "Input"],
Cell[14728, 480, 316, 8, 46, "Input"],
Cell[15047, 490, 1407, 40, 129, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[16491, 535, 45, 0, 33, "Subsection"],
Cell[16539, 537, 472, 14, 158, "Input"],
Cell[17014, 553, 696, 21, 218, "Input"],
Cell[17713, 576, 610, 19, 158, "Input"],
Cell[18326, 597, 1743, 49, 450, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[20106, 651, 41, 0, 33, "Subsection"],
Cell[20150, 653, 51, 1, 97, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[20238, 659, 32, 0, 33, "Subsection"],
Cell[20273, 661, 58, 1, 97, "Input"]
}, Closed]]
}, Open  ]],
Cell[CellGroupData[{
Cell[20380, 668, 27, 0, 60, "Section"],
Cell[CellGroupData[{
Cell[20432, 672, 34, 0, 41, "Subsection"],
Cell[20469, 674, 30, 0, 28, "Input"],
Cell[20502, 676, 183, 6, 29, "Text"],
Cell[20688, 684, 60, 1, 28, "Input"]
}, Open  ]],
Cell[CellGroupData[{
Cell[20785, 690, 41, 0, 41, "Subsection"],
Cell[CellGroupData[{
Cell[20851, 694, 54, 1, 97, "Input"],
Cell[20908, 697, 349, 5, 210, "Print"]
}, Closed]],
Cell[CellGroupData[{
Cell[21294, 707, 52, 1, 97, "Input"],
Cell[21349, 710, 228, 3, 210, "Print"]
}, Closed]],
Cell[CellGroupData[{
Cell[21614, 718, 54, 1, 97, "Input"],
Cell[21671, 721, 232, 3, 210, "Print"]
}, Closed]],
Cell[CellGroupData[{
Cell[21940, 729, 61, 1, 97, "Input"],
Cell[22004, 732, 352, 5, 147, "Print"]
}, Closed]],
Cell[CellGroupData[{
Cell[22393, 742, 67, 1, 97, "Input"],
Cell[22463, 745, 497, 7, 332, "Print"]
}, Closed]],
Cell[CellGroupData[{
Cell[22997, 757, 65, 1, 97, "Input"],
Cell[23065, 760, 256, 4, 147, "Print"]
}, Closed]],
Cell[CellGroupData[{
Cell[23358, 769, 53, 1, 97, "Input"],
Cell[23414, 772, 432, 6, 272, "Print"]
}, Closed]],
Cell[CellGroupData[{
Cell[23883, 783, 51, 1, 97, "Input"],
Cell[23937, 786, 397, 6, 272, "Print"]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[24383, 798, 56, 0, 33, "Subsection"],
Cell[24442, 800, 188, 5, 63, "Input"],
Cell[24633, 807, 243, 6, 63, "Input"],
Cell[24879, 815, 242, 6, 63, "Input"],
Cell[25124, 823, 370, 10, 82, "Input"]
}, Closed]]
}, Open  ]]
}
]
*)

(* End of internal cache information *)
