hayk
hayk

Reputation: 468

SymPy integration with assumptions

Basically I'm trying to run the following code.

import sympy as sp

alpha = sp.Symbol(r'\alpha')
x = sp.Symbol('x')
sp.Q.is_true(alpha != -1)

sp.integrate(x**alpha, x)

This results in the following Piecewise function.

enter image description here

Since I specify global assumptions that alpha != -1, I expected it will simply give me the first expression. So two questions I have:

  1. how do you properly define the assumptions so that the sp.integrate does not ignore them;
  2. is there a way to access (extract) the first (or second) expression from the Piecewise function?

Thanks in advance!

PS. Defining conds='separate' in the sp.integrate only returns the first expression for some reason. So if I needed the second part of the piecewise function, I wouldn't be able to get it.

PPS. In case this matters, I have python 3.8.0 and sympy 1.4.

Upvotes: 1

Views: 1353

Answers (1)

smichr
smichr

Reputation: 19145

There is no way to give a specific value as an assumption for a symbol so it can be used in the integration. The best you can do is specify positive, negative, etc... But as for extracting the desired expression from the Piecewise, you can either get it as the particular argument or else feed in a dummy value for x that would extract it. Like the following:

>> from sympy.abc import x
>> from sympy import Piecewise, Dummy
>> eq = Piecewise((x + 1, x < 0), (1/x, True))
>> eq.args[0]
(x + 1, x < 0)
>> _.args[0]
x + 1
>> d = Dummy(negative=True)
>> eq.subs(x, d)
d + 1
>> _.subs(d, x)
x + 1

Upvotes: 3

Related Questions