Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Julia Symbolics, how can I do a mathematical summation?

Tags:

julia

I am using Symbolics.jl and I want to make a mathematical summation, equivalent to the function Sum from Sympy (https://docs.sympy.org/latest/modules/concrete.html)

The documentation of Symbolics.jl suggests that it is possible:

  • Discrete math (representations of summations, products, binomial coefficients, etc.)

However, in the frequently asked questions, the opposite is suggested:

Loops are allowed, but the amount of loop iterations should not require that you know the value of the symbol x.

like image 989
CedEvolModelo Avatar asked Dec 30 '25 00:12

CedEvolModelo


1 Answers

You can use +(x...) for a summation for a vector of symbols.

julia> @variables x[1:5]
1-element Vector{Symbolics.Arr{Num, 1}}:
 x[1:5]

julia> +(x...)
x[1] + x[2] + x[3] + x[4] + x[5]

julia> Symbolics.derivative(+(x...), x[2])
1

Beware of sum(x) as it seems to be not expanded and yields incorrect results:

julia> sum(x)
Symbolics._mapreduce(identity, +, x, Colon(), (:init => false,))

julia> Symbolics.derivative(sum(x), x[2])
0

Last but not least, make another step and define the summation symbol to get a nice experience:

julia> ∑(v) = +(v...)
∑ (generic function with 1 method)

julia> ∑(x)
x[1] + x[2] + x[3] + x[4] + x[5]

julia> Symbolics.derivative(100∑(x), x[2])
100
like image 78
Przemyslaw Szufel Avatar answered Jan 01 '26 22:01

Przemyslaw Szufel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!