3
$\begingroup$

How can I modify multiple rules simultaneously in a single list of rules? Right now I generate two lists, where both rules are replaced with the same value in each single list. But what I want is the result at the bottom. One replaced list with both elements updated accordingly. I know that the table is going to give me two separate list, however that is as close as I have gotten thus far.

parameterNamesThreadedThroughNumericList = {k1p -> 0.034859, 
k1m -> 86.3298, k2p -> 0.12406, k2m -> 79.72530, pfb -> 9.70214, 
k3p -> 0.016358, k3m -> 18.5253, k4p -> 4.53765, k4m -> 28.02712, 
k5p -> 1.17405, k5m -> 41.91223, k6p -> 9.86758, k6m -> 51.70695, 
kn -> 1.06146, kc -> 0.02758, k8p -> 2.20933, k8m -> 82.13274, 
k9p -> 10.47200, k9m -> 90.98189, nfbs -> 0.08481, nfbX -> 3.58059, 
state -> 1.}
alternativeParameterLists = {state -> 0., pfb -> .5};
Table[parameterNamesThreadedThroughNumericList //.
   Rule[p : Alternatives @@ 
  alternativeParameterLists[[All, 1]], _] :> 
   Rule[p, alternativeParameterLists[[i, 2]]*alternativeParameterLists[[i, 1]] /. 
 parameterNamesThreadedThroughNumericList
     ],{i, 1, 2, 1}]

{{k1p -> 0.034859, k1m -> 86.3298, k2p -> 0.12406, k2m -> 79.7253, 
 pfb -> 0., k3p -> 0.016358, k3m -> 18.5253, k4p -> 4.53765, 
 k4m -> 28.0271, k5p -> 1.17405, k5m -> 41.9122, k6p -> 9.86758, 
 k6m -> 51.707, kn -> 1.06146, kc -> 0.02758, k8p -> 2.20933, 
 k8m -> 82.1327, k9p -> 10.472, k9m -> 90.9819, nfbs -> 0.08481, 
 nfbX -> 3.58059, state -> 0.},
 {k1p -> 0.034859, k1m -> 86.3298, 
 k2p -> 0.12406, k2m -> 79.7253, pfb -> 4.85107, k3p -> 0.016358, 
 k3m -> 18.5253, k4p -> 4.53765, k4m -> 28.0271, k5p -> 1.17405, 
 k5m -> 41.9122, k6p -> 9.86758, k6m -> 51.707, kn -> 1.06146, 
 kc -> 0.02758, k8p -> 2.20933, k8m -> 82.1327, k9p -> 10.472, 
 k9m -> 90.9819, nfbs -> 0.08481, nfbX -> 3.58059, state -> 4.85107}}

{k1p -> 0.034859, 
k1m -> 86.3298, k2p -> 0.12406, k2m -> 79.72530, pfb -> 4.85107, 
k3p -> 0.016358, k3m -> 18.5253, k4p -> 4.53765, k4m -> 28.02712, 
k5p -> 1.17405, k5m -> 41.91223, k6p -> 9.86758, k6m -> 51.70695, 
kn -> 1.06146, kc -> 0.02758, k8p -> 2.20933, k8m -> 82.13274, 
k9p -> 10.47200, k9m -> 90.98189, nfbs -> 0.08481, nfbX -> 3.58059, 
state -> 0.}

I've seen that this question is related to How to replace multiple variables at once However, because I am replacing old rules with new rules based on parts of the expression I believe it is different enough to be considered unique.

$\endgroup$
1

1 Answer 1

4
$\begingroup$
Normal@Merge[{parameterNamesThreadedThroughNumericList, alternativeParameterLists}, 
 Times @@ # &]

or

Normal@Merge[Times@@#&][{parameterNamesThreadedThroughNumericList, 
   alternativeParameterLists}]

both give

{k1p -> 0.034859, k1m -> 86.3298, k2p -> 0.12406, k2m -> 79.7253, pfb -> 4.85107, k3p -> 0.016358, k3m -> 18.5253, k4p -> 4.53765, k4m -> 28.0271, k5p -> 1.17405, k5m -> 41.9122, k6p -> 9.86758, k6m -> 51.707, kn -> 1.06146, kc -> 0.02758, k8p -> 2.20933, k8m -> 82.1327, k9p -> 10.472, k9m -> 90.9819, nfbs -> 0.08481, nfbX -> 3.58059, state -> 0.}

$\endgroup$
3
  • $\begingroup$ You don't even need to convert to Association first. +1 of course. $\endgroup$
    – Mr.Wizard
    Commented Oct 9, 2016 at 18:37
  • $\begingroup$ Thank you @Mr.Wizard. I didn't know Merge worked with lists of rules. $\endgroup$
    – kglr
    Commented Oct 9, 2016 at 19:12
  • $\begingroup$ This is much cleaner than the approach than I was taking. The original code was written pre-Associations. This is motivation enough to port the whole package to a newer version. $\endgroup$
    – tarhawk
    Commented Oct 11, 2016 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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