haifengl / smile

Statistical Machine Intelligence & Learning Engine
https://haifengl.github.io
Other
6.01k stars 1.12k forks source link

"Array Index Out Of Bounds Exception" when you run KrigingInterpolation #288

Closed ViaSacra closed 6 years ago

ViaSacra commented 6 years ago

Hello, I'm trying to run KrigingInterpolation, but when I create a class object, it always flies out of the array in the get () method. To start, I use the data from the test script. The error occurs here, in the solve () method:

  // Copy right hand side with pivoting
            for (int j = 0; j < nrhs; j++) {
                for (int i = 0; i < m; i++) {
                    X.set(i, j, B.get(piv[i], j));
                }
            }

LAPACK returns an array of piv [] with values and get (), but these values exceed the scope of the get () array

@Override
    public double get(int i, int j) {
        return A[j*nrows + i];
    }

Here is the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at ru.msu.cmc.sp.kriging.JMatrix.get(JMatrix.java:137)
    at ru.msu.cmc.sp.kriging.LU.solve(LU.java:136)
    at ru.msu.cmc.sp.kriging.LU.solve(LU.java:110)
    at ru.msu.cmc.sp.kriging.KrigingInterpolation.<init>(Kriging.java:63)
    at ru.msu.cmc.sp.kriging.KrigingInterpolation.<init>(Kriging.java:19)
    at ru.msu.cmc.sp.kriging.KrigingInterpolation.test(KrigingTest.java:131)

Here is the test I use:

     public void test() {
        double[][] x = {{0, 0}, {1, 1}};
        double[] y = {0, 1};
        Kriging instance = new Kriging(x, y);
        double[] x1 = {0.5, 0.5};
        assertEquals(0, instance.interpolate(x[0]), 1E-7);
        assertEquals(1, instance.interpolate(x[1]), 1E-7);
        assertEquals(0.5, instance.interpolate(x1), 1E-7);
     }
}

For correct work LAPACK I did:

  1. I downloaded the stable version of jar files from https://spark.apache.org/downloads.html and uploaded them to the project.
  2. Through MinGW installed a stable version of GFortran, and added absolute paths to Path.
  3. I threw all * .dll in system32 from the project https://github.com/haifengl/smile/bl...rpolation.java
  4. In the Debug Config settings, added the following lines to the VM arguments:
    -Dcom.github.fommil.netlib.BLAS=com.github.fommil.netlib.F2jBLAS
    -Dcom.github.fommil.netlib.LAPACK=com.github.fommil.netlib.F2jLAPACK
    -Dcom.github.fommil.netlib.ARPACK=com.github.fommil.netlib.F2jARPACK
    -Dcom.github.fommil.netlib.NativeSystemBLAS.natives=netlib-native_system-myos-myarch.so

Help please, I really need KrigingInterpolation!

ViaSacra commented 6 years ago

Thank you in advance, the project has successfully earned. I created pom.xml and added the following:

<dependencies>
        <dependency>
            <groupId>com.github.haifengl</groupId>
            <artifactId>smile-core</artifactId>
            <version>1.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.github.haifengl</groupId>
            <artifactId>smile-interpolation</artifactId>
            <version>1.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.github.fommil.netlib</groupId>
            <artifactId>all</artifactId>
            <version>1.1.2</version>
            <type>pom</type>
        </dependency>
        <dependency> 
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.13</version>
        </dependency>
        <dependency> 
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.8.0-beta1</version>
        </dependency>
        <dependency> 
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.8.0-beta1</version>
        </dependency>
   </dependencies>
haifengl commented 6 years ago

So it is okay with netlib but fails with JMatrix? BTW, what's ru.msu.cmc.sp in your error trace?

    at ru.msu.cmc.sp.kriging.JMatrix.get(JMatrix.java:137)
    at ru.msu.cmc.sp.kriging.LU.solve(LU.java:136)
    at ru.msu.cmc.sp.kriging.LU.solve(LU.java:110)
    at ru.msu.cmc.sp.kriging.KrigingInterpolation.<init>(Kriging.java:63)
    at ru.msu.cmc.sp.kriging.KrigingInterpolation.<init>(Kriging.java:19)
    at ru.msu.cmc.sp.kriging.KrigingInterpolation.test(KrigingTest.java:131)
ViaSacra commented 6 years ago

This is a package. The problem was in bad jar, so LAPACK produced incorrect calculations and the matrix was flying out of bounds. After creating Maven and connecting libs through it all works correctly. The only warning that is present is: INFO Factory - netlib module is not available in the classpath. Pure Java matrix library will be employed. Can this affect the correctness of the calculations and how can I fix it?

haifengl commented 6 years ago

What's your environment? OS? JDK?