Skip to content

Instantly share code, notes, and snippets.

@mattpap
Created June 4, 2013 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpap/5704366 to your computer and use it in GitHub Desktop.
Save mattpap/5704366 to your computer and use it in GitHub Desktop.
In [1]: from sympy.integrals.heurisch import heurisch
In [2]: f = 1/((sin(x)+cos(x)+1)**2)
In [3]: heurisch(f, x)
Out[3]:
2
⎛ ⎛x⎞ ⎞ ⎛x⎞ ⎛ ⎛x⎞ ⎞ ⎛x⎞ ⎛x⎞
2⋅log⎜tan⎜─⎟ + 1⎟⋅tan⎜─⎟ 2⋅log⎜tan⎜─⎟ + 1⎟ tan ⎜─⎟ 3⋅tan⎜─⎟
⎝ ⎝2⎠ ⎠ ⎝2⎠ ⎝ ⎝2⎠ ⎠ ⎝2⎠ ⎝2⎠
- ──────────────────────── - ───────────────── + ──────────── + ────────────
⎛x⎞ ⎛x⎞ ⎛x⎞ ⎛x⎞
2⋅tan⎜─⎟ + 2 2⋅tan⎜─⎟ + 2 2⋅tan⎜─⎟ + 2 2⋅tan⎜─⎟ + 2
⎝2⎠ ⎝2⎠ ⎝2⎠ ⎝2⎠
In [4]: cancel(_)
Out[4]:
⎛ 2 ⎞
⎜ ⎛ ⎛x⎞ ⎞ ⎛x⎞ ⎛ ⎛x⎞ ⎞ ⎛x⎞ ⎛x⎞⎟
-⎜2⋅log⎜tan⎜─⎟ + 1⎟⋅tan⎜─⎟ + 2⋅log⎜tan⎜─⎟ + 1⎟ - tan ⎜─⎟ - 3⋅tan⎜─⎟⎟
⎝ ⎝ ⎝2⎠ ⎠ ⎝2⎠ ⎝ ⎝2⎠ ⎠ ⎝2⎠ ⎝2⎠⎠
────────────────────────────────────────────────────────────────────
⎛x⎞
2⋅tan⎜─⎟ + 2
⎝2⎠
In [5]: ratsimp(_)
Out[5]:
⎛x⎞
tan⎜─⎟
⎛ ⎛x⎞ ⎞ ⎝2⎠ 1
- log⎜tan⎜─⎟ + 1⎟ + ────── + 1 - ──────────
⎝ ⎝2⎠ ⎠ 2 ⎛x⎞
tan⎜─⎟ + 1
⎝2⎠
In [6]: diff(_, x)
Out[6]:
2 2
⎛x⎞ ⎛x⎞
2 tan ⎜─⎟ tan ⎜─⎟
⎛x⎞ ⎝2⎠ 1 ⎝2⎠ 1
tan ⎜─⎟ ─────── + ─ - ─────── - ─
⎝2⎠ 1 2 2 2 2
─────── + ─ - ─────────── - ─────────────
4 4 ⎛x⎞ 2
tan⎜─⎟ + 1 ⎛ ⎛x⎞ ⎞
⎝2⎠ ⎜tan⎜─⎟ + 1⎟
⎝ ⎝2⎠ ⎠
In [7]: _.subs(tan(x/2), sin(x)/(1 + cos(x)))
Out[7]:
2 2
1 sin (x) 1 sin (x)
- ─ - ─────────────── ─ + ───────────────
2 2 2 2 2
2⋅(cos(x) + 1) 2⋅(cos(x) + 1) 1 sin (x)
- ───────────────────── - ─────────────────── + ─ + ───────────────
2 sin(x) 4 2
⎛ sin(x) ⎞ 1 + ────────── 4⋅(cos(x) + 1)
⎜1 + ──────────⎟ cos(x) + 1
⎝ cos(x) + 1⎠
In [8]: cancel(_)
Out[8]:
4 2 2 2 2 4 3 2
sin (x) + 2⋅sin (x)⋅cos (x) + 4⋅sin (x)⋅cos(x) + 2⋅sin (x) + cos (x) + 4⋅cos (x) + 6⋅cos (x) + 4⋅cos(x) + 1
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2 2 2 2 3 2 4 3
4⋅sin (x)⋅cos (x) + 8⋅sin (x)⋅cos(x) + 4⋅sin (x) + 8⋅sin(x)⋅cos (x) + 24⋅sin(x)⋅cos (x) + 24⋅sin(x)⋅cos(x) + 8⋅sin(x) + 4⋅cos (x) + 16⋅cos
────────────────────────────────
2
(x) + 24⋅cos (x) + 16⋅cos(x) + 4
In [9]: ratsimp(_)
Out[9]:
4 2 2 2 2 3 2
- sin (x) - sin (x)⋅cos (x) - 2⋅sin (x)⋅cos(x) - sin (x) + 2⋅sin(x)⋅cos (x) + 6⋅sin(x)⋅cos (x) + 6⋅sin(x)⋅cos(x)
- ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2 2 2 2 3 2 4
4⋅sin (x)⋅cos (x) + 8⋅sin (x)⋅cos(x) + 4⋅sin (x) + 8⋅sin(x)⋅cos (x) + 24⋅sin(x)⋅cos (x) + 24⋅sin(x)⋅cos(x) + 8⋅sin(x) + 4⋅cos (x) + 16⋅co
+ 2⋅sin(x) 1
────────────────────────────────── + ─
3 2 4
s (x) + 24⋅cos (x) + 16⋅cos(x) + 4
In [10]: ratsimpmodprime(_, [sin(x)**2 + cos(x)**2 - 1], sin(x), cos(x))
Out[10]:
1
─────────────────────────────────────────
2⋅sin(x)⋅cos(x) + 2⋅sin(x) + 2⋅cos(x) + 2
In [11]: factor(_)
Out[11]:
1
───────────────────────────
2⋅(sin(x) + 1)⋅(cos(x) + 1)
In [12]: ratsimpmodprime(f, [sin(x)**2 + cos(x)**2 - 1], sin(x), cos(x))
Out[12]:
1
─────────────────────────────────────────
2⋅sin(x)⋅cos(x) + 2⋅sin(x) + 2⋅cos(x) + 2
In [13]: factor(_)
Out[13]:
1
───────────────────────────
2⋅(sin(x) + 1)⋅(cos(x) + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment