Skip to main content
added 1 character in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

Fold ReplaceAll using A custom ordering function to sort rules sorted by a custom ordering function:

order[a_Rule, b_Rule] := (a /. b) =!= a;

expr = A^3 B^4/b + A^3 B^4 C X/d;
rules = {A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ}

{A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ}

Sort[rules, order]

{A^3 B^4 C -> Δ, X -> ρ, A^3 B^4 -> Γ}

Using ReplaceAll with original or sorted rules does not give the desired result:

expr /. rules

Γ/b + (C X Γ)/d

expr /. Sort[rules, order]

Γ/b + (X Δ)/d

We get the desired result by Folding ReplaceAll over the sorted rules:

Fold[# /. #2 &Fold[ReplaceAll, expr, Sort[rules, order]]

Γ/b + (Δ ρ)/d

or using ReplaceRepeated with the sorted rules:

expr //. Sort[rules, order]

Γ/b + (Δ ρ)/d

Carl's approach is the cleanest as it does not require any additional processing:

Simplify[expr, rules /. Rule -> Equal]

Γ/b + (Δ ρ)/d

Fold ReplaceAll using rules sorted by a custom ordering function:

order[a_Rule, b_Rule] := (a /. b) =!= a;

expr = A^3 B^4/b + A^3 B^4 C X/d;
rules = {A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ}

{A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ}

Sort[rules, order]

{A^3 B^4 C -> Δ, X -> ρ, A^3 B^4 -> Γ}

expr /. rules

Γ/b + (C X Γ)/d

expr /. Sort[rules, order]

Γ/b + (X Δ)/d

Fold[# /. #2 &, expr, Sort[rules, order]]

Γ/b + (Δ ρ)/d

Carl's approach is the cleanest as it does not require any additional processing:

Simplify[expr, rules /. Rule -> Equal]

Γ/b + (Δ ρ)/d

A custom ordering function to sort rules:

order[a_Rule, b_Rule] := (a /. b) =!= a;

expr = A^3 B^4/b + A^3 B^4 C X/d;
rules = {A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ}

{A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ}

Sort[rules, order]

{A^3 B^4 C -> Δ, X -> ρ, A^3 B^4 -> Γ}

Using ReplaceAll with original or sorted rules does not give the desired result:

expr /. rules

Γ/b + (C X Γ)/d

expr /. Sort[rules, order]

Γ/b + (X Δ)/d

We get the desired result by Folding ReplaceAll over the sorted rules:

Fold[ReplaceAll, expr, Sort[rules, order]]

Γ/b + (Δ ρ)/d

or using ReplaceRepeated with the sorted rules:

expr //. Sort[rules, order]

Γ/b + (Δ ρ)/d

Carl's approach is the cleanest as it does not require any additional processing:

Simplify[expr, rules /. Rule -> Equal]

Γ/b + (Δ ρ)/d

added 98 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

Fold ReplaceAll using rules sorted by a custom ordering function:

order[a_Rule, b_Rule] := (a /. b) =!= a;

expr = A^3 B^4/b + A^3 B^4 C X/d;
rules = {A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ};

{A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ}

Sort[rules, order]

{A^3 B^4 C -> Δ, X -> ρ, A^3 B^4 -> Γ}

expr /. rules

Γ/b + (C X Γ)/d

expr /. Sort[rules, order]

Γ/b + (X Δ)/d

Fold[# /. #2 &, expr, Sort[rules, order]]

Γ/b + (Δ ρ)/d

Carl's approach is the cleanest as it does not require any additional processing:

Simplify[expr, rules /. Rule -> Equal]

Γ/b + (Δ ρ)/d

Fold ReplaceAll using rules sorted by a custom ordering function:

order[a_Rule, b_Rule] := (a /. b) =!= a;

expr = A^3 B^4/b + A^3 B^4 C X/d;
rules = {A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ};

Sort[rules, order]

{A^3 B^4 C -> Δ, X -> ρ, A^3 B^4 -> Γ}

expr /. rules

Γ/b + (C X Γ)/d

expr /. Sort[rules, order]

Γ/b + (X Δ)/d

Fold[# /. #2 &, expr, Sort[rules, order]]

Γ/b + (Δ ρ)/d

Fold ReplaceAll using rules sorted by a custom ordering function:

order[a_Rule, b_Rule] := (a /. b) =!= a;

expr = A^3 B^4/b + A^3 B^4 C X/d;
rules = {A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ}

{A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ}

Sort[rules, order]

{A^3 B^4 C -> Δ, X -> ρ, A^3 B^4 -> Γ}

expr /. rules

Γ/b + (C X Γ)/d

expr /. Sort[rules, order]

Γ/b + (X Δ)/d

Fold[# /. #2 &, expr, Sort[rules, order]]

Γ/b + (Δ ρ)/d

Carl's approach is the cleanest as it does not require any additional processing:

Simplify[expr, rules /. Rule -> Equal]

Γ/b + (Δ ρ)/d

added 98 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

Sorting rulesFold ReplaceAll using rules sorted by a custom ordering function: order[a_Rule, b_Rule] := (a /. b) =!= a;

order[a_Rule, b_Rule] := (a /. b) =!= a;

expr = A^3 B^4/b + A^3 B^4 C X/d;
rules = {A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ};

Sort[rules, order]

{A^3 B^4 C -> Δ, X -> ρ, A^3 B^4 -> Γ}

expr /. rules

Γ/b + (C X Γ)/d

expr /. Sort[rules, order]

Γ/b + (X Δ)/d

Fold[# /. #2 &, expr, Sort[rules, order]]

Γ/b + (Δ ρ)/d

Sorting rules by a custom ordering function: order[a_Rule, b_Rule] := (a /. b) =!= a;

expr = A^3 B^4/b + A^3 B^4 C X/d;
rules = {A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ};

Sort[rules, order]

{A^3 B^4 C -> Δ, X -> ρ, A^3 B^4 -> Γ}

expr /. rules

Γ/b + (C X Γ)/d

expr /. Sort[rules, order]

Γ/b + (X Δ)/d

Fold ReplaceAll using rules sorted by a custom ordering function:

order[a_Rule, b_Rule] := (a /. b) =!= a;

expr = A^3 B^4/b + A^3 B^4 C X/d;
rules = {A^3 B^4 -> Γ, X -> ρ, A^3 B^4 C -> Δ};

Sort[rules, order]

{A^3 B^4 C -> Δ, X -> ρ, A^3 B^4 -> Γ}

expr /. rules

Γ/b + (C X Γ)/d

expr /. Sort[rules, order]

Γ/b + (X Δ)/d

Fold[# /. #2 &, expr, Sort[rules, order]]

Γ/b + (Δ ρ)/d

Post Undeleted by kglr
Post Deleted by kglr
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading