1

I am trying to write a replacement rule that will substitute values for d_1 and d_2 only the places they are outside the Phi equation:

enter image description here

I was able to write a function that replaced Phi[d_1] and Phi[d_2] with Phi[d1] and Phi[d2], thereby creating two new variables and then apply my transformation rules only to the original d_1 and d_2. But this does not generalize for when the function Phi contains a linear combination of d_1 and d_2, or some other value that I don't want to substitute for. Can you help me figure how to do this?

In mathematica code:

E^(-2 r T) k^2 (1 + E^(
  2 Subscript[d, 1] (Subscript[d, 1] - Subscript[d, 2])) + 
  2 E^(1/2 (\!\(
  \*SubsuperscriptBox[\(d\), \(1\), \(2\)] - 
  \*SubsuperscriptBox[\(d\), \(2\), \(2\)]\))) (-1 + \[CapitalPhi][
   Subscript[d, 1]]) - 
  1] (Subscript[d, 1] - Subscript[d, 2])) \[CapitalPhi][
  2 Subscript[d, 1] - Subscript[d, 2]] - \[CapitalPhi][Subscript[d,
  2]])

Or, for easier copy/paste (brackets corrected):

E^(-2 r T) k^2 (1 + E^(2 Subscript[d, 1] (Subscript[d, 1] - Subscript[d, 2])) + 2 E^(1/2 (\!\(\*SubsuperscriptBox[\(d\), \(1\), \(2\)] -\*SubsuperscriptBox[\(d\), \(2\), \(2\)]\))) (-1 + \[CapitalPhi][Subscript[d, 1]]) - E^(2 Subscript[d, 1] (Subscript[d, 1] - Subscript[d, 2])) \[CapitalPhi][2 Subscript[d, 1] - Subscript[d, 2]] - \[CapitalPhi][Subscript[d,2]])
pyrex
  • 399
  • 1
  • 8

1 Answers1

2

You could exploit the fact that ReplaceAll does not replace the same thing twice.

For instance:

In[505]:= eq /. {a_Φ :> a, 
   Subscript[d, a : 1 | 2] :> 
    ToExpression["d" <> IntegerString[a]]}

Out[505]//InputForm=
(k^2*(1 + E^(2*d1*(d1 - d2)) + 2*E^((d1^2 - d2^2)/2)*
    (-1 + Φ[Subscript[d, 1]]) - E^(2*d1*(d1 - d2))*
    Φ[2*Subscript[d, 1] - Subscript[d, 2]] - Φ[Subscript[d, 2]]))/
 E^(2*r*T)

(eq is the equation in your question)

The first replacement a_Φ :> a doesn't do anything, but it tells ReplaceAll not to replace sub-expressions of Φ anymore.

P.S. Thanks @march for the comment (a : Φ[___] changed to a_Φ).

JungHwan Min
  • 4,664
  • 1
  • 22
  • 36