1
$\begingroup$

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]])
$\endgroup$
6
  • $\begingroup$ Please enter your expression in a code block using Mathematica code. $\endgroup$
    – Bob Hanlon
    Sep 7, 2016 at 2:55
  • $\begingroup$ edited accordingly $\endgroup$
    – pyrex
    Sep 7, 2016 at 3:16
  • 1
    $\begingroup$ There brackets don't balance so the expression will not evaluate $\endgroup$
    – Bob Hanlon
    Sep 7, 2016 at 3:24
  • 1
    $\begingroup$ Something on the lines of yourexpression /. {a : Φ[___] :> a, Subscript[d, a : 1 | 2] :> ToExpression["d" <> IntegerString[a]]}? The first replacement prevents other replacements of expressions with head Φ. $\endgroup$ Sep 7, 2016 at 3:32
  • $\begingroup$ corrected the brackets on the copy/paste version $\endgroup$
    – pyrex
    Sep 7, 2016 at 3:36

1 Answer 1

2
$\begingroup$

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_Φ).

$\endgroup$
2
  • 1
    $\begingroup$ In this case, you could also do a_Φ :> a. $\endgroup$
    – march
    Sep 7, 2016 at 3:46
  • $\begingroup$ Thank you so much everyone for the prompt reply, this was exactly what i was looking for. $\endgroup$
    – pyrex
    Sep 7, 2016 at 5:23

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.