HI Luc, Thanks for your explanation. Now I understand the problem. I thought the 'equals' method is the one from Object.equals instead of AbstractRealMatrix.equals. But, now I think you are right that I used AbstractRealMatrix.equals since it could generate 'true' value in the first problem. Otherwise Object.equals method should generate 'false' in both cases.
Best Regards, Kosumo On Mon, Jun 7, 2010 at 1:37 PM, <luc.maison...@free.fr> wrote: > > ----- "Kosumo" <mas.kos...@gmail.com> a écrit : > > > Dear all, > > > > I got confused with the "equals" method applied to OpenMapRealMatrix > > instance. > > I have this following code: > > > > RealMatrix raw = vectorGenerator.getMatrix(); > > RealMatrix test = > > TestHelper.parseMatrixFromFile("resources/raw_matrix.txt"); > > > > both the raw and test matrices are the instance of OpenMapRealMatrix. > > The > > test matrix is generated by reading the matrix value from a text > > file. > > Both of them are intended to have the same value as follow: > > > > 0.0000 0.0000 1.0000 1.0000 0.0000 0.0000 0.0000 0.0000 > > 0.0000 > > 0.0000 0.0000 0.0000 0.0000 0.0000 1.0000 0.0000 0.0000 > > 1.0000 > > 0.0000 1.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1.0000 > > 0.0000 > > 0.0000 1.0000 0.0000 0.0000 0.0000 0.0000 1.0000 0.0000 > > 0.0000 > > 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1.0000 0.0000 > > 1.0000 > > 1.0000 0.0000 0.0000 0.0000 0.0000 1.0000 0.0000 0.0000 > > 0.0000 > > 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 > > 1.0000 > > 1.0000 0.0000 1.0000 1.0000 0.0000 0.0000 0.0000 0.0000 > > 0.0000 > > 1.0000 0.0000 1.0000 0.0000 0.0000 0.0000 0.0000 0.0000 > > 0.0000 > > 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1.0000 0.0000 > > 1.0000 > > 0.0000 0.0000 0.0000 0.0000 0.0000 2.0000 0.0000 0.0000 > > 1.0000 > > 1.0000 0.0000 1.0000 0.0000 0.0000 0.0000 0.0000 1.0000 > > 0.0000 > > 0.0000 0.0000 0.0000 1.0000 1.0000 0.0000 0.0000 0.0000 > > 0.0000 > > > > I want to test if they really have the same value. I tried to use > > "equals" > > method, Eclipse indicates that 'equals' is method from Object class. > > It should not. There is an equals method in AbstractRealMatrix overriding > the method from Object. Of course, this method still takes an Object as > a parameter, as specified in the top level method. > > This implementation of the equals method compares all elements with a loop > using the "!=" operator applied tou primitive double values. > > > I suppose it will generate false since both matrices are not the same > > object. However when I execute this statement: > > > > raw.equals(test) > > > > it generate "true". > > Since all numbers here seem to be exact integer values and since integer > values > lower than 10^15 can be represented exactly in IEEE754 double, the true > result is > expected. > > > > > In other case where the value of both matrix: > > 0.0000 0.0000 0.1306 0.1633 0.0000 0.0000 0.0000 0.0000 > > 0.0000 > > 0.0000 0.0000 0.0000 0.0000 0.0000 0.1306 0.0000 0.0000 > > 0.1306 > > 0.0000 0.2177 0.0000 0.0000 0.0000 0.0000 0.0000 0.2177 > > 0.0000 > > 0.0000 0.2177 0.0000 0.0000 0.0000 0.0000 0.1633 0.0000 > > 0.0000 > > 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1633 0.0000 > > 0.1306 > > 0.1306 0.0000 0.0000 0.0000 0.0000 0.1306 0.0000 0.0000 > > 0.0000 > > 0.0000 0.0171 0.0102 0.0128 0.0256 0.0102 0.0128 0.0171 > > 0.0102 > > 0.0954 0.0000 0.0954 0.1193 0.0000 0.0000 0.0000 0.0000 > > 0.0000 > > 0.1306 0.0000 0.1306 0.0000 0.0000 0.0000 0.0000 0.0000 > > 0.0000 > > 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1633 0.0000 > > 0.1306 > > 0.0000 0.0000 0.0000 0.0000 0.0000 0.2613 0.0000 0.0000 > > 0.1306 > > 0.0954 0.0000 0.0954 0.0000 0.0000 0.0000 0.0000 0.1590 > > 0.0000 > > 0.0000 0.0000 0.0000 0.1633 0.3266 0.0000 0.0000 0.0000 > > 0.0000 > > > > the above statement give me "false" value. > > I think the "false" result is also normal. The numbers generated from > your vectorGenerator are probably different from the ones read in the > file, due to limited precision. If for example the numbers are written > the file as in this message (i.e. with four decimal places), then the > difference may go up to 5.0e-5. Increasing the accuracy in the file may > help but really opens a can of worms because some numbers which can be > represented perfectly in decimal (like 0.1) cannot be stored perfectly > in memory (in the IEEE754 format), so you would have to make sure you > use enough decimal places (16 is a good start) in the text representation > and hope the conversion operations that occurs at write and read time are > accurate enough ... > > When double values are used in a computer, equality shoud be used > with caution. > > If you intend to make sure two matrices are "close enough", a better way > is to use a check like: > > if (m1.subtract(m2).getNorm() < threshold) { > // the matrices m1 and m2 are close > } > > The value of the threshold to use depend on the values stored in your > matrices. > If for example all matrix elements are between 0.0 and 1.0, you could use > something like 100 * MathUtils.EPSILON * m1.getColumndimension(). > > Luc > > > > > Can anyone explain about this? I tried to look at the API. I found > > that OpenMapRealMatrix is subclass of AbstractRealMatrix. > > AbstractRealMatrix has an 'equals' method, but I can't access it > > since > > Eclipse still indicate that the 'equals' method is the one from > > Object > > class. > > How can I use 'equals' method from AbstractRealMatrix? Is it possible > > to use > > it for comparing value from two matrices? > > Thanks in advance, > > > > Kosumo > > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscr...@commons.apache.org > For additional commands, e-mail: user-h...@commons.apache.org > >