How to evaluate a Piecewise under specific conditions?

231 views
Skip to first unread message

My Name

unread,
Aug 10, 2020, 6:06:02 AM8/10/20
to sympy
I do this:

import sympy
sympy.srepr(sympy.integrate(S('cos(x * y)'), S('(x, 0, a)')))

It returns this:

"Piecewise(ExprCondPair(Mul(Pow(Symbol('y'), Integer(-1)), sin(Mul(Symbol('a'), Symbol('y')))), And(StrictGreaterThan(Symbol('y'), -oo), StrictLessThan(Symbol('y'), oo), Unequality(Symbol('y'), Integer(0)))), ExprCondPair(Symbol('a'), true))"

How do I find the value of the Piecewise expression when y is nonzero? I have tried using sympy.refine with Q.nonzero, but that has not worked. Indeed the docs for refine warn, "Relations in assumptions are not implemented (yet)". Does that mean there's no way to find the value of the integral assuming y is nonzero?

Oscar

unread,
Aug 10, 2020, 6:53:01 AM8/10/20
to sympy
You can use the old assumptions:

In [10]: x = Symbol('x')                                                                                                                      


In [11]: y = Symbol('y')                                                                                                                      


In [12]: a = Symbol('a')                                                                                                                      


In [13]: integrate(cos(x*y), (x, 0, a))                                                                                                        

Out[13]:

sin(ay)                            

⎪────────  for y > -∞ y < y 0

  y                                

                                   

  a              otherwise        


In [14]: y = Symbol('y', nonzero=True)                                                                                                        


In [15]: integrate(cos(x*y), (x, 0, a))                                                                                                        

Out[15]:

sin(ay)

────────

  y  



The new assumptions and refine can handle nonzero. The point about relations not being implemented is that there is not currently a way to use the assumption that e.g. x > y but that's not needed here so this should work:

In [16]: y = Symbol('y')                                                                                                                      


In [17]: integrate(cos(x*y), (x, 0, a))                                                                                                        

Out[17]:

sin(ay)                            

⎪────────  for y > -∞ y < y 0

  y                                

                                   

  a              otherwise        


In [18]: refine(_, Q.nonzero(y))                                                                                                              

Out[18]:

sin(ay)                            

⎪────────  for y > -∞ y < y 0

  y                                

                                   

  a              otherwise



I guess that this just isn't implemented yet in refine.


Oscar

My Name

unread,
Aug 11, 2020, 8:57:37 AM8/11/20
to sympy
Thanks very much for the explanation.

The assumptions issues in Sympy have deterred me from using Sympy almost completely. It's not clear to me why the old assumption system had to be replaced. It's not clear to me what's unimplemented in the new system.

You say above, "The point about relations not being implemented is that there is not currently a way to use the assumption that e.g. x > y but that's not needed here so this should work". But your code shows Sympy "using" the assumptions y > -∞ ∧ y < ∞, which are relations. I you are using the word "use" in a way I don't understanding, and I'm left with this vague warning about relations being unusable in Sympy, and I go back to Maple.

Aaron Meurer

unread,
Aug 11, 2020, 3:16:55 PM8/11/20
to sympy
On Tue, Aug 11, 2020 at 6:57 AM My Name <mnam...@gmail.com> wrote:
>
> Thanks very much for the explanation.
>
> The assumptions issues in Sympy have deterred me from using Sympy almost completely. It's not clear to me why the old assumption system had to be replaced. It's not clear to me what's unimplemented in the new system.
>
> You say above, "The point about relations not being implemented is that there is not currently a way to use the assumption that e.g. x > y but that's not needed here so this should work". But your code shows Sympy "using" the assumptions y > -∞ ∧ y < ∞, which are relations. I you are using the word "use" in a way I don't understanding, and I'm left with this vague warning about relations being unusable in Sympy, and I go back to Maple.

The old assumptions only implement a subset of possible intervals,
with adjectives like "positive", "negative", "finite". You can
represent something like x > 0 because that's the same thing as
"positive", but you can't represent x > y because that isn't
representable by the set of things that the old assumptions can
represent. The only way to represent more advanced relations like x >
y is to use a different syntax from what the old assumptions use,
which is the idea behind the new assumptions.

