How to sum power series of -ln(1+x) assuming -1<x<1?

164 views
Skip to first unread message

first last

unread,
Aug 14, 2020, 5:10:08 PM8/14/20
to sympy
In Maple, it's this:

sum((-1)^k / k * z^k, k=1..infinity) assuming -1<z, z<1;

In sympy, how to do the "assuming -1<z, z<1" part?

David Bailey

unread,
Aug 14, 2020, 7:17:20 PM8/14/20
to sy...@googlegroups.com

Dear group,

Am I correct that the write-up about assumptions found here relates to the old-style assumptions:

https://docs.sympy.org/latest/modules/assumptions/assume.html

Is there any documentation relating to the new assumptions?

It would be really helpful if the documentation for old or new assumptions indicated which type it related to.

David

first last

unread,
Aug 17, 2020, 6:30:26 PM8/17/20
to sympy
I'll take the response as, there's no way to get Sympy to do this sum.

first last

unread,
Aug 17, 2020, 6:32:53 PM8/17/20
to sympy
P.S. Maxima and Axiom are also unable to do this sum. Mathematica and Maple are able to do it.

first last

unread,
Aug 17, 2020, 6:41:25 PM8/17/20
to sympy
P.S. Precalculus students can do it, but interestingly, no open-source CAS can do it.

Christian Gould

unread,
Aug 18, 2020, 2:00:47 AM8/18/20
to sy...@googlegroups.com
David,

I'm not on this project, but I think it would save the devs time if you would specify the sum you are referring to.

-- Kind Regards,
Christian

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/sympy/bbdc2130-172b-4d04-a542-95321e3de4d1o%40googlegroups.com.

Aaron Meurer

unread,
Aug 18, 2020, 3:08:23 AM8/18/20
to sympy
That's the new assumptions. I agree the documentation doesn't really
specify well enough which are which. "Old" and "new" are mostly terms
used by the development team to refer to the different parts of the
system, not so much terms meant for end-users of the library. But in
general

old assumptions: Symbol('x', positive=True). expr.is_positive,
expr._eval_is_positive
new assumptions: Q.positive(x), ask(), assume(), refine().

Anything using Q is new assumptions. Anything using is_assumption or
_eval_is_assumption is the old assumptions.

Aaron Meurer
> --
> 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 view this discussion on the web visit https://groups.google.com/d/msgid/sympy/659f3813-0d40-3bbd-0bc7-5948dfcf9777%40dbailey.co.uk.

asciisi...@gmail.com

unread,
Aug 18, 2020, 9:43:57 AM8/18/20
to sympy
I'll try to clarify. Putting software aside momentarily, in pure math, for z real or complex with abs(z) < 1, for k from 1 to infinity, the following power-series summations hold:

-log(1-x) = sum (z^k / k)
log(1-x) = sum(-1 * z^k / k)
log(1+x) = sum(-1 * z^k * (-1)^k / k)

