[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
for loop implementation
From: |
John W. Eaton |
Subject: |
for loop implementation |
Date: |
Tue, 12 Feb 2008 02:44:59 -0500 |
On 11-Feb-2008, EI wrote:
| Hello,
|
| I was modifying "for" loop implementation in Freemat and realized that
| neither Freemat 3.5 nor Octave 3.0 match the behavior of "for" loops in
| matlab.
|
| If you run the script below you will see that the first loop performs 20
| iterations in matlab, but only 19 in Octave. "Loop 2" below performs 20
| iterations in both systems.
|
| For the second test you should change "if 1" to "if 0". Plot "for loop vs.
| my implementation" In matlab is constant zero. However, in Octave the plot
| shows the difference of more than 20*eps.
|
| A side note, in matlab "for loop vs. my implementation" is constant zero,
| but "vectorized vs. my implementation" shows difference of up to 4*eps. This
| means that "k=[first:step:last]" and "for k=first:step:last" use different
| algorithms and produce different results!!!!
In Octave, we were using
base + i * increment
to compute the values when doing something like this:
[ base:increment:limit ]
which converts a range (stored as base, increment, and limit values
only) to an array with all values expanded, but the for loop
implmentation was using
tmp = base;
for (i = 0; i < nelem; i++, tmp += increment)
...
which obviously fails for cases like this when base + increment == base.
I've fixed this problem so both calculations are performed using
multiplication. I think this change fixes the precision problem you
see.
| Compatible "for" loop implementation will go in the next release of Freemat,
| and Freemat already has almost correct implementation of
| "k=[first:step:last]" construct (a fairly complicated algorithm).
Would you care to share the code for this?
Octave already goes to some lengths to get the number of elements
right.
Thanks,
jwe