(* ::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: EPM`Patterns` *)


(* :Book: Essentials of Programming in Mathematica *)


(* :Author: Paul Wellin *)


(* :Summary: This package contains the functions defined in Chapter 4 Patterns and rules. *)


(* :Context: EPM`Patterns` *)


(* :Package Version: 1.1 *)


(* :Copyright: Copyright 2015, Paul R Wellin *)


(* :History:
	Version 1.0 Dec 2012;
	Version 1.1 Apr 2017, check $VersionNumber for Perimeter;
 *)


(* :Keywords: dice, finding subsequences, square matrix, area of triangle, perimeter *)


(* :Source: Patterns.nb *)


(* :Mathematica Version: 10.3 *)


(* Tested in Mathematica 11.1 *)


Remove[Color,Dice,FindSubsequence,FixArray,SignedArea,squareMatrixQ]


(* BeginPackage["EPM`Patterns`"] *)


Color::usage="Color is an option for Dice that specifies the background color. The default value is Darker@Blue.";


Dice::usage="Dice[\!\(\*
StyleBox[\"n\", \"TI\"]\)] displays a regular six-sided die with value \!\(\*
StyleBox[\"n\", \"TI\"]\).";


FindSubsequence::usage="FindSubsequence[\!\(\*
StyleBox[\"digs\", \"TI\"]\)\!\(\*
StyleBox[\",\", \"TI\"]\)\!\(\*
StyleBox[\" \", \"TI\"]\)\!\(\*
StyleBox[\"subseq\", \"TI\"]\)] returns the positions in the sequence of digits \!\(\*
StyleBox[\"digs\", \"TI\"]\) where the subsequence \!\(\*
StyleBox[\"subseq\", \"TI\"]\) occurs.";


FixArray::usage="FixArray[\!\(\*
StyleBox[\"mat\", \"TI\"]\)] replaces any occurrences of the string \"NAN\" with the column mean for each column in which the string appears. ";


If[$VersionNumber<11.1,
Perimeter::usage="Perimeter[Triangle[{\!\(\*SubscriptBox[
StyleBox[\"pt\", \"TI\"], \(1\)]\),\!\(\*SubscriptBox[
StyleBox[\"pt\", \"TI\"], \(2\)]\),\!\(\*SubscriptBox[
StyleBox[\"pt\", \"TI\"], \(3\)]\)}]] computes the perimeter of the triangle determined by the three points \!\(\*SubscriptBox[
StyleBox[\"pt\", \"TI\"], \(1\)]\), \!\(\*SubscriptBox[
StyleBox[\"pt\", \"TI\"], \(2\)]\), and \!\(\*SubscriptBox[
StyleBox[\"pt\", \"TI\"], \(3\)]\)."];


SignedArea::usage="SignedArea[Triangle[{\!\(\*SubscriptBox[
StyleBox[\"pt\", \"TI\"], \(1\)]\),\!\(\*SubscriptBox[
StyleBox[\"pt\", \"TI\"], \(2\)]\),\!\(\*SubscriptBox[
StyleBox[\"pt\", \"TI\"], \(3\)]\)}]] computes the area of the triangle determined by the three points \!\(\*SubscriptBox[
StyleBox[\"pt\", \"TI\"], \(1\)]\), \!\(\*SubscriptBox[
StyleBox[\"pt\", \"TI\"], \(2\)]\), and \!\(\*SubscriptBox[
StyleBox[\"pt\", \"TI\"], \(3\)]\). This value may be negative depending upon the order of the three points. The area is zero if the points are collinear.";


squareMatrixQ::usage="squareMatrixQ[\!\(\*
StyleBox[\"mat\", \"TI\"]\)] yields true if \!\(\*
StyleBox[\"mat\", \"TI\"]\) is a square matrix.";


Options[Dice]={Color->Darker@Blue};


Begin["`Private`"]


Dice[n_,OptionsPattern[]]:=Module[{rules,off={OptionValue[Color],Disk[]},on={White,Disk[]}},
rules={
{1->off,2->off,3->off,4->off,5->on,6->off,7->off,8->off,9->off},
{1->off,2->off,3->on,4->off,5->off,6->off,7->on,8->off,9->off},
{1->off,2->off,3->on,4->off,5->on,6->off,7->on,8->off,9->off},
{1->on,2->off,3->on,4->off,5->off,6->off,7->on,8->off,9->on},
{1->on,2->off,3->on,4->off,5->on,6->off,7->on,8->off,9->on},
 {1->on,2->off,3->on,4->on,5->off,6->on,7->on,8->off,9->on}
};
GraphicsGrid[Map[Graphics,
Partition[Range[9],3]/. rules[[n]],
{2}],Background->First[off],Spacings->10,ImageSize->40]
]


FindSubsequence[lis_List,subseq_List]:=Module[{p,len=Length[subseq]},
p=Partition[lis,len,1];
Position[p,subseq]/.{num_?IntegerQ}:>{num,num+len-1}]


FindSubsequence[n_Integer,subseq_Integer]:=Module[{nlist=IntegerDigits[n],sublist=IntegerDigits[subseq]},
FindSubsequence[nlist,sublist]
]


FixArray[mat_]:=Module[{fixcolumn},
fixcolumn[col_]:=mat[[All,col]]/."NAN":>Mean[Cases[mat[[All,col]],_?NumberQ]];
Transpose[Map[fixcolumn,Range[Length[First[mat]]]]]
]


If[$VersionNumber<11.1,
Perimeter[Triangle[pts:{p1_,p2_,p3_}]]:=RegionMeasure[Line[pts/.{pt1_,pn__}:>{pt1,pn,pt1}]]
]


SignedArea[Triangle[{v1_,v2_,v3_}]]:=1/2 Det[{v1,v2,v3}/. {x_,y_}:>{x,y,1}]


End[]


(*EndPackage[]*)



