(* ::Package:: *)

(************************************************************************)
(* This file was generated automatically by the Mathematica front end.  *)
(* It contains Initialization cells from a Notebook file, which         *)
(* typically will have the same name as this file except ending in      *)
(* ".nb" instead of ".m".                                               *)
(*                                                                      *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs.  Doing so is equivalent   *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end.                                                                 *)
(*                                                                      *)
(* DO NOT EDIT THIS FILE.  This entire file is regenerated              *)
(* automatically each time the parent Notebook file is saved in the     *)
(* Mathematica front end.  Any changes you make to this file will be    *)
(* overwritten.                                                         *)
(************************************************************************)



(* :Name: PwM`Chap01Introduction` *)


(* :Book: Programming with Mathematica: An Introduction *)


(* :Author: Paul Wellin *)


(* :Summary: This package contains the functions defined in Chapter 1, An introduction to Mathematica. *)


(* :Context: PwM`Chap01Introduction` *)


(* :Package Version: 1.0 *)


(* :Copyright: Copyright 2012, Cambridge University Press *)


(* :History: Version 1.0 by Paul Wellin, Dec 2012 *)


(* :Keywords: regular polygons, percolation. *)


(* :Source: Chap01Introduction.nb *)


(* :Mathematica Version: 8.0.4 *)


BeginPackage["PwM`Chap01Introduction`"]


RegularPolygon::usage="RegularPolygon[\!\(\*
StyleBox[\"n\", \"TI\"]\)] creates a regular \!\(\*
StyleBox[\"n\", \"TI\"]\)-gon graphics primitive that can be displayed with Graphics.";


BondPercolation::baddims="The arguments `1` and `2`, giving the grid dimensions, should be positive integers.";


BondPercolation::usage="BondPercolation[{\!\(\*
StyleBox[\"m\", \"TI\"]\),\!\(\*
StyleBox[\"n\", \"TI\"]\)},\!\(\*
StyleBox[\"prob\", \"TI\"]\)] simulates a bond percolation on an \!\(\*
StyleBox[\"m\", \"TI\"]\)\[Cross]\!\(\*
StyleBox[\"n\", \"TI\"]\) rectangular lattice using 0<\!\(\*
StyleBox[\"prob\", \"TI\"]\)<1 as the probability of a bond forming between a site and its neighbors.";


FindPercolationPath::usage="FindPercolationPath[\!\(\*
StyleBox[\"gr\", \"TI\"]\),{\!\(\*
StyleBox[\"dimx\", \"TI\"]\),\!\(\*
StyleBox[\"dimy\", \"TI\"]\)}] returns a connected path (if it exists) for the graph \!\(\*
StyleBox[\"gr\", \"TI\"]\) of dimension \!\(\*
StyleBox[\"dimx\", \"TI\"]\)\[Cross]\!\(\*
StyleBox[\"dimy\", \"TI\"]\).";


Options[BondPercolation]=Options[Graph];


Begin["`Private`"]


vertices[n_]:=Table[{Cos[2\[Pi] \[Alpha]/n],Sin[2\[Pi] \[Alpha]/n]},{\[Alpha],0,n}]


RegularPolygon/:Graphics[RegularPolygon[n_]]:=
Graphics[Line[vertices[n]],AspectRatio->Automatic]


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


FindPercolationPath[gr_Graph,dims:{dimx_,dimy_}]:=Module[{bot,top,spFun},
bot=Select[VertexList[GridGraph[dims]],Mod[#,dimx]==1&];
top=Select[VertexList[GridGraph[dims]],Mod[#,dimx]==0&];
spFun=FindShortestPath[gr,All,All];
Cases[Outer[spFun,bot,top],lis_List/;Length[lis]!=0,{2}]
]


End[]


EndPackage[]