Also, the old assumptions aren't going away. We originally were going
to do that, but the current plan is to keep it around as it works just
fine for those things that it can represent.

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/1cf4691a-1092-42c2-b4c5-0eb8ff068404n%40googlegroups.com.

My Name

unread,
Aug 11, 2020, 4:34:34 PM8/11/20
to sympy
I have lost years of time trying to do simple things in CAS, between Sympy's assumptions-documentation, Maxima's lack of support for lexical scoping, Maple's awfully designed and implemented user-interface and bad documentation, and Mathematica's flaws. I'd have paid $200,000 for a CAS done well if anyone had ever made one.

My Name

unread,
Aug 11, 2020, 4:38:14 PM8/11/20
to sympy
Thanks trying to clear up Sympy's assumptions for me, but I get feeling the only people who know how to use it are people familiar with the source code. I may resort that, because Maple has brought me to a standstill, but I might save time by building my own CAS from scratch to do what I need done. Usually faster than deciphering other people's source code.

On Tuesday, August 11, 2020 at 12:16:55 PM UTC-7 asme...@gmail.com wrote:

Oscar Benjamin

unread,
Aug 11, 2020, 7:31:51 PM8/11/20
to sympy
On Tue, 11 Aug 2020 at 21:38, My Name <mnam...@gmail.com> wrote:
>
> Thanks trying to clear up Sympy's assumptions for me, but I get feeling the only people who know how to use it are people familiar with the source code. I may resort that, because Maple has brought me to a standstill, but I might save time by building my own CAS from scratch to do what I need done. Usually faster than deciphering other people's source code.

I don't know what your particular task is but I'm fairly sure it would
be quicker to learn how to use sympy for your problem than to build
your own CAS.

People here are pretty helpful if you ask your question in a
reasonable way. You could probably resolve any difficulties you have
with SymPy more efficiently by having a nice polite discussion here
rather than moaning!


Oscar

My Name

unread,
Aug 11, 2020, 8:39:13 PM8/11/20
to sympy
I have an A.I. degree from Stanford, and I've read Church, Post, Godel, and Kleene, and every time I try to use Sympy, I end up furious to the point of seriously considering quitting tech altogether. Just now I stupidly wondered whether the old assumptions would suffice for my task, and I stupidly tried to find their documentation via the table of contents, put that table of contents is still the same piece of shit it was last time I tried two years ago, so I don't know how your fucking assumptions work, but I do know how to implement my own fucking assumptions.

I'm 49, and the older I get, the less polite I get about shit software.

My Name

unread,
Aug 11, 2020, 8:42:25 PM8/11/20
to sympy
and having programmed since 1980, I know what a discussion forum is, and I know it's no substitute for competent fucking table of contents

On Tuesday, August 11, 2020 at 4:31:51 PM UTC-7 Oscar wrote:

My Name

unread,
Aug 11, 2020, 8:49:37 PM8/11/20
to sympy
Now I lost another day to this motherfucker, despite having sworn it off years ago. Good thing its free open source software, so no one has to take responsibility and take insult from my insults. It's just a piece of shit on the sidewalk for which no one's responsible, and I keep stepping in it. If I want documented assumptions, I'm encouraged to document them myself, which would take longer than simply starting from scratch.

My Name

unread,
Aug 11, 2020, 8:54:25 PM8/11/20
to sympy
I didn't come here expecting to speak my mind, but that was before I fucked off a few hours trying to sum the power series of log plus one. What shit programmers developed this time-waster.

Aaron Meurer

unread,
Aug 11, 2020, 9:29:45 PM8/11/20
to sympy
Please follow our code of conduct here
https://github.com/sympy/sympy/blob/master/CODE_OF_CONDUCT.md. Your
most recent messages are completely inappropriate for this forum.

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/effa910a-33dc-45b6-8841-1f5ae9143c7bn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages