2
$\begingroup$

I have a polynomial in $\rho$ with many parameters (I will omit the polynomial code in the Mathematica expressions below because it is too long):

$$ \rho ^3 J (\mu +1) \left[(A \delta \tau _0+B (1-\delta ) \tau _1\right]+ \rho ^2 \left[\left(A \delta \tau _0+B (1-\delta ) \tau _1\right) \left((\mu +1) V_B-J\right)+A (1-\delta ) J (\mu +1) \tau _0+B \delta J (\mu +1) \tau _1+\delta \mu ^2 \tau _0+(1-\delta ) \mu ^2 \tau _1\right]+ \rho \left[A (1-\delta ) \tau _0 \left((\mu +1) V_B-J\right)-V_B \left(A \delta \tau _0+B (1-\delta ) \tau _1\right)+B \delta \tau _1 \left((\mu +1) V_B-J\right)+(1-\delta ) \mu ^2 \tau _0+\delta \mu ^2 \tau _1\right] -A (1-\delta ) \tau _0 V_B-B \delta \tau _1 V_B $$

Notice that $B$ is both a parameter and an index for $V_B$.

Say I saved this polynomial in p, then when I use Replace using levelspec, such as:

Replace[p, {δ -> 0, B -> 0}, 6]

Mathematica substitutes both $B$ in the coefficients and in the subscripts (why would anyone want this?):

$$ \rho \left(A \tau _0 \left((\mu +1) V_B-J\right)+\mu ^2 \tau _0\right)+\rho ^2 \left(A J (\mu +1) \tau _0+\mu ^2 \tau _1\right)-A \tau _0 V_0 $$

Notice now there's a $V_0$ and a $V_B$. If I don't use levelspec, and use /. instead, all $B$ are replaced, in all indices (which is obvisouly what I do not want).

How to avoid subscripts substitution?

polynomial code is here:

p=-A (1 - δ) Subscript[V, B] Subscript[τ, 0] - B δ Subscript[V, B] Subscript[τ, 1] + J (1 + μ) ρ^3 (A δ Subscript[τ, 0] + B (1 - δ) Subscript[τ, 1]) + ρ ((1 - δ) μ^2 Subscript[τ, 0] + A (1 - δ) (-J + (1 + μ) Subscript[V, B]) Subscript[τ, 0] + δ μ^2 Subscript[τ, 1] + B δ (-J + (1 + μ) Subscript[V, B]) Subscript[τ, 1] - Subscript[V, B] (A δ Subscript[τ, 0] + B (1 - δ) Subscript[τ,1])) + ρ^2 (δ μ^2 Subscript[τ, 0] + A J (1 - δ) (1 + μ) Subscript[τ, 0] + (1 - δ) μ^2 Subscript[τ, 1] + B J δ (1 + μ) Subscript[τ, 1] + (-J + (1 + μ) Subscript[V, B]) (A δ Subscript[τ, 0] + B (1 - δ) Subscript[τ, 1]))

$\endgroup$

3 Answers 3

9
$\begingroup$

The real solution here is not to use B in a subscript, or better: not to use Subscript at all.

If you can't or don't want to do that, you can add an extra transformation rule for all Subscript expressions. Once ReplaceAll processes a subexpression, it won't touch it anymore.

expr = Subscript[x, B] + B;

expr /. B -> 1
(* 1 + Subscript[x, 1] *)

expr /. {s_Subscript :> s, B -> 1}
(* 1 + Subscript[x, B] *)
$\endgroup$
3
$\begingroup$

Mathematica does not concern itself with why you might want to do something, it does what you indicate that you want done.

Convert the subscripts to strings prior to replacing B

p /. {Subscript[var_, sub__] :> Subscript[var, ToString[sub]], δ -> 0, 
  B -> 0}

Or, if you don't want the subscripts left as strings

p /. {Subscript[var_, sub__] :> Subscript[var, ToString[sub]], 
  δ -> 0, B -> 0} /. 
    {Subscript[var_, sub__] :> Subscript[var, ToExpression[sub]]}
$\endgroup$
2
$\begingroup$

Try this:

    p /. Subscript[a_, B] -> Subscript[a, x] /. {δ -> 0, 
   B -> 0} /. Subscript[a_, x] -> Subscript[a, B]

yielding the following

enter image description here

Have fun!

$\endgroup$

Your Answer

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

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