# An elementary symbolics question: simplifying a nested/continued fraction?

**URL:** <https://discourse.julialang.org/t/an-elementary-symbolics-question-simplifying-a-nested-continued-fraction/117170>\
**Category:** General Usage\
**Tags:** symbolics\
**Created:** [July 18, 2024, 6:22am UTC](https://discourse.julialang.org/t/an-elementary-symbolics-question-simplifying-a-nested-continued-fraction/117170 "2024-07-18T06:22:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)\
**Post date:** [July 18, 2024, 6:22am UTC](https://discourse.julialang.org/t/an-elementary-symbolics-question-simplifying-a-nested-continued-fraction/117170/1 "2024-07-18T06:22:42Z")

</div>

(Julia version 1.10.3, Symbolics version 5.33.0)

Here’s a simple example:

```julia
using Symbolics
@variables y
eq = 1/(7+1/(2+1/(4+y)))
simplify(eq)
simplify_fractions(eq)
expand(eq)

```

None of the last three work - the fraction remains in its fully nested form. What I want, of course, is the output

```julia
(2y+9)/(15y+67)

```

I would have thought this would be a very standard and straightforward process, so clearly I’m missing something, or doing something incorrectly - but I don’t know what! Do I need the `SymbolicUtils.jl` package? (I though that its functionality had been subsumed into `Symbolics.jl`, however.)

I’ve looked through the list of methods in the Symbolics package, and I can’t see anything else that looks as though it should work.

Anyway - how do I reduce a nested or finite continued fraction to a single fraction? Many thanks.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 18, 2024, 7:06am UTC](https://discourse.julialang.org/t/an-elementary-symbolics-question-simplifying-a-nested-continued-fraction/117170/2 "2024-07-18T07:06:52Z")

</div>

> [@amca01](#):
>
> Anyway - how do I reduce a nested or finite continued fraction to a single fraction? Many thanks.

This likely needs more rules added to simplification.

> <https://github.com/JuliaSymbolics/SymbolicUtils.jl/blob/master/src/simplify_rules.jl>

---

<div class="post-metadata">

**Author:** ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)\
**Post date:** [July 18, 2024, 2:47pm UTC](https://discourse.julialang.org/t/an-elementary-symbolics-question-simplifying-a-nested-continued-fraction/117170/3 "2024-07-18T14:47:53Z")

</div>

Many thanks - I was initially surprised that such an elementary (to me) functionality was not already available, but if it is a matter of adding some routines to the simplification code, I’ll leave that to the experts!

I discovered I could do the simplification using the `AbstractAlgebra` package, using a polynomial ring R over the rationals, and the total ring of fractions over R. This, however, may be too abstract for some people. Anyway, I think that being able to reduce a nested/continued fraction to a single fraction would be worth including in `Symbolics`, if it was not too much trouble to code.

Again, many thanks.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 18, 2024, 6:11pm UTC](https://discourse.julialang.org/t/an-elementary-symbolics-question-simplifying-a-nested-continued-fraction/117170/4 "2024-07-18T18:11:08Z")

</div>

> [@amca01](#):
>
> I discovered I could do the simplification using the `AbstractAlgebra` package, using a polynomial ring R over the rationals, and the total ring of fractions over R. This, however, may be too abstract for some people. Anyway, I think that being able to reduce a nested/continued fraction to a single fraction would be worth including in `Symbolics`, if it was not too much trouble to code.

Yes, we’d be happy to have extensions which make use of AbstractAlgebra/OSCAR. I think most users would be scared off by their interfaces, but using them within Symbolics would be a way to make them accessible. We just haven’t done all of the integrations we’ve wanted to do.

---

<div class="post-metadata">

**Author:** ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)\
**Post date:** [July 21, 2024, 4:28am UTC](https://discourse.julialang.org/t/an-elementary-symbolics-question-simplifying-a-nested-continued-fraction/117170/5 "2024-07-21T04:28:01Z")

</div>

Another option is simply to import the `SymPy` package. Although this is not a “native Julia” package as such, it seems to work well enough:

```julia
import SymPy as sy
sy.@syms y
eq = 1/(7+1/(2+1/(4+y)))
sy.simplify(eq)

```

gives the result

```julia
(2y+9)/(15y+67)

```

At the present time, this seems to be the best way for me to do symbolic work in Julia. It helps that I have a fairly good knowledge of SymPy in Python. But I prefer using Julia simply because it’s faster and more efficient.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 21, 2024, 8:27am UTC](https://discourse.julialang.org/t/an-elementary-symbolics-question-simplifying-a-nested-continued-fraction/117170/6 "2024-07-21T08:27:49Z")

</div>

Yes, and there are conversion functions to make it so you can move between the two.
