How to assign up-values for Derivative?

I have defined several custom analytic functions. Here is the simplest example:

ln[x_, a_?NumericQ] := Piecewise[{{Log[x], Re[a] > 0}, {-Log[1/x], True}}]

Now I would like to let Mathematica know how to carry out derivatives on this: I need to make D and Series work on ln as if it were Log:

ln /: D[ln[f_, g_], x_] := D[Log[f], x];

Works wonderfully:

But Series doesn't work because it is using Derivative instead of D.

So, now I try TagSetDelayed on Derivative:

But as you can see, it doesn't work because ln is too deep. What can I do to make Series work?

• I'm not exactly sure I understood your question (though I answered already)... If you're just worried why the Series didn't simplify with your definition for D, then it's because you didn't define ln for symbolic a. That's why I used a numeric a in my answer in the series. – Jens Sep 10 '14 at 21:19

You don't need to use TagSetDelayed for the definition of the derivative because Derivative doesn't have attribute Protected.

I'll extend add the derivative definition to arbitrary order n:

ClearAll[ln];
Derivative[n_, 0][ln][x_, a_] := Derivative[n][Log][x]

ln[x_, a_?NumericQ] :=
Piecewise[{{Log[x], Re[a] > 0}, {-Log[1/x], True}}]

ln[x, -1/2]

$-\log \left(\frac{1}{x}\right)$

D[ln[x, a], x]

$\frac{1}{x}$

D[ln[Cos[x] + x, a], x]

$$\frac{1-\sin (x)}{x+\cos (x)}$$

Series[ln[Cos[x] + x, 1/2], {x, 0, 2}]

$x-x^2+O\left(x^3\right)$

Series[ln[Cos[x] + x, a], {x, 0, 2}]

$\ln (1,a)+x-x^2+O\left(x^3\right)$

The symbolic argument a also gives a result now because the definitions for the derivatives work for symbolic a, as well. Only the zeroth-order term is not simplified because it only knows what that evaluates to when a is numeric.

• Very nice! What I'm really trying to do is to define a function that works as seamlessly as possible with Mathematica's analytic functions like Integrate Limit Series etc.. do you know of a simple way to do this? Should I ask it as a separate question? – QuantumDot Sep 11 '14 at 11:35