# How does Mathematica handle a list of rule pairs when performing a substitution?

I am trying to solve linear second order differential equations using mathematica.

I have generated a list of rules, as in eq3rules in the below code.

When I perform the below substitution,

de3 /.eq3rules

Will mathematica follow the pairs of assignment rules? ie, y[x][[1]] will pair with y'[x][[1]],and y”[x][[1]]?

eq3 = y[x] == {E^x, Exp[2 x]}
(* y[x] == {E^x, E^(2 x)} *)

Wronskian[eq3[[2]], x]
(* E^(3 x) *)

(* W is non zero, so these are linearly independent *)

de3 = 0 == y”[x] – 3 y'[x] + 2*y[x]
(* 0 == 2 y[x] – 3 Derivative[1][y][x] + yâ€²â€²[x] *)

eq3rules = {eq3, D[eq3, x], D[eq3, {x, 2}]} /. Equal -> Rule
(* {y[x] -> {E^x, E^(2 x)}, y'[x] -> {E^x, 2 E^(2 x)}, y”[x] -> {E^x, 4 E^(2 x)}} *)

de3 /. eq3rules
(* 0 == {0, 0} *)

If it does not follow these rules, should I somehow group the rule sets for each solution into their own list elements?

So the new list would be

{{y_1[x],y_1′[x],y_1”[x]},{y_2[x],y_2′[x],y_2”[x]}}

Thank you

=================

=================

1

=================

Rules of the type

{y[x] -> {E^x, E^(2 x)}

replace the full left-hand side with the right-hand side. In this case you replace a scalar with a vector. I have the impression that you actually would want replacement rules like

{y[x] -> E^x, y[x] -> E^(2 x)}

So, you need a way to split the first form into the second one (and the same for all derivatives). Something like Distribute will do that:

Distribute[#, List] & /@ eq3rules
(* {{y[x] -> E^x, y[x] -> E^(2 x)},
{Derivative[1][y][x] -> E^x, Derivative[1][y][x] -> 2 E^(2 x)},
{(yâ€²â€²[x] -> E^x, yâ€²â€²[x] -> 4 E^(2 x)}
} *)

You now have 3 sets of 2 rules; one set for every derivative order. But since you want the rules for the various derivatives to be matched you need to add a Transpose to get 2 sets of 3 rules:

resRules=
Distribute[#, List] & /@ eq3rules // Transpose
(* {{y[x] -> E^x, Derivative[1][y][x] -> E^x, yâ€²â€²[x] -> E^x},
{y[x] -> E^(2 x), Derivative[1][y][x] -> 2 E^(2 x), yâ€²â€²[x] ->4 E^(2 x)}} *)

de3 /. resRules
(* {True, True} *)

Thank you so much! This, to me, is very idiomatic and elegant
– Jay Stanley
Sep 7 at 19:35

You’re welcome. A simple replacement like eq3rules /. Rule[a_, {b_, c_}] -> {a -> b, a -> c} instead of the Distribute would also do the trick.
– Sjoerd C. de Vries
Sep 7 at 19:42