Sage: Ticket #22915: Distribute symbolic sums over the terms of their first (sum) argument
https://trac.sagemath.org/ticket/22915
<p>
Motivation : see this sage-support <a class="ext-link" href="https://groups.google.com/forum/#!topic/sage-support/wLo2RQ-KSag"><span class="icon"></span>post</a> and Ralf Stephan's <a class="ext-link" href="https://groups.google.com/d/msg/sage-support/wLo2RQ-KSag/uyimvFsoCAAJ"><span class="icon"></span>answer</a>.
</p>
<p>
A similar enhancement could distribute symbolic products over the factors of their first (product) argument, but symbolic products are currently broken (see <a class="closed ticket" href="https://trac.sagemath.org/ticket/22914" title="defect: Symbolic products are currently broken. (closed: wontfix)">#22914</a>).
</p>
<p>
This ticket has been abandoned in favor of <a class="closed ticket" href="https://trac.sagemath.org/ticket/22937" title="enhancement: Implement a "distribute" method (closed: fixed)">#22937</a>.
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/22915
Trac 1.1.6charpentSun, 30 Apr 2017 18:31:23 GMTbranch set
https://trac.sagemath.org/ticket/22915#comment:1
https://trac.sagemath.org/ticket/22915#comment:1
<ul>
<li><strong>branch</strong>
set to <em>u/charpent/expand_sum</em>
</li>
</ul>
<p>
This first implementation follows the guidelines <a class="ext-link" href="https://groups.google.com/d/msg/sage-support/wLo2RQ-KSag/uyimvFsoCAAJ"><span class="icon"></span>suggested</a> by Ralf Stephan. It is probably highly perfectible :
</p>
<ul><li>The implementation is "pure Sage" : I am aware of the possibility of speeding it considerably by declaring types to the necessary functions and their arguments, but I'm unsure about the relevant types (I'm quite novice in Cython...).
</li><li>I am not sure that this distribution should occur in every expansion : one could add a keyword to the <code>expand()</code> method/function, with edault value being either True or False (to be discussed).
</li><li>Since the <code>expand()</code> method essentially dispatches the work to the components of the <code>Expression</code>, This specialized expansion could (should ?) take place as a method of <code>Function_sum</code>, but I'm currently unablke on how to do this (this is my first ever attempt to work on the Sage symbolics in the library).
</li></ul><p>
Suggestions, criticisms, even lazzi are welcome as long as they are constructive...
</p>
<p>
I'm not marking this as <code>needs_review</code>, since the documentation remains to be written. I may push a documentation patch and mark it <code>needs_review</code> soon.
</p>
TicketgitSun, 30 Apr 2017 20:10:23 GMTcommit set
https://trac.sagemath.org/ticket/22915#comment:2
https://trac.sagemath.org/ticket/22915#comment:2
<ul>
<li><strong>commit</strong>
set to <em>19bd0bec91c8a10feb67c5f19e8a97baa9053163</em>
</li>
</ul>
<p>
Branch pushed to git repo; I updated commit sha1. New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="https://git.sagemath.org/sage.git/commit/?id=19bd0bec91c8a10feb67c5f19e8a97baa9053163"><span class="icon"></span>19bd0be</a></td><td><code>Documentation of the distribution of symbolic sums over sums.</code>
</td></tr></table>
TicketcharpentSun, 30 Apr 2017 20:15:06 GMTstatus, component changed
https://trac.sagemath.org/ticket/22915#comment:3
https://trac.sagemath.org/ticket/22915#comment:3
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>needs_review</em>
</li>
<li><strong>component</strong>
changed from <em>PLEASE CHANGE</em> to <em>symbolics</em>
</li>
</ul>
<p>
Passes ptestl<code>ong</code> with thye same problems as with 8.0.beta4 (see <a class="ext-link" href="https://groups.google.com/d/msg/sage-release/UdllnAJFufA/-e8ArqwFBAAJ"><span class="icon"></span>this post</a> on sage-release).
</p>
<p>
==> <code>needs_review</code>. But again, this is functional, but largely perfectible.
</p>
TicketrwsMon, 01 May 2017 05:33:25 GMTdescription changed; cc set
https://trac.sagemath.org/ticket/22915#comment:4
https://trac.sagemath.org/ticket/22915#comment:4
<ul>
<li><strong>cc</strong>
<em>rws</em> added
</li>
<li><strong>description</strong>
modified (<a href="/ticket/22915?action=diff&version=4">diff</a>)
</li>
</ul>
TicketrwsTue, 02 May 2017 08:28:35 GMT
https://trac.sagemath.org/ticket/22915#comment:5
https://trac.sagemath.org/ticket/22915#comment:5
<p>
I would call this only if the expression contains sums, test it via e.g.:
</p>
<pre class="wiki">sage: w=SR.wild()
sage: ex = sum(x+x^2,x,1,n,hold=True)
sage: ex.has(sum(w,x,1,m,hold=True))
False
sage: ex.has(sum(w,x,1,n,hold=True))
True
</pre><p>
Minor cosmetics would be to give ES a more descriptive name. Also you need spaces after commata, semicola, and around equal signs for convention. Finally please test if your documentation builds, the doctest spacing looks wrong. Oh and <code>sums whos first argument is a sumn are</code> contains two typos.
</p>
TicketrwsTue, 02 May 2017 08:36:18 GMT
https://trac.sagemath.org/ticket/22915#comment:6
https://trac.sagemath.org/ticket/22915#comment:6
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/22915#comment:5" title="Comment 5">rws</a>:
</p>
<blockquote class="citation">
<p>
I would call this only if the expression contains sums, test it via e.g.:
</p>
<pre class="wiki">sage: ex.has(sum(w,x,1,n,hold=True))
True
</pre></blockquote>
<p>
Ah sorry that needs <a class="new ticket" href="https://trac.sagemath.org/ticket/22926" title="enhancement: Symbolic has_operator() (new)">#22926</a> to be able to test for <code>sum(...,w,....)</code>.
</p>
TicketcharpentTue, 02 May 2017 08:54:34 GMTstatus changed
https://trac.sagemath.org/ticket/22915#comment:7
https://trac.sagemath.org/ticket/22915#comment:7
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>needs_work</em>
</li>
</ul>
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/22915#comment:5" title="Comment 5">rws</a>:
</p>
<blockquote class="citation">
<p>
I would call this only if the expression contains sums, test it via e.g.:
</p>
<pre class="wiki">sage: w=SR.wild()
sage: ex = sum(x+x^2,x,1,n,hold=True)
sage: ex.has(sum(w,x,1,m,hold=True))
False
sage: ex.has(sum(w,x,1,n,hold=True))
True
</pre></blockquote>
<p>
Indeed. But isn't that more expensive than directly testing for the operator being sum (and recurse) ?
</p>
<blockquote class="citation">
<p>
Minor cosmetics would be to give ES a more descriptive name.
</p>
</blockquote>
<p>
Will do (maybe : see below). But that's only an internal subfunction.
</p>
<blockquote class="citation">
<p>
Also you need spaces after commata, semicola, and around equal signs for convention.
</p>
</blockquote>
<p>
OK. (I'm probably the worst typist in the world...).
</p>
<blockquote class="citation">
<p>
Finally please test if your documentation builds, the doctest spacing looks wrong. Oh and <code>sums whos first argument is a sumn are</code> contains two typos.
</p>
</blockquote>
<p>
Indeed...
</p>
<p>
But I'm beginning to have misgivings. "Distribute symbolic sums over sums" isn't lthe only case where this might be useful. We have also :
</p>
<ul><li>Distribute symbolic products over products (when we have symbolic products...).
</li><li>Distribute sums (symbolic and add_vararg) over derivation (I currently get nonsense, BTW...)
</li><li>Ditto for integration.
</li></ul><p>
and I may forget other "obvious" cases which are not obvious to me...
</p>
<p>
I'm starting to think that we may need a <code>distribute</code> method (with possibly optional arguments allowing to specify what should distributed over what), rather than (over)-extending the <code>expand</code> method.
</p>
<p>
What do you think ? I'm setting this ticket to <code>needs_work</code> while expecting your comments
</p>
TicketrwsTue, 02 May 2017 13:21:50 GMT
https://trac.sagemath.org/ticket/22915#comment:8
https://trac.sagemath.org/ticket/22915#comment:8
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/22915#comment:7" title="Comment 7">charpent</a>:
</p>
<blockquote class="citation">
<p>
I'm starting to think that we may need a <code>distribute</code> method (with possibly optional arguments allowing to specify what should distributed over what), rather than (over)-extending the <code>expand</code> method.
</p>
</blockquote>
<p>
I don't object.
</p>
TicketcharpentWed, 03 May 2017 14:54:59 GMT
https://trac.sagemath.org/ticket/22915#comment:9
https://trac.sagemath.org/ticket/22915#comment:9
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/22915#comment:8" title="Comment 8">rws</a>:
</p>
<blockquote class="citation">
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/22915#comment:7" title="Comment 7">charpent</a>:
</p>
<blockquote class="citation">
<p>
I'm starting to think that we may need a <code>distribute</code> method (with possibly optional arguments allowing to specify what should distributed over what), rather than (over)-extending the <code>expand</code> method.
</p>
</blockquote>
<p>
I don't object.
</p>
</blockquote>
<p>
Created <a class="closed ticket" href="https://trac.sagemath.org/ticket/22937" title="enhancement: Implement a "distribute" method (closed: fixed)">#22937</a>. I'll make an inital proposal there (not quite soon, alas...). I'll also post something on <code>sage-devel</code>.
</p>
Ticketrobert_dodierThu, 04 May 2017 05:48:10 GMT
https://trac.sagemath.org/ticket/22915#comment:10
https://trac.sagemath.org/ticket/22915#comment:10
<p>
For what it's worth, Maxima has <code>declare(foo, additive)</code> (also <code>outative</code> and <code>linear</code>) which seems to have the desired effect. E.g.
</p>
<pre class="wiki"> (%i2) 'sum(f(i) + g(i), i,1,n);
(%o2) 'sum(g(i)+f(i),i,1,n)
(%i3) declare(nounify(sum), additive) $
(%i4) 'sum(f(i) + g(i), i,1,n);
(%o4) 'sum(g(i),i,1,n)+'sum(f(i),i,1,n)
</pre><p>
That's handled by the global simplifier, so it's all or nothing -- you can't expand one expression and not another. Dunno if that's an issue. Also, this action is purely formal -- if it's an infinite sum, the simplifier happily plows ahead without testing convergence.
</p>
<p>
There is also a more general mechanism for distributing operators and functions over other operators and functions, via the symbol property DISTRIBUTE_OVER. This is used to distribute some functions over lists, matrices, and equations. The declaration has to be in Lisp, e.g. <code>:lisp (setf (get 'mfoo 'distribute_over) '(mbar mbaz mquux))</code> which says that operator MFOO distributes over MBAR, MBAZ, and MQUUX. Again the general simplifier puts that declaration into effect.
</p>
<p>
Anyway, hope that sheds some light.
</p>
TicketchapotonFri, 19 May 2017 11:41:15 GMT
https://trac.sagemath.org/ticket/22915#comment:11
https://trac.sagemath.org/ticket/22915#comment:11
<p>
Please take take ASAP of the apply/python3 issue introduced in <a class="closed ticket" href="https://trac.sagemath.org/ticket/22937" title="enhancement: Implement a "distribute" method (closed: fixed)">#22937</a>.
</p>
TicketcharpentFri, 19 May 2017 11:54:25 GMT
https://trac.sagemath.org/ticket/22915#comment:12
https://trac.sagemath.org/ticket/22915#comment:12
<p>
Dear Robert,
</p>
<p>
Sorry, I didn't see your (quite enlightening) answer until right now (tywo weeks later).
</p>
<p>
I've implemented (part of) what I want in <a class="closed ticket" href="https://trac.sagemath.org/ticket/22937" title="enhancement: Implement a "distribute" method (closed: fixed)">#22937</a>. But I'll consider comparing tgis implementation with changing the relevant properties on-the-fly to the same effect.
</p>
<p>
Anyway, the present ticket is abandoned. I just forgot to close it.
</p>
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/22915#comment:10" title="Comment 10">robert_dodier</a>:
</p>
<blockquote class="citation">
<p>
For what it's worth, Maxima has <code>declare(foo, additive)</code> (also <code>outative</code> and <code>linear</code>) which seems to have the desired effect. E.g.
</p>
<pre class="wiki"> (%i2) 'sum(f(i) + g(i), i,1,n);
(%o2) 'sum(g(i)+f(i),i,1,n)
(%i3) declare(nounify(sum), additive) $
(%i4) 'sum(f(i) + g(i), i,1,n);
(%o4) 'sum(g(i),i,1,n)+'sum(f(i),i,1,n)
</pre><p>
That's handled by the global simplifier, so it's all or nothing -- you can't expand one expression and not another. Dunno if that's an issue. Also, this action is purely formal -- if it's an infinite sum, the simplifier happily plows ahead without testing convergence.
</p>
<p>
There is also a more general mechanism for distributing operators and functions over other operators and functions, via the symbol property DISTRIBUTE_OVER. This is used to distribute some functions over lists, matrices, and equations. The declaration has to be in Lisp, e.g. <code>:lisp (setf (get 'mfoo 'distribute_over) '(mbar mbaz mquux))</code> which says that operator MFOO distributes over MBAR, MBAZ, and MQUUX. Again the general simplifier puts that declaration into effect.
</p>
<p>
Anyway, hope that sheds some light.
</p>
</blockquote>
TicketcharpentFri, 19 May 2017 11:57:47 GMTstatus, description, milestone changed
https://trac.sagemath.org/ticket/22915#comment:13
https://trac.sagemath.org/ticket/22915#comment:13
<ul>
<li><strong>status</strong>
changed from <em>needs_work</em> to <em>positive_review</em>
</li>
<li><strong>description</strong>
modified (<a href="/ticket/22915?action=diff&version=13">diff</a>)
</li>
<li><strong>milestone</strong>
changed from <em>sage-8.0</em> to <em>sage-duplicate/invalid/wontfix</em>
</li>
</ul>
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/22915#comment:11" title="Comment 11">chapoton</a>:
</p>
<blockquote class="citation">
<p>
Please take take ASAP of the apply/python3 issue introduced in <a class="closed ticket" href="https://trac.sagemath.org/ticket/22937" title="enhancement: Implement a "distribute" method (closed: fixed)">#22937</a>.
</p>
</blockquote>
<blockquote>
<p>
Indeed (see my answer on <a class="closed ticket" href="https://trac.sagemath.org/ticket/22937" title="enhancement: Implement a "distribute" method (closed: fixed)">#22937</a>).
</p>
</blockquote>
<p>
I forgot to close the present ticket, which I do now (setting its milestone to sage-duplicate/invalid/wontfix and giving it positive review)..
</p>
TicketembrayThu, 13 Jul 2017 07:54:31 GMTstatus changed; resolution set
https://trac.sagemath.org/ticket/22915#comment:14
https://trac.sagemath.org/ticket/22915#comment:14
<ul>
<li><strong>status</strong>
changed from <em>positive_review</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>wontfix</em>
</li>
</ul>
<p>
Closing tickets in the sage-duplicate/invalid/wontfix module with positive_review (i.e. someone has confirmed they should be closed).
</p>
Ticket