(* 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[     15695,        582]
NotebookOptionsPosition[     12417,        463]
NotebookOutlinePosition[     12884,        484]
CellTagsIndexPosition[     12841,        481]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{

Cell[CellGroupData[{
Cell["PwM`Chap03Lists`", "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`Chap03Lists`"}]}], " ", "*)"}]], "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", " ", "3"}]}]}], ",", " ", 
   RowBox[{"Lists", "."}]}], " ", "*)"}]], "Input"],

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

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

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{
   RowBox[{":", "Copyright", ":", " ", 
    RowBox[{"Copyright", " ", "2012"}]}], ",", " ", 
   RowBox[{"Paul", " ", "R", " ", "Wellin"}]}], " ", "*)"}]], "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[{"matrix", " ", "columns"}]}]}], ",", " ", "lattice", ",", " ", 
   "Ngram"}], " ", "*)"}]], "Input"],

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

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

Cell[CellGroupData[{

Cell["BeginPackage", "Subsection"],

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

Cell[CellGroupData[{

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

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"AddColumn", "::", "usage"}], "=", 
   "\"\<AddColumn[mat, col, pos] inserts the column vector col to the matrix \
mat at position pos.\>\""}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"Lattice", "::", "usage"}], "=", 
   "\"\<Lattice[{{xmin, xmax}, {ymin, ymax}}] creates a two-dimensional \
rectangular lattice with bounds given by the integers xmin, xmax, etc.\>\""}],
   ";"}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"NGrams", "::", "usage"}], "=", 
   "\"\<NGrams[text, n] splits text into overlapping lists of n words.\>\""}],
   ";"}]], "Input"]
}, Closed]],

Cell["Options", "Subsection"],

Cell[CellGroupData[{

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

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

Cell[CellGroupData[{

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

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"AddColumn", "[", 
    RowBox[{"mat_", ",", "col_", ",", "pos_"}], "]"}], "/;", 
   RowBox[{
    RowBox[{"Length", "[", "col", "]"}], "\[Equal]", 
    RowBox[{"Length", "[", "mat", "]"}]}]}], ":=", 
  RowBox[{"Transpose", "[", 
   RowBox[{"Insert", "[", 
    RowBox[{
     RowBox[{"Transpose", "[", "mat", "]"}], ",", "col", ",", "pos"}], "]"}], 
   "]"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"Lattice", "[", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"xmin_Integer", ",", "xmax_Integer"}], "}"}], ",", 
    RowBox[{"{", 
     RowBox[{"ymin_Integer", ",", "ymax_Integer"}], "}"}]}], "]"}], ":=", 
  RowBox[{"Module", "[", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"hlines", ",", "vlines", ",", "coords"}], "}"}], ",", 
    "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"hlines", "=", 
      RowBox[{"Table", "[", 
       RowBox[{
        RowBox[{"{", 
         RowBox[{
          RowBox[{"{", 
           RowBox[{"xmin", ",", "y"}], "}"}], ",", 
          RowBox[{"{", 
           RowBox[{"xmax", ",", "y"}], "}"}]}], "}"}], ",", 
        RowBox[{"{", 
         RowBox[{"y", ",", "ymin", ",", "ymax"}], "}"}]}], "]"}]}], ";", 
     "\[IndentingNewLine]", 
     RowBox[{"vlines", "=", 
      RowBox[{"Table", "[", 
       RowBox[{
        RowBox[{"{", 
         RowBox[{
          RowBox[{"{", 
           RowBox[{"x", ",", "ymin"}], "}"}], ",", 
          RowBox[{"{", 
           RowBox[{"x", ",", "ymax"}], "}"}]}], "}"}], ",", 
        RowBox[{"{", 
         RowBox[{"x", ",", "xmin", ",", "xmax"}], "}"}]}], "]"}]}], ";", 
     "\[IndentingNewLine]", 
     RowBox[{"coords", "=", 
      RowBox[{"Flatten", "[", 
       RowBox[{
        RowBox[{"{", 
         RowBox[{"hlines", ",", "vlines"}], "}"}], ",", "1"}], "]"}]}], ";", 
     "\[IndentingNewLine]", 
     RowBox[{"Graphics", "[", 
      RowBox[{"Line", "[", "coords", "]"}], "]"}]}]}], "]"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"NGrams", "[", 
   RowBox[{"text_", ",", "n_"}], "]"}], ":=", 
  RowBox[{"Partition", "[", "\[IndentingNewLine]", 
   RowBox[{
    RowBox[{"StringSplit", "[", 
     RowBox[{"text", ",", 
      RowBox[{"RegularExpression", "[", "\"\<\\\\W+\>\"", "]"}]}], "]"}], ",",
     "n", ",", "1"}], "]"}]}]], "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["Quit current session:", "Text"],

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`Chap03Lists`"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

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

