0
$\begingroup$

I was using a rule replacement that creates a matrix. At one rule, I added a conditional to return one vector and then added that (after the conditional and within the same replacement) to another. This gives an error, whereas if I put the addition inside the conditional, it does what I expect. Since I have the workaround, this is not an urgent problem but it took me a while to figure out how to work around the problem. There is something having to do with a subtlety of replacement rules I don't understand. May I know what makes the first version fail?

ClearAll[l1, l2, l3, l4, l5];
SeedRandom[20];
l1 = RandomReal[{0.01, 0.05}, 10];
l2 = RandomReal[{0.001, 0.005}, 10];
l3 = RandomReal[{0.0001, 0.0005}, 10];
l4 = RandomReal[{0.00001, 0.00005}, 10];
l5 = RandomReal[{0.00001, 0.00005}, 10];
Tuples[Join[{0, .1}, {1, 2}], 2] /. {
  {0, 0} -> l1,
  {0, a_} /; a != 0 -> If[a != .1, (l2/a), l3] + l4 ,
  {a_, 0} /; a != 0 -> If[a != .1, l2, l3],
  {a_, b_} /; (a != 0) && (b != 0) -> If[b != .1, (l2/b), l5]
  }

Incorrect result (note the extra layer in the list after the first list):

(* {{0.0346891, 0.042335, 0.0139251, 0.0372237, 0.0482159, 0.0369557, 
  0.0142226, 0.0386726, 0.0308678, 
  0.0381804}, {{0.000305425, 0.000521417, 0.000460095, 0.000402933, 
   0.000349666, 0.000203166, 0.000462641, 0.000499664, 0.000210436, 
   0.000213133}, {0.000297734, 0.000513725, 0.000452403, 0.000395241, 
   0.000341974, 0.000195474, 0.00045495, 0.000491972, 0.000202745, 
   0.000205441}, {0.000287534, 0.000503525, 0.000442203, 0.000385041, 
   0.000331774, 0.000185274, 0.00044475, 0.000481772, 0.000192545, 
   0.000195241}, {0.000316639, 0.00053263, 0.000471309, 0.000414146, 
   0.00036088, 0.000214379, 0.000473855, 0.000510877, 0.00022165, 
   0.000224346}, {0.000287595, 0.000503587, 0.000442265, 0.000385103, 
   0.000331836, 0.000185336, 0.000444811, 0.000481834, 0.000192606, 
   0.000195303}, {0.000318435, 0.000534427, 0.000473105, 0.000415943, 
   0.000362676, 0.000216176, 0.000475652, 0.000512674, 0.000223447, 
   0.000226143}, {0.000294051, 0.000510042, 0.000448721, 0.000391558, 
   0.000338291, 0.000191791, 0.000451267, 0.000488289, 0.000199062, 
   0.000201758}, {0.000309041, 0.000525032, 0.000463711, 0.000406548, 
   0.000353282, 0.000206782, 0.000466257, 0.00050328, 0.000214052, 
   0.000216748}, {0.000302288, 0.00051828, 0.000456958, 0.000399796, 
   0.000346529, 0.000200029, 0.000459505, 0.000496527, 0.0002073, 
   0.000209996}, {0.000288887, 0.000504879, 0.000443557, 0.000386395, 
   0.000333128, 0.000186628, 0.000446104, 0.000483126, 0.000193899, 
   0.000196595}}, {{0.0045267, 0.00483969, 0.00452058, 0.00501934, 
   0.00215483, 0.00274077, 0.002371, 0.0031683, 0.00263791, 
   0.00301823}, {0.00451901, 0.004832, 0.00451289, 0.00501164, 
   0.00214714, 0.00273308, 0.00236331, 0.00316061, 0.00263022, 
   0.00301054}, {0.00450881, 0.0048218, 0.00450269, 0.00500144, 
   0.00213694, 0.00272288, 0.00235311, 0.00315041, 0.00262002, 
   0.00300034}, {0.00453792, 0.00485091, 0.00453179, 0.00503055, 
   0.00216604, 0.00275199, 0.00238222, 0.00317951, 0.00264913, 
   0.00302944}, {0.00450887, 0.00482186, 0.00450275, 0.00500151, 
   0.002137, 0.00272294, 0.00235317, 0.00315047, 0.00262008, 
   0.0030004}, {0.00453971, 0.0048527, 0.00453359, 0.00503235, 
   0.00216784, 0.00275378, 0.00238401, 0.00318131, 0.00265092, 
   0.00303124}, {0.00451533, 0.00482832, 0.00450921, 0.00500796, 
   0.00214345, 0.0027294, 0.00235963, 0.00315692, 0.00262654, 
   0.00300685}, {0.00453032, 0.00484331, 0.0045242, 0.00502295, 
   0.00215844, 0.00274439, 0.00237462, 0.00317191, 0.00264153, 
   0.00302184}, {0.00452357, 0.00483656, 0.00451744, 0.0050162, 
   0.00215169, 0.00273764, 0.00236787, 0.00316516, 0.00263478, 
   0.00301509}, {0.00451017, 0.00482315, 0.00450404, 0.0050028, 
   0.00213829, 0.00272424, 0.00235446, 0.00315176, 0.00262138, 
   0.00300169}}, {{0.00227743, 0.00243393, 0.00227437, 0.00252375, 
   0.0010915, 0.00138447, 0.00119958, 0.00159823, 0.00133304, 
   0.0015232}, {0.00226974, 0.00242624, 0.00226668, 0.00251606, 
   0.0010838, 0.00137678, 0.00119189, 0.00159054, 0.00132535, 
   0.0015155}, {0.00225954, 0.00241604, 0.00225648, 0.00250586, 
   0.0010736, 0.00136658, 0.00118169, 0.00158034, 0.00131515, 
   0.0015053}, {0.00228865, 0.00244514, 0.00228558, 0.00253496, 
   0.00110271, 0.00139568, 0.0012108, 0.00160944, 0.00134425, 
   0.00153441}, {0.0022596, 0.0024161, 0.00225654, 0.00250592, 
   0.00107367, 0.00136664, 0.00118175, 0.0015804, 0.00131521, 
   0.00150537}, {0.00229044, 0.00244694, 0.00228738, 0.00253676, 
   0.00110451, 0.00139748, 0.00121259, 0.00161124, 0.00134605, 
   0.00153621}, {0.00226606, 0.00242255, 0.002263, 0.00251237, 
   0.00108012, 0.00137309, 0.00118821, 0.00158685, 0.00132166, 
   0.00151182}, {0.00228105, 0.00243754, 0.00227799, 0.00252736, 
   0.00109511, 0.00138808, 0.0012032, 0.00160185, 0.00133665, 
   0.00152681}, {0.0022743, 0.00243079, 0.00227123, 0.00252061, 
   0.00108836, 0.00138133, 0.00119645, 0.00159509, 0.0013299, 
   0.00152006}, {0.00226089, 0.00241739, 0.00225783, 0.00250721, 
   0.00107496, 0.00136793, 0.00118304, 0.00158169, 0.0013165, 
   0.00150666}}, {0.000277264, 0.000493255, 0.000431934, 0.000374771, 
  0.000321505, 0.000175004, 0.00043448, 0.000471502, 0.000182275, 
  0.000184971}, {0.0000361249, 0.0000377894, 0.0000206777, 
  0.0000428898, 0.0000111111, 0.0000444345, 0.000028785, 0.0000141051,
   0.0000396807, 0.0000405228}, {0.00449854, 0.00481153, 0.00449242, 
  0.00499117, 0.00212667, 0.00271261, 0.00234284, 0.00314014, 
  0.00260975, 0.00299007}, {0.00224927, 0.00240577, 0.00224621, 
  0.00249559, 0.00106333, 0.00135631, 0.00117142, 0.00157007, 
  0.00130488, 0.00149503}, {0.00449854, 0.00481153, 0.00449242, 
  0.00499117, 0.00212667, 0.00271261, 0.00234284, 0.00314014, 
  0.00260975, 0.00299007}, {0.0000361249, 0.0000377894, 0.0000206777, 
  0.0000428898, 0.0000111111, 0.0000444345, 0.000028785, 0.0000141051,
   0.0000396807, 0.0000405228}, {0.00449854, 0.00481153, 0.00449242, 
  0.00499117, 0.00212667, 0.00271261, 0.00234284, 0.00314014, 
  0.00260975, 0.00299007}, {0.00224927, 0.00240577, 0.00224621, 
  0.00249559, 0.00106333, 0.00135631, 0.00117142, 0.00157007, 
  0.00130488, 0.00149503}, {0.00449854, 0.00481153, 0.00449242, 
  0.00499117, 0.00212667, 0.00271261, 0.00234284, 0.00314014, 
  0.00260975, 0.00299007}, {0.0000361249, 0.0000377894, 0.0000206777, 
  0.0000428898, 0.0000111111, 0.0000444345, 0.000028785, 0.0000141051,
   0.0000396807, 0.0000405228}, {0.00449854, 0.00481153, 0.00449242, 
  0.00499117, 0.00212667, 0.00271261, 0.00234284, 0.00314014, 
  0.00260975, 0.00299007}, {0.00224927, 0.00240577, 0.00224621, 
  0.00249559, 0.00106333, 0.00135631, 0.00117142, 0.00157007, 
  0.00130488, 0.00149503}}
*)

