0
$\begingroup$

I need to create rule2 with the same right hand side of rule1. This RHS contains If statements (and in fact quite a few more things that are calculated with the matched symbols, before the final expression).

rule2 = LHS -> rule1[[2]] ignores the If statement, but works.

rule2 = LHS :> rule1[[2]] does not work (does not associate matched stuff on the LHS)

I cannot use Condition (/;), because it clashes with another function that adds another Condition to the Rule

Minimal example:

rule1 = F[X_,n_?(#>=2&),a_,b_,ind__] :> If[a===b || Length[{ind}]===0, CANCEL, n*G[X,n,a,b,ind] ]
rule2 = H[X_,n_?(#>=0&),a_,b_,ind__] -> (rule1[[2]]/.n->n+2)

F[A,3,a,b,j]/.rule1 (*3 G[A, 3, a, b, j]*)
H[A,3,a,a,j]/.rule2 (*should CANCEL because a==b*)

Any help would be so useful.

$\endgroup$
1
  • $\begingroup$ You can use ReplacePart to replace part 1 (the LHS) of rule1 without affecting anything else $\endgroup$
    – Lukas Lang
    Commented Dec 15, 2023 at 16:40

1 Answer 1

2
$\begingroup$

I think that it's likely you could improve this whole situation with a redesign. You're basically trying to simulate what happens with SetDelayed, and it's probably easier to just use SetDelayed. But without full context, I don't know how to prescribe that for you. So, in the meantime, what I would suggest is to pull out the duplicated piece (the right hand side of your rule1) into its own representation:

RHS[X_, n_, a_, b_, ind__] := If[a === b || Length[{ind}] === 0, CANCEL, n*G[X, n, a, b, ind]]

rule1A = F[X_, n_?(# >= 2 &), a_, b_, ind__] :> RHS[X, n, a, b, ind];
rule2A = H[X_, n_?(# >= 0 &), a_, b_, ind__] :> RHS[X, n + 2, a, b, ind];

F[A, 3, a, b, j] /. rule1A
(* 3 G[A, 3, a, b, j] *)

H[A, 3, a, a, j] /. rule2A
(* CANCEL *)
$\endgroup$

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.