RE: Can anyone simplify this?

*To*: mathgroup at smc.vnet.net*Subject*: [mg36477] RE: [mg36461] Can anyone simplify this?*From*: "DrBob" <drbob at bigfoot.com>*Date*: Sun, 8 Sep 2002 03:31:02 -0400 (EDT)*Reply-to*: <drbob at bigfoot.com>*Sender*: owner-wri-mathgroup at wolfram.com

If I haven't missed a step, the following should work nicely -- best done before defining any of the symbols that appear: Attributes[dummyIf] = HoldAll; Attributes[dummyWhich] = HoldAll; expr = {If[Aa == Ab, a Sa + b Sb, If[Ca == 0 || Cb == 0, a Sa + b Sb, SS]], \ If[Aa == Ab, a Ca + b Cb, If[Ca == 0 || Cb == 0, a Ca + b Cb, CC]], If[Ca == 0 && Cb == 0, "Nil", If[Z1 == Z2, If[Ca < 0, Aa, If[Cb < 0, Ab, If[Ca > 0, If[Aa > 90, Aa - 90, Aa + 90], If[Cb > 0, If[Ab > 90, Ab - 90, Ab + 90]]]]], If[Aa == Ab, Aa, If[Ca == 0 && Cb == 0, "Nil", If[ Ca == 0, Ab, If[Cb == 0, Aa, If[Y == 0, "Nil", If[X > 180, X - 180, If[X < 0, X + 180, X]]]]]]]]]} /. {If -> dummyIf}; rule1 = dummyIf[a_, b_, dummyIf[c_, d_, e_]] -> dummyWhich[a, b, c, d, True, e]; rule2 = dummyIf[a_, dummyIf[ b_, c_, d_], e_] -> dummyWhich[a && b, c, a, d, True, e]; rule3 = dummyIf[a_, dummyIf[b_, c_, d_]] -> dummyWhich[a && b, c, a, d]; rule4 = dummyWhich[a__, b_, dummyIf[c_, d_, e_], f___] -> dummyWhich[a, b && c, d, b, e, f]; rule5 = dummyWhich[a__, b_, dummyWhich[c_, d_, e___], f___] -> dummyWhich[a, b && c, d, b, dummyWhich[e], f]; rule6 = dummyWhich[] :> Null; rule7 = HoldPattern[True && a_] :> a; expr //. {rule1, rule2, rule3, rule4, rule5, rule6, rule7} /. {dummyWhich -> Which The result of that last line is: {Which[Aa == Ab, a Sa + b Sb, Ca == 0 || Cb == 0, a Sa + b Sb, True, SS], Which[Aa == Ab, a Ca + b Cb, Ca == 0 || Cb == 0, a Ca + b Cb, True, CC], Which[Ca == 0 && Cb == 0, "Nil", Z1 == Z2 && Ca < 0, Aa, Z1 == Z2 && Cb < 0, Ab, (Z1 == Z2 && Ca > 0) && Aa > 90, Aa - 90, Z1 == Z2 && Ca > 0, Aa + 90, Z1 == Z2 && (Cb > 0 && Ab > 90), Ab - 90, Z1 == Z2 && Cb > 0, Ab + 90, Z1 == Z2, Null, Aa == Ab, Aa, Ca == 0 && Cb == 0, "Nil", Ca == 0, Ab, Cb == 0, Aa, Y == 0, "Nil", X > 180, X - 180, X < 0, X + 180, True, X]} I've used Null where that would be the result of your original logic -- maybe you want it to be "Nil". Or maybe you want to use Null instead of "Nil". Your choice. Wherever you see Null, there's possibly a case you haven't covered. Bobby Treat -----Original Message----- From: Moranresearch at aol.com [mailto:Moranresearch at aol.com] To: mathgroup at smc.vnet.net Subject: [mg36477] [mg36461] Can anyone simplify this? I have substituted the letters SS, CC, X, Y and Z1 and Z2 for some complicated expressions just to illustrate the form of the function. This function works and all the conditions are necessary but I am sure a more elegant programming solution perhaps using While could be found. Any suggestions to tighten this up? Thank you. f[{Sa_, Ca_, Aa_, Sb_, Cb_, Ab_, a_, b_}] := {If[Aa == Ab, a Sa + b Sb, If[Ca == 0 || Cb == 0, a Sa + b Sb, SS]], If[Aa == Ab, a Ca + b Cb, If[Ca == 0 || Cb == 0, a Ca + b Cb, CC]], If[Ca == 0 && Cb == 0, "Nil", If[Z1 == Z2, If[Ca < 0, Aa, If[Cb < 0, Ab, If[Ca > 0, If[Aa > 90, Aa - 90, Aa + 90], If[Cb > 0, If[Ab > 90, Ab - 90, Ab + 90]]]]], If[Aa == Ab, Aa, If[Ca == 0 && Cb == 0, "Nil", If[Ca == 0, Ab, If[Cb == 0, Aa, If[Y == 0, "Nil", If[X > 180, X - 180, If[X < 0, X + 180, X]]]]]]]]]}