How to add assumptions to the integrated function

I posted the question here because it seemed to me the least unrelated to the question.

Of the following integral I am interested only in the real part. How can I force to consider a>x>0?
Or, alternatively, how can I select only the second one between the two results?

julia> using SymPy

julia> @syms x y a b
(x, y, a, b)

julia> integrate(sqrt(a^2-x^2),x)
⎧     2      ⎛x⎞
⎪  ⅈ⋅a ⋅acosh⎜─⎟                                  3             │ 2│
⎪            ⎝a⎠         ⅈ⋅a⋅x                 ⅈ⋅x              │x │
⎪- ───────────── - ───────────────── + ───────────────────  for │──│ > 1
⎪        2                 _________             _________      │ 2│
⎪                         ╱       2             ╱       2       │a │
⎪                        ╱       x             ╱       x
⎪                  2⋅   ╱   -1 + ──    2⋅a⋅   ╱   -1 + ──
⎪                      ╱          2          ╱          2
⎨                    ╲╱          a         ╲╱          a
⎪
⎪                                    ________
⎪                                   ╱      2
⎪                                  ╱      x
⎪              2     ⎛x⎞   a⋅x⋅   ╱   1 - ──
⎪             a ⋅asin⎜─⎟         ╱         2
⎪                    ⎝a⎠       ╲╱         a
⎪             ────────── + ──────────────────                otherwise
⎩                 2                2

You can set assumptions when you declare the variables:

julia> using SymPy

julia> @syms x::positive a::positive;

julia> integrate(sqrt(a^2-x^2), x)
 2     ⎛x⎞        _________
a ⋅asin⎜─⎟       ╱  2    2 
       ⎝a⎠   x⋅╲╱  a  - x  
────────── + ──────────────
    2              2       

To extract the second case from the general result, I tried some things with refine and SymPy.Q but without success.

the condition that x<=a or that a>=1/2 can’t be added easily, can it?

Maybe SymPy.Q(LessThan(x, a)) can work sometimes? I’m not sure…

The list of the available “old” assumptions is here Assumptions - SymPy 1.12 documentation . There may be tricks to get |x/a|>1 assumed, but I’m not aware of them.

To answer how you might pull out values from the Piecewise object, say you called it u, this pattern can help:

 [(expr=uᵢ.expr, cond=uᵢ.cond) for uᵢ ∈ u.args]
1 Like