简体   繁体   English

需要帮助调试Java代码(EJML库)

[英]Need help debugging Java code (EJML library)

Can someone help me find the bug in the following code. 有人可以帮我找到以下代码中的错误。 I receive a Java exception, but I have no idea why it occurs. 我收到一个Java异常,但不知道为什么会发生。 Thanks for your attention! 感谢您的关注! The description of the exception is as follows: 异常的说明如下:

>    Exception in thread "main" java.lang.ExceptionInInitializerError
>    Caused by: java.lang.ArrayIndexOutOfBoundsException
>       at java.lang.System.arraycopy(Native Method)
>       at org.ejml.data.DenseMatrix64F.set(Unknown Source)
>       at org.ejml.data.DenseMatrix64F.<init>(Unknown Source)
>       at org.ejml.simple.SimpleMatrix.<init>(Unknown Source)
>       at test1.<clinit>(test1.java:14)

for this code: 对于此代码:

    import org.ejml.simple.SimpleMatrix;
    import java.lang.Math;

    import java.util.Scanner;

    public class test1 {
        class testcase3 {
            public testcase3() {
                Mkt = 100000;
                E1name = new SimpleMatrix(2, 5, true, -10, -5, 0, 5, 10, 1, 2, 3,
                        4, 5);
                E1name2 = new SimpleMatrix(2, 5, true, 1, 2, 3, 4, 5, -10, -5, 0,
                        5, 10);
                E2name = new SimpleMatrix(2, 2, true, 1, 3, 1, 2);
                E2name2 = new SimpleMatrix(2, 2, true, 1, 2, 1, 3);
                EM1 = new SimpleMatrix(2, 5, true, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
                EM2 = new SimpleMatrix(2, 2, true, 0, 0, 0, 0);
                T = new SimpleMatrix(2, 2, true, 0, 0, 0, 0);
                Pa = new SimpleMatrix(1, 2, true, 0, 0);
                Position = 0;
            }

            public double Mkt;
            public SimpleMatrix E1name;
            public SimpleMatrix E2name;
            public SimpleMatrix E1name2;
            public SimpleMatrix E2name2;
            public SimpleMatrix EM1;
            public SimpleMatrix EM2;
            public SimpleMatrix T;
            public SimpleMatrix Pa;
            public int Position;

            public int newBidAsk(double bidPrice, double askPrice) {
                // log("I received a new Top-of-Book update, bid=" + bidPrice +
                // ", offer=" + askPrice);

                // data transformation
                double newMkt = (bidPrice + askPrice) / 2;
                double Grow = newMkt - Mkt;
                double Spread = (askPrice - bidPrice) / 2;

                SimpleMatrix seq = new SimpleMatrix(1, 2, true, Grow, Spread);
                SimpleMatrix z = new SimpleMatrix(1, 2, true, 0, 0);

                z.set(0, 0, Nametrans(seq.getIndex(0, 0), E1name));
                z.set(0, 1, Nametrans(seq.getIndex(0, 1), E2name));
                int act = 0;

                // parameter updating
                SimpleMatrix Px = Hmmupdate(Pa, z, EM1, EM2);
                SimpleMatrix newPa = Hmmpredict(Px, T);
                SimpleMatrix E1n = Hmmempredict(newPa, EM1, EM2, 5);
                SimpleMatrix E2n = Hmmempredict(newPa, EM1, EM2, 2);
                SimpleMatrix E = Eab(newMkt, E1n, E2n, E1name2, E2name2);
                int E1 = E.getIndex(0, 0);
                int E2 = E.getIndex(0, 1);

                // action
                if (Position == 0) {
                    int L = (int) Math.max(0,
                            Math.max(E1 - askPrice, bidPrice - E2));
                    switch (L) {
                    case 1:
                        act = 0;
                        break;
                    case 2:
                        act = 1;
                        break;
                    case 3:
                        act = -1;
                        break;
                    }
                } else if (Position == -1) {
                    int L = (int) Math.max(-askPrice, -E1);
                    switch (L) {
                    case 1:
                        act = 1;
                        break;
                    case 2:
                        act = 0;
                        break;
                    }
                } else if (Position == 1) {
                    int L = (int) Math.max(bidPrice, E2);
                    switch (L) {
                    case 1:
                        act = -1;
                        break;
                    case 2:
                        act = 0;
                        break;
                    }
                }

                Position = act + Position;
                // log("The act is " + Position);
                return Position;
            }

            public SimpleMatrix Eab(double Mkt, SimpleMatrix E1, SimpleMatrix E2,
                    SimpleMatrix E1name2, SimpleMatrix E2name2) {
                SimpleMatrix Easkbid = new SimpleMatrix(1, 2, true, Mkt, Mkt);
                for (int i = 0; i < 5; i++) {
                    for (int j = 0; j < 2; j++) {
                        Easkbid.set(0, 0,
                                Easkbid.getIndex(0, 0) + (Nametrans(i, E1name2))
                                        * E1.getIndex(0, i) * E2.getIndex(0, j));
                        Easkbid.set(0, 1,
                                Easkbid.getIndex(0, 1) + (Nametrans(i, E2name2))
                                        * E1.getIndex(0, i) * E2.getIndex(0, j));
                    }
                }
                return Easkbid;
            }

            public SimpleMatrix Hmmempredict(SimpleMatrix Pxkla, SimpleMatrix EM1,
                    SimpleMatrix EM2, int length) {
                SimpleMatrix E = new SimpleMatrix(1, length, true);
                for (int i = 0; i < 2; i++) {
                    for (int j = 0; j < length; j++) {
                        E.set(1,
                                j,
                                E.getIndex(1, j) + EM2.getIndex(i, j)
                                        * Pxkla.getIndex(1, i));
                    }
                }
                E = normalize(E);
                return E;
            }

            public SimpleMatrix Hmmpredict(SimpleMatrix Pxk, SimpleMatrix T) {
                SimpleMatrix Pxkla = new SimpleMatrix(1, 2, true, 0, 0);
                for (int i = 0; i < 2; i++) {
                    for (int j = 0; j < 2; j++) {
                        Pxkla.set(0, i, Pxkla.getIndex(0, i) + Pxkla.getIndex(0, j)
                                * T.getIndex(j, i));
                    }
                }
                Pxkla = normalize(Pxkla);
                return Pxkla;
            }

            public SimpleMatrix Hmmupdate(SimpleMatrix Pxka, SimpleMatrix zk,
                    SimpleMatrix EM1, SimpleMatrix EM2) {
                SimpleMatrix Pxk = new SimpleMatrix(1, 2, true, 0, 0);
                for (int i = 0; i < 2; i++) {
                    Pxk.set(0,
                            i,
                            (Pxka.getIndex(0, i)
                                    * EM1.getIndex(i, zk.getIndex(0, 0)) * EM2
                                    .getIndex(i, zk.getIndex(0, 1))));
                }
                Pxk = normalize(Pxk);
                return Pxk;
            }

            public SimpleMatrix normalize(SimpleMatrix B) {
                double temp = 0;
                for (int i = 0; i < B.numCols(); i++) {
                    temp = B.getIndex(0, i);
                }
                SimpleMatrix A = B;
                for (int j = 0; j < A.numCols(); j++) {
                    A.set(0, j, B.getIndex(0, j) / temp);
                }
                return A;
            }

            public double Nametrans(double a, SimpleMatrix Nama) {
                int n = Nama.numCols();
                double b = 0;
                for (int i = 0; i < n; i++) {
                    if (a == Nama.getIndex(0, i)) {
                        b = Nama.getIndex(1, i);
                    }
                }
                return b;
            }
        }

        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            double bid = s.nextDouble();
            double ask = s.nextDouble();
            testcase3 tc3;
            int act = tc3.newBidAsk(bid, ask);
            System.out.printf("%d", act);

        }
    }

Stack traces are great, they tell you the very line - or the very line after - an error occurs. 堆栈跟踪很棒,它们告诉您-或之后的一行-发生错误。 The only thing hinky I can see about your code is possible the negative numbers; 关于您的代码,我唯一能看到的奇数可能是负数。 try making your matrix without them and see if that clears up the error. 尝试在没有它们的情况下制作矩阵,看看是否可以清除错误。 If it does, you could try passing real doubles there instead of integers that have to be converted by the runtime. 如果是这样,您可以尝试在其中传递实数双精度数,而不是运行时必须转换的整数。

And, if it does, the library writers should have mentioned it in their javadoc, or perhaps you've discovered a (horrors!) major bug in their code. 而且,如果确实如此,则库编写者应该在其javadoc中提到它,或者您可能已经在其代码中发现了一个(严重的错误)重大错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM