Could double summation be implemented without JuMP?

Hello and welcome to Julia!

In short, the answer is: yes, you can do the same thing in base Julia than what Python+Numpy allows, with almost the same syntax.

The only syntactic difference lies in sum((TRC - i)**2). In this expression, i is a number (a scalar), TRC is an array of such numbers, and in this context the computation is supposed to mean:

  • tmp1 = TRC - i build the array obtained by subtracting the number i to every element of TRC
  • tmp2 = tmp1**2 build the array obtained by squaring tmp1 elementwise
  • sum(tmp2) compute the sum of all elements in tmp2

You see here that the arithmetic operations used to build tmp1 and tmp2 above are to be understood as elementwise operations; they are not simple operations between scalar numbers. This is where languages differ:

  • plain python lists do not allow this
    • [1,2,3] - 1 is an error
  • for numpy arrays, arithmetic operations are implicitly considered to be elementwise
    • numpy.array([1, 2, 3]) - 1 == numpy.array([0, 1, 2])
  • Julia allows to do this without any library, but you need to be more explicit by using dot operators
    • [1,2,3] .- 1 == [0,1,2]

Apart from the need to “dot” the operators (and the fact that power is denoted by ^ in Julia), the same syntax is supported:

julia> TRC = [1.0, 2.1, 3.2]
3-element Vector{Float64}:
 1.0
 2.1
 3.2

julia> delta_TRC = [sum((i .- TRC).^2) for i in TRC]
3-element Vector{Float64}:
 6.050000000000001
 2.4200000000000004
 6.050000000000001

julia> sum(delta_TRC)
14.520000000000001

You can even write the double summation in one expression, which could be more performant:

julia> sum(sum((i .- TRC).^2) for i in TRC)
14.520000000000001
3 Likes