Maple and Mathematica can both do those, using their sum functions. (I'm not 100% confident in their handling of edge cases like z=-1 and z=+1, but my testing their has been haphazard.)

I keep changing my story about Maxima, as I learn more about it. Yesterday I said Maxima cannot do those sums. Last night I learned Maxima can do them, via its "simplify_sum" feature, whose documentation is hidden in an obscure Chapter 84. Maxima manual's "Summation" chapter includes no mention of "simplify_sum". (I'm curious how many decades this Maxima documentation-bug has persisted without anyone simply moving "simplify_sum" to the chapter on sums. All 55 years of Maxima's history?)

Sympy can sum those into Piecewise expressions:

>>> sympy.summation(sympy.S('z^k / k'), sympy.S('(k, 1, oo)'))
Piecewise((-log(1 - z), (z >= -1) & (z < 1)), (Sum(z**k/k, (k, 1, oo)), True))
>>>

The catch is, there's no way to ask Sympy "what's that Piecewise expression assuming abs(z) < 1"? Neither old nor new Sympy assumptions can express that query.

I see two issues: Lack of functionality and room for documentation-improvement. I haven't designed and implemented my own assumptions-system, so I can't speak to its difficulty. Maxima's been worked on by countless geniuses for 55 years and still has an admittedly weak assumptions system; so maybe assumptions are especially hard for CAS-designers.

The second issue is documentation. I have spent many months in many CAS's trying to sum the power series of log(1+x). I could have accomplished the same in a day is the CAS's had been documented better.

first last

unread,
Aug 18, 2020, 10:35:43 AM8/18/20
to sympy
P.S.

While Maxima makes it to the finish-line:

(%i2) sum(z^k / k, k, 1, inf);
                                   inf
                                   ====   k
                                   \     z
(%o2)                               >    --
                                   /     k
                                   ====
                                   k = 1
(%i3) sum(z^k / k, k, 1, inf), simplify_sum;
(%o3)                            - log(1 - z)

it does so by cheating, giving that answer without requiring any assumption that abs(z) < 1. Sympy does a better job at returning an answer (Piecewise) that indicates that z's size matters to convergence. 

Aaron Meurer

unread,
Aug 18, 2020, 5:08:37 PM8/18/20
to sympy
summation() should probably have a conds argument similar to
integrate() that lets you disable the piecewise.

You can always manually extract it from the expression:

>>> sympy.summation(sympy.S('z^k / k'), sympy.S('(k, 1, oo)')).args[0].args[0]
-log(1 - z)

Ideally refine() would let you do this, but it doesn't seem to work yet.

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/c9f81be1-4dec-4bb4-8079-af1f044d0ab3n%40googlegroups.com.

first last

unread,
Aug 18, 2020, 5:23:03 PM8/18/20
to sympy
Thanks, but I am not calling summation directly from the interpreter command-line. Summation is getting called by other functions, and the summand (the expression being summed) is getting passed as a parameters that's not always log's power series. So at compile-time, I don't know whether the result will be a Piecewise or not, and I'd have to build intelligence into my code to examine the structure of summation's results, which starts getting elaborate. It might be possible to get it working for me that way, but definitely not as straightforward as working with Maple or Mathematica on this particular task.
> To unsubscribe from this group and stop receiving emails from it, send an email to sy...@googlegroups.com.

first last

unread,
Aug 18, 2020, 6:27:40 PM8/18/20
to sympy
Where is the design for the new assumptions-system coming from? Is the new design based on academic papers, or on previous software, or being designed from the ground up here? I'm interested in automated reasoning for its own sake, but not an expert.


On Tuesday, August 18, 2020 at 2:08:37 PM UTC-7, Aaron Meurer wrote:
> To unsubscribe from this group and stop receiving emails from it, send an email to sy...@googlegroups.com.

Oscar Benjamin

unread,
Aug 19, 2020, 4:10:24 AM8/19/20
to sympy
Ideally this would be something that refine could handle and also
ideally there would be an argument to summation to specify the
conditions so that it could internally use refine or something else
internally. That's just not yet implemented in sympy but I think it
will be in future.

There are a couple of ways to make this work right now. One is that
you can directly substitute the conditions in the Piecewise for True.

In [109]: z = Symbol('z', real=True)

In [110]: k = Symbol('k', integer=True)

In [111]: S = summation(z**k/k, (k, 1, oo))

In [112]: print(S)
Piecewise((-log(1 - z), (z >= -1) & (z < 1)), (Sum(z**k/k, (k, 1, oo)), True))

In [113]: S.subs({z >= -1: True, z < 1: True})
Out[113]: -log(1 - z)

That only works if you know exactly what form the conditions could
take though (no clever inference is going on during this call to
subs).

The reason that the old assumptions don't work here is because there
is no predicate that can be attached to a symbol to specify that |z|<1
in the same way that I can declare z to be real or k to be an integer
above. Often though it is possible to replace the symbol with an
expression in terms of another symbol so that the assumptions on the
new symbol ensure the expression has the desired properties. We can do
that in this example with the substitution z = tanh(x) where x is real
(assuming we want |z| < 1):

In [114]: x = Symbol('x', real=True)

In [115]: S.subs(z, tanh(x))
Out[115]: -log(1 - tanh(x))

In [116]: S.subs(z, tanh(x)).subs(tanh(x), z)
Out[116]: -log(1 - z)

That works because the evaluation of the Piecewise during the call to
subs uses the old assumptions and finds that the conditions work out
to True.

With that we can do:

In [124]: print(summation(z**k/k, (k, 1, oo)).subs(z, tanh(x)).subs(tanh(x), z))
-log(1 - z)

In [125]: print(summation((-z)**k/k, (k, 1, oo)).subs(z,
tanh(x)).subs(tanh(x), z))
-log(z + 1)

This won't always work though and you might find different results
using different expressions rather than tanh(x) for example you could
use z=x/(1+x**2) or something else.

Ideally this would be handled by refine. It wouldn't be hard to handle
this particular case, especially since Piecewise with inequalities is
probably the number one reason users want to use refine.

--
Oscar
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/59e83ffd-4306-4d0c-9200-1a9676de34b9o%40googlegroups.com.

first last

unread,
Aug 21, 2020, 3:40:49 PM8/21/20
to sy...@googlegroups.com
Thanks; your workaround is intriguing. I cannot contribute to Sympy currently, on account of time limitations (and using Maxima instead of Sympy for now). It's not ideal that your explanations and code examples are omitted from the Sympy docs.

asciisi...@gmail.com

unread,
Aug 22, 2020, 8:40:53 PM8/22/20
to sympy
Actually, no, that workaround is only further delaying my attempt to do something very simple in a CAS. I had to abandon Maxima today. I lost a couple of years of work I invested in Sympy when I came up its inability to sum the power series of loa (1+x). I am horrified I have to return to Mathematica for something so simple, and now I have no open-source platform on which to publish my book, and whether my book can be viewed in ten years will depend on the state of Wolfram's business in ten years.

Oscar Benjamin

unread,
Aug 22, 2020, 9:40:57 PM8/22/20
to sympy
I wouldn't have suggested that if I realised you were planning to put
it into a book. As Aaron and I said this is something that should be
handled by refine but isn't implemented yet.

All the same it is straight forward to extract the result manually
when you want to from the Piecewise using either .args or .subs
(that's all refine would do in this case if it was implemented yet).
Does that not work for your problem?
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/de49bbd5-7da1-4a1f-b253-66152f90bae1n%40googlegroups.com.

first last

unread,
Aug 22, 2020, 9:59:09 PM8/22/20
to sy...@googlegroups.com
That doesn't work for my problem, as explained above. I'm four years older than when I started trying to use CAS for extremely simple operations. Nothing to show for it and running out of money for rent. Goodbye.

You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/jDCY_lqGsJU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxSqE1exS%3D4zCctT3R4xwKXKY0JZaF3UjXvqpzYgou%3D-1w%40mail.gmail.com.

first last

unread,
Aug 22, 2020, 10:02:15 PM8/22/20
to sy...@googlegroups.com
I wanted a CAS to teach me about convergence conditions. I definitely don't want a CAS where I have to write the assumptions handler before it can teach me. If I write the assumptions handler wrong, I teach myself wrong math. Totally not why I installed four CAS.

On Sat, Aug 22, 2020 at 6:40 PM Oscar Benjamin <oscar.j....@gmail.com> wrote:
You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/jDCY_lqGsJU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxSqE1exS%3D4zCctT3R4xwKXKY0JZaF3UjXvqpzYgou%3D-1w%40mail.gmail.com.

David Bailey

unread,
Aug 23, 2020, 11:26:39 AM8/23/20
to sy...@googlegroups.com
On 23/08/2020 03:01, first last wrote:
I wanted a CAS to teach me about convergence conditions. I definitely don't want a CAS where I have to write the assumptions handler before it can teach me. If I write the assumptions handler wrong, I teach myself wrong math. Totally not why I installed four CAS.

I think it is important to be realistic. A CAS cannot teach you about math, any more than it can give a totally authoritative answer to anything - because all software has bugs and limitations. I used to use Mathematica, and I can tell you that it too had its bugs and limitations. The problem is that if you find a problem with one CAS and try it on another, independently developed, CAS there is a fair chance it will work, but of course they will probably be other examples where it will fail and SymPy will succeed.

SymPy is available as totally free software, and I don't like to see a user, who can't show the courtesy to reveal his name, abusing the people who do so much work for us all.

Many thanks to everyone else for bearing up so well to this abuse, and for the work you all put in on SymPy, and for your extremely helpful replies to questions!

David

Aaron Meurer

unread,
Aug 24, 2020, 5:15:03 PM8/24/20
to sympy
On Sun, Aug 23, 2020 at 9:26 AM David Bailey <da...@dbailey.co.uk> wrote:
>
> On 23/08/2020 03:01, first last wrote:
>
> I wanted a CAS to teach me about convergence conditions. I definitely don't want a CAS where I have to write the assumptions handler before it can teach me. If I write the assumptions handler wrong, I teach myself wrong math. Totally not why I installed four CAS.
>
> I think it is important to be realistic. A CAS cannot teach you about math, any more than it can give a totally authoritative answer to anything - because all software has bugs and limitations. I used to use Mathematica, and I can tell you that it too had its bugs and limitations. The problem is that if you find a problem with one CAS and try it on another, independently developed, CAS there is a fair chance it will work, but of course they will probably be other examples where it will fail and SymPy will succeed.
>
> SymPy is available as totally free software, and I don't like to see a user, who can't show the courtesy to reveal his name, abusing the people who do so much work for us all.

To be clear about something, there is no issue with people posting to
this list or even contributing to SymPy anonymously. However, everyone
who participates in the SymPy community is expected to be courteous
and civil, as outlined in our code of conduct
https://github.com/sympy/sympy/blob/master/CODE_OF_CONDUCT.md,
regardless of whether they reveal their name.

Aaron Meurer

>
> Many thanks to everyone else for bearing up so well to this abuse, and for the work you all put in on SymPy, and for your extremely helpful replies to questions!
>
> David
>
> --
> 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 view this discussion on the web visit https://groups.google.com/d/msgid/sympy/dbbf050c-3466-3de5-f767-ecff78b052af%40dbailey.co.uk.
Reply all
Reply to author
Forward
0 new messages