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


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


(* :Author: Paul Wellin *)


(* :Summary: This package contains the functions defined in Chapter 12, Optimizing Mathematica programs. *)


(* :Context: PwM`Chap12Optimizing` *)


(* :Package Version: 1.0 *)


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


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


(* :Keywords: timing, blanagrams, Mandebrot set, sieving algorithms *)


(* :Source: Chap12Optimizing.nb *)


(* :Mathematica Version: 8.0.4 *)


Remove[Sieve]


BeginPackage["PwM`Chap12Optimizing`",{"PwM`Chap09Strings`"}]


AverageTiming::usage="AverageTiming[\!\(\*
StyleBox[\"expr\", \"TI\"]\), \!\(\*
StyleBox[\"trials\", \"TI\"]\)] evaluates \!\(\*
StyleBox[\"expr\", \"TI\"]\) a number of times given by \!\(\*
StyleBox[\"trials\", \"TI\"]\) and averages the evaluation timings.";


BlanagramsParallel::usage="BlanagramsParallel[\"\!\(\*
StyleBox[\"word\", \"TI\"]\)\"] finds all blanagrams of the string, \!\(\*
StyleBox[\"word\", \"TI\"]\), running the computation in parallel. It requires the package PwM`Chap09Strings` to be installed.";


Mandelbrot::usage="Mandelbrot[\!\(\*
StyleBox[\"c\", \"TI\"]\)] iterates the function \!\(\*SuperscriptBox[
StyleBox[\"z\", \"TI\"], \(2\)]\) + \!\(\*
StyleBox[\"c\", \"TI\"]\) until its absolute value is greater than two. It returns the length of the iteration; if unbounded, it returns the iteration limit, which is, by default, equal to 250.";


Sieve::usage="Sieve[\!\(\*
StyleBox[\"n\", \"TI\"]\)] uses a sieving algorithm due to Eratosthenes to find all prime numbers less than \!\(\*
StyleBox[\"n\", \"TI\"]\).";


Begin["`Private`"]


SetAttributes[AverageTiming,HoldAll]


AverageTiming[expr_,trials_]:=Mean[Table[First[AbsoluteTiming[expr]],{trials}]]


BlanagramsParallel[word_String]:=Module[{blana},
ParallelNeeds["PwM`Chap09Strings`"];
blana=Table[
Map[StringReplacePart[word,ch,{#,#}]&,Range[StringLength[word]]],
{ch,CharacterRange["a","z"]}];
DeleteDuplicates@Flatten[ParallelMap[PwM`Chap09Strings`Anagrams,blana,{2}]]
]


Mandelbrot[c_Complex,iter_:250]:=Length[NestWhileList[#^2+c&,0,Abs[#]<2&,1,iter]]


Sieve[n_Integer]:=Module[{ints=Range[n]},
Do[ints[[2 p;;-1;;p]]=1,
{p,2,Sqrt[n]}];
DeleteCases[ints,1]]


End[]


EndPackage[]



