My fault, I just wnat to add each element of array a and arry b, here the example is too simple, actually, I also need to make some complex calculation for each element of array a and array b and then sum them togher with for loop
julia> some_complex_calculation(x) = x + sin(x)
some_complex_calculation (generic function with 1 method)
julia> a = [1,2,3,4,5]; b = [6,7,8,9,10]; # same length (and comas, if these are column-vectors)
julia> c = some_complex_calculation.(a) .+ some_complex_calculation.(b) # note the dots
My initial plan is doing this, but this will take on a lot of memory allocation, so I plan to use for loop to expand the equations, and add them with for loop and it works better, but now, I have to add two formulas with different array length together(add each element of each formulas together), it’s easy to realize it with dot, but not easy with for loop since their length is not the same, that’s why I ask this question, it’s also hard to explain clearly my question, thanks anyway
You can just have y = dot(d,e) before the loop, or y += dot(d,e) after. You want to iterate over these two things one after the other, that’s what the code with two dots does. Your first post takes the product of the iteration spaces, you want the union.
This (edited to make capitals!) could be
mapreduce(+, A, B, C) do a,b,c
a' * b * exp(c+2)
but unfortunately that’ll be no faster at the moment, as it just calls map which materialises the array before summing.
and both the first part and the second part will cause allocation, remember here is a simple example, in reality both parts are very long equations with temperory allocation, but without array calculation ( like .+, .-), it will be faster, and that’s why I want for loop
I think there are some misconceptions there about what allocates and what does not, but it is hard to tell without a more realistic example. Equations being long or short, being many or a few, do not really relate to the issues.
Or maybe you can see this equation, this is part of my program. Let’s see dy, it includes three dot calculation, the first one is for 5 element array, the second and the third dot calculation is for 3 element array. My initial formula doesn’t have the second and the third dot, so I just write it like this