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


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


(* :Author: Paul Wellin *)


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


(* :Context: PwM`Chap04Patterns` *)


(* :Package Version: 1.01 *)


(* :Copyright: Copyright 2012, Paul R. Wellin *)


(* :History: Version 1.0 by Paul Wellin, Dec 2012
Version 1.01 changed name of SquareMatrixQ to be compatible with Mathematica 10 Nov 2014 *)


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


(* :Keywords: dice, arrays, multiplication, square matrix, area of triangle *)


(* :Source: Chap04Patterns.nb *)


(* :Mathematica Version: 10.0.1 *)


Remove[TriangleArea]


BeginPackage["PwM`Chap04Patterns`"]


Dice::usage="Dice[n] displays a regular six-sided die with value n.";


FixArray::usage="FixArray[mat] replaces any occurrences of the string \"NAN\" with the column mean for each column in which the string appears. ";


MultiplyCount::usage="MultiplyCount[expr] computes the number of explicit multiplications in the arithmetic expression expr.";


squareMatrixQ::usage="squareMatrixQ[mat] yields true if mat is a square matrix.";


TriangleArea::usage="TriangleArea[vertices] computes the area of the triangle specified by the three-element list vertices.";


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]
]


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


MultiplyCount[expr_?PolynomialQ]:=Total@Cases[{expr},Power[_,exp_]:>exp-1,Infinity]+Total@Cases[{expr},fac_Times:>Length[fac]-1,Infinity]


squareMatrixQ[mat_/;MatrixQ[mat]]:=Dimensions[mat][[1]]==Dimensions[mat][[2]]


TriangleArea[tri:{{_,_},{_,_},{_,_}}]:=Det[Map[PadRight[#,3,1]&,tri]]/2


End[]


EndPackage[]



