(* Content-type: application/vnd.wolfram.mathematica *)

(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)

(* CreatedBy='Mathematica 8.0' *)

(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[       157,          7]
NotebookDataLength[     16140,        592]
NotebookOptionsPosition[     12576,        466]
NotebookOutlinePosition[     13289,        494]
CellTagsIndexPosition[     13163,        488]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{

Cell[CellGroupData[{
Cell["PwM`Chap01Introduction`", "Section",
 InitializationGroup->True],

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`Chap01Introduction`"}]}], " ", 
  "*)"}]], "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", " ", "functions", 
      " ", "defined", " ", "in", " ", "Chapter", " ", "1"}]}]}], ",", " ", 
   RowBox[{"An", " ", "introduction", " ", "to", " ", 
    RowBox[{"Mathematica", "."}]}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Context", ":", " ", "PwM`Chap01Introduction`"}]}], " ", 
  "*)"}]], "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[{"regular", " ", "polygons"}]}]}], ",", " ", 
   RowBox[{"percolation", "."}]}], " ", "*)"}]], "Input"],

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

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

Cell[CellGroupData[{

Cell["BeginPackage", "Subsection"],

Cell[CellGroupData[{

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

Cell[BoxData["\<\"PwM`Chap01Introduction`\"\>"], "Output"]
}, Open  ]]
}, Closed]],

Cell[CellGroupData[{

Cell["Usage messages for public functions", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"RegularPolygon", "::", "usage"}], "=", 
   "\"\<RegularPolygon[n] creates a regular n-gon graphics primitive that can \
be displayed with Graphics.\>\""}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"BondPercolation", "::", "baddims"}], "=", 
   "\"\<The arguments `1` and `2`, giving the grid dimensions, should be \
positive integers.\>\""}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"BondPercolation", "::", "usage"}], "=", 
   "\"\<BondPercolation[{m,n},prob] simulates a bond percolation on an m\
\[Cross]n rectangular lattice using 0<prob<1 as the probability of a bond \
forming between a site and its neighbors.\>\""}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"FindPercolationPath", "::", "usage"}], "=", 
   "\"\<FindPercolationPath[gr,{dimx,dimy}] returns a connected path (if it \
exists) for the graph gr of dimension dimx\[Cross]dimy.\>\""}], 
  ";"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["Options", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"Options", "[", "BondPercolation", "]"}], "=", 
   RowBox[{"Options", "[", "Graph", "]"}]}], ";"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["Private context", "Subsection"],

Cell[CellGroupData[{

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

Cell[BoxData["\<\"PwM`Chap01Introduction`Private`\"\>"], "Output"]
}, Open  ]]
}, Closed]],

Cell[CellGroupData[{

Cell["Function definitions (public and private)", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{"vertices", "[", "n_", "]"}], ":=", 
  RowBox[{"Table", "[", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{
      RowBox[{"Cos", "[", 
       RowBox[{"2", "\[Pi]", " ", 
        RowBox[{"\[Alpha]", "/", "n"}]}], "]"}], ",", 
      RowBox[{"Sin", "[", 
       RowBox[{"2", "\[Pi]", " ", 
        RowBox[{"\[Alpha]", "/", "n"}]}], "]"}]}], "}"}], ",", 
    RowBox[{"{", 
     RowBox[{"\[Alpha]", ",", "0", ",", "n"}], "}"}]}], "]"}]}]], "Input"],

