1
$\begingroup$

I have a large list of transformation rules that I want to simplify.

I find Experimental`OptimizeExpression works as it gets me the answers.

My question is:

I can only get this to work when I convert my list of rules into a list of assignments - which is obviously what i want to avoid doing - by using

list1 /.Rule -> Set

How can I avoid making the assignments and still useExperimental`OptimizeExpression?

Please note:

I have a solution that gives me the outcome, I want to avoid the side effect of the assignments. Whilst alternative solutions are appreciated, the real issue I have is the side effect that assigns values to the symbols in the rule sets!

I apologise for my incompetence making this clear.

For example

list1 = {a -> b + c, d -> b + c};
list2 = {b -> 1, c -> 1};
Experimental`OptimizeExpression[{list1 /. Rule -> Set, list2 /. Rule -> Set}];
%[[1, 2]]

(* Out= {{2, 2}, {1, 1}} *)

This does indeed simplify my list of transformation rules, however the assignment has some drawbacks as my rules are now evaluating, which I clearly do not want. I am creating OwnValues for each individual transformation rule, effectively disabling the transformation rules.

list1
list2
(* Out= {2 -> 2, 2 -> 2} *)
(* Out= {1 -> 1, 1 -> 1} *)

PLEASE NOTE THE DESIRED RESULT SHOULD BE

(* Out= {a -> b + c, d -> b + c} *)
(* Out= {b -> 1, c -> 1}; *)

I can clear the assignments, and then all works fine, but there must be a more elegant solution.

Clear[a, b, c, d]
list1
list2

(* Out= {a -> b + c, d -> b + c} *)
(* Out= {b -> 1, c -> 1} *)

Now there have kindly been several alternative solutions for the above example provided and I have tried a lot myself as well, however I cannot get any of them to work with the full set of lists which is more complex (and imported from another application so it looks a bit messy):

lists = {v[4] -> v[22], v[6] -> v[16], v[15] -> v[17], v[31] -> v[22]*v[39], 
   v[32] -> v[75], v[33] -> v[22], v[35] -> v[21], v[41] -> v[22] + v[26] - v[63], 
   v[45] -> v[23], v[51] -> v[22], v[2] -> 0.25, v[3] -> 14, v[17] -> 100000000, 
   v[20] -> 5, v[22] -> 2000000000000, v[23] -> 1, v[18] -> 400000000000, 
   v[19] -> 200000000000, v[24] -> 0.5, v[27] -> 0.7, v[29] -> -1, v[34] -> 0.03, 
   v[36] -> 0.05, v[37] -> 333, v[38] -> 222, v[39] -> 0.3, v[46] -> 0.01, 
   v[47] -> 1000, v[52] -> 0.0175, v[53] -> 0, v[54] -> 0, v[55] -> 0, v[56] -> 0, 
   v[57] -> 0, v[58] -> 0, v[59] -> 0, v[60] -> 1, v[61] -> 0, v[64] -> 3, 
   v[65] -> 0.4, v[67] -> 0.0625, v[66] -> 0.4, v[68] -> 0.25, v[69] -> 0.25, 
   v[71] -> 2.5, v[73] -> 0.5, v[70] -> 2.5, v[72] -> 4, v[74] -> 0.0625, 
   v[1] -> v[6]/v[3] + v[7] + v[14] + v[25] + (-v[6] + v[11])/v[64], 
   v[7] -> v[5]*v[41], v[8] -> v[18]*v[43]*v[55], v[9] -> v[19]*v[43]*v[56], 
   v[10] -> v[26] + (1 - v[24])*v[44] + v[24]*v[51] - v[63], 
   v[11] -> (v[2]*v[33])/(v[3]^(-1) + v[30]), v[12] -> ((1 - v[2])*v[51])/v[50], 
   v[13] -> v[33] v[39], v[14] -> (v[13] - v[31])/v[66], v[25] -> v[8] + v[18], 
   v[26] -> v[9] + v[19], v[28] -> v[36], 
   v[30] -> v[
      34]/((v[4]/v[22])^(v[27]/v[29]) ((v[22]*v[45])/(v[20]*v[35]))^v[29]^(-1)), 
   v[40] -> (v[18] + v[19])/v[22], 
   v[42] -> -(v[6]/v[3]) + (1 - v[24])*v[44] + v[24]*v[51], 
   v[43] -> v[32] - v[36] + ((-v[12] + v[15])*v[59])/(v[15] v[65]), 
   v[44] -> (v[6]/v[16])^v[2] (v[15]/v[17])^(1 - v[2])*v[22], 
   v[48] -> (v[12] - v[15])/(v[15]*v[65]), 
   v[49] -> -(v[6]/v[3]) - v[7] - v[25] + (1 - v[24])*v[44] + 
     v[24] v[51] - (-v[6] + v[11])/v[64], v[62] -> v[21] (1 + v[43] v[58]), 
   v[63] -> (v[18] + v[19]) (1 - v[60]) + 
     v[40]*((1 - v[24])*v[44] + v[24]*v[51]) v[60], 
   v[75] -> ((-v[15] + v[17]/(1 - v[36])) (1 - v[36]))/v[17], 
   v[5] -> (-(v[6]/v[3]) + v[41])/v[41], v[16] -> (v[2]*v[22])/(v[3]^(-1) + v[34]), 
   v[21] -> (v[22]*v[23])/v[20], v[50] -> ((1 - v[2])*v[22])/v[17]};

Daniel Lichtblau's suggestion was:

polys = lists /. Rule -> Subtract;
vars = Cases[lists, _Symbol, Infinity] // Union;
gb = GroebnerBasis[polys, vars];
reds = PolynomialReduce[vars, gb, vars][[All, 2]];
Thread[vars->reds]

(* Out = {v[1] -> 0., v[2] -> 0., v[3] -> 0., v[4] -> 0., v[5] -> 0., v[6] -> 0., v[7] -> 0., v[8] -> 0., v[9] -> 0., v[10] -> 0., v[11] -> 0., v[12] -> 0., v[13] -> 0., v[14] -> 0., v[15] -> 0., ...*)

I am most likely missing the point (Groebnerbasis[] are not something I am familiar with) but this is not the desired result.

Te compare with an approach using Experimental`OptimizeExpression, this provides the expected outcome, but I assign values to symbols to make it work (undesirable):

