COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

• Email
• Follow

```Hi,

&#931;(k:0->999)&#931;(n:0->999)&#931;(m:0->999) exp(-*(n-m)*k)

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

Cheers,
K
```
 0

See related articles to this posting

```Why don't you post the actual FOR loops instead of the above?

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

```"Matt Fig" <spamanon@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
```
 0

```"keisyu keisyu" <sandalwalk@yahoo.co.jp> wrote in message <hnbnpu\$rlf\$1@fred.mathworks.com>...
> Hi,
>
>
> &#931;(k:0->999)&#931;(n:0->999)&#931;(m:0->999) exp(-*(n-m)*k)
>
> If I use For loop, it takes forever...
>
> Cheers,
> K

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
```
 0

```"Oleg Komarov" <oleg.komarovRemove.this@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 ;-)
```
 0

```On 11 Mar, 22:37, "keisyu keisyu" <sandalw...@yahoo.co.jp> wrote:
> Hi,
>
>
> =CE=A3(k:0->999)=CE=A3(n:0->999)=CE=A3(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
```
 0

```"keisyu keisyu" <sandalwalk@yahoo.co.jp> wrote in message <hnbnpu\$rlf\$1@fred.mathworks.com>...
> Hi,
>
>
> &#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).
```
 0

```keisyu keisyu wrote:

>
> &#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) .
```
 0

```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
```
 0

```"keisyu keisyu" <sandalwalk@yahoo.co.jp> wrote in message <hnc55j\$jq\$1@fred.mathworks.com>...
> 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

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
```
 0

```keisyu keisyu wrote:
> 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

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.
```
 0

```Walter Roberson wrote:
> keisyu keisyu wrote:
>> 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
>
> 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.

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

11 Replies
455 Views

Similar Articles

11/30/2013 7:04:12 PM
page loaded in 42393 ms. (0)