Is there any way to build a differentiable sum of indexed types?

90 views
Skip to first unread message

Jason Moore

unread,
Jun 26, 2015, 4:58:23 PM6/26/15
to sy...@googlegroups.com
I'd like to do this:

>>> r = IndexedBase['m']
>>> i = idx('i')
>>> h = Symbol('h')
>>> N = Symbol('N', integer=True)
>>> J = Sum(h * r[i]**2, (i, 1, N))
>>> J.diff(r[i])
2 * h * r[i]

This currently doesn't work (Sum doesn't like idx). But are there other ways to do this in SymPy? Or should this work?

The big picture is that I want to be able write a scalar expression that involves integrals of continuous functions, but then numerically approximate it with various numerical integration patterns. And finally I need to find the gradient of the expression with respect to discrete variables, like r[i] above.

Jason Moore

unread,
Jun 27, 2015, 8:14:29 PM6/27/15
to sy...@googlegroups.com
Here is as an attempt but the derivative fails:

In [5]: i = sm.symbols('i', integer=True)

In [6]: s=sm.Sum(sm.Indexed('r',i),(i,1,3))

In [7]: s
Out[7]: Sum(r[i], (i, 1, 3))

In [8]: s.diff(sm.Indexed('r',i))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-f8ca8cff3c71> in <module>()
----> 1 s.diff(sm.Indexed('r',i))

/home/moorepants/anaconda/lib/python2.7/site-packages/sympy/core/expr.pyc in diff(self, *symbols, **assumptions)
   2773         new_symbols = list(map(sympify, symbols))  # e.g. x, 2, y, z
   2774         assumptions.setdefault("evaluate", True)
-> 2775         return Derivative(self, *new_symbols, **assumptions)
   2776
   2777     ###########################################################################

/home/moorepants/anaconda/lib/python2.7/site-packages/sympy/core/function.pyc in __new__(cls, expr, *variables, **assumptions)
   1016                 from sympy.utilities.misc import filldedent
   1017                 raise ValueError(filldedent('''
-> 1018                 Can\'t calculate %s-th derivative wrt %s.''' % (count, v)))
   1019
   1020             if all_zero and not count == 0:

ValueError:
Can't calculate 1-th derivative wrt r[i].


Francesco Bonazzi

unread,
Jun 30, 2015, 11:00:04 AM6/30/15
to sy...@googlegroups.com
If you include this PR:
https://github.com/sympy/sympy/pull/9314

Your second example gives 0, which doesn't look quite correct.

I would rather expect a Sum(delta[i, j], (i, 1, 3)) as a result.

Jason Moore

unread,
Jul 1, 2015, 1:01:53 AM7/1/15
to sy...@googlegroups.com
I've checked out that PR briefly, but don't really understand it yet. I will look closer. I'm not sure what delta represents.

Is there even a way to represent a series like this in SymPy:

\sum^{N}_{i=1} r_i^2 = r_1^2 + r_2^2 + ... + r_N^2

where r is a sequence of values {r_1, r_2, ..., r_N}

I was assuming that the Indexed types were appropriate for that.

This seems like it does the correct thing:

In [5]: import sympy as sm

In [6]: i = sm.symbols('i', integer=True)

In [7]: sm.Sum(sm.Indexed('r', i), (i, 1, 3)).doit()
Out[7]: r[1] + r[2] + r[3]


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/777eae48-c6e2-4693-98fd-7a98a7bf10a7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason Moore

unread,
Jul 1, 2015, 1:31:46 AM7/1/15
to sy...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages