Re: Simplify a module
- To: mathgroup at smc.vnet.net
- Subject: [mg39490] Re: Simplify a module
- From: Dr Bob <drbob at bigfoot.com>
- Date: Wed, 19 Feb 2003 04:42:20 -0500 (EST)
- References: <200302172317.SAA14748@smc.vnet.net>
- Reply-to: drbob at bigfoot.com
- Sender: owner-wri-mathgroup at wolfram.com
Here's a marginally simpler solution: Cases[Hold[ If[Mod[e, 3] == 1, a1 = 1]; If[Mod[e, 3] == 2, a1 = 2]; If[Mod[e, 8] == 1, a2 = 1]; If[Mod[e, 8] == 3, a2 = 3]; If[Mod[e, 8] == 5, a2 = 5]; If[Mod[e, 8] == 7, a2 = 7]; If[Mod[e, 49] == 1, a3 = 1]; If[Mod[e, 49] == 2, a3 = 25]; If[Mod[e, 49] == 3, a3 = 33]; If[Mod[e, 49] == 4, a3 = 37]; If[Mod[e, 49] == 5, a3 = 10]; If[Mod[e, 49] == 6, a3 = 41]; If[Mod[e, 49] == 8, a3 = 43]; If[Mod[e, 49] == 9, a3 = 11]; If[Mod[e, 49] == 10, a3 = 5]; If[Mod[e, 49] == 11, a3 = 9]; If[Mod[e, 49] == 12, a3 = 45]; If[Mod[e, 49] == 13, a3 = 34]; If[Mod[e, 49] == 15, a3 = 36]; If[Mod[e, 49] == 16, a3 = 46]; If[Mod[e, 49] == 17, a3 = 26]; If[Mod[e, 49] == 18, a3 = 30]; If[Mod[e, 49] == 19, a3 = 31]; If[Mod[e, 49] == 20, a3 = 27]; If[Mod[e, 49] == 22, a3 = 29]; If[Mod[e, 49] == 23, a3 = 32]; If[Mod[e, 49] == 24, a3 = 47]; If[Mod[e, 49] == 25, a3 = 2]; If[Mod[e, 49] == 26, a3 = 17]; If[Mod[e, 49] == 27, a3 = 20]; If[Mod[e, 49] == 29, a3 = 22]; If[Mod[e, 49] == 30, a3 = 18]; If[Mod[e, 49] == 31, a3 = 19]; If[Mod[e, 49] == 32, a3 = 23]; If[Mod[e, 49] == 33, a3 = 3]; If[Mod[e, 49] == 34, a3 = 13]; If[Mod[e, 49] == 36, a3 = 15]; If[Mod[e, 49] == 37, a3 = 4]; If[Mod[e, 49] == 38, a3 = 40]; If[Mod[e, 49] == 39, a3 = 44]; If[Mod[e, 49] == 40, a3 = 38]; If[Mod[e, 49] == 41, a3 = 6]; If[Mod[e, 49] == 43, a3 = 8]; If[Mod[e, 49] == 44, a3 = 39]; If[Mod[e, 49] == 45, a3 = 12]; If[Mod[e, 49] == 46, a3 = 16]; If[Mod[e, 49] == 47, a3 = 24]; If[Mod[e, 49] == 48, a3 = 48];], If[ Mod[_, g_] == h_, ax_ = k_] :> {ax, g, h, k}, Infinity]; split = Split[%, #1[[1]] === #2[[1]] &]; mytab = {#[[1, {1, 2}]], Flatten[#[[All, {3, 4}]]]} & /@ split; dcalc3[e_] := Block[{a1, a2, a3}, {T1, M1, T2, M2, T3, M3} = {2, 392, 3, 147, 47, 24}; (Evaluate[#[[1, 1]]] = Switch[Mod[e, #[[ 1, 2]]], Evaluate[Sequence @@ #[[2]]], _, 0]) & /@ mytab; dcalc3@# == dcalc2@# & /@ Range[1176]; And @@ % True Could someone explain why something like the following doesn't work? I've tried several versions and haven't been able to use a named function like sw in place of the identical in-line function. sw[{{a_, base_}, seq_}] := (Evaluate[a] = Switch[Mod[e, base], Evaluate[Sequence @@ seq], _, 0]) dcalc4[e_] := Block[{a1, a2, a3}, {T1, M1, T2, M2, T3, M3} = {2, 392, 3, 147, 47, 24}; Evaluate[sw /@ mytab]; Mod[a1*T1*M1 + a2*T2*M2 + a3*T3*M3, 1176]] Bobby On Mon, 17 Feb 2003 18:17:34 -0500 (EST), Wolf, Hartmut <Hartmut.Wolf@t- systems.com> wrote: > >> -----Original Message----- >> From: flip [mailto:flip_alpha at safebunch.com] To: mathgroup at smc.vnet.net > To: mathgroup at smc.vnet.net >> Sent: Sunday, February 16, 2003 12:14 PM >> To: mathgroup at smc.vnet.net >> Subject: [mg39490] Simplify a module >> >> >> Hi All, >> >> Can someone recommend a simplification to this module (just a bunch of >> if >> checks). >> >> It was specified to be done that way (and I know how to sove the problem >> using PowerMod in one step, so please bear with me here). >> >> Thanks, Flip >> >> (* to email me remove "_alpha" *) >> >> Anyway, here goes. >> >> dcalc[ein_] := Module[{e = ein, a1 = 0, a2 = 0, a3 = 0}, >> {T1, M1, T2, M2, T3, M3} = {2, 392, 3, 147, 47, 24}; >> If[Mod[e, 3] == 1, a1 = 1]; If[Mod[e, 3] == 2, a1 = 2]; >> If[Mod[e, 8] == 1, a2 = 1]; >> If[Mod[e, 8] == 3, a2 = 3]; If[Mod[e, 8] == 5, a2 = 5]; >> If[Mod[e, 8] == 7, a2 = 7]; >> If[Mod[e, 49] == 1, a3 = 1]; If[Mod[e, 49] == 2, a3 = 25]; >> If[Mod[e, 49] == 3, a3 = 33]; If[Mod[e, 49] == 4, a3 = 37]; >> If[Mod[e, 49] == 5, a3 = 10]; If[Mod[e, 49] == 6, a3 = 41]; >> If[Mod[e, 49] == 8, a3 = 43]; If[Mod[e, 49] == 9, a3 = 11]; >> If[Mod[e, 49] == 10, a3 = 5]; If[Mod[e, 49] == 11, a3 = 9]; >> If[Mod[e, 49] == 12, a3 = 45]; If[Mod[e, 49] == 13, a3 = 34]; >> If[Mod[e, 49] == 15, a3 = 36]; If[Mod[e, 49] == 16, a3 = 46]; >> If[Mod[e, 49] == 17, a3 = 26]; If[Mod[e, 49] == 18, a3 = 30]; >> If[Mod[e, 49] == 19, a3 = 31]; If[Mod[e, 49] == 20, a3 = 27]; >> If[Mod[e, 49] == 22, a3 = 29]; If[Mod[e, 49] == 23, a3 = 32]; >> If[Mod[e, 49] == 24, a3 = 47]; If[Mod[e, 49] == 25, a3 = 2]; >> If[Mod[e, 49] == 26, a3 = 17]; If[Mod[e, 49] == 27, a3 = 20]; >> If[Mod[e, 49] == 29, a3 = 22]; If[Mod[e, 49] == 30, a3 = 18]; >> If[Mod[e, 49] == 31, a3 = 19]; If[Mod[e, 49] == 32, a3 = 23]; >> If[Mod[e, 49] == 33, a3 = 3]; If[Mod[e, 49] == 34, a3 = 13]; >> If[Mod[e, 49] == 36, a3 = 15]; If[Mod[e, 49] == 37, a3 = 4]; >> If[Mod[e, 49] == 38, a3 = 40]; If[Mod[e, 49] == 39, a3 = 44]; >> If[Mod[e, 49] == 40, a3 = 38]; If[Mod[e, 49] == 41, a3 = 6]; >> If[Mod[e, 49] == 43, a3 = 8]; If[Mod[e, 49] == 44, a3 = 39]; >> If[Mod[e, 49] == 45, a3 = 12]; If[Mod[e, 49] == 46, a3 = 16]; >> If[Mod[e, 49] == 47, a3 = 24]; If[Mod[e, 49] == 48, a3 = 48]; >> Return[Mod[a1*T1*M1 + a2*T2*M2 + a3*T3*M3, 1176]]] >> >> >> >> >> >> >> >> > Flip, > > to "simplify" your module, resort to metaprogramming, i.e. write another > program that constructs that module from data you supply from a table. > > The following is a somewhat foolish example, just to show the idea. To > begin with, I cut out part of your coding... > > In[2]:= > Cases[Hold[If[Mod[e, 3] == 1, a1 = 1]; If[Mod[e, 3] == 2, a1 = 2]; > If[Mod[e, 8] == 1, a2 = 1]; > ......,and so on...... > If[Mod[e, 49] == 47, a3 = 24]; If[Mod[e, 49] == 48, a3 = 48];], > If[Mod[_, g_] == h_, ax_ = k_] :> {ax, g, h, k}, Infinity]; > > ...to extract your data > > In[3]:= Split[%, #1[[1]] === #2[[1]] &]; > > ...group, and transform it to a handy structure > > In[4]:= ctab = {#[[1, {1, 2}]], #[[All, {3, 4}]]} & /@ %; > > > Of course in future you'll just start with ctab, or something similar. > > > > In[5]:= dcalc2[e_] := Block[{a1, a2, a3}, {T1, M1, T2, M2, T3, M3} = {2, > 392, 3, 147, 47, 24}; > (Evaluate[#[[1, 1]]] = Switch[Mod[e, #[[1, 2]]], Evaluate[Apply[Sequence, > #[[2, All]], {0, 1}]], _, 0 ]) & /@ ctab; > Mod[a1*T1*M1 + a2*T2*M2 + a3*T3*M3, 1176]] > > In[6]:= d100 = dcalc /@ Range[100]; > In[7]:= d100x = dcalc2 /@ Range[100]; > In[8]:= d100 == d100x > Out[8]= True > > As said, just to get an idea; this is not a suggestion as how to code it, > don't use the hard-wired symbols a1, a2, a3 between ctab and dcalc2 > (generate them as needed) or thread, avoid global T1, M2, etc. Your > exercise. > > -- > Hartmut > > > PS, perhaps something like > > dcalc3[e_] := Mod[Plus @@ MapThread[ > Switch[Mod[e, #1[[1, 2]]], Evaluate[Apply[Sequence, #1[[2, All]], {0, > 1}]], > _, 0 ]*#2*#3 &, {ctab, {2, 3, 47}, {392, 147, 24}}], 1176] > > (a1, etc. not used) > > > -- majort at cox-internet.com Bobby R. Treat
- References:
- RE: Simplify a module
- From: "Wolf, Hartmut" <Hartmut.Wolf@t-systems.com>
- RE: Simplify a module