0
$\begingroup$

Can anybody explain why these two pieces of code are giving me different results?

Heres the inputs:

eq1 = f[v3, w3] == a1 c1 f[v1, w1] + a1 c2 f[v1, w2] + a2 c1 f[v2, w1] + a2 c2 f[v2, w2];
eq2 = f[v4, w4] == b1 d1 f[v1, w1] + b1 d2 f[v1, w2] + b2 d1 f[v2, w1] + b2 d2 f[v2, w2];


eq3 = Simplify[eq1, a1 c2 b2 d1 == a2 c1 b1 d2]
eq4 = Simplify[eq2, a1 c2 b2 d1 == a2 c1 b1 d2]

And here are the two expressions

expr = b1 d2 f[v3, w3] - a1 c2 f[v4, w4];
Simplify[
 expr /. {
  f[v3, w3] -> a1 c1 f[v1, w1] + a1 c2 f[v1, w2] + a2 c1 f[v2, w1] + a2 c2 f[v2, w2],
  f[v4, w4] -> b1 d1 f[v1, w1] + b1 d2 f[v1, w2] + b2 d1 f[v2, w1] + b2 d2 f[v2, w2]}]

expr = b1 d2 f[v3, w3] - a1 c2 f[v4, w4];
Simplify[
 expr /. {
  Map[Reverse, ToRules[eq3]],
  Map[Reverse, ToRules[eq4]]}]

Heres the output:

a1 b1 (-c2 d1 + c1 d2) f[v1, w1] + (-a1 b2 c2 d1 + a2 b1 c1 d2) f[v2,w1] + (a2 b1 - a1 b2) c2 d2 f[v2, w2]

{b1 d2 (a1 c1 f[v1, w1] + a1 c2 f[v1, w2] + a2 c1 f[v2, w1] + a2 c2 f[v2, w2]) - a1 c2 f[v4,w4], -a1 c2 (b1 d1 f[v1, w1] + b1 d2 f[v1, w2] + b2 d1 f[v2,w1] +b2 d2 f[v2, w2]) + b1 d2 f[v3, w3]}

This is especially strange considering the output of the Map[Reverse, ToRules[eqx]] results in the same expression as the code in the first block.

The second piece of code also seems to return a single item list, whereas the second does not.

Any clues?

$\endgroup$
5
  • 1
    $\begingroup$ what are the definitions of eq3 and eq4? $\endgroup$ Feb 1 '16 at 21:18
  • $\begingroup$ eq3 = Simplify[eq1, a1 c2 b2 d1 == a2 c1 b1 d2] eq4 = Simplify[eq2, a1 c2 b2 d1 == a2 c1 b1 d2] And the eq1 and 2 are more of the same, with functions multiplied by constants $\endgroup$
    – Alex DB
    Feb 1 '16 at 21:23
  • $\begingroup$ I would've made a MWE but I think if you ignore the long polynomials it's fairly minimal $\endgroup$
    – Alex DB
    Feb 1 '16 at 21:24
  • 1
    $\begingroup$ I am sorry, but you have to specify eq1 and eq2 as well. Otherwise you're asking us why one expression replacement differs from another one with unknown replacements. $\endgroup$ Feb 1 '16 at 21:29
  • $\begingroup$ Sure, I see what you're saying. I've edited the question to state the explicit input $\endgroup$
    – Alex DB
    Feb 2 '16 at 20:00
1
$\begingroup$

The difference between both results is caused by a subtle difference in the inputs.

The first replacement has a simple list of replacement rules:

expr /. {
  f[v3, w3] -> a1 c1 f[v1, w1] + a1 c2 f[v1, w2] + a2 c1 f[v2, w1] + a2 c2 f[v2, w2],
  f[v4, w4] -> b1 d1 f[v1, w1] + b1 d2 f[v1, w2] + b2 d1 f[v2, w1] + b2 d2 f[v2, w2]}

The second replacement is almost the same except that it contains a second set of curly braces, generated by your Map operations:

expr /. {
      {f[v3, w3] -> a1 c1 f[v1, w1] + a1 c2 f[v1, w2] + a2 c1 f[v2, w1] + a2 c2 f[v2, w2]},
      {f[v4, w4] -> b1 d1 f[v1, w1] + b1 d2 f[v1, w2] + b2 d1 f[v2, w1] + b2 d2 f[v2, w2]}}

This use or ReplaceAll is actually not documented. The help page will show you that the replacements are specified either by a simple rule or a list of rules. In your 2nd replacement, you have a list consisting of two lists of rules. So, it's a case of

a + b /. {a -> c, b -> d}
(* c + d *)

vs

a + b /. {{a -> c}, {b -> d}}
(* {b + c, a + d} *)

As you can see, this undocumented version of ReplaceAll effectively returns a list of replaced expressions, one for every replacement list.

The second replacement needs a Flatten to be equivalent to the first one

$\endgroup$
4
  • $\begingroup$ Thanks! as a follow up question, Is there a way to make the map function produce a single list? $\endgroup$
    – Alex DB
    Feb 3 '16 at 16:38
  • $\begingroup$ Map produces a single list, but you have put two of them in a list, so, as I said in my answer, you have to use Flatten to make it a single one. $\endgroup$ Feb 3 '16 at 16:41
  • $\begingroup$ Yeah I did that. I was just wondering if there was a way to use a single map function to make two rules. Something like Map[{{Reverse, ToRules[1]},{Reverse, ToRules[2]}}] I suppose even writing that, I can see why that might not be possible. $\endgroup$
    – Alex DB
    Feb 3 '16 at 20:43
  • 1
    $\begingroup$ @AlexDB What about #2 -> #1 & @@@ {eq3, eq4}? $\endgroup$ Feb 3 '16 at 22:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.