2
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Perhaps use UpValues, something like Custom`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$
    – march
    Commented Jun 14, 2018 at 21:19
  • $\begingroup$ That would only work for some special cases, though, wouldn't it? I'd ideally like Series to treat Custom`sum the same way as the standard Sum, at least with respect to differentiation. $\endgroup$
    – Telperion
    Commented Jun 14, 2018 at 21:38

1 Answer 1

2
$\begingroup$

You can change the system options to enable customized differentiation of symbols. Note that I use the symbol sum instead of Custom`sum 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 System`Private`InternalSeries:

sum /: System`Private`InternalSeries[sum[i_, l_, u_, func_], {z_, z0_, n_}] := MapAt[
    sum[i, l, u, #]&,
    System`Private`InternalSeries[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)$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.