Tensor product inside function

So I have the following code

function CC_trap(X::Array{Float64,2}, tag::Int64)
    a = 0.0

    for i=1:size(X)[1]
      	    for j=1:size(X)[2]

		    global a += weights_CC[i]*X[i,j]
	    end
    end

    a = result_integral*Parameters.dv[tag]

end

But I’m getting the error
syntax: global a: a is a local variable in its enclosing scope

What would be the proper Julia way of doing this?

There are global variables in your code. This is the way to make the function compile.

function CC_trap(X::Array{Float64,2}, tag::Int64)
    a = 0.0
    global weights_CC
    global Parameters
    for i=1:size(X)[1]
        for j=1:size(X)[2]
            a += weights_CC[i]*X[i,j]
        end
    end
    a = result_integral*Parameters.dv[tag]
end

However, I have some doubts about using global variables in the first place. They rarely lead to efficient (type-stable) code. Usually they can be avoided, and it is worthwhile to think about ways in which that could be done. It usually leads to a better design.

1 Like