# Any Other Method to Calculate Indefinite Integral Besides Using SymPy?

**URL:** <https://discourse.julialang.org/t/any-other-method-to-calculate-indefinite-integral-besides-using-sympy/90707>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [November 23, 2022, 1:31pm UTC](https://discourse.julialang.org/t/any-other-method-to-calculate-indefinite-integral-besides-using-sympy/90707 "2022-11-23T13:31:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [November 23, 2022, 1:31pm UTC](https://discourse.julialang.org/t/any-other-method-to-calculate-indefinite-integral-besides-using-sympy/90707/1 "2022-11-23T13:31:21Z")

</div>

Hi all,

I use this code to calculate the indefinite integral of a complex trigonometric function:

```julia
# To compute an indefinite integral
using SymPy

# we can also use @vars x y z
x = symbols("x")

integrate((sin((x^(2) + 1)^(4)))^(3)*(cos(x^(2) + 1)^(4))*((x^(2)+1)^(3))*x)

```

it took long time, just like the computer is thinking and forgetting the trigonometric formula thus making it very long for me to wait. Maybe the problem is my processor or RAM is not good enough. If there is any other method to calculate the complex function of indefinite integral do tell me.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [November 23, 2022, 4:30pm UTC](https://discourse.julialang.org/t/any-other-method-to-calculate-indefinite-integral-besides-using-sympy/90707/2 "2022-11-23T16:30:43Z")

</div>

You seem to have a typo with your integrand, I can’t be sure. The following has an integral identified using `SymbolicNumericIntegration`:

```julia
sin((x^(2) + 1)^(4))^(3)*(cos((x^(2) + 1)^(4))*((x^(2)+1)^(3)))*x

```

With `SymPy` it seems to need a bit of help with the substitution. This somewhat excessive pattern mirrors what might be done in a textbook:

```julia
using SymPy
@syms x dx v dv
constant(ex) = prod(x for x in ex.as_ordered_factors() if x.is_constant())
ex = sin((x^(2) + 1)^(4))^(3)*(cos((x^(2) + 1)^(4))*((x^(2)+1)^(3)))*x * dx

u = (x^2 + 1)^4
du = diff(u, x) * dx
c = constant(du)
duₑ = du/c
ex1 = subs(ex, duₑ => dv/c, u=>v, dv=>1)
integrate(ex1, v)(v => u)

```

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [November 23, 2022, 5:01pm UTC](https://discourse.julialang.org/t/any-other-method-to-calculate-indefinite-integral-besides-using-sympy/90707/3 "2022-11-23T17:01:43Z")

</div>

the compiling is not even finished till now… I will try your code now then. Thanks @j_verzani

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [November 23, 2022, 7:36pm UTC](https://discourse.julialang.org/t/any-other-method-to-calculate-indefinite-integral-besides-using-sympy/90707/4 "2022-11-23T19:36:32Z")

</div>

> [@j\_verzani](#):
>
> ```julia
> using SymPy
> @syms x dx v dv
> constant(ex) = prod(x for x in ex.as_ordered_factors() if x.is_constant())
> ex = sin((x^(2) + 1)^(4))^(3)*(cos((x^(2) + 1)^(4))*((x^(2)+1)^(3)))*x * dx
> 
> u = (x^2 + 1)^4
> du = diff(u, x) * dx
> c = constant(du)
> duₑ = du/c
> ex1 = subs(ex, duₑ => c*dv, u=>v, dv=>1)
> integrate(ex1, v)
> 
> ```

This is the solution from the textbook:

![Capture d’écran_2022-11-24_02-35-09](https://global.discourse-cdn.com/julialang/original/3X/9/1/91be2e7b74f72cebf8254a7350ad2d2e15cfe03a.png)

Why is the code gives a solution of :  
 2 \sin^{4} v

how to know what is v ?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 23, 2022, 7:43pm UTC](https://discourse.julialang.org/t/any-other-method-to-calculate-indefinite-integral-besides-using-sympy/90707/5 "2022-11-23T19:43:54Z")

</div>

Did you try Julia?

```julia
julia> using Symbolics
julia> using SymbolicNumericIntegration

julia> @variables x
1-element Vector{Num}:
 x

julia> expr=sin((x^(2) + 1)^(4))^(3)*(cos((x^(2) + 1)^(4))*((x^(2)+1)^(3)))*x
x*((1 + x^2)^3)*(sin((1 + x^2)^4)^3)*cos((1 + x^2)^4)

julia> @time integrate(expr)
 74.138733 seconds (323.74 M allocations: 16.519 GiB, 4.00% gc time, 77.46% compilation time: 0% of which was recompilation)
(0, x*(sin(1 + x^8 + 4(x^2) + 4(x^6) + 6(x^4))^3)*cos(1 + x^8 + 4(x^2) + 4(x^6) + 6(x^4)) + (x^7)*(sin(1 + x^8 + 4(x^2) + 4(x^6) + 6(x^4))^3)*cos(1 + x^8 + 4(x^2) + 4(x^6) + 6(x^4)) + 3(x^3)*(sin(1 + x^8 + 4(x^2) + 4(x^6) + 6(x^4))^3)*cos(1 + x^8 + 4(x^2) + 4(x^6) + 6(x^4)) + 3(x^5)*(sin(1 + x^8 + 4(x^2) + 4(x^6) + 6(x^4))^3)*cos(1 + x^8 + 4(x^2) + 4(x^6) + 6(x^4)), Inf)

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [November 23, 2022, 8:01pm UTC](https://discourse.julialang.org/t/any-other-method-to-calculate-indefinite-integral-besides-using-sympy/90707/6 "2022-11-23T20:01:23Z")

</div>

I fixed my example to substitute back (and adjust the constant, which I had incorrect.)

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [November 24, 2022, 5:22pm UTC](https://discourse.julialang.org/t/any-other-method-to-calculate-indefinite-integral-besides-using-sympy/90707/7 "2022-11-24T17:22:21Z")

</div>

Thanks @ufechner7 , but isn’t `btime` better to be used than `time`?

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [November 24, 2022, 7:53pm UTC](https://discourse.julialang.org/t/any-other-method-to-calculate-indefinite-integral-besides-using-sympy/90707/8 "2022-11-24T19:53:33Z")

</div>

You change this

```julia
ex1 = subs(ex, duₑ => c*dv, u=>v, dv=>1)

```

into this

```julia
ex1 = subs(ex, duₑ => dv/c, u=>v, dv=>1)

```

what is the logic of `dv/c` and `c*dv` ?

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [November 24, 2022, 8:01pm UTC](https://discourse.julialang.org/t/any-other-method-to-calculate-indefinite-integral-besides-using-sympy/90707/9 "2022-11-24T20:01:49Z")

</div>

Just that subs didn’t work with that constant 8 that comes out of the derivative. So it is taken out then put back in.
