Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2003
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2003

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

Search the Archive

RE: Simplify a module

  • To: mathgroup at smc.vnet.net
  • Subject: [mg39476] RE: [mg39450] Simplify a module
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Mon, 17 Feb 2003 18:17:34 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: flip [mailto:flip_alpha at safebunch.com]
To: mathgroup at smc.vnet.net
>Sent: Sunday, February 16, 2003 12:14 PM
>To: mathgroup at smc.vnet.net
>Subject: [mg39476] [mg39450] 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)



  • Prev by Date: Re: Matrix sizing in 2 dimensional input
  • Next by Date: simultan non-linear equations
  • Previous by thread: Re: Simplify a module
  • Next by thread: Re: Simplify a module