# Help Debugging my first Julia Conversion of Maple Code

**URL:** <https://discourse.julialang.org/t/help-debugging-my-first-julia-conversion-of-maple-code/119112>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 6, 2024, 10:02am UTC](https://discourse.julialang.org/t/help-debugging-my-first-julia-conversion-of-maple-code/119112 "2024-09-06T10:02:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rowing0914](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rowing0914/32/211810_2.png) [@Rowing0914](https://discourse.julialang.org/u/Rowing0914)\
**Post date:** [September 6, 2024, 10:02am UTC](https://discourse.julialang.org/t/help-debugging-my-first-julia-conversion-of-maple-code/119112/1 "2024-09-06T10:02:15Z")

</div>

I’m trying to replicate the behavior of a Maple code snippet (from [1] on Page 15) in Julia, as I’m new to Julia I am not sure if this is being implemented correctly and a particular line of my code is running super slowly. Thank you for checking my post in advance!

### Reference Code in Maple

```maple
h := t - x^5 + x^7 - x^(11)*t:
hy := subs(x = y*t^(1/5), h):
hyt := simplify(hy/t):
newton := x -> x - subs(y=x, hyt/diff(hyt, y)):
x[0] := 1:
for k from 1 to 6 do
    x[k] := newton(x[k-1]):
    s[k] := series(x[k], t=0, 15):
    lprint(op(1, s[k] - s[k-1]));
end do:

```

### My Attempt in Julia

```python

using SymPy

# Define the symbolic variables
@syms t x y

# Define the function h
h = t - x^5 + x^7 - x^11 * t

# Substitute x = y * t^(1/5) into h
hy = subs(h, x => y * t^(1/5))

# Simplify hy / t
hyt = simplify(hy / t)

# Define the Newton iteration function
function newton(x_val)
    diff_hyt = diff(hyt, y)
    return x_val - subs(hyt, y => x_val) / subs(diff_hyt, y => x_val)
end

# Initialize x_vals as symbolic list with the first element as a symbolic 1
x_vals = [Sym(1)] # Start with symbolic 1

# Array to store series expansions
s_vals = []

# Perform 6 iterations of Newton's method
for k in 1:6
    # Perform Newton's method step
    x_next = newton(x_vals[end])
   push!(x_vals, simplify(x_next)) # Append the simplified result
    
    # Compute series expansion up to 15 terms
    s_current = series(x_vals[end], t, 0, 15)
    push!(s_vals, s_current)
    
    # Print the difference between successive series expansions
    if k > 1
        diff_series = s_vals[k] - s_vals[k-1]
        println("Difference in series at iteration $k: ", numerator(diff_series))
    end
end

```

### Issue

The code takes quite long…

```julia-auto
push!(x_vals, simplify(x_next))

```

### References

[1] Sommese, Andrew J., Jan Verschelde, and Charles W. Wampler. “Introduction to numerical algebraic geometry.” Graduate School on Systems of Polynomial Equations: From Algebraic Geometry to Industrial Applications (2003): 14-25.

* * *

**Any insights or suggestions on how to resolve this issue would be greatly appreciated. Thanks!**

---

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [September 6, 2024, 4:17pm UTC](https://discourse.julialang.org/t/help-debugging-my-first-julia-conversion-of-maple-code/119112/2 "2024-09-06T16:17:10Z")

</div>

Isn’t [`SymPy.jl`](https://github.com/JuliaPy/SymPy.jl) the Julia bindings for the Python package?

You may have better luck (and performance) using a native Julia library like [`Symbolics.jl`](https://github.com/JuliaSymbolics/Symbolics.jl).

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [September 6, 2024, 8:30pm UTC](https://discourse.julialang.org/t/help-debugging-my-first-julia-conversion-of-maple-code/119112/3 "2024-09-06T20:30:02Z")

</div>

> [@Rowing0914](#):
>
> The code takes quite long…
> 
> `push!(x_vals, simplify(x_next))`

It’s safe to assume that `push!` won’t take too long, so the question is why `simplify` takes so long. To diagnose the problem, you could print out `x_next` to see what’s being simplified.

---

<div class="post-metadata">

**Author:** ![Rowing0914](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rowing0914/32/211810_2.png) [@Rowing0914](https://discourse.julialang.org/u/Rowing0914)\
**Post date:** [September 9, 2024, 7:53am UTC](https://discourse.julialang.org/t/help-debugging-my-first-julia-conversion-of-maple-code/119112/5 "2024-09-09T07:53:10Z")

</div>

> [@greatpet](#):
>
> It’s safe to assume that `push!` won’t take too long, so the question is why `simplify` takes so long. To diagnose the problem, you could print out `x_next` to see what’s being simplified.

Sorry for my late response. I should’ve mentioned but, yeah, I’d looked into it and had found that the expression was rather longer… But still compared to SageMath script that I’d written it was too long to iterate through so I aksed the question. Anyway, I’d try to rewrite this using [`Symbolics.jl`](https://github.com/JuliaSymbolics/Symbolics.jl)!

---

<div class="post-metadata">

**Author:** ![Rowing0914](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rowing0914/32/211810_2.png) [@Rowing0914](https://discourse.julialang.org/u/Rowing0914)\
**Post date:** [September 9, 2024, 7:54am UTC](https://discourse.julialang.org/t/help-debugging-my-first-julia-conversion-of-maple-code/119112/6 "2024-09-09T07:54:02Z")

</div>

> [@jmair](#):
>
> Isn’t [`SymPy.jl`](https://github.com/JuliaPy/SymPy.jl) the Julia bindings for the Python package?
> 
> You may have better luck (and performance) using a native Julia library like [`Symbolics.jl`](https://github.com/JuliaSymbolics/Symbolics.jl).

Got it! I will try this!
