Thread a list of replacement rules into a list
You can useMapThread
:
list = {-1 - ϵ + 1/2 ϵ^2 x''[0], 1 - ϵ + 1/2 ϵ^ 2 x''[0]};
rules = {x''[0] -> -1, x''[0] -> 1};
MapThread[ReplaceAll, {list, rules}]
{-1 - ϵ - ϵ^2/2, 1 - ϵ + ϵ^2/2}
You can also use Inner
as follows:
Inner[ReplaceAll, list, rules, List]
{-1 - ϵ - ϵ^2/2, 1 - ϵ + ϵ^2/2}
How to do it with Thread
:
If you use Thread
you need to wrap ReplaceAll[...]
with Unevaluated
to prevent ReplaceAll
being evaluated before Thread
gets to do its job:
Thread[Unevaluated[ReplaceAll[list, rules]]]
{-1 - ϵ - ϵ^2/2, 1 - ϵ + ϵ^2/2}
Another way to use Thread
:
ReplaceAll @@@ Thread[{list, rules}]
{-1 - ϵ - ϵ^2/2, 1 - ϵ + ϵ^2/2}
Least Mathematica way:
My candidate for doing this in the least Mathematicaesq way in Mathematica is:
For[results = {}; i = 1, i <= 2, i++, AppendTo[results, list[[i]] /. rules[[i]]]]
results
{-1 - ϵ - ϵ^2/2, 1 - ϵ + ϵ^2/2}
Halloween specials:
☺ = # /. #2 & @@@ ({##}\[Transpose]) &;
☺[list, rules]
{-1 - ϵ - ϵ^2/2, 1 - ϵ + ϵ^2/2}
☺☺ = {# /. #3, #2 /. #4} & @@ (## & @@@ {##}) &;
☺☺[list, rules]
{-1 - ϵ - ϵ^2/2, 1 - ϵ + ϵ^2/2}
"The least Mathematica way possible"? Something like this?
lst = {-1 - ϵ + 1/2 ϵ2 x''[0], 1 - ϵ + 1/2 ϵ2 x''[0]};
rules = {x''[0] -> -1, x''[0] -> 1};
Table[ lst[[i]] /. rules[[i]], {i, Length@rules}]