Cell[CellGroupData[{

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

Cell[BoxData[
 StyleBox["\<\"AddColumn[\\!\\(\\*\\nStyleBox[\\\"mat\\\", \\\"TI\\\"]\\), \
\\!\\(\\*\\nStyleBox[\\\"col\\\", \\\"TI\\\"]\\), \
\\!\\(\\*\\nStyleBox[\\\"pos\\\", \\\"TI\\\"]\\)] inserts the column vector \
\\!\\(\\*\\nStyleBox[\\\"col\\\", \\\"TI\\\"]\\) to the matrix \
\\!\\(\\*\\nStyleBox[\\\"mat\\\", \\\"TI\\\"]\\) at position \
\\!\\(\\*\\nStyleBox[\\\"pos\\\", \\\"TI\\\"]\\).\"\>", "MSG"]], "Print", \
"PrintUsage"]
}, Closed]],

Cell[CellGroupData[{

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

Cell[BoxData[
 StyleBox["\<\"Lattice[{{\!\(\*\\nStyleBox[\\\"xmin\\\", \\\"TI\\\"]\), \
\!\(\*\\nStyleBox[\\\"xmax\\\", \\\"TI\\\"]\)}, {\!\(\*\\nStyleBox[\\\"ymin\\\
\", \\\"TI\\\"]\), \!\(\*\\nStyleBox[\\\"ymax\\\", \\\"TI\\\"]\)}}] creates a \
two-dimensional rectangular lattice with bounds given by the integers \
\!\(\*\\nStyleBox[\\\"xmin\\\", \\\"TI\\\"]\), \
\!\(\*\\nStyleBox[\\\"xmax\\\", \\\"TI\\\"]\), etc.\"\>", "MSG"]], "Print", \
"PrintUsage"]
}, Closed]],

Cell[CellGroupData[{

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

Cell[BoxData[
 StyleBox["\<\"NGrams[\!\(\*\\nStyleBox[\\\"text\\\", \\\"TI\\\"]\), \
\!\(\*\\nStyleBox[\\\"n\\\", \\\"TI\\\"]\)] splits \!\(\*\\nStyleBox[\\\"text\
\\\", \\\"TI\\\"]\) into overlapping lists of \!\(\*\\nStyleBox[\\\"n\\\", \\\
\"TI\\\"]\) words.\"\>", "MSG"]], "Print", "PrintUsage"]
}, Closed]]
}, Closed]],

