2
$\begingroup$

I have a function which depends on w,x,y,z. Let me call it Φ[w, x, y, z]. I want to square (a Φ[w1, x1, y1, z1] + b Φ[w2, x2, y2, z2]) and in the expansion I want to replace the product of the two Φ expressions with another function Ψ[w1, x1, y1, z1, w2, x2, y2, z2]. I will define only Ψ, not Φ. How can I do that?

$\endgroup$
1
  • 3
    $\begingroup$ Can you show what you have tried so far, to clarify the question? Did you try ReplaceAll ? $\endgroup$
    – Szabolcs
    Commented Oct 21, 2015 at 12:27

1 Answer 1

6
$\begingroup$

One possibility that comes to mind is using UpValues (nice SE post explaining it).

If I understand you right, you basically want this:

phi /: phi[a__] * phi[b__] = psi[a, b]
phi /: phi[a__]^2 = psi[a, a]

where you basically tell Mathematica that if phi appears anywhere, it should look in what context it is -- i.e., in a product or a square -- and replace it by the appropriate expression on the right side of the assignment.

This will be global, so anytime you write phi, Mathematica will check for context (which introduces an overhead) but you won't have to think about applying rules. (Which can also be dangerous if you use phi for different functions).

$\endgroup$
0

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.