Skip to main content
3 of 4
Routine clean-up
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

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

AndreasP
  • 608
  • 5
  • 10