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[Custom`sum[idx,1,2,func[idx,z]],{z,0,1}]
and the result is supposed to be
Custom`sum[idx,1,2,func[idx,0]]+Custom`sum[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[Custom`sum[idx,1,2,func[idx,x]],{x,0,1}]=Custom`sum[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[Custom`sum[idx_,a_,b_,c_],k_]:=Custom`sum[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 Custom`sum 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.
UpValues
, something likeCustom`sum /: Series[Custom`sum[idx, 1, 2, func[idx, z]], {z, 0, 1}] = Custom`sum[idx, 1, 2, func[idx, 0]] + Custom`sum[idx, 1, 2, func'[idx, 0]]*z + O[z]^2
but with patterns on the left-hand side. $\endgroup$