Cell[BoxData[
 RowBox[{"RegularPolygon", "/:", 
  RowBox[{"Graphics", "[", 
   RowBox[{"RegularPolygon", "[", "n_", "]"}], "]"}], ":=", 
  "\[IndentingNewLine]", 
  RowBox[{"Graphics", "[", 
   RowBox[{
    RowBox[{"Line", "[", 
     RowBox[{"vertices", "[", "n", "]"}], "]"}], ",", 
    RowBox[{"AspectRatio", "\[Rule]", "Automatic"}]}], "]"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"BondPercolation", "[", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"m_", ",", "n_"}], "}"}], ",", "prob_", ",", 
    RowBox[{"opts", ":", 
     RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=", 
  "\[IndentingNewLine]", 
  RowBox[{"Module", "[", 
   RowBox[{
    RowBox[{"{", "gr", "}"}], ",", "\[IndentingNewLine]", 
    RowBox[{"If", "[", 
     RowBox[{
      RowBox[{"Not", "[", 
       RowBox[{
        RowBox[{"IntegerQ", "[", "m", "]"}], "&&", 
        RowBox[{"IntegerQ", "[", "n", "]"}]}], "]"}], ",", 
      "\[IndentingNewLine]", 
      RowBox[{"Message", "[", 
       RowBox[{
        RowBox[{"BondPercolation", "::", "baddims"}], ",", "m", ",", "n"}], 
       "]"}], ",", "\[IndentingNewLine]", 
      RowBox[{
       RowBox[{"gr", "=", 
        RowBox[{"GridGraph", "[", 
         RowBox[{"{", 
          RowBox[{"m", ",", "n"}], "}"}], "]"}]}], ";", "\[IndentingNewLine]", 
       RowBox[{"Graph", "[", 
        RowBox[{
         RowBox[{"Pick", "[", 
          RowBox[{
           RowBox[{"EdgeList", "[", "gr", "]"}], ",", "\[IndentingNewLine]", 
           RowBox[{"RandomVariate", "[", 
            RowBox[{
             RowBox[{"BernoulliDistribution", "[", "prob", "]"}], ",", 
             "\[IndentingNewLine]", 
             RowBox[{"EdgeCount", "[", "gr", "]"}]}], "]"}], ",", "1"}], 
          "]"}], ",", "\[IndentingNewLine]", "opts"}], "]"}]}]}], 
     "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "]"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"FindPercolationPath", "[", 
   RowBox[{"gr_Graph", ",", 
    RowBox[{"dims", ":", 
     RowBox[{"{", 
      RowBox[{"dimx_", ",", "dimy_"}], "}"}]}]}], "]"}], ":=", 
  RowBox[{"Module", "[", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"bot", ",", "top", ",", "spFun"}], "}"}], ",", 
    "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"bot", "=", 
      RowBox[{"Select", "[", 
       RowBox[{
        RowBox[{"VertexList", "[", 
         RowBox[{"GridGraph", "[", "dims", "]"}], "]"}], ",", 
        RowBox[{
         RowBox[{
          RowBox[{"Mod", "[", 
           RowBox[{"#", ",", "dimx"}], "]"}], "\[Equal]", "1"}], "&"}]}], 
       "]"}]}], ";", "\[IndentingNewLine]", 
     RowBox[{"top", "=", 
      RowBox[{"Select", "[", 
       RowBox[{
        RowBox[{"VertexList", "[", 
         RowBox[{"GridGraph", "[", "dims", "]"}], "]"}], ",", 
        RowBox[{
         RowBox[{
          RowBox[{"Mod", "[", 
           RowBox[{"#", ",", "dimx"}], "]"}], "\[Equal]", "0"}], "&"}]}], 
       "]"}]}], ";", "\[IndentingNewLine]", 
     RowBox[{"spFun", "=", 
      RowBox[{"FindShortestPath", "[", 
       RowBox[{"gr", ",", "All", ",", "All"}], "]"}]}], ";", 
     "\[IndentingNewLine]", 
     RowBox[{"Cases", "[", 
      RowBox[{
       RowBox[{"Outer", "[", 
        RowBox[{"spFun", ",", "bot", ",", "top"}], "]"}], ",", 
       RowBox[{"lis_List", "/;", 
        RowBox[{
         RowBox[{"Length", "[", "lis", "]"}], "\[NotEqual]", "0"}]}], ",", 
       RowBox[{"{", "2", "}"}]}], "]"}]}]}], "\[IndentingNewLine]", 
   "]"}]}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

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

Cell[CellGroupData[{

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

Cell[BoxData["\<\"PwM`Chap01Introduction`Private`\"\>"], "Output"]
}, Open  ]]
}, 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[
 RowBox[{"Quit", "[", "]"}]], "Input"],

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

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

