From: Michael on
In thermodynamics we often have to evaluate expressions like the
following:

Sum_i Sum_k (x_i * x_k * A_ik * (1-K_ik)) where A_ik=sqrt(A_i * A_k)

The indices i and k run from 1 to the number of components in a
mixture. For a mixture of 200 components (very typical) we often have
to do this calculation around 1000 times. If one writes two regular DO
loops to perform the calculation the code is very slow. I am
soliciting ideas as what is the optimal way to implement the sum shown
here.
From: steve on
On Apr 7, 8:08 pm, Michael <ma...(a)athenavisual.com> wrote:
> In thermodynamics we often have to evaluate expressions like the
> following:
>
> Sum_i Sum_k (x_i * x_k * A_ik * (1-K_ik))   where A_ik=sqrt(A_i * A_k)
>
> The indices i and k run from 1 to the number of components in a
> mixture. For a mixture of 200 components (very typical) we often have
> to do this calculation around 1000 times. If one writes two regular DO
> loops to perform the calculation the code is very slow. I am
> soliciting ideas as what is the optimal way to implement the sum shown
> here.

Show us your code, so we have at least some idea as to how
you implemented the algorithm.

One possibly obvious answer is to use 4 do-loops.

--
steve
From: glen herrmannsfeldt on
Michael <makis(a)athenavisual.com> wrote:
> In thermodynamics we often have to evaluate expressions like the
> following:

> Sum_i Sum_k (x_i * x_k * A_ik * (1-K_ik)) where A_ik=sqrt(A_i * A_k)

> The indices i and k run from 1 to the number of components in a
> mixture. For a mixture of 200 components (very typical) we often have
> to do this calculation around 1000 times. If one writes two regular DO
> loops to perform the calculation the code is very slow. I am
> soliciting ideas as what is the optimal way to implement the sum shown
> here.

You can do a little better calculating sqrt(A_i) separately,
then multiplying them. That reduces the number of square roots.
You (or your compiler) can factor the x_i*sqrt(A_i) out of the
inner loop. Those will make it a little faster.

Mostly, though, unless you are using a 30 year old computer,
it shouldn't be all that slow.

-- glen
From: James Van Buskirk on
"Michael" <makis(a)athenavisual.com> wrote in message
news:789ec456-00fb-4e17-80be-1d7af7cdbfd3(a)z6g2000yqz.googlegroups.com...

> Sum_i Sum_k (x_i * x_k * A_ik * (1-K_ik)) where A_ik=sqrt(A_i * A_k)

real(kind(x)) B(size(x))
....
B = x*sqrt(abs(A))
your_result = sum(B)**2-dot_product(B,matmul(K,B))

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


From: Gordon Sande on
On 2010-04-08 00:08:46 -0300, Michael <makis(a)athenavisual.com> said:

> In thermodynamics we often have to evaluate expressions like the
> following:
>
> Sum_i Sum_k (x_i * x_k * A_ik * (1-K_ik)) where A_ik=sqrt(A_i * A_k)
>
> The indices i and k run from 1 to the number of components in a
> mixture. For a mixture of 200 components (very typical) we often have
> to do this calculation around 1000 times. If one writes two regular DO
> loops to perform the calculation the code is very slow. I am
> soliciting ideas as what is the optimal way to implement the sum shown
> here.

In n-body calculations there is a similar formal structure of an interaction
between pairs. But the interaction is known to be small for many pairs so
they can be dropped. If you problem has similar structure then you could
use similar approximations. But you have not indicated that this might be
the case beyond indicating that your problem has a physical basis.

This is just a problem specific way of saying that you should be worried
about your algorithms and not concerned with polishing your current code.
Stopping doing really dumb things helps but is not really code polishing.

A_ik=sqrt(A_i*A_k)=sqrt(A_i)*sqrt(A_k) so one might want to use x_i*sqrt(A_i)
insted of your currect expression. That leaves K_ik as the great unkown that
you forgot to describe. Is it a strength of an interaction? Would it allow
terms to be ignored? ...?