blocks = lists;
Block[{blocks2 = blocks /. Rule -> Set}, 
res=Experimental`OptimizeExpression[blocks2]];
res[[1]]

(* Out = {2000000000000, 4.92958*10^12, 100000000, 6.*10^11, 0.05, 2000000000000, 400000000000, 1.6*10^12, 1, 2000000000000, 0.25, 14, 100000000, 5, 2000000000000, ...} *)

And the illustration how the assignments make the symbols in the original rules evaluate, thereby making the transformation list useless:

Thread[vars->res[[1]]]

(* Out = {2000000000000 -> 2000000000000, 4.92958*10^12 -> 4.92958*10^12, 100000000 -> 100000000, 6.*10^11 -> 6.*10^11, 0.05 -> 0.05...} *)

Without having to resort to assigning via /.Rule->Set I would have gotten the correct answer:

(* Out = {v[1] -> 2000000000000, v[2] -> 4.92958*10^12, v[3] -> 100000000, v[4] -> 6.*10^11, v[5] -> 0.05, v[6] -> 2000000000000, v[7] -> 400000000000, v[8] -> 1.6*10^12, v[9] -> 1, ...} *)

$\endgroup$
  • $\begingroup$ I tried scoping (Block and Module) as well as several nonstandard evaluation tricks but I am missing something obvious (or less likely, ran into something nontrivial). $\endgroup$ – Sander Aug 14 '14 at 8:35
  • 1
    $\begingroup$ You wrote I convert these into equations, this is not true. An equation in Mathematica is something like lhs==rhs but you use Set (=, only one =) which is the assignment operator. I'm pretty sure that you don't want this in the first place. $\endgroup$ – halirutan Aug 14 '14 at 10:06
  • $\begingroup$ Thanks, I guess this is my struggle: how to avoid making an assignment and still use Experimental`OptimizeExpression to simplify a list of rules. $\endgroup$ – Sander Aug 14 '14 at 10:08
  • 2
    $\begingroup$ I'm sorry, but this question still doesn't make any sense. You still have not shown real input or described the output that you want. Experimental`OptimizeExpression does not require "a list of assignments" even if that is what your code produced, which as halirutan explains it does not. Give an example of the simplified rules that you expect as output or this question cannot be answered. $\endgroup$ – Mr.Wizard Aug 14 '14 at 10:35
  • $\begingroup$ @Mr.Wizard Thanks for explaining that Experimental`OptimizeExpression does not require assignments per se. Trying to inject assignments caused the problems in the first place, so if this is not required, the problem will go away.. $\endgroup$ – Sander Aug 14 '14 at 12:18
3
$\begingroup$

Update

Following the new examples you gave I believe this does what you want:

Thread[lists[[All, 1]] -> (lists[[All, 2]] //. lists)]

Or somewhat more cleanly:

MapAt[# //. lists &, lists, {All, 2}]

Or tersely:

# -> (#2 //. lists) & @@@ lists

Output:

{v[4] -> 2000000000000, v[6] -> 4.92958*10^12, v[15] -> 100000000, v[31] -> 6.*10^11, 
 v[32] -> 0.05, v[33] -> 2000000000000, v[35] -> 400000000000, v[41] -> 1.6*10^12, 
 v[45] -> 1, v[51] -> 2000000000000, v[2] -> 0.25, v[3] -> 14, v[17] -> 100000000, 
 v[20] -> 5, v[22] -> 2000000000000, v[23] -> 1, v[18] -> 400000000000, 
 v[19] -> 200000000000, v[24] -> 0.5, v[27] -> 0.7, v[29] -> -1, v[34] -> 0.03, 
 v[36] -> 0.05, v[37] -> 333, v[38] -> 222, v[39] -> 0.3, v[46] -> 0.01, v[47] -> 1000, 
 v[52] -> 0.0175, v[53] -> 0, v[54] -> 0, v[55] -> 0, v[56] -> 0, v[57] -> 0, v[58] -> 0, 
 v[59] -> 0, v[60] -> 1, v[61] -> 0, v[64] -> 3, v[65] -> 0.4, v[67] -> 0.0625, 
 v[66] -> 0.4, v[68] -> 0.25, v[69] -> 0.25, v[71] -> 2.5, v[73] -> 0.5, v[70] -> 2.5, 
 v[72] -> 4, v[74] -> 0.0625, v[1] -> 2.*10^12, v[7] -> 1.24789*10^12, v[8] -> 0, 
 v[9] -> 0, v[10] -> 1.6*10^12, v[11] -> 4.92958*10^12, v[12] -> 1.*10^8, 
 v[13] -> 6.*10^11, v[14] -> 0., v[25] -> 400000000000, v[26] -> 200000000000, 
 v[28] -> 0.05, v[30] -> 0.03, v[40] -> 3/10, v[42] -> 1.64789*10^12, 
 v[43] -> -1.38778*10^-17, v[44] -> 2.*10^12, v[48] -> 0., v[49] -> 0.000244141, 
 v[62] -> 400000000000, v[63] -> 6.*10^11, v[75] -> 0.05, v[5] -> 0.77993, 
 v[16] -> 4.92958*10^12, v[21] -> 400000000000, v[50] -> 15000.}

The left-hand sides of the rules do not match but the values (right-hand sides) do with only a couple of exceptions and with small variance. I believe this is correct and your example output is in error. If however the order of your rules is truly what you want you will need to sort the left-hand-sides separately:

Thread[Sort @ lists[[All, 1]] -> (lists[[All, 2]] //. lists)]

Output:

{v[1] -> 2000000000000, v[2] -> 4.92958*10^12, v[3] -> 100000000, v[4] -> 6.*10^11, 
 v[5] -> 0.05, v[6] -> 2000000000000, v[7] -> 400000000000, v[8] -> 1.6*10^12, v[9] -> 1, 
 v[10] -> 2000000000000, v[11] -> 0.25, v[12] -> 14, v[13] -> 100000000, v[14] -> 5, 
 v[15] -> 2000000000000, v[16] -> 1, v[17] -> 400000000000, v[18] -> 200000000000, 
 v[19] -> 0.5, v[20] -> 0.7, v[21] -> -1, v[22] -> 0.03, v[23] -> 0.05, v[24] -> 333, 
 v[25] -> 222, v[26] -> 0.3, v[27] -> 0.01, v[28] -> 1000, v[29] -> 0.0175, v[30] -> 0, 
 v[31] -> 0, v[32] -> 0, v[33] -> 0, v[34] -> 0, v[35] -> 0, v[36] -> 0, v[37] -> 1, 
 v[38] -> 0, v[39] -> 3, v[40] -> 0.4, v[41] -> 0.0625, v[42] -> 0.4, v[43] -> 0.25, 
 v[44] -> 0.25, v[45] -> 2.5, v[46] -> 0.5, v[47] -> 2.5, v[48] -> 4, v[49] -> 0.0625, 
 v[50] -> 2.*10^12, v[51] -> 1.24789*10^12, v[52] -> 0, v[53] -> 0, v[54] -> 1.6*10^12, 
 v[55] -> 4.92958*10^12, v[56] -> 1.*10^8, v[57] -> 6.*10^11, v[58] -> 0., 
 v[59] -> 400000000000, v[60] -> 200000000000, v[61] -> 0.05, v[62] -> 0.03, 
 v[63] -> 3/10, v[64] -> 1.64789*10^12, v[65] -> -1.38778*10^-17, v[66] -> 2.*10^12, 
 v[67] -> 0., v[68] -> 0.000244141, v[69] -> 400000000000, v[70] -> 6.*10^11, 
 v[71] -> 0.05, v[72] -> 0.77993, v[73] -> 4.92958*10^12, v[74] -> 400000000000, 
 v[75] -> 15000.}

I don't know what you mean when you write: "Exactly what I am looking for ..." as I don't know what output you expect from your operation. If you merely want {{2, 2}, {1, 1}} as the output then:

list1 = {a -> b + c, d -> b + c};
list2 = {b -> 1, c -> 1};

{list1, list2} /. list2 // Values
{{2, 2}, {1, 1}}

Can you give a different example where OptimizeExpression is necessary?

$\endgroup$
  • $\begingroup$ Thanks for looking into this and apologies for stating the problem not clear enough. I am trying to avoid the creation of OwnValues as these create a side effect when I want to use the transformation list after the optimization. (a->b+c evaluates to 2->2). That is the problem I am experiencing. The example in itself is less relevant as it works well (which is what I mean with "exactly what I am looking for"), even with the 1000 odd rules I am simplifying with it. $\endgroup$ – Sander Aug 14 '14 at 9:45
  • $\begingroup$ @Sander I still need to see an example of the actual input and output that you desire. As it is written I can't tell what isn't the undesirable side effect. What is your desired output? $\endgroup$ – Mr.Wizard Aug 14 '14 at 9:53
  • $\begingroup$ Desired output is for OwnValues@a to be undefined as it is before I call the Optimization. (* in= OwnValues@a Out= {HoldPattern[a] :> b + c} *). The same for OwnValues of b to d as they change my list1 and list2 of symbols into expressions that evaluate. $\endgroup$ – Sander Aug 14 '14 at 9:59
  • $\begingroup$ Seems my question can be posed much simpler. Will update now. $\endgroup$ – Sander Aug 14 '14 at 10:05
  • $\begingroup$ Thanks; amazing how I got bogged down resolving the assignment side effects when there was a much more elegant solution. $\endgroup$ – Sander Aug 15 '14 at 4:13
4
$\begingroup$

As others have noted, it's not entirely clear what you want. A possibility is you want to "simplify" the rules so that, on application, all four variables would give numbers. I'll show a way to obtain that and possibly you can modify to suit the actual need.

We'll start with the lists of rules and create defining polynomials and also extract the variables (trivial here but one would want this automated for large problems).

list1 = {a -> b + c, d -> b + c};
list2 = {b -> 1, c -> 1};
polys = Flatten[{list1, list2} /. Rule -> Subtract]
vars = Variables[polys]

(* Out[158]= {a - b - c, -b - c + d, -1 + b, -1 + c}

Out[159]= {a, b, c, d} *)

Now create a Groebner bases and use it to reduce all the variables.

gb = GroebnerBasis[polys, vars];
reds = PolynomialReduce[vars, gb, vars][[All, 2]]

(* {a - b - c, -b - c + d, -1 + b, -1 + c}

{2, 1, 1, 2} *)

Now we create new rules for all variables, with their reduced values on the right hand sides.

newrules = Thread[vars -> reds]
(* {a -> 2, b -> 1, c -> 1, d -> 2} *)

Hope that gives a start in the right direction, whatever that direction might be.

--- edit ---

Prompted in comments, I'll add a bigger example. It's random but it's indicative of the type of issues one can get into when faced with rules that might have interdependencies.

First we create a random set of rules that involve replacements amongst themselves and also numbers. This is a fairly trivial case insofar as it can be redone using linear algebra.

vars = {a, b, c, d, e, f, g, h};
n = Length[vars];
randomRHS = RandomInteger[{-5, 5}, {n, n + 1}].Prepend[vars, 1];
rules = Thread[vars -> randomRHS]

(* Out[174]= {a -> -4 - 5 a + 2 c + 3 d - 3 f - g - 3 h, 
 b -> 2 + 5 a + 4 b - 3 c - 2 d + 3 e - 5 f + 4 g - 3 h, 
 c -> 1 + 3 a + b - 4 c - 5 d - 5 e + 5 g + h, 
 d -> 1 - 2 a - 2 b + 4 c + d - 4 e - 4 f - 3 g + 3 h, 
 e -> -5 + 3 a + c - 3 d - 4 e - 4 g, 
 f -> 1 + 3 a - 2 b + 2 c + 3 d + 5 e - 3 f + 4 g - 4 h, 
 g -> -4 - 2 a + b + 5 c + 5 d + 3 e + 3 f + 4 g - 2 h, 
 h -> -2 + 4 a - 3 b + 5 c + 3 e - 3 g - h} *)

Now recast as "simpler" rules.

polys = rules /. Rule -> Subtract;
gb = GroebnerBasis[polys, vars];
reds = PolynomialReduce[vars, gb, vars][[All, 2]];
newrules = Thread[vars -> reds]

(* Out[183]= {a -> 23497/1518406, b -> 294955/759203, c -> 313959/759203,
  d -> 216021/1518406, e -> -(598498/759203), f -> 691035/1518406, 
 g -> -(194588/759203), h -> -(999106/759203)} *)

Is this any improvement over just using linear algebra? Not really. But it might be preferred in cases where the rules involve nonlinear dependencies.

--- end edit ---

--- edit #2 ---

Here is how to handle that full example. it is slightly more work than I had anticipated because the original rule set is no longer "polynomial" (some variables now appear in exponents). it can still be resolved algebraically.

I will repeat the full definition so this example will be self-contained.

lists = {v[4] -> v[22], v[6] -> v[16], v[15] -> v[17], 
   v[31] -> v[22]*v[39], v[32] -> v[75], v[33] -> v[22], 
   v[35] -> v[21], v[41] -> v[22] + v[26] - v[63], v[45] -> v[23], 
   v[51] -> v[22], v[2] -> 0.25, v[3] -> 14, v[17] -> 100000000, 
   v[20] -> 5, v[22] -> 2000000000000, v[23] -> 1, 
   v[18] -> 400000000000, v[19] -> 200000000000, v[24] -> 0.5, 
   v[27] -> 0.7, v[29] -> -1, v[34] -> 0.03, v[36] -> 0.05, 
   v[37] -> 333, v[38] -> 222, v[39] -> 0.3, v[46] -> 0.01, 
   v[47] -> 1000, v[52] -> 0.0175, v[53] -> 0, v[54] -> 0, v[55] -> 0,
    v[56] -> 0, v[57] -> 0, v[58] -> 0, v[59] -> 0, v[60] -> 1, 
   v[61] -> 0, v[64] -> 3, v[65] -> 0.4, v[67] -> 0.0625, 
   v[66] -> 0.4, v[68] -> 0.25, v[69] -> 0.25, v[71] -> 2.5, 
   v[73] -> 0.5, v[70] -> 2.5, v[72] -> 4, v[74] -> 0.0625, 
   v[1] -> v[6]/v[3] + v[7] + v[14] + v[25] + (-v[6] + v[11])/v[64], 
   v[7] -> v[5]*v[41], v[8] -> v[18]*v[43]*v[55], 
   v[9] -> v[19]*v[43]*v[56], 
   v[10] -> v[26] + (1 - v[24])*v[44] + v[24]*v[51] - v[63], 
   v[11] -> (v[2]*v[33])/(v[3]^(-1) + v[30]), 
   v[12] -> ((1 - v[2])*v[51])/v[50], v[13] -> v[33] v[39], 
   v[14] -> (v[13] - v[31])/v[66], v[25] -> v[8] + v[18], 
   v[26] -> v[9] + v[19], v[28] -> v[36], 
   v[30] -> 
    v[34]/((v[4]/v[22])^(v[27]/v[29]) ((v[22]*v[45])/(v[20]*v[35]))^
        v[29]^(-1)), v[40] -> (v[18] + v[19])/v[22], 
   v[42] -> -(v[6]/v[3]) + (1 - v[24])*v[44] + v[24]*v[51], 
   v[43] -> v[32] - v[36] + ((-v[12] + v[15])*v[59])/(v[15] v[65]), 
   v[44] -> (v[6]/v[16])^v[2] (v[15]/v[17])^(1 - v[2])*v[22], 
   v[48] -> (v[12] - v[15])/(v[15]*v[65]), 
   v[49] -> -(v[6]/v[3]) - v[7] - v[25] + (1 - v[24])*v[44] + 
     v[24] v[51] - (-v[6] + v[11])/v[64], 
   v[62] -> v[21] (1 + v[43] v[58]), 
   v[63] -> (v[18] + v[19]) (1 - v[60]) + 
     v[40]*((1 - v[24])*v[44] + v[24]*v[51]) v[60], 
   v[75] -> ((-v[15] + v[17]/(1 - v[36])) (1 - v[36]))/v[17], 
   v[5] -> (-(v[6]/v[3]) + v[41])/v[41], 
   v[16] -> (v[2]*v[22])/(v[3]^(-1) + v[34]), 
   v[21] -> (v[22]*v[23])/v[20], v[50] -> ((1 - v[2])*v[22])/v[17]};

Now create "polynomials" and extract all proper" variables. The Cases is to remove "variables" that are symbolic powers.

polys = Numerator[Together[Rationalize[lists] /. Rule -> Subtract]];
vars = Cases[Variables[polys], v[_]];

Proceed as above.

gb = GroebnerBasis[polys, vars];
reds = PolynomialReduce[vars, gb, vars][[All, 2]];
newrules = Thread[vars -> reds];

This gets us most of the way, except a few variables of interest are not unraveled. By substituting the values we have we can then get numerical values for the stragglers.

morerules = Solve[(polys /. newrules) == 0]

(* Out[51]= {{v[5] -> 443/568, v[30] -> 3/100, v[49] -> 0, 
  v[63] -> 600000000000}} *)

Now put these into the incomplete reducta and reform the rules.

allrules = With[{newreds = reds /. First[morerules]},
  Thread[vars -> newreds]]

(* Out[58]= {v[1] -> 2000000000000, v[2] -> 1/4, v[3] -> 14, 
 v[4] -> 2000000000000, v[5] -> 443/568, v[6] -> 350000000000000/71, 
 v[7] -> 88600000000000/71, v[8] -> 0, v[9] -> 0, 
 v[10] -> 1600000000000, v[11] -> 350000000000000/71, 
 v[12] -> 100000000, v[13] -> 600000000000, v[14] -> 0, 
 v[15] -> 100000000, v[16] -> 350000000000000/71, v[17] -> 100000000, 
 v[18] -> 400000000000, v[19] -> 200000000000, v[20] -> 5, 
 v[21] -> 400000000000, v[22] -> 2000000000000, v[23] -> 1, 
 v[24] -> 1/2, v[25] -> 400000000000, v[26] -> 200000000000, 
 v[27] -> 7/10, v[28] -> 1/20, v[29] -> -1, v[30] -> 3/100, 
 v[31] -> 600000000000, v[32] -> 1/20, v[33] -> 2000000000000, 
 v[34] -> 3/100, v[35] -> 400000000000, v[36] -> 1/20, v[37] -> 333, 
 v[38] -> 222, v[39] -> 3/10, v[40] -> 3/10, v[41] -> 1600000000000, 
 v[42] -> 117000000000000/71, v[43] -> 0, v[44] -> 2000000000000, 
 v[45] -> 1, v[46] -> 1/100, v[47] -> 1000, v[48] -> 0, v[49] -> 0, 
 v[50] -> 15000, v[51] -> 2000000000000, v[52] -> 7/400, v[53] -> 0, 
 v[54] -> 0, v[55] -> 0, v[56] -> 0, v[57] -> 0, v[58] -> 0, 
 v[59] -> 0, v[60] -> 1, v[61] -> 0, v[62] -> 400000000000, 
 v[63] -> 600000000000, v[64] -> 3, v[65] -> 2/5, v[66] -> 2/5, 
 v[67] -> 1/16, v[68] -> 1/4, v[69] -> 1/4, v[70] -> 5/2, 
 v[71] -> 5/2, v[72] -> 4, v[73] -> 1/2, v[74] -> 1/16, v[75] -> 1/20} *)

--- end edit #2 ---

$\endgroup$
  • $\begingroup$ Join[list1 /. list2, list2] seems a bit cleaner in this case. :o) (i.e. we still need better examples; if not from the OP, from you.) $\endgroup$ – Mr.Wizard Aug 14 '14 at 17:15
  • $\begingroup$ The problems with applying the rule lists to one another are in detecting dependencies and handling of large lists of lists. Also in handling circularities, if one were to use //. instead of /.. $\endgroup$ – Daniel Lichtblau Aug 14 '14 at 17:19
  • $\begingroup$ I think I actually understand that but without better examples your method appears to be performing a trivial operation. $\endgroup$ – Mr.Wizard Aug 14 '14 at 17:20
  • $\begingroup$ Trivial, yes. But with such surgical precision. $\endgroup$ – Daniel Lichtblau Aug 14 '14 at 17:21
  • 1
    $\begingroup$ Are you going to make me beg? $\endgroup$ – Mr.Wizard Aug 14 '14 at 17:22
1
$\begingroup$

Expanding on Mr.Wizard's answer, the side effects can be eliminated simply be doing the work inside a function rather than at top-level.

list1 = {a -> b + c, d -> b + c};
list2 = {b -> 1, c -> 1};
f[list1_, list2_] := {list1, list2} /. list2 // Values
f[list1, list2]
{{2, 2}, {1, 1}}
 Column@{{a, b, c, d}, list1, list2}

vars

$\endgroup$
  • $\begingroup$ Thank you, however I want to use ExperimentalOptimizeExpression because the rules I need to have simplified are much more complex. Please note, ExperimentalOptimizeExpression provides the right output for very complex situations, for this reason I am using trivial rules; the problem I have is that the assignment I make to pass the parameters into the function has the undesired side effect of assigning values to symbols I want to keep symbolic. $\endgroup$ – Sander Aug 14 '14 at 15:11
  • $\begingroup$ To indicate the issue, expanding on your solution but using ExperimentalOptimizeExpression; please note how the outcome is correct, but the transformation lists changes if the function is f[list1_, list2_] := ExperimentalOptimizeExpression[{list1 /. Rule -> Set, list2 /. Rule -> Set}] $\endgroup$ – Sander Aug 14 '14 at 15:33
  • 1
    $\begingroup$ @Sander. Your problem has nothing to do with Experimental`OptimizeExpression. That is a complete irrelevancy. It has to do with the way you use Set. You would get the same results with f[list1_, list2_] := {list1 /. Rule -> Set, list2 /. Rule -> Set} $\endgroup$ – m_goldberg Aug 14 '14 at 15:42
  • $\begingroup$ @Sander. You should use replacement rules inside a scoping construct to get what you want. Mr.Wizard did not use Set. I did not use Set. You should not use Set. $\endgroup$ – m_goldberg Aug 14 '14 at 15:48
  • $\begingroup$ @Sander. Experimental`OptimizeExpression is doing nothing for you. It is intended to optimize code for compilation. I see nothing about compilation in your question. Forget about Experimental`OptimizeExpression. I mean this sincerely and in your best interests. $\endgroup$ – m_goldberg Aug 14 '14 at 15:55

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.