MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: RE: Simplify a module

  • To: mathgroup at smc.vnet.net
  • Subject: [mg39503] Re: [mg39487] RE: Simplify a module
  • From: Dr Bob <drbob at bigfoot.com>
  • Date: Thu, 20 Feb 2003 05:13:39 -0500 (EST)
  • References: <200302190942.EAA03041@smc.vnet.net>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

That doesn't work, at least not here on my computer:

sw[{{a_, base_}, seq_}] := (a = Switch[Mod[e, base],
   Evaluate[Sequence @@ seq], _, 0])
dcalc4[ein_] := Block[{a1, a2, a3, e = ein}, {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]]
dcalc4[5]

Mod[784 a1 + 441 a2 + 1128 a3, 1176]

For simplicity, I like this one:

m = If[Mod[#, 7] == 0, 0, PowerMod[#, -1, 49]] & /@ Range[0, 48];
f[e_] := Mod[784Mod[e, 3] + 441Mod[e, 8]Mod[e, 2] + 1128 m[[1 + Mod[e, 
49]]], 1176]
f[5]

941

I was interested in versions of your solution for other reasons.

Bobby

On Wed, 19 Feb 2003 04:42:05 -0500 (EST), Wolf, Hartmut <Hartmut.Wolf@t- 
systems.com> wrote:

> Bobby,
>
> dcalc4[ein_] := Block[{a1, a2, a3, e = ein}, {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]]
>
> does it. BTW,
>
> sw[{{a_, base_}, seq_}] := (a = Switch[Mod[e, base], Evaluate[Sequence @@ 
> seq], _, 0])
>
> Evaluate[a] is not needed after destructuring.
>
>
> Nonetheless, I'd not recommend this solution neither my dcalc2, as I
> hopefully have made clear in my first posting, more so something like in 
> my
> PS, as e.g.
> ct1 = ctab[[All, 1, 2]];  (* get rid of hard-wired names *)
> ct2 = ctab[[All, 2]];     (* I'd not flatten this *)
> ct3 = {2, 3, 47};         (* perhaps better Transpose[{ct3,ct4}] ? *)
> ct4 = {392, 147, 24};
>
> dcalc3x[e_] := Mod[#, 1176] &[
> MapThread[
> Switch[Mod[e, #1], Evaluate[Apply[Sequence, #2, {0, 1}]],
> _, 0 ] &, {ct1, ct2}].(ct3*ct4)]
>
> Now rework ct1,...,ct4 into a single structure and supply this as a 
> second
> parameter to dcalc.
>
> --
> Hartmut Wolf
>
>
>> -----Original Message-----
>> From: Dr Bob [mailto:drbob at bigfoot.com]
To: mathgroup at smc.vnet.net
> To: mathgroup at smc.vnet.net
>> Sent: Tuesday, February 18, 2003 6:10 PM
>> To: Wolf, Hartmut; mathgroup at smc.vnet.net
>> Subject: [mg39503] [mg39487] Re:  Simplify a module
>>
>>
>> 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;
>> Mod[a1*T1*M1 + a2*T2*M2 + a3*T3*M3, 1176]]
>>
>> 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
>>> To: mathgroup at smc.vnet.net
>>>> Sent: Sunday, February 16, 2003 12:14 PM
>>>> To: mathgroup at smc.vnet.net
>>>> Subject: [mg39503] [mg39487]  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
>>
>
>
>



-- 
majort at cox-internet.com
Bobby R. Treat



  • Prev by Date: Re: RE: Simplify a module
  • Next by Date: Re: Showing thick lines - a problem?
  • Previous by thread: RE: Simplify a module
  • Next by thread: Re: RE: Simplify a module