Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

instead of For loop

0 views
Skip to first unread message

keisyu keisyu

unread,
Mar 11, 2010, 4:37:02 PM3/11/10
to
Hi,

Please advise how to program following equation without For loop

Σ(k:0->999)Σ(n:0->999)Σ(m:0->999) exp(-*(n-m)*k)

If I use For loop, it takes forever...

Cheers,
K

Matt Fig

unread,
Mar 11, 2010, 5:01:08 PM3/11/10
to
Why don't you post the actual FOR loops instead of the above?

What does this mean: -*(n-m)

Oleg Komarov

unread,
Mar 11, 2010, 5:09:05 PM3/11/10
to
"Matt Fig" <spam...@yahoo.com> wrote in message <hnbp74$rjm$1...@fred.mathworks.com>...

> Why don't you post the actual FOR loops instead of the above?
>
> What does this mean: -*(n-m)

I interpret it as -1*(n-m). I implemente it as:

Out = sum(exp(reshape(-bsxfun(@times,bsxfun(@minus, (0:999).',0:999),0:999),[],1)));

which gives inf...

I'm sure it's some form of note matrix...but I'm not a matematician.

Oleg

James Tursa

unread,
Mar 11, 2010, 5:19:05 PM3/11/10
to
"keisyu keisyu" <sanda...@yahoo.co.jp> wrote in message <hnbnpu$rlf$1...@fred.mathworks.com>...

Well, one of the terms look like exp(-(0-999)*999) = exp(999*999) = inf.
So here is how to calculate the entire sum without any for loops:

inf

What are you doing?

James Tursa

Matt Fig

unread,
Mar 11, 2010, 5:19:05 PM3/11/10
to
"Oleg Komarov" <oleg.komaro...@hotmail.it> wrote in message
> I interpret it as -1*(n-m). I implemente it as:
>
> Out = sum(exp(reshape(-bsxfun(@times,bsxfun(@minus, (0:999).',0:999),0:999),[],1)));
>
> which gives inf...
>
> I'm sure it's some form of note matrix...but I'm not a matematician.
>
> Oleg


That is why I requested the actual code. If the OP *doesn't* get inf, then what was posted above in non-MATLAB notation is incorrect or something else is wrong. The OP claimed that the FOR loops were taking a long time, which seems to imply that there was some value being produced which was not inf. I guess we shall see ;-)

Rune Allnor

unread,
Mar 11, 2010, 5:20:43 PM3/11/10
to
On 11 Mar, 22:37, "keisyu keisyu" <sandalw...@yahoo.co.jp> wrote:
> Hi,
>
> Please advise how to program following equation without For loop
>
> Σ(k:0->999)Σ(n:0->999)Σ(m:0->999) exp(-*(n-m)*k)

>
> If I use For loop, it takes forever...

You *will* have to use a for loop. You might hide
that fact behind the voodoo of 'vectorization',
but the for-loop will be there, nonetheless.

Apart from that, depending on the details that
are missing (the '-*' somebody already commented on)
you might be able to simplify the expression a bit,
possibly being able to exploit symmetries etc.

Rune

Matt J

unread,
Mar 11, 2010, 5:48:02 PM3/11/10
to
"keisyu keisyu" <sanda...@yahoo.co.jp> wrote in message <hnbnpu$rlf$1...@fred.mathworks.com>...
> Hi,
>
> Please advise how to program following equation without For loop
>
> &#931;(k:0->999)&#931;(n:0->999)&#931;(m:0->999) exp(-*(n-m)*k)
>

Are you sure the sum isn't supposed to range over positive n-m?
Otherwise, you're inevitably going to get inf as the result (as Oleg did in his version of the calculation).

Walter Roberson

unread,
Mar 11, 2010, 6:36:50 PM3/11/10
to
keisyu keisyu wrote:

> Please advise how to program following equation without For loop
>
> &#931;(k:0->999)&#931;(n:0->999)&#931;(m:0->999) exp(-*(n-m)*k)

You appear to be missing a character or expression inside the exp(), between
the - and the * . If it is a simple expression such as the constant 2, then
you are not going to be able to calculate that sum except symbolically, as it
would involve adding numbers that range from exp(-1996002) to exp(1996002) .

keisyu keisyu

unread,
Mar 11, 2010, 8:25:07 PM3/11/10
to
Hi,

Sorry, my actual target equation is too complicated to ask.. So, I wanted to simplify....
How about below ? The point is , I want to sum up by "k", "m", "n".

&#931;(k:1->999)&#931;(n:1->999)&#931;(m:1->999) (n-m)/k

Cheers,
K

James Tursa

unread,
Mar 11, 2010, 9:09:05 PM3/11/10
to
"keisyu keisyu" <sanda...@yahoo.co.jp> wrote in message <hnc55j$jq$1...@fred.mathworks.com>...

But that is useless information. We can't advise you how to vectorize or otherwise get rid of the for loops without seeing the function you are summing. Maybe there are some vectorized functions you can use, or maybe some custom C mex code will work, or maybe ... ??? You haven't given us anything to go on except apparently non-applicable posts.

James Tursa

Walter Roberson

unread,
Mar 11, 2010, 9:28:03 PM3/11/10
to

Use symmetry. Because n and m are independant and have the same range,
for each n>m generating a positive result for k, there is a
corresponding n<m generating the exact negative of the value. And of
course when n == m, the result will be zero, for no net contribution.
Thus, the result of the triple summation will be 0, provided that k is
not allowed to be 0.

Walter Roberson

unread,
Mar 12, 2010, 4:42:13 AM3/12/10
to

In the more general case, there is not necessarily any good manual
method of simplifying the summation. It can help a lot to have a
symbolic mathematics package.

For example, suppose the inner sum is exp(-(n-m)/k)

maple> simplify(sum(sum(sum(exp(-(n-m)/k),m=a..b),n=c..d),k=e..f))
assuming a>=0,a<=b, c>=0,c<=d, e>=0,e<=f;


-sum(exp(1/k)*(exp((-d+b)/k)-exp((-d-1+a)/k)-exp((-c+b+1)/k)+exp((-c+a)/k))/(-\
1+exp(1/k))^2,k = e .. f)

maple> simplify(eval(%,[a=0,b=999,c=0,d=999]));

sum((-2+exp(-1000/k)+exp(1000/k))/(-1+exp(1/k))^2*exp(1/k),k = e .. f)

But that's as far as maple can take it symbolically. If you substitute
in particular upper and lower bounds, maple will construct the addition,
a whole bunch of terms of the form
(-2+exp(-1000/19)+exp(1000/19))/(-1+exp(1/19))^2*exp(1/19)

[The sum will come out as about 1.8E434 for any reasonable upper bound
you are likely to compute, but it does turn out that this sum goes to
infinity -- an infinite number of additions of values whose lower limit
is 1000000.]

There is no general closed-form formula that covers all summations, for
much the same reasons that there is no general closed-form formula that
covers all integrations.

You can _usually_ reduce a triple summation in which each iteration
produces a single value, with a vectorized double summation -- which
might simply have the effect of reducing the number of function calls.
It depends on the calculation: some kinds of calculations can be done by
a highly-optimized compiled vectorized library, but the library might
only be invoked for "sufficiently big" vectors (e.g, 5000 or 10000) due
to the overhead of moving the data around into the format needed by the
libraries.

0 new messages