RE: bug in plus method ?
>===== Original Message From jama@nist.gov =====
>I've been using JAMA to build statistical models which iterate
>through a method and add the resulting matrix (Rmatrix) from each
>iteration to an overall matrix (Omatrix) at the end of each iteration.
>
>
>At the end of the first iteration
>
>if (iteration ==0)
>
>Omatrix=Rmatrix;
>
>and for subsequent iterations
>
>else
>
>Omatrix=Omatrix.plus(Rmatrix);
>
(My appologies if this has already been answered...)
This appears to be a Java issue, rather than a JAMA issue. I believe the
problem lies in the assignment "Omatrix=Rmatrix" and in the manner in which
Java manages objects: everything in Java is done by reference, rather than by
value. This is to say, when you do assignments, you do not actually duplicate
the object, but rather you give the same object two "handles" -- the two names
just happen to point to the same matrix, so if you modify one, you are
modifying the other. If you are familiar with C or C++ think of it as being
the same as copying a pointer.
So what happens in iteration zero is that Omatrix and Rmatrix are made to both
point to the same matrix. Then, in the second iteration, the Omatrix (now just
another name for Rmatrix) is added to itself and whatever used to be in
Rmatrix is destroyed. Subsequent iterations would continue to double the
matrix, *unless* you reassign Rmatrix to point to another matrix. What is
probably catching you by surprize is that Rmatrix is being modified, whereas
you intended for it not to be (try to print the values of Rmatrix after the
loop finishes, you will see that Omatrix and Rmatrix are one and the same).
Anyhow, what you probably intended is this:
if( iteration == 0
Omatrix = Rmatrix.clone(); // Create a new copy of Rmatrix.
else
Omatrix = Omatrix.add(Rmatrix);
Hope this helps,
Marcio Luis Teixeira
Date Index |
Thread Index |
Problems or questions? Contact list-master@nist.gov