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


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


(* :Author: Paul Wellin *)


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


(* :Context: EPM`Optimization` *)


(* :Package Version: 1.0 *)


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


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


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


(* :Source: Optimization.nb *)


(* :Mathematica Version: 10.3 *)


(* Tested in Mathematica 11.1 *)


Remove[AverageTiming,BlanagramsParallel,Mandelbrot,Sieve]


(* BeginPackage["EPM`Optimization`", {"EPM`Strings`"}] *)


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 EPM`09Strings` 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["EPM`"];
blana=Table[
Map[StringReplacePart[word,ch,{#,#}]&,Range[StringLength[word]]],
{ch,CharacterRange["a","z"]}];
DeleteDuplicates@Flatten[ParallelMap[EPM`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[]*)



