You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Sébastien Brisard <se...@m4x.org> on 2011/08/13 15:19:03 UTC

[math] A bug in OpenMathRealVector

Hi,
while testing the implementation of UnmodifiableRealVector which has
previously been discussed on Commons Developers, I came across the
following problem. The simple code below

=====
import org.apache.commons.math.linear.OpenMapRealVector;
import org.apache.commons.math.linear.RealVector;

public class DemoBugOpenMapRealVector {
    public static void main(String[] args) {
        final RealVector u = new OpenMapRealVector(3, 1E-6);
        u.setEntry(0, 1.);
        u.setEntry(1, 0.);
        u.setEntry(2, 2.);
        final RealVector v = new OpenMapRealVector(3, 1E-6);
        v.setEntry(0, 0.);
        v.setEntry(1, 3.);
        v.setEntry(2, 0.);
        System.out.println(u);
        System.out.println(v);
        System.out.println(u.ebeMultiply(v));
    }
}

=====

Raises an exception
Exception in thread "main"
org.apache.commons.math.MathRuntimeException$6: map has been modified
while iterating
	at org.apache.commons.math.MathRuntimeException.createConcurrentModificationException(MathRuntimeException.java:373)
	at org.apache.commons.math.util.OpenIntToDoubleHashMap$Iterator.advance(OpenIntToDoubleHashMap.java:564)
	at org.apache.commons.math.linear.OpenMapRealVector.ebeMultiply(OpenMapRealVector.java:372)
	at DemoBugOpenMapRealVector.main(DemoBugOpenMapRealVector.java:16)

I think I misunderstood the use of OpenMapRealVector. Could you help me?
Sebastien

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] A bug in OpenMathRealVector

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hi.

> this is now MATH-645. Apparently, you've already found the origin of
> the bug.

I think so; but I left the issue open in case someone wants to give an
explanation of why this was not detected with the previously existing unit
tests (some of which also exercise "ebeMultiply").


Regards,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] A bug in OpenMathRealVector

Posted by Sébastien Brisard <se...@m4x.org>.
Hi,
this is now MATH-645. Apparently, you've already found the origin of
the bug. If not, I'm happy to help (if it can wait for a few days).
Sébastien

2011/8/13 Gilles Sadowski <gi...@harfang.homelinux.org>:
> Hello.
>
>> while testing the implementation of UnmodifiableRealVector which has
>> previously been discussed on Commons Developers, I came across the
>> following problem. The simple code below
>>
>> =====
>> import org.apache.commons.math.linear.OpenMapRealVector;
>> import org.apache.commons.math.linear.RealVector;
>>
>> public class DemoBugOpenMapRealVector {
>>     public static void main(String[] args) {
>>         final RealVector u = new OpenMapRealVector(3, 1E-6);
>>         u.setEntry(0, 1.);
>>         u.setEntry(1, 0.);
>>         u.setEntry(2, 2.);
>>         final RealVector v = new OpenMapRealVector(3, 1E-6);
>>         v.setEntry(0, 0.);
>>         v.setEntry(1, 3.);
>>         v.setEntry(2, 0.);
>>         System.out.println(u);
>>         System.out.println(v);
>>         System.out.println(u.ebeMultiply(v));
>>     }
>> }
>>
>> =====
>>
>> Raises an exception
>> Exception in thread "main"
>> org.apache.commons.math.MathRuntimeException$6: map has been modified
>> while iterating
>>       at org.apache.commons.math.MathRuntimeException.createConcurrentModificationException(MathRuntimeException.java:373)
>>       at org.apache.commons.math.util.OpenIntToDoubleHashMap$Iterator.advance(OpenIntToDoubleHashMap.java:564)
>>       at org.apache.commons.math.linear.OpenMapRealVector.ebeMultiply(OpenMapRealVector.java:372)
>>       at DemoBugOpenMapRealVector.main(DemoBugOpenMapRealVector.java:16)
>>
>> I think I misunderstood the use of OpenMapRealVector.
>
> :-)
> I rather think that you discovered a bug!
>
> In the code (at lines 369-374 in "OpenMapRealVector"):
> ---CUT---
> OpenMapRealVector res = new OpenMapRealVector(this);
> Iterator iter = res.entries.iterator();
>  while (iter.hasNext()) {
>    iter.advance();
>    res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
> }
> ---CUT---
>
> No exception occurs if the second line in the above excerpt is changed to
> ---CUT---
> Iterator iter = entries.iterator();
> ---CUT---
> (thus not iterating on the "res" vector which is modified concurrently)
>
> I still don't understand why the problem does not show up in the unit tests.
>
>> Could you help me?
>
> Could you open a JIRA ticket?
>
>
> Thanks for *your* help,
> Gilles
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] A bug in OpenMathRealVector

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hello.

> while testing the implementation of UnmodifiableRealVector which has
> previously been discussed on Commons Developers, I came across the
> following problem. The simple code below
> 
> =====
> import org.apache.commons.math.linear.OpenMapRealVector;
> import org.apache.commons.math.linear.RealVector;
> 
> public class DemoBugOpenMapRealVector {
>     public static void main(String[] args) {
>         final RealVector u = new OpenMapRealVector(3, 1E-6);
>         u.setEntry(0, 1.);
>         u.setEntry(1, 0.);
>         u.setEntry(2, 2.);
>         final RealVector v = new OpenMapRealVector(3, 1E-6);
>         v.setEntry(0, 0.);
>         v.setEntry(1, 3.);
>         v.setEntry(2, 0.);
>         System.out.println(u);
>         System.out.println(v);
>         System.out.println(u.ebeMultiply(v));
>     }
> }
> 
> =====
> 
> Raises an exception
> Exception in thread "main"
> org.apache.commons.math.MathRuntimeException$6: map has been modified
> while iterating
> 	at org.apache.commons.math.MathRuntimeException.createConcurrentModificationException(MathRuntimeException.java:373)
> 	at org.apache.commons.math.util.OpenIntToDoubleHashMap$Iterator.advance(OpenIntToDoubleHashMap.java:564)
> 	at org.apache.commons.math.linear.OpenMapRealVector.ebeMultiply(OpenMapRealVector.java:372)
> 	at DemoBugOpenMapRealVector.main(DemoBugOpenMapRealVector.java:16)
> 
> I think I misunderstood the use of OpenMapRealVector.

:-)
I rather think that you discovered a bug!

In the code (at lines 369-374 in "OpenMapRealVector"):
---CUT---
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = res.entries.iterator();
  while (iter.hasNext()) {
    iter.advance();
    res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
---CUT---

No exception occurs if the second line in the above excerpt is changed to
---CUT---
Iterator iter = entries.iterator();
---CUT---
(thus not iterating on the "res" vector which is modified concurrently)

I still don't understand why the problem does not show up in the unit tests.

> Could you help me?

Could you open a JIRA ticket?


Thanks for *your* help,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org