Cell[CellGroupData[{

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

Cell[CellGroupData[{

Cell["AddColumn", "Subsubsection"],

Cell[CellGroupData[{

Cell[BoxData[{
 RowBox[{
  RowBox[{"mat", "=", 
   RowBox[{"RandomInteger", "[", 
    RowBox[{"9", ",", 
     RowBox[{"{", 
      RowBox[{"4", ",", "4"}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", 
 RowBox[{"MatrixForm", "[", "mat", "]"}]}], "Input"],

Cell[BoxData[
 TagBox[
  RowBox[{"(", "\[NoBreak]", GridBox[{
     {"2", "1", "5", "6"},
     {"4", "7", "0", "8"},
     {"0", "2", "6", "5"},
     {"7", "9", "7", "9"}
    },
    GridBoxAlignment->{
     "Columns" -> {{Center}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, 
      "RowsIndexed" -> {}},
    GridBoxSpacings->{"Columns" -> {
        Offset[0.27999999999999997`], {
         Offset[0.7]}, 
        Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
        Offset[0.2], {
         Offset[0.4]}, 
        Offset[0.2]}, "RowsIndexed" -> {}}], "\[NoBreak]", ")"}],
  Function[BoxForm`e$, 
   MatrixForm[BoxForm`e$]]]], "Output"]
}, Closed]],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{
  RowBox[{"AddColumn", "[", 
   RowBox[{"mat", ",", 
    RowBox[{"{", 
     RowBox[{"a", ",", "b", ",", "c", ",", "d"}], "}"}], ",", "3"}], "]"}], "//",
   "MatrixForm"}]], "Input"],

Cell[BoxData[
 TagBox[
  RowBox[{"(", "\[NoBreak]", GridBox[{
     {"2", "1", "a", "5", "6"},
     {"4", "7", "b", "0", "8"},
     {"0", "2", "c", "6", "5"},
     {"7", "9", "d", "7", "9"}
    },
    GridBoxAlignment->{
     "Columns" -> {{Center}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, 
      "RowsIndexed" -> {}},
    GridBoxSpacings->{"Columns" -> {
        Offset[0.27999999999999997`], {
         Offset[0.7]}, 
        Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
        Offset[0.2], {
         Offset[0.4]}, 
        Offset[0.2]}, "RowsIndexed" -> {}}], "\[NoBreak]", ")"}],
  Function[BoxForm`e$, 
   MatrixForm[BoxForm`e$]]]], "Output"]
}, Closed]]
}, Closed]],

Cell[CellGroupData[{

Cell["Lattice", "Subsubsection"],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"Lattice", "[", 
  RowBox[{
   RowBox[{"{", 
    RowBox[{
     RowBox[{"-", "3"}], ",", "3"}], "}"}], ",", 
   RowBox[{"{", 
    RowBox[{
     RowBox[{"-", "2"}], ",", "2"}], "}"}]}], "]"}]], "Input"],

Cell[BoxData[
 GraphicsBox[
  LineBox[{{{-3, -2}, {3, -2}}, {{-3, -1}, {3, -1}}, {{-3, 0}, {3, 0}}, {{-3, 
   1}, {3, 1}}, {{-3, 2}, {3, 2}}, {{-3, -2}, {-3, 2}}, {{-2, -2}, {-2, 
   2}}, {{-1, -2}, {-1, 2}}, {{0, -2}, {0, 2}}, {{1, -2}, {1, 2}}, {{2, -2}, {
   2, 2}}, {{3, -2}, {3, 2}}}],
  ImageSize->{220.7999999999999, Automatic}]], "Output"]
}, Closed]]
}, Closed]],

Cell[CellGroupData[{

Cell["NGrams", "Subsubsection"],

Cell[CellGroupData[{

Cell[BoxData[{
 RowBox[{
  RowBox[{"sentence", "=", "\"\<Colorless green ideas sleep furiously.\>\""}],
   ";"}], "\[IndentingNewLine]", 
 RowBox[{"NGrams", "[", 
  RowBox[{"sentence", ",", "3"}], "]"}]}], "Input"],

Cell[BoxData[
 RowBox[{"{", 
  RowBox[{
   RowBox[{"{", 
    RowBox[{"\<\"Colorless\"\>", ",", "\<\"green\"\>", ",", "\<\"ideas\"\>"}],
     "}"}], ",", 
   RowBox[{"{", 
    RowBox[{"\<\"green\"\>", ",", "\<\"ideas\"\>", ",", "\<\"sleep\"\>"}], 
    "}"}], ",", 
   RowBox[{"{", 
    RowBox[{"\<\"ideas\"\>", ",", "\<\"sleep\"\>", ",", "\<\"furiously\"\>"}],
     "}"}]}], "}"}]], "Output"]
}, Closed]]
}, Closed]]
}, Open  ]]
}, Open  ]]
},
AutoGeneratedPackage->Automatic,
ScreenStyleEnvironment->"Working",
WindowSize->{
  Scaled[0.5], 
  Scaled[0.75]},
WindowMargins->{{15, Automatic}, {Automatic, 15}},
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[579, 22, 63, 1, 60, "Section",
 InitializationGroup->True],
Cell[645, 25, 398, 13, 29, "Text"],
Cell[CellGroupData[{
Cell[1068, 42, 30, 0, 41, "Subsection"],
Cell[1101, 44, 126, 3, 28, "Input"],
Cell[1230, 49, 239, 6, 28, "Input"],
Cell[1472, 57, 146, 4, 28, "Input"],
Cell[1621, 63, 315, 8, 46, "Input"],
Cell[1939, 73, 129, 3, 28, "Input"],
Cell[2071, 78, 147, 4, 28, "Input"],
Cell[2221, 84, 209, 5, 28, "Input"],
Cell[2433, 91, 256, 8, 28, "Input"],
Cell[2692, 101, 211, 6, 28, "Input"],
Cell[2906, 109, 149, 4, 28, "Input"],
Cell[3058, 115, 173, 5, 28, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[3268, 125, 34, 0, 33, "Subsection"],
Cell[3305, 127, 88, 1, 63, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[3430, 133, 57, 0, 33, "Subsection"],
Cell[3490, 135, 206, 5, 63, "Input"],
Cell[3699, 142, 252, 6, 80, "Input"],
Cell[3954, 150, 177, 5, 46, "Input"]
}, Closed]],
Cell[4146, 158, 29, 0, 33, "Subsection"],
Cell[CellGroupData[{
Cell[4200, 162, 37, 0, 33, "Subsection"],
Cell[4240, 164, 74, 1, 63, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[4351, 170, 63, 0, 33, "Subsection"],
Cell[4417, 172, 414, 12, 102, "Input"],
Cell[4834, 186, 1513, 45, 251, "Input"],
Cell[6350, 233, 340, 9, 102, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[6727, 247, 41, 0, 33, "Subsection"],
Cell[6771, 249, 51, 1, 63, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[6859, 255, 32, 0, 33, "Subsection"],
Cell[6894, 257, 58, 1, 63, "Input"]
}, Closed]]
}, Open  ]],
Cell[CellGroupData[{
Cell[7001, 264, 27, 0, 60, "Section"],
Cell[CellGroupData[{
Cell[7053, 268, 34, 0, 41, "Subsection"],
Cell[7090, 270, 37, 0, 62, "Text"],
Cell[7130, 272, 30, 0, 63, "Input"],
Cell[7163, 274, 183, 6, 62, "Text"],
Cell[7349, 282, 60, 1, 63, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[7446, 288, 41, 0, 33, "Subsection"],
Cell[CellGroupData[{
Cell[7512, 292, 52, 1, 63, "Input"],
Cell[7567, 295, 438, 7, 95, "Print"]
}, Closed]],
Cell[CellGroupData[{
Cell[8042, 307, 50, 1, 63, "Input"],
Cell[8095, 310, 459, 7, 135, "Print"]
}, Closed]],
Cell[CellGroupData[{
Cell[8591, 322, 49, 1, 63, "Input"],
Cell[8643, 325, 299, 4, 95, "Print"]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[8991, 335, 56, 0, 33, "Subsection"],
Cell[CellGroupData[{
Cell[9072, 339, 34, 0, 32, "Subsubsection"],
Cell[CellGroupData[{
Cell[9131, 343, 256, 7, 102, "Input"],
Cell[9390, 352, 659, 19, 176, "Output"]
}, Closed]],
Cell[CellGroupData[{
Cell[10086, 376, 205, 6, 63, "Input"],
Cell[10294, 384, 679, 19, 176, "Output"]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[11022, 409, 32, 0, 26, "Subsubsection"],
Cell[CellGroupData[{
Cell[11079, 413, 223, 8, 63, "Input"],
Cell[11305, 423, 347, 6, 401, "Output"]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[11701, 435, 31, 0, 26, "Subsubsection"],
Cell[CellGroupData[{
Cell[11757, 439, 214, 5, 102, "Input"],
Cell[11974, 446, 391, 11, 63, "Output"]
}, Closed]]
}, Closed]]
}, Open  ]]
}, Open  ]]
}
]
*)

(* End of internal cache information *)
