2

I have a function myFunc which I is to be displayed cleanly if arguments are symbolic, but which can also be numerically evaluated. I do this by defining a complicated auxiliary function pR which is evaluated only if its arguments are numerical (probably not a good idea?).

kin[a_, b_, c_] = a^2 + b^2 + c^2 - 2 a b - 2 a c - 2 b c;
pR[s_?NumericQ, m0_?NumericQ, m1_?NumericQ] := 
  1/s*Sqrt[kin[s, m0^2, m1^2]]*Log[(2 m0 m1)/(-s + m0^2 + m1^2 - Sqrt[kin[s, m0^2, m1^2]])]

The function (watered-down) I wish to define is

myFunc[n_?IntegerQ, s_, m0_, m1_] := 
  Sum[
    Binomial[n + 1, 2 idx3 + 1]*((s + m0^2 - m1^2)/(2 s))^(n - 2 idx3) 
     *(kin[s, m0^2, m1^2]/(4 s^2))^idx3, {idx3, 0, (n + 1)/2}] pR[s, m0, m1];

So for example:

myFunc[4,s,m,m]//Simplify
(*  ((m^4 - 3 m^2 s + s^2) pR[s, m, m])/s^2   *)

and the complicated mess is in pR. The problem I'm running into is:

  1. I don't know how to code it so that the user can forcibly display the function pR in its entirety -- even if its arguments are symbolic, overriding the ?NumericQ.
  2. I would like to be able to take derivatives (D), Limits, and perform a Taylor Series expansion on myFunc appropriately handling the auxiliary function pR. This should trigger the override in the previous point.

Any ideas? Thanks!

QuantumDot
  • 19,601
  • 7
  • 45
  • 121
  • 1
    Perhaps I misunderstood something, but it's not clear to me why you need ?NumericQ at all here. Why did you use it? – Szabolcs Jan 08 '13 at 20:59
  • If I didn't have ?NumericQ, then the output of my example myFunc[4,s,m,m] would be drastically more complicated. – QuantumDot Jan 08 '13 at 21:12
  • Instead of using NumericQ, you could simply use an inert head in place of pR. If you need the full expression (for numeric evaluation, series expansion, etc.), just ReplaceAll that inert head with pR: expr /. someHead -> pR. – Szabolcs Jan 08 '13 at 21:26
  • @Szabolcs You mean as done in the answer below? :^) – Mr.Wizard Jan 08 '13 at 21:27
  • @Mr.Wizard No, even simpler than that. No need for and expandPR which contains the definition of pR. I find that unnecessarily complicated. – Szabolcs Jan 08 '13 at 21:29
  • @Szabolcs Okay, I see the distinction. Nevertheless that inert head would have to be named (or Formated; potentially confusing) something understandable. You still need two symbols either way, right? – Mr.Wizard Jan 08 '13 at 21:34
  • @Mr.Wizard I don't think that there's a need for Format or anything complicated at all. Just don't put pR in myFunc. Put inertpR instead. Then when you need to have the full expression, just do ... /. inertpR -> pR. Usually this can be done either before before or after doing operations like taking derivatives, series expansion or substituting numerical values. Instead of having an auxiliary function expandPR, we have a short one liner (/. inertpR -> pR), so it's easy to be flexible about naming that head. Also, we can use pR whenever we want without needing to apply... – Szabolcs Jan 08 '13 at 21:52
  • ... expandPR in addition. Of course whether to put the function definition into a rule or replace the function head instead is a matter of preference and there's no point in writing another full answer for this. – Szabolcs Jan 08 '13 at 21:52

1 Answers1

2

For the first question, you just use an expansion function which applies the pattern without the numeric constraint:

expandPR[exp_] := exp /. pR[s_, m0_, m1_] :> 
 1/s*Sqrt[kin[s, m0^2, m1^2]]*Log[(2 m0 m1)/(-s + m0^2 + m1^2 - Sqrt[kin[s, m0^2, m1^2]])]

myFunc[4,s,m,m]//expandPR//Simplify
((m^4 - 3 m^2 s + s^2) pR[s, m, m])/s^2
myFunc[4,s,m,m]//expandPR//Simplify
(Sqrt[s (-4 m^2 + s)] (m^4 - 3 m^2 s + s^2) Log[-((
 2 m^2)/(-2 m^2 + s + Sqrt[s (-4 m^2 + s)]))])/s^3

For the second one, you can always expand the definition and find the derivative. If that doesn't work for you, if for instance you want to express the derivative with respect to other pR functions, you can always define custom up-values for the derivative operator D and try to do some contraction over your function definition or something like that:

 pR /: D[pR[s_, m0_, m1_], d_] := D[pR[s, m0, m1] // expandPR, d]

 D[pR[x, k, m], k]

(1/(2 k m x)) Sqrt[k^4 - 2 k^2 m^2 + m^4 - 2 k^2 x - 2 m^2 x + x^2] (k^2 + m^2 - x - Sqrt[ k^4 - 2 k^2 m^2 + m^4 - 2 k^2 x - 2 m^2 x + x^2]) (-(( 2 k m (2 k - (4 k^3 - 4 k m^2 - 4 k x)/( 2 Sqrt[k^4 - 2 k^2 m^2 + m^4 - 2 k^2 x - 2 m^2 x + x^2])))/(k^2 + m^2 - x - Sqrt[ k^4 - 2 k^2 m^2 + m^4 - 2 k^2 x - 2 m^2 x + x^2])^2) + (2 m)/( k^2 + m^2 - x - Sqrt[ k^4 - 2 k^2 m^2 + m^4 - 2 k^2 x - 2 m^2 x + x^2])) + ((4 k^3 - 4 k m^2 - 4 k x) Log[(2 k m)/( k^2 + m^2 - x - Sqrt[ k^4 - 2 k^2 m^2 + m^4 - 2 k^2 x - 2 m^2 x + x^2])])/( 2 x Sqrt[k^4 - 2 k^2 m^2 + m^4 - 2 k^2 x - 2 m^2 x + x^2])

jVincent
  • 14,766
  • 1
  • 42
  • 74
  • Very interesting. I've never seen :> before. It looks like RuleDelayed according to the help. I'm not sure why it must be implemented here. – QuantumDot Jan 08 '13 at 21:16
  • @QuantumDot Seeing as replacement rules, including RuleDelayed, are arguably the foundation of the Mathematica language I think you better get familiar with them! In addition the tutorials here a perspective on replacement rules, replete with examples, that you may find helpful. – Mr.Wizard Jan 08 '13 at 21:21