Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'failure with Fold' topicStudent Support Forum > General > "failure with Fold"

Next Comment >Help | Reply To Topic
Author Comment/Response
Eduardo Lopez
05/18/12 10:48am

Hello,

I have been using the Fold statement to perform nested sums and have found that sometimes it give erroneous results. I specify an example below where it works and one where it does not work. Can somebody please advice me on how to proceed? This feels very much like a Mathematica bug.

Note: The reason why I use Fold is because I'm doing a calculation in which the number of nested sums is not specified a priori, but emerges as part of the calculation, so I need the number of nested sums to change as parameters of the calculation change.

Here are the examples:

1) Where it fails.
1.1) I want to perform the following sum:

(1/2^3) Sum[
Sum[Sum[(-t1 + t1 (-1 + 2 t1)) (-1 - t2 + t2 (-1 + 2 t2)) (-2 - t3 +
t3 (-1 + 2 t3)), {t3, Max[2, t2], 3}], {t2, Max[2, t1],
3}], {t1, 2, 3}]
When I execute it, I obtain 238. I have also check this by hand, and it is correct.
1.2) When I use fold:
(1/2^3) Fold[
Sum, (-t1 + t1 (-1 + 2 t1)) (-1 - t2 + t2 (-1 + 2 t2)) (-2 - t3 +
t3 (-1 + 2 t3)), {{t3, Max[2, t2], 3}, {t2, Max[2, t1], 3}, {t1,
2, 3}}]
I obtain 6509/45 (incorrect).

2) Case when it works:
(1/2^2) Sum[
Sum[(-t1 + t1 (-1 + 2 t1)) (-1 - t2 + t2 (-1 + 2 t2)), {t2,
Max[2, t1], 3}], {t1, 2, 3}]
and
(1/2^2) Fold[
Sum, (-t1 + t1 (-1 + 2 t1)) (-1 - t2 + t2 (-1 + 2 t2)), {{t2,
Max[2, t1], 3}, {t1, 2, 3}}]
give the same result of 47.

I assume Mathematica is trying to do some kind of shortcut or something, but I don't think this matters. Other summands work for both fold and the direct "by hand" approach of writing out the sum. Obviously, I need to explore a range of calculations and judging by this, I'm not sure when the calculations are going to fail.

Please point me in the right direction on how to proceed with this. Best Regards

URL: ,

Subject (listing for 'failure with Fold')
Author Date Posted
failure with Fold Eduardo Lopez 05/18/12 10:48am
Re: failure with Fold Bill Simpson 05/21/12 10:11pm
Re: failure with Fold yehuda 05/21/12 10:27pm
Re: Re: failure with Fold Eduardo Lopez 05/22/12 11:12am
Next Comment >Help | Reply To Topic