# SymPy Simplify Took too Long for Area of Surface Revolution of a sphere

**URL:** <https://discourse.julialang.org/t/sympy-simplify-took-too-long-for-area-of-surface-revolution-of-a-sphere/92988>\
**Category:** General Usage\
**Tags:** package\
**Created:** [January 15, 2023, 9:37am UTC](https://discourse.julialang.org/t/sympy-simplify-took-too-long-for-area-of-surface-revolution-of-a-sphere/92988 "2023-01-15T09:37:59Z")\
**Posts on this page:** 7\
**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:** [January 15, 2023, 9:37am UTC](https://discourse.julialang.org/t/sympy-simplify-took-too-long-for-area-of-surface-revolution-of-a-sphere/92988/1 "2023-01-15T09:37:59Z")

</div>

Hi all,

I want to calculate symbolically area of a surface of a sphere.

y = f(x) = \sqrt{r^{2} - x^{2}} with -r \le x \le r

this is my code:

```julia
using SymPy

x, r = symbols("x r")

f(x) = sqrt(r^2 - x^2)
# simplify(diff(f(x),x))
# we cannot use diff inside the fd(x) since QuadGK can't comprehend diff
fd(x) = -x/(sqrt(r^2 - x^2))
g(x) = sqrt(1 + (fd(x))^2)

h(x) = f(x)*g(x)
Area(x) = 2pi*h(x)

d = integrate(Area(x), (x, -r,r))

println("Area = ", d)

```

the problem is the final result should be: 4 \pi r^{2}

what I get is the unfinished `simplify` after 60 minutes of waiting… and the form of integral still:

![Capture d’écran_2023-01-15_16-32-28](https://global.discourse-cdn.com/julialang/original/3X/5/2/5220c4925bea977ee5e4bc969eb5e9afa62293ac.png)

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 15, 2023, 10:26am UTC](https://discourse.julialang.org/t/sympy-simplify-took-too-long-for-area-of-surface-revolution-of-a-sphere/92988/2 "2023-01-15T10:26:44Z")

</div>

You need to add some assumptions.

See [Core - SymPy 1.12.dev documentation](https://docs.sympy.org/dev/modules/core.html#module-sympy.core.assumptions)

In particular `r` should be `nonnegative`.

---

<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:** [January 15, 2023, 10:46am UTC](https://discourse.julialang.org/t/sympy-simplify-took-too-long-for-area-of-surface-revolution-of-a-sphere/92988/3 "2023-01-15T10:46:39Z")

</div>

Great !

Now I am trying this:

```julia
using SymPy

x = symbols("x")
r = symbols("r", nonnegative=True)
# check by typing r.is_nonnegative

f(x) = sqrt(r^2 - x^2)
# simplify(diff(f(x),x))
# we cannot use diff inside the fd(x) since QuadGK can't comprehend diff
fd(x) = -x/(sqrt(r^2 - x^2))
g(x) = sqrt(1 + (fd(x))^2)

h(x) = f(x)*g(x)
Area(x) = 2pi*h(x)

d = simplify(integrate(Area(x), (x, -r,r)))

```

by tomorrow (if it takes 24 hours and still not finished yet, I will tell)…

the above code that took 60 minutes still not done yet and it is already 2 hours…

---

<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:** [January 15, 2023, 1:12pm UTC](https://discourse.julialang.org/t/sympy-simplify-took-too-long-for-area-of-surface-revolution-of-a-sphere/92988/4 "2023-01-15T13:12:16Z")

</div>

The answer is very numerics:

12.5663706143592⋅r^{2}

---

<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:** [January 15, 2023, 2:12pm UTC](https://discourse.julialang.org/t/sympy-simplify-took-too-long-for-area-of-surface-revolution-of-a-sphere/92988/5 "2023-01-15T14:12:59Z")

</div>

Use PI, not pi. When you compute 2pi it converts to a float.

---

<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:** [January 15, 2023, 2:29pm UTC](https://discourse.julialang.org/t/sympy-simplify-took-too-long-for-area-of-surface-revolution-of-a-sphere/92988/6 "2023-01-15T14:29:50Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> ```julia
> f(x) = sqrt(r^2 - x^2)
> # simplify(diff(f(x),x))
> # we cannot use diff inside the fd(x) since QuadGK can't comprehend diff
> fd(x) = -x/(sqrt(r^2 - x^2))
> g(x) = sqrt(1 + (fd(x))^2)
> 
> h(x) = f(x)*g(x)
> 
> ```

To elaborate, it also _really_ helps to call `simplify` prior to computing:

```julia
julia> @time ex = integrate(2PI*simplify(h(x)), (x, -r, r))
  0.043363 seconds (13.71 k allocations: 715.979 KiB, 9.07% compilation time)
     2
4⋅π⋅r 

```

---

<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:** [January 15, 2023, 3:06pm UTC](https://discourse.julialang.org/t/sympy-simplify-took-too-long-for-area-of-surface-revolution-of-a-sphere/92988/7 "2023-01-15T15:06:08Z")

</div>

> [@j\_verzani](#):
>
> `integrate(2PI*simplify(h(x)), (x, -r, r))`

Yes it works and produce the result faster than integrate first then simplify… thank you very much!
