# Could double summation be implemented without JuMP?

**URL:** <https://discourse.julialang.org/t/could-double-summation-be-implemented-without-jump/102785>\
**Category:** General Usage\
**Created:** [August 14, 2023, 9:22am UTC](https://discourse.julialang.org/t/could-double-summation-be-implemented-without-jump/102785 "2023-08-14T09:22:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MKY](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@MKY](https://discourse.julialang.org/u/MKY)\
**Post date:** [August 14, 2023, 9:22am UTC](https://discourse.julialang.org/t/could-double-summation-be-implemented-without-jump/102785/1 "2023-08-14T09:22:37Z")

</div>

Hello, I encounter a problem when trying to convert these(Python) to Julia:

```julia
delta_TRC = [sum((i - TRC)**2) for i in TRC]
        return sum(delta_TRC)

```

I found that the syntax of sum function provided by JuMP is very similar to these, but it is possible to implement only with the standard function ( whose syntax: sum(f, itr; [init]) )?  
I am new to both Python and Julia, so I would appreciate if you explain the issue in detail…  
Thank you.  
The following is for reference, if needed:

```julia
from scipy.optimize import minimize 
class Portfolio:
    def __init__ (self,port_a,port_b,port_c,port_d):    
        self.port_a=port_a
        self.port_b=port_b
        self.port_c=port_c
        self.port_d=port_d

        self.df=data[["Date",port_a,port_b,port_c,port_d]].dropna().reset_index(drop=True)
        ret=self.df[[port_a,port_b,port_c,port_d]]
        self.Rcov=ret.cov()
        self.cov=np.array(ret.cov())     

    def risk_budget_objective(self,weights,cov):
        weights = np.array(weights) #one-dimension array
        sigma = np.sqrt(np.dot(weights, np.dot(cov, weights)))  
        MRC = np.dot(cov,weights)/sigma  
        TRC = weights * MRC
        delta_TRC = [sum((i - TRC)**2) for i in TRC]
        return sum(delta_TRC)

```

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [August 14, 2023, 1:52pm UTC](https://discourse.julialang.org/t/could-double-summation-be-implemented-without-jump/102785/2 "2023-08-14T13:52:23Z")

</div>

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](https://docs.julialang.org/en/v1/manual/mathematical-operations/#man-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
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
julia> sum(sum((i .- TRC).^2) for i in TRC)
14.520000000000001

```
