# How to use sympy to replace a symbol in a derivative?

**URL:** <https://discourse.julialang.org/t/how-to-use-sympy-to-replace-a-symbol-in-a-derivative/92521>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 4, 2023, 7:28pm UTC](https://discourse.julialang.org/t/how-to-use-sympy-to-replace-a-symbol-in-a-derivative/92521 "2023-01-04T19:28:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [January 4, 2023, 7:28pm UTC](https://discourse.julialang.org/t/how-to-use-sympy-to-replace-a-symbol-in-a-derivative/92521/1 "2023-01-04T19:28:53Z")

</div>

Hello, good afternoon.

Im trying to use sympy to calc an gradient descent in some symbols and having troubles with index. This algorithm is pretty simple using to calc derivative by hand. Using sympy things gets wierd.

```julia
m, b, i, n = symbols("m b i n");
x, y = symbols("x y", cls=sympy.Function);

sumofsquares = sympy.Sum((m * x(i) + b - y(i))^2, (i, 0, n));
print("sum of squares:");
display(sumofsquares);

# derivative of m
dm = sympy.diff(sumofsquares, m);

# derivatime of b
db = sympy.diff(sumofsquares, b);
print("derivative of m:"); display(dm);
print("derivative of b:");

points = Tuple.(eachrow(df));
println(points);
dmf = replace(subs(dm, n, length(points) -1), x, i => points[i].x)

```

but it fail with the error

```julia
ArgumentError: invalid index: i of type Sym

Stacktrace:
 [1] to_index(i::Sym)
   @ Base ./indices.jl:300
 [2] to_index(A::Vector{Tuple{Int64, Int64}}, i::Sym)
   @ Base ./indices.jl:277
 [3] to_indices
   @ ./indices.jl:333 [inlined]
 [4] to_indices
   @ ./indices.jl:325 [inlined]
 [5] getindex(A::Vector{Tuple{Int64, Int64}}, I::Sym)
   @ Base ./abstractarray.jl:1241
 [6] top-level scope
   @ In[91]:3

```

---

<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 4, 2023, 8:22pm UTC](https://discourse.julialang.org/t/how-to-use-sympy-to-replace-a-symbol-in-a-derivative/92521/2 "2023-01-04T20:22:22Z")

</div>

I don’t know what `df` is, so can’t run all of this, but maybe you should use `@syms` to create the symbolic functions. The following works as I think you want:

```julia
using SymPy
@syms m b i n x() y()
sumofsquares = sympy.Sum((m * x(i) + b - y(i))^2, (i, 0, n))

# derivative of m
dm = diff(sumofsquares, m)

# derivatime of b
db = diff(sumofsquares, b)

```

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [January 4, 2023, 8:55pm UTC](https://discourse.julialang.org/t/how-to-use-sympy-to-replace-a-symbol-in-a-derivative/92521/3 "2023-01-04T20:55:01Z")

</div>

I will try the way you did, but the code works for me until last line.

dmf = replace(subs(dm, n, length(points) -1), x, i =\> points[i].x)

Here I got issues with replace functions and the error says Im using a invalid index, because i is a syms variable

---

<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 4, 2023, 9:04pm UTC](https://discourse.julialang.org/t/how-to-use-sympy-to-replace-a-symbol-in-a-derivative/92521/4 "2023-01-04T21:04:53Z")

</div>

Can you share your df variable?

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [January 4, 2023, 9:21pm UTC](https://discourse.julialang.org/t/how-to-use-sympy-to-replace-a-symbol-in-a-derivative/92521/5 "2023-01-04T21:21:21Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/2/9/29126fb578e5dc58fcb4c32b90e63a7ce6327c8e.png)

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [January 4, 2023, 9:24pm UTC](https://discourse.julialang.org/t/how-to-use-sympy-to-replace-a-symbol-in-a-derivative/92521/6 "2023-01-04T21:24:14Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/d/d/dd82afe9e8f330530034aa88cdf71903b675a73b.png)

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/e/5e80647761daadc4021d9f1c44fe2dc252cef395.png)

issue is in replace function. All but replace(…) works ok.

---

<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 4, 2023, 9:46pm UTC](https://discourse.julialang.org/t/how-to-use-sympy-to-replace-a-symbol-in-a-derivative/92521/7 "2023-01-04T21:46:49Z")

</div>

Try this:

```julia
dm = subs(dm, n=>length(points) - 1).doit()
for (i,r) ∈ enumerate(points)
    dm = subs(dm, x(i-1)=>r.x, y(i-1) => r.y)
end

```

(It seems you need to expand the sum before you can substitute in for `x(i)`)
