# Series expansion of function which commutes with differentiation

I am trying to perform a series expansion, where Series acts on a custom summation function, similar to Sum. This is convenient, since the sum is not to be evaluated in Mathematica, but will later be turned into C++ code. An example would be

Series[Customsum[idx,1,2,func[idx,z]],{z,0,1}]


and the result is supposed to be

Customsum[idx,1,2,func[idx,0]]+Customsum[idx,1,2,func'[idx,0]]*z+O[z^2]


where idx is the index of summation, running from 1 to 2. Instead I get

Series[Customsum[idx,1,2,func[idx,x]],{x,0,1}]=Customsum[idx,1,2,func[idx,0]]+2 x (func^(0,1))[idx,0]+O[x^2]


which does not make a lot of sense, since we now have an index without a summation.

I suppose the problem is, that Series does not know how to deal with the sum function. How do I convince Mathematica to perform the expansion 'normally', i.e. that the derivatives of Series commute with the summation (that they can be pulled in, so to speak) and only act on the summand?

I have attempted writing an explicit rule, i.e.

Unprotect[D];
D[Customsum[idx_,a_,b_,c_],k_]:=Customsum[idx,a,b,D[c,k]];
Protect[D];


which works correctly with normal derivation, but it seems to have no effect on the behaviour of Series. Another idea I had was to create a rule, so that Series and Customsum commute, which does work in the simple case above, though it does not solve the problem in general, for example when the summation is inside another function.

Edit:

I have solved my problem by basically re-implementing Series with the normal derivative operation D, and using the UpValues idea of @march to get the sum and derivative to commute. I only needed a first order approximation, so it wasn't too difficult, though I would still be interested in solving the problem with the built-in Series function.

• Perhaps use UpValues, something like Customsum /: Series[Customsum[idx, 1, 2, func[idx, z]], {z, 0, 1}] = Customsum[idx, 1, 2, func[idx, 0]] + Customsum[idx, 1, 2, func'[idx, 0]]*z + O[z]^2 but with patterns on the left-hand side. – march Jun 14 '18 at 21:19
• That would only work for some special cases, though, wouldn't it? I'd ideally like Series to treat Customsum the same way as the standard Sum, at least with respect to differentiation. – Telperion Jun 14 '18 at 21:38

You can change the system options to enable customized differentiation of symbols. Note that I use the symbol sum instead of Customsum below. First, exclude sum from the normal handling of derivatives:

With[
{
old = DeleteDuplicates @ Append[
OptionValue[SystemOptions[], "DifferentiationOptions" -> "ExcludedFunctions"],
sum
]
},

SetSystemOptions["DifferentiationOptions" -> "ExcludedFunctions" -> old]
];


Then, derivatives of sum are left inert:

D[sum[i, 1, 2, x], x] //TeXForm


$$\frac{\partial \operatorname{sum}(i,1,2,x)}{\partial x}$$

You can now add custom differentiation rules for sum:

D[sum[i_, l_, u_, func_], x__] ^:= sum[i, l, u, D[func, x]]


Derivatives of sum now behave the way you want:

D[sum[idx, 1, 2, func[idx, x]], x, x] //TeXForm


$$\operatorname{sum}\left(\operatorname{idx},1,2,\operatorname{func}^{(0,2)}(\operatorname{idx}, x)\right)$$

Unfortunately, Series ignores this rule, and goes directly to the Derivative form. To have Series handle sum as desired, you need to add a rule to the internal symbol SystemPrivateInternalSeries:

sum /: SystemPrivateInternalSeries[sum[i_, l_, u_, func_], {z_, z0_, n_}] := MapAt[
sum[i, l, u, #]&,
SystemPrivateInternalSeries[func, {z, z0, n}],
{3, All}
]


Finally, using Series produces the desired output:

Series[sum[idx, 1, 2, func[idx, z]], {z, 0, 1}] //TeXForm


$$\operatorname{sum}(\operatorname{idx},1,2,\operatorname{func}(\operatorname{idx},0))+z \operatorname{sum}\left(\operatorname{idx},1,2,\operatorname{func}^{(0,1)}(\operatorname{id x},0)\right)+O\left(z^2\right)$$