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.
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
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