Replacing the addition outside the conditional inside the conditional:

Tuples[Join[{0, .1}, {1, 2}], 2] /. {
  {0, 0} -> l1,
  {0, a_} /; a != 0 -> If[a != .1, (l2/a) + l4, l3 + l4],
  {a_, 0} /; a != 0 -> If[a != .1, l2, l3],
  {a_, b_} /; (a != 0) && (b != 0) -> If[b != .1, (l2/b), l5]
  }

Yields the expected result:

(* {{0.0346891, 0.042335, 0.0139251, 0.0372237, 0.0482159, 0.0369557, 
  0.0142226, 0.0386726, 0.0308678, 0.0381804}, {0.000305425, 
  0.000513725, 0.000442203, 0.000414146, 0.000331836, 0.000216176, 
  0.000451267, 0.00050328, 0.0002073, 0.000196595}, {0.0045267, 
  0.004832, 0.00450269, 0.00503055, 0.002137, 0.00275378, 0.00235963, 
  0.00317191, 0.00263478, 0.00300169}, {0.00227743, 0.00242624, 
  0.00225648, 0.00253496, 0.00107367, 0.00139748, 0.00118821, 
  0.00160185, 0.0013299, 0.00150666}, {0.000277264, 0.000493255, 
  0.000431934, 0.000374771, 0.000321505, 0.000175004, 0.00043448, 
  0.000471502, 0.000182275, 0.000184971}, {0.0000361249, 0.0000377894,
   0.0000206777, 0.0000428898, 0.0000111111, 0.0000444345, 
  0.000028785, 0.0000141051, 0.0000396807, 0.0000405228}, {0.00449854,
   0.00481153, 0.00449242, 0.00499117, 0.00212667, 0.00271261, 
  0.00234284, 0.00314014, 0.00260975, 0.00299007}, {0.00224927, 
  0.00240577, 0.00224621, 0.00249559, 0.00106333, 0.00135631, 
  0.00117142, 0.00157007, 0.00130488, 0.00149503}, {0.00449854, 
  0.00481153, 0.00449242, 0.00499117, 0.00212667, 0.00271261, 
  0.00234284, 0.00314014, 0.00260975, 0.00299007}, {0.0000361249, 
  0.0000377894, 0.0000206777, 0.0000428898, 0.0000111111, 
  0.0000444345, 0.000028785, 0.0000141051, 0.0000396807, 
  0.0000405228}, {0.00449854, 0.00481153, 0.00449242, 0.00499117, 
  0.00212667, 0.00271261, 0.00234284, 0.00314014, 0.00260975, 
  0.00299007}, {0.00224927, 0.00240577, 0.00224621, 0.00249559, 
  0.00106333, 0.00135631, 0.00117142, 0.00157007, 0.00130488, 
  0.00149503}, {0.00449854, 0.00481153, 0.00449242, 0.00499117, 
  0.00212667, 0.00271261, 0.00234284, 0.00314014, 0.00260975, 
  0.00299007}, {0.0000361249, 0.0000377894, 0.0000206777, 
  0.0000428898, 0.0000111111, 0.0000444345, 0.000028785, 0.0000141051,
   0.0000396807, 0.0000405228}, {0.00449854, 0.00481153, 0.00449242, 
  0.00499117, 0.00212667, 0.00271261, 0.00234284, 0.00314014, 
  0.00260975, 0.00299007}, {0.00224927, 0.00240577, 0.00224621, 
  0.00249559, 0.00106333, 0.00135631, 0.00117142, 0.00157007, 
  0.00130488, 0.00149503}}
*)
$\endgroup$
  • 4
    $\begingroup$ Use RuleDelayed instead of Rule, e.g., {0, a_} /; a != 0 :> If[a != .1, (l2/a), l3] + l4 $\endgroup$ – Carl Woll Nov 12 '19 at 18:48
  • $\begingroup$ Works like a charm! Thank you. Why is the rule delayed required for the first case and not the second or is there a reason the second case "gets lucky"? $\endgroup$ – Mark R Nov 12 '19 at 22:18
  • $\begingroup$ I do (now) see the reason for the rule delayed and am just confused by why it worked in one case but not the other. $\endgroup$ – Mark R Nov 13 '19 at 2:58
  • $\begingroup$ If[a != .1, (l2/a), l3] + l4 evaluates to {0.0000142226 +If[a!=0.1,l2/a,l3],0.0000386726 +If[a!=0.1,l2/a,l3]}. This is when the extra pair of list braces appear. $\endgroup$ – Carl Woll Nov 13 '19 at 3:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.