Cell[CellGroupData[{

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

Cell[CellGroupData[{

Cell[BoxData["RegularPolygon"], "Input"],

Cell[BoxData[
 StyleBox["\<\"RegularPolygon[n] creates a regular n-gon graphics primitive \
that can be displayed with Graphics.\"\>", "MSG"]], "Print", "PrintUsage",
 CellTags->"Info3637581910-7637176"]
}, Closed]],

Cell[CellGroupData[{

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

Cell[BoxData[
 StyleBox["\<\"BondPercolation[{m,n},prob] simulates a bond percolation on an \
m\[Cross]n rectangular lattice using 0<prob<1 as the probability of a bond \
forming between a site and its neighbors.\"\>", "MSG"]], "Print", "PrintUsage",
 CellTags->"Info3637581954-7637176"]
}, Closed]]
}, Closed]],

Cell[CellGroupData[{

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

Cell[CellGroupData[{

Cell["RegularPolygon", "Subsubsection"],

Cell[BoxData[
 RowBox[{"Table", "[", 
  RowBox[{
   RowBox[{"Graphics", "[", 
    RowBox[{"RegularPolygon", "[", "n", "]"}], "]"}], ",", 
   RowBox[{"{", 
    RowBox[{"n", ",", "3", ",", "6"}], "}"}]}], "]"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["BondPercolation", "Subsubsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{"gr", "=", 
   RowBox[{"BondPercolation", "[", 
    RowBox[{
     RowBox[{"{", 
      RowBox[{"13", ",", "21"}], "}"}], ",", "0.47"}], "]"}]}], 
  ";"}]], "Input"],

Cell[BoxData[
 RowBox[{"HighlightGraph", "[", 
  RowBox[{
   RowBox[{"GridGraph", "[", 
    RowBox[{"{", 
     RowBox[{"13", ",", "21"}], "}"}], "]"}], ",", "gr", ",", 
   RowBox[{"GraphHighlightStyle", "\[Rule]", "\"\<DehighlightGray\>\""}]}], 
  "]"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["FindPercolationPath", "Subsubsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{"path", "=", 
   RowBox[{"FindPercolationPath", "[", 
    RowBox[{"gr", ",", 
     RowBox[{"{", 
      RowBox[{"13", ",", "21"}], "}"}]}], "]"}]}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{"HighlightGraph", "[", 
  RowBox[{
   RowBox[{"GridGraph", "[", 
    RowBox[{"{", 
     RowBox[{"13", ",", "21"}], "}"}], "]"}], ",", 
   RowBox[{"Apply", "[", 
    RowBox[{"UndirectedEdge", ",", 
     RowBox[{"Map", "[", 
      RowBox[{
       RowBox[{
        RowBox[{"Partition", "[", 
         RowBox[{"#", ",", "2", ",", "1"}], "]"}], "&"}], ",", "path"}], 
      "]"}], ",", 
     RowBox[{"{", "2", "}"}]}], "]"}], ",", 
   RowBox[{"GraphHighlightStyle", "\[Rule]", "\"\<DehighlightGray\>\""}]}], 
  "]"}]], "Input"]
}, Closed]]
}, Open  ]]
}, Open  ]]
},
AutoGeneratedPackage->Automatic,
WindowSize->{801, 688},
WindowMargins->{{117, 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->{
 "Info3637581910-7637176"->{
  Cell[10183, 374, 203, 3, 43, "Print",
   CellTags->"Info3637581910-7637176"]},
 "Info3637581954-7637176"->{
  Cell[10484, 385, 287, 4, 61, "Print",
   CellTags->"Info3637581954-7637176"]}
 }
*)
(*CellTagsIndex
CellTagsIndex->{
 {"Info3637581910-7637176", 12949, 479},
 {"Info3637581954-7637176", 13059, 482}
 }
*)
(*NotebookFileOutline
Notebook[{
Cell[CellGroupData[{
Cell[579, 22, 70, 1, 60, "Section",
 InitializationGroup->True],
Cell[652, 25, 398, 13, 29, "Text"],
Cell[CellGroupData[{
Cell[1075, 42, 30, 0, 41, "Subsection"],
Cell[1108, 44, 136, 4, 28, "Input"],
Cell[1247, 50, 239, 6, 28, "Input"],
Cell[1489, 58, 146, 4, 28, "Input"],
Cell[1638, 64, 379, 9, 46, "Input"],
Cell[2020, 75, 139, 4, 28, "Input"],
Cell[2162, 81, 147, 4, 28, "Input"],
Cell[2312, 87, 241, 7, 28, "Input"],
Cell[2556, 96, 256, 8, 28, "Input"],
Cell[2815, 106, 213, 6, 28, "Input"],
Cell[3031, 114, 156, 4, 28, "Input"],
Cell[3190, 120, 173, 5, 28, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[3400, 130, 34, 0, 33, "Subsection"],
Cell[CellGroupData[{
Cell[3459, 134, 98, 2, 51, "Input"],
Cell[3560, 138, 58, 0, 70, "Output"]
}, Open  ]]
}, Closed]],
Cell[CellGroupData[{
Cell[3667, 144, 57, 0, 33, "Subsection"],
Cell[3727, 146, 219, 5, 63, "Input"],
Cell[3949, 153, 208, 5, 63, "Input"],
Cell[4160, 160, 304, 6, 80, "Input"],
Cell[4467, 168, 253, 6, 63, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[4757, 179, 29, 0, 33, "Subsection"],
Cell[4789, 181, 155, 4, 51, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[4981, 190, 37, 0, 33, "Subsection"],
Cell[CellGroupData[{
Cell[5043, 194, 74, 1, 51, "Input"],
Cell[5120, 197, 66, 0, 70, "Output"]
}, Open  ]]
}, Closed]],
Cell[CellGroupData[{
Cell[5235, 203, 63, 0, 33, "Subsection"],
Cell[5301, 205, 475, 14, 33, "Input"],
Cell[5779, 221, 357, 9, 53, "Input"],
Cell[6139, 232, 1494, 39, 224, "Input"],
Cell[7636, 273, 1586, 45, 148, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[9259, 323, 41, 0, 33, "Subsection"],
Cell[CellGroupData[{
Cell[9325, 327, 51, 1, 51, "Input"],
Cell[9379, 330, 66, 0, 70, "Output"]
}, Open  ]]
}, Closed]],
Cell[CellGroupData[{
Cell[9494, 336, 32, 0, 33, "Subsection"],
Cell[9529, 338, 58, 1, 51, "Input"]
}, Closed]]
}, Open  ]],
Cell[CellGroupData[{
Cell[9636, 345, 27, 0, 60, "Section"],
Cell[CellGroupData[{
Cell[9688, 349, 34, 0, 41, "Subsection"],
Cell[9725, 351, 52, 1, 28, "Input"],
Cell[9780, 354, 187, 6, 29, "Text"],
Cell[9970, 362, 67, 1, 28, "Input"]
}, Open  ]],
Cell[CellGroupData[{
Cell[10074, 368, 41, 0, 41, "Subsection"],
Cell[CellGroupData[{
Cell[10140, 372, 40, 0, 28, "Input"],
Cell[10183, 374, 203, 3, 43, "Print",
 CellTags->"Info3637581910-7637176"]
}, Closed]],
Cell[CellGroupData[{
Cell[10423, 382, 58, 1, 22, "Input"],
Cell[10484, 385, 287, 4, 61, "Print",
 CellTags->"Info3637581954-7637176"]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[10820, 395, 56, 0, 33, "Subsection"],
Cell[CellGroupData[{
Cell[10901, 399, 39, 0, 32, "Subsubsection"],
Cell[10943, 401, 219, 6, 51, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[11199, 412, 40, 0, 26, "Subsubsection"],
Cell[11242, 414, 197, 7, 51, "Input"],
Cell[11442, 423, 264, 7, 82, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[11743, 435, 44, 0, 26, "Subsubsection"],
Cell[11790, 437, 198, 6, 51, "Input"],
Cell[11991, 445, 545, 16, 112, "Input"]
}, Closed]]
}, Open  ]]
}, Open  ]]
}
]
*)

(* End of internal cache information *)
