python - sympy integration with quotient and cot(x) not getting simplifed results - Stack Overflow

时间: 2025-01-06 admin 业界

I am trying to use sympy to integrate 1 / (1 + cot(x)) for variable x, (or its equivalent sin(x) / (sin(x) + cos(x)) ).

I tried

from sympy import *
x = symbols("x", real=True)
integrate(sin(x) / (sin(x) + cos(x)), x)

and the results look good:

But while I'm trying

from sympy import *
x = symbols("x", real=True)
integrate(1 / (1 + cot(x)), x)

it produces

which is not wrong because I tried to manually simplify that to the results above.

as it is not simplified, I called the simplify() function for this result, but the following is what I got:

However, that's again correct but not well simplified. My questions are:

  1. In such a case how am I able to get a correctly simplifed result if I try to integrate integrate(1 / (1 + cot(x)), x) (or any other similar functions as this type).
  2. If 1 is impossible, how do I manually hint sympy so that it can produce a correctly simplified result for me?

I am trying to use sympy to integrate 1 / (1 + cot(x)) for variable x, (or its equivalent sin(x) / (sin(x) + cos(x)) ).

I tried

from sympy import *
x = symbols("x", real=True)
integrate(sin(x) / (sin(x) + cos(x)), x)

and the results look good:

But while I'm trying

from sympy import *
x = symbols("x", real=True)
integrate(1 / (1 + cot(x)), x)

it produces

which is not wrong because I tried to manually simplify that to the results above.

as it is not simplified, I called the simplify() function for this result, but the following is what I got:

However, that's again correct but not well simplified. My questions are:

  1. In such a case how am I able to get a correctly simplifed result if I try to integrate integrate(1 / (1 + cot(x)), x) (or any other similar functions as this type).
  2. If 1 is impossible, how do I manually hint sympy so that it can produce a correctly simplified result for me?
Share Improve this question edited yesterday jared 8,8513 gold badges14 silver badges43 bronze badges asked yesterday Sam YSam Y 1147 bronze badges 2
  • 3 One possibility is integrate((1/(1+cot(x))).rewrite('sincos'), x). – Oscar Benjamin Commented yesterday
  • Define “correctly simplified”. If my integrand contained cot and tan … that is probably what I would assume the answer to contain. – lastchance Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

As Oscar points out, it is easier to get to a desired form after integration by getting the expression in a desired form before you start. Otherwise, getting to that form after integration can be tedious because of assumptions that block certain simplifications:

from sympy import *
x = symbols("x", real=True)
eq=integrate(1 / (1 + cot(x)), x)

from sympy.simplify.fu import TR22
p=Symbol('p',positive=True)
s=bottom_up(eq,TR22).subs(sec(x),p).expand().subs(p,sec(x))
s=logcombine(2*(s-x/2),force=True)/2+x/2 # seems like logcombine could gather better on its own
s=bottom_up(_,fu)
s=expand_trig(s)
s=factor_terms(s)
s=expand_log(s,force=True)
>>> s
(x - log(sin(x) + cos(x)))/2