2

I have the next code, which multiplies the probability matrix p the certain number of times. For the first 50 iterations everything is ok, the sum of probabilities in each row is equal to 1, but then I recieve the sum > 1 and approximately on the 70th iteration I recieve infinity values. And I do not understand why.

The sum of probabilities in each row must be equal to 1. This is the classic Markov's chain model. And regardless of num of multiplications you must receive the sum = 1 in each row. I suppose there is a problem in a floating-point calculation.

public class Test {
    public static void main(String[] args) {
        int trials = Integer.parseInt(args[0]);

        double[][] p = {
                {0.02, 0.92, 0.02, 0.02, 0.02},
                {0.02, 0.02, 0.38, 0.38, 0.2},
                {0.02, 0.02, 0.02, 0.92, 0.02},
                {0.92, 0.02, 0.02, 0.02, 0.02},
                {0.47, 0.02, 0.47, 0.02, 0.02}};

        for (int t = 0; t < trials; t++) {
            p = multiply(p, p);
        }

        for (int i = 0; i < p.length; i++) {
            for (int j = 0; j < p[i].length; j++) {
                System.out.printf("%9.4f", p[i][j]);
            }
            System.out.println();
        }
    }

    public static double[][] multiply(double[][] a, double[][] b) {
        int w = a[0].length;
        int l = b.length;
        if (w != l) {
            throw new IllegalArgumentException("The number of columns " +
                    "in the first matrix must be equal to the number " +
                    "of rows in second matrix!" + w + " " + l);
        }

        double[][] result = new double[a.length][b[0].length];
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < b[0].length; j++) {
                for (int k = 0; k < b.length; k++) {
                    result[i][j] += a[i][k] * b[k][j];
                }
            }
        }
        return result;
    }
}

/*
output for the trials = 30:
   0,2730   0,2657   0,1462   0,2472   0,0678
   0,2730   0,2657   0,1462   0,2472   0,0678
   0,2730   0,2657   0,1462   0,2472   0,0678
   0,2730   0,2657   0,1462   0,2472   0,0678
   0,2730   0,2657   0,1462   0,2472   0,0678

output for the trials = 45:
   0,2732   0,2659   0,1463   0,2474   0,0679
   0,2732   0,2659   0,1463   0,2474   0,0679
   0,2732   0,2659   0,1463   0,2474   0,0679
   0,2732   0,2659   0,1463   0,2474   0,0679
   0,2732   0,2659   0,1463   0,2474   0,0679

output for the trials = 55:
   0,5183   0,5044   0,2775   0,4693   0,1288
   0,5183   0,5044   0,2775   0,4693   0,1288
   0,5183   0,5044   0,2775   0,4693   0,1288
   0,5183   0,5044   0,2775   0,4693   0,1288
   0,5183   0,5044   0,2775   0,4693   0,1288

output for the trials = 70:
 Infinity Infinity Infinity Infinity Infinity
 Infinity Infinity Infinity Infinity Infinity
 Infinity Infinity Infinity Infinity Infinity
 Infinity Infinity Infinity Infinity Infinity
 Infinity Infinity Infinity Infinity Infinity
*/
Community
  • 1
  • 1
Rippar
  • 23
  • 3

2 Answers2

4

Floating point calculations, especially those with large fractional parts, are associated with rounding in most cases. The question is how and what to round?

Floating point matrix multiplication

You can use matrix multiplication with BigDecimal instead of double. In this case, the 34-digit DECIMAL128 format can be enough for the first 151 trials. Result can be represented using scientific notation with an exponent if needed.

To convert double to BigDecimal use the valueOf method and don't use the constructor BigDecimal(double), otherwise you can get unpredictable appendix to each number. I guess, this is the main mistake in the calculations with double.

In this case, starting from the 5th trial the row sums become less than 1 because of rounding, and then they become even less, and after the 151st trial you will get an ArithmeticException underflow, because the numbers are getting smaller than the rounding bounds.

Spherical horse in vacuum

For more precise calculations you can use UNLIMITED precision arithmetic. In this case, the sums of the rows are stably equal to 1 on each iteration, but it thinks too long, even in parallel mode, so I stopped at the 20th trial with such a MathContext. It takes about five minutes to calculate the 20th iteration, that's enough for me. The time for further trials grows in geometric progression with an approximate common ratio of 3, but it continues to work without a memory leaks. I hope this is the case, so I think it might take approximately more than a year to calculate the first 30 trials...

Working code

The first 151 trials in 34-digit DECIMAL128 format, every tenth trial is printed using scientific notation. You can convert these values to doubleValue and round the row sums back to 1 until the 65th trial, but in this case you will get 0.0 after the 128th trial:

public static void main(String[] args) {
    int d = 5; // dimensions
    int trials = 151;

    // rules for numerical operators
    MathContext mc = MathContext.DECIMAL128;

    BigDecimal[][] p = toBigDecimal(new double[][]{
            {0.02, 0.92, 0.02, 0.02, 0.02},
            {0.02, 0.02, 0.38, 0.38, 0.2},
            {0.02, 0.02, 0.02, 0.92, 0.02},
            {0.92, 0.02, 0.02, 0.02, 0.02},
            {0.47, 0.02, 0.47, 0.02, 0.02}});

    // matrix multiplication
    for (int t = 0; t < trials; t++) {
        long time = System.currentTimeMillis();
        p = parallelMatrixMultiplication(mc, d, d, d, p, p);
        // print every tenth trial
        if (t % 10 == 0)
            outputMatrix(p, t, time);
    }
}
static void outputMatrix(BigDecimal[][] matrix, int t, long time) {
    System.out.println("trial: " + t);
    for (BigDecimal[] row : matrix) {
        BigDecimal sum = BigDecimal.valueOf(0);
        for (BigDecimal element : row) {
            sum = sum.add(element);
            // string representation of this element, using
            // scientific notation with an exponent if needed
            System.out.print(element.toString() + " ");
        }
        // sum of the row in the same format
        System.out.println("|| " + sum.toString());
    }
    System.out.println("time: " + (System.currentTimeMillis() - time));
}
static BigDecimal[][] toBigDecimal(double[][] matrix) {
    return Arrays.stream(matrix)
            .map(row -> Arrays.stream(row)
                    .mapToObj(BigDecimal::valueOf)
                    .toArray(BigDecimal[]::new))
            .toArray(BigDecimal[][]::new);
}
/**
 * Parallel Matrix multiplication
 *
 * @param mc rules for numerical operators
 * @param m  rows of 'a' matrix
 * @param n  columns of 'a' matrix
 *           and rows of 'b' matrix
 * @param p  columns of 'b' matrix
 * @param a  first matrix 'm×n'
 * @param b  second matrix 'n×p'
 * @return result matrix 'm×p'
 */
static BigDecimal[][] parallelMatrixMultiplication(
        MathContext mc, int m, int n, int p,
        BigDecimal[][] a, BigDecimal[][] b) {
    return IntStream.range(0, m)
            .parallel()
            .mapToObj(i -> IntStream.range(0, p)
                    .mapToObj(j -> IntStream.range(0, n)
                            .mapToObj(k -> a[i][k].multiply(b[k][j], mc))
                            .reduce((bd1, bd2) -> bd1.add(bd2, mc))
                            .orElse(new BigDecimal("0")))
                    .toArray(BigDecimal[]::new))
            .toArray(BigDecimal[][]::new);
}

Output:

trial: 0
0.0470 0.0380 0.3602 0.3692 0.1856 || 1.0000
0.4520 0.0380 0.1172 0.3692 0.0236 || 1.0000
0.8570 0.0380 0.0362 0.0452 0.0236 || 1.0000
0.0470 0.8480 0.0362 0.0452 0.0236 || 1.0000
0.0470 0.4430 0.0362 0.4502 0.0236 || 1.0000
time: 48
trial: 10
0.2730292887828770329701825732219565 0.2657263599045893296731643158997608 0.1461853247179238943858654633117944 0.2472282818117836636296180707045289 0.06783074478282607934116957686195689 || 0.99999999999999999999999999999999749
0.2730292887828770329701825732219565 0.2657263599045893296731643158997608 0.1461853247179238943858654633117944 0.2472282818117836636296180707045289 0.06783074478282607934116957686195689 || 0.99999999999999999999999999999999749
0.2730292887828770329701825732219566 0.2657263599045893296731643158997610 0.1461853247179238943858654633117944 0.2472282818117836636296180707045290 0.06783074478282607934116957686195692 || 0.99999999999999999999999999999999792
0.2730292887828770329701825732219565 0.2657263599045893296731643158997607 0.1461853247179238943858654633117943 0.2472282818117836636296180707045288 0.06783074478282607934116957686195687 || 0.99999999999999999999999999999999717
0.2730292887828770329701825732219565 0.2657263599045893296731643158997608 0.1461853247179238943858654633117944 0.2472282818117836636296180707045289 0.06783074478282607934116957686195689 || 0.99999999999999999999999999999999749
time: 11
trial: 20
0.2730292887828770329701825732212719 0.2657263599045893296731643158990943 0.1461853247179238943858654633114277 0.2472282818117836636296180707039089 0.06783074478282607934116957686178676 || 0.99999999999999999999999999999748956
0.2730292887828770329701825732212719 0.2657263599045893296731643158990943 0.1461853247179238943858654633114277 0.2472282818117836636296180707039089 0.06783074478282607934116957686178676 || 0.99999999999999999999999999999748956
0.2730292887828770329701825732212719 0.2657263599045893296731643158990943 0.1461853247179238943858654633114277 0.2472282818117836636296180707039089 0.06783074478282607934116957686178676 || 0.99999999999999999999999999999748956
0.2730292887828770329701825732212715 0.2657263599045893296731643158990941 0.1461853247179238943858654633114276 0.2472282818117836636296180707039086 0.06783074478282607934116957686178666 || 0.99999999999999999999999999999748846
0.2730292887828770329701825732212719 0.2657263599045893296731643158990943 0.1461853247179238943858654633114277 0.2472282818117836636296180707039089 0.06783074478282607934116957686178676 || 0.99999999999999999999999999999748956
time: 9
trial: 30
0.2730292887828770329701825725200014 0.2657263599045893296731643152165814 0.1461853247179238943858654629359535 0.2472282818117836636296180700689079 0.06783074478282607934116957668756482 || 0.99999999999999999999999999742900902
0.2730292887828770329701825725200014 0.2657263599045893296731643152165814 0.1461853247179238943858654629359535 0.2472282818117836636296180700689079 0.06783074478282607934116957668756482 || 0.99999999999999999999999999742900902
0.2730292887828770329701825725200014 0.2657263599045893296731643152165814 0.1461853247179238943858654629359535 0.2472282818117836636296180700689079 0.06783074478282607934116957668756482 || 0.99999999999999999999999999742900902
0.2730292887828770329701825725200011 0.2657263599045893296731643152165811 0.1461853247179238943858654629359533 0.2472282818117836636296180700689076 0.06783074478282607934116957668756474 || 0.99999999999999999999999999742900784
0.2730292887828770329701825725200014 0.2657263599045893296731643152165814 0.1461853247179238943858654629359535 0.2472282818117836636296180700689079 0.06783074478282607934116957668756482 || 0.99999999999999999999999999742900902
time: 10
trial: 40
0.2730292887828770329701818544190935 0.2657263599045893296731636163232827 0.1461853247179238943858650784504105 0.2472282818117836636296174198278500 0.06783074478282607934116939828428938 || 0.99999999999999999999999736730492608
0.2730292887828770329701818544190935 0.2657263599045893296731636163232827 0.1461853247179238943858650784504105 0.2472282818117836636296174198278500 0.06783074478282607934116939828428938 || 0.99999999999999999999999736730492608
0.2730292887828770329701818544190935 0.2657263599045893296731636163232827 0.1461853247179238943858650784504105 0.2472282818117836636296174198278500 0.06783074478282607934116939828428938 || 0.99999999999999999999999736730492608
0.2730292887828770329701818544190931 0.2657263599045893296731636163232822 0.1461853247179238943858650784504101 0.2472282818117836636296174198278496 0.06783074478282607934116939828428926 || 0.99999999999999999999999736730492426
0.2730292887828770329701818544190935 0.2657263599045893296731636163232827 0.1461853247179238943858650784504105 0.2472282818117836636296174198278500 0.06783074478282607934116939828428938 || 0.99999999999999999999999736730492608
time: 8
trial: 50
0.2730292887828770329694465190894863 0.2657263599045893296724479495854144 0.1461853247179238943854713652542387 0.2472282818117836636289515729844414 0.06783074478282607934098671333025123 || 0.99999999999999999999730412024383203
0.2730292887828770329694465190894863 0.2657263599045893296724479495854144 0.1461853247179238943854713652542387 0.2472282818117836636289515729844414 0.06783074478282607934098671333025123 || 0.99999999999999999999730412024383203
0.2730292887828770329694465190894863 0.2657263599045893296724479495854144 0.1461853247179238943854713652542387 0.2472282818117836636289515729844414 0.06783074478282607934098671333025123 || 0.99999999999999999999730412024383203
0.2730292887828770329694465190894859 0.2657263599045893296724479495854140 0.1461853247179238943854713652542385 0.2472282818117836636289515729844411 0.06783074478282607934098671333025115 || 0.99999999999999999999730412024383065
0.2730292887828770329694465190894863 0.2657263599045893296724479495854144 0.1461853247179238943854713652542387 0.2472282818117836636289515729844414 0.06783074478282607934098671333025123 || 0.99999999999999999999730412024383203
time: 9
trial: 60
0.2730292887828770322164631415718825 0.2657263599045893289396052100083683 0.1461853247179238939823090523745175 0.2472282818117836629471244053337530 0.06783074478282607915391732039518029 || 0.99999999999999999723941912968370159
0.2730292887828770322164631415718825 0.2657263599045893289396052100083683 0.1461853247179238939823090523745175 0.2472282818117836629471244053337530 0.06783074478282607915391732039518029 || 0.99999999999999999723941912968370159
0.2730292887828770322164631415718825 0.2657263599045893289396052100083683 0.1461853247179238939823090523745175 0.2472282818117836629471244053337530 0.06783074478282607915391732039518029 || 0.99999999999999999723941912968370159
0.2730292887828770322164631415718818 0.2657263599045893289396052100083676 0.1461853247179238939823090523745172 0.2472282818117836629471244053337525 0.06783074478282607915391732039518012 || 0.99999999999999999723941912968369922
0.2730292887828770322164631415718825 0.2657263599045893289396052100083683 0.1461853247179238939823090523745175 0.2472282818117836629471244053337530 0.06783074478282607915391732039518029 || 0.99999999999999999723941912968370159
time: 9
trial: 70
0.2730292887828762611614845635464324 0.2657263599045885785086398831140649 0.1461853247179234811441006635406023 0.2472282818117829647561047310298819 0.06783074478282588759485895488280742 || 0.99999999999999717316518879611378892
0.2730292887828762611614845635464324 0.2657263599045885785086398831140649 0.1461853247179234811441006635406023 0.2472282818117829647561047310298819 0.06783074478282588759485895488280742 || 0.99999999999999717316518879611378892
0.2730292887828762611614845635464324 0.2657263599045885785086398831140649 0.1461853247179234811441006635406023 0.2472282818117829647561047310298819 0.06783074478282588759485895488280742 || 0.99999999999999717316518879611378892
0.2730292887828762611614845635464319 0.2657263599045885785086398831140646 0.1461853247179234811441006635406021 0.2472282818117829647561047310298813 0.06783074478282588759485895488280730 || 0.99999999999999717316518879611378720
0.2730292887828762611614845635464324 0.2657263599045885785086398831140649 0.1461853247179234811441006635406023 0.2472282818117829647561047310298819 0.06783074478282588759485895488280742 || 0.99999999999999717316518879611378892
time: 8
trial: 80
0.2730292887820867008634218082462759 0.2657263599038201372001462555418668 0.1461853247175007348187111094682799 0.2472282818110680171519592786367428 0.06783074478262973111909295411775437 || 0.99999999999710532115333140601091977
0.2730292887820867008634218082462759 0.2657263599038201372001462555418668 0.1461853247175007348187111094682799 0.2472282818110680171519592786367428 0.06783074478262973111909295411775437 || 0.99999999999710532115333140601091977
0.2730292887820867008634218082462759 0.2657263599038201372001462555418668 0.1461853247175007348187111094682799 0.2472282818110680171519592786367428 0.06783074478262973111909295411775437 || 0.99999999999710532115333140601091977
0.2730292887820867008634218082462755 0.2657263599038201372001462555418664 0.1461853247175007348187111094682796 0.2472282818110680171519592786367424 0.06783074478262973111909295411775427 || 0.99999999999710532115333140601091817
0.2730292887820867008634218082462759 0.2657263599038201372001462555418668 0.1461853247175007348187111094682799 0.2472282818110680171519592786367428 0.06783074478262973111909295411775437 || 0.99999999999710532115333140601091977
time: 12
trial: 90
0.2730292879735769568454317780258554 0.2657263591169362384688919123320142 0.1461853242846084982613861358960980 0.2472282810789616715920519228547587 0.06783074458176550023240385632850718 || 0.99999999703584886540016560543723348
0.2730292879735769568454317780258554 0.2657263591169362384688919123320142 0.1461853242846084982613861358960980 0.2472282810789616715920519228547587 0.06783074458176550023240385632850718 || 0.99999999703584886540016560543723348
0.2730292879735769568454317780258554 0.2657263591169362384688919123320142 0.1461853242846084982613861358960980 0.2472282810789616715920519228547587 0.06783074458176550023240385632850718 || 0.99999999703584886540016560543723348
0.2730292879735769568454317780258548 0.2657263591169362384688919123320136 0.1461853242846084982613861358960977 0.2472282810789616715920519228547582 0.06783074458176550023240385632850703 || 0.99999999703584886540016560543723133
0.2730292879735769568454317780258554 0.2657263591169362384688919123320142 0.1461853242846084982613861358960980 0.2472282810789616715920519228547587 0.06783074458176550023240385632850718 || 0.99999999703584886540016560543723348
time: 8
trial: 100
0.2730284600608555597809096423856914 0.2657255533490468070347525429793999 0.1461848810036310065886299754607182 0.2472275314032015596942117582195087 0.06783053889710522849818932256856966 || 0.99999696471384016159669324161388786
0.2730284600608555597809096423856914 0.2657255533490468070347525429793999 0.1461848810036310065886299754607182 0.2472275314032015596942117582195087 0.06783053889710522849818932256856966 || 0.99999696471384016159669324161388786
0.2730284600608555597809096423856914 0.2657255533490468070347525429793999 0.1461848810036310065886299754607182 0.2472275314032015596942117582195087 0.06783053889710522849818932256856966 || 0.99999696471384016159669324161388786
0.2730284600608555597809096423856910 0.2657255533490468070347525429793995 0.1461848810036310065886299754607179 0.2472275314032015596942117582195082 0.06783053889710522849818932256856957 || 0.99999696471384016159669324161388617
0.2730284600608555597809096423856914 0.2657255533490468070347525429793999 0.1461848810036310065886299754607182 0.2472275314032015596942117582195087 0.06783053889710522849818932256856966 || 0.99999696471384016159669324161388786
time: 12
trial: 110
0.2721819935822030657304032357038538 0.2649017279742810454592283638310650 0.1457316659745904561852246134110141 0.2464610551981708732338898147466929 0.06762024478566887448452655718718783 || 0.99689668751491431509327258487981363
0.2721819935822030657304032357038538 0.2649017279742810454592283638310650 0.1457316659745904561852246134110141 0.2464610551981708732338898147466929 0.06762024478566887448452655718718783 || 0.99689668751491431509327258487981363
0.2721819935822030657304032357038538 0.2649017279742810454592283638310650 0.1457316659745904561852246134110141 0.2464610551981708732338898147466929 0.06762024478566887448452655718718783 || 0.99689668751491431509327258487981363
0.2721819935822030657304032357038534 0.2649017279742810454592283638310646 0.1457316659745904561852246134110139 0.2464610551981708732338898147466923 0.06762024478566887448452655718718771 || 0.99689668751491431509327258487981191
0.2721819935822030657304032357038538 0.2649017279742810454592283638310650 0.1457316659745904561852246134110141 0.2464610551981708732338898147466929 0.06762024478566887448452655718718783 || 0.99689668751491431509327258487981363
time: 8
trial: 120
0.01132311287302140636187625536070984 0.01102024467759399595775450897719919 0.006062620386037311053865263230157305 0.01025308952324214872533623929149362 0.002813087133841649504461690768456174 || 0.041472154593736511603293957628016129
0.01132311287302140636187625536070984 0.01102024467759399595775450897719919 0.006062620386037311053865263230157305 0.01025308952324214872533623929149362 0.002813087133841649504461690768456174 || 0.041472154593736511603293957628016129
0.01132311287302140636187625536070984 0.01102024467759399595775450897719919 0.006062620386037311053865263230157305 0.01025308952324214872533623929149362 0.002813087133841649504461690768456174 || 0.041472154593736511603293957628016129
0.01132311287302140636187625536070982 0.01102024467759399595775450897719917 0.006062620386037311053865263230157291 0.01025308952324214872533623929149360 0.002813087133841649504461690768456168 || 0.041472154593736511603293957628016049
0.01132311287302140636187625536070984 0.01102024467759399595775450897719919 0.006062620386037311053865263230157305 0.01025308952324214872533623929149362 0.002813087133841649504461690768456174 || 0.041472154593736511603293957628016129
time: 8
trial: 130
1.044639308739343328720850715228411E-1416 1.016697520482136028467012050376325E-1416 5.593207133268761858504096038885519E-1417 9.459218919844845701317393883059301E-1417 2.595276963035115177423085757384926E-1417 || 3.8261071308363516309123203335377106E-1416
1.044639308739343328720850715228411E-1416 1.016697520482136028467012050376325E-1416 5.593207133268761858504096038885519E-1417 9.459218919844845701317393883059301E-1417 2.595276963035115177423085757384926E-1417 || 3.8261071308363516309123203335377106E-1416
1.044639308739343328720850715228411E-1416 1.016697520482136028467012050376325E-1416 5.593207133268761858504096038885519E-1417 9.459218919844845701317393883059301E-1417 2.595276963035115177423085757384926E-1417 || 3.8261071308363516309123203335377106E-1416
1.044639308739343328720850715228409E-1416 1.016697520482136028467012050376324E-1416 5.593207133268761858504096038885513E-1417 9.459218919844845701317393883059288E-1417 2.595276963035115177423085757384923E-1417 || 3.8261071308363516309123203335377054E-1416
1.044639308739343328720850715228411E-1416 1.016697520482136028467012050376325E-1416 5.593207133268761858504096038885519E-1417 9.459218919844845701317393883059301E-1417 2.595276963035115177423085757384926E-1417 || 3.8261071308363516309123203335377106E-1416
time: 8
trial: 140
1.511841812795049791388737404792298E-1449388 1.471403392128599201290355981548488E-1449388 8.094702488176411118777608478542366E-1449389 1.368974205715227102195005233661688E-1449388 3.755983711962022452727563939141449E-1449389 || 5.5372880306527194520246158617708555E-1449388
1.511841812795049791388737404792298E-1449388 1.471403392128599201290355981548488E-1449388 8.094702488176411118777608478542366E-1449389 1.368974205715227102195005233661688E-1449388 3.755983711962022452727563939141449E-1449389 || 5.5372880306527194520246158617708555E-1449388
1.511841812795049791388737404792298E-1449388 1.471403392128599201290355981548488E-1449388 8.094702488176411118777608478542366E-1449389 1.368974205715227102195005233661688E-1449388 3.755983711962022452727563939141449E-1449389 || 5.5372880306527194520246158617708555E-1449388
1.511841812795049791388737404792296E-1449388 1.471403392128599201290355981548485E-1449388 8.094702488176411118777608478542352E-1449389 1.368974205715227102195005233661686E-1449388 3.755983711962022452727563939141443E-1449389 || 5.5372880306527194520246158617708465E-1449388
1.511841812795049791388737404792298E-1449388 1.471403392128599201290355981548488E-1449388 8.094702488176411118777608478542366E-1449389 1.368974205715227102195005233661688E-1449388 3.755983711962022452727563939141449E-1449389 || 5.5372880306527194520246158617708555E-1449388
time: 7
trial: 150
3.736410561203285932862056593199857E-1484172552 3.636469852606817133931883241380801E-1484172552 2.000548708909203057880207355325268E-1484172552 3.383323332480596714663696894190756E-1484172552 9.282649209930868784637712909494675E-1484172553 || 1.36850173761929897178016153750461495E-1484172551
3.736410561203285932862056593199857E-1484172552 3.636469852606817133931883241380801E-1484172552 2.000548708909203057880207355325268E-1484172552 3.383323332480596714663696894190756E-1484172552 9.282649209930868784637712909494675E-1484172553 || 1.36850173761929897178016153750461495E-1484172551
3.736410561203285932862056593199857E-1484172552 3.636469852606817133931883241380801E-1484172552 2.000548708909203057880207355325268E-1484172552 3.383323332480596714663696894190756E-1484172552 9.282649209930868784637712909494675E-1484172553 || 1.36850173761929897178016153750461495E-1484172551
3.736410561203285932862056593199849E-1484172552 3.636469852606817133931883241380794E-1484172552 2.000548708909203057880207355325263E-1484172552 3.383323332480596714663696894190750E-1484172552 9.282649209930868784637712909494657E-1484172553 || 1.36850173761929897178016153750461217E-1484172551
3.736410561203285932862056593199857E-1484172552 3.636469852606817133931883241380801E-1484172552 2.000548708909203057880207355325268E-1484172552 3.383323332480596714663696894190756E-1484172552 9.282649209930868784637712909494675E-1484172553 || 1.36850173761929897178016153750461495E-1484172551
time: 7
2

TL;DR

It's just math. At 57th iteration numbers exceed one and by multipling them together, they soon reach Infinity.

Longer version

You are multiplying and adding numbers that are 0 > num > 1. With your data example it gets further away from 0 by multiplying (obviously) and getting closer to 1 by adding together.
Your multiplications are mostly 0.92 * 0.38, 0.47 * 0.92 etc. By adding them together they are slowly getting closer to 1.
At 57th iteration some sums are exceeding 1. After that the multiplication goes into a geometric progression and soon reaches Infinity. To better see the increase, try:

public static double[][] multiply(double[][] a, double[][] b) {
    int w = a[0].length;
    int l = b.length;
    if (w != l) {
        throw new IllegalArgumentException("The number of columns " +
                "in the first matrix must be equal to the number " +
                "of rows in second matrix!" + w + " " + l);
    }
    double[][] result = new double[a.length][b[0].length];
    for (int i = 0; i < a.length; i++) {
        int c = 0;
        for (int j = 0; j < b[0].length; j++) {
            System.out.println();
            //For debug
            //System.out.println("["+i+","+j+"]");
            for (int k = 0; k < b.length; k++) {
                //For debug
                //System.out.println("ADD: ["+i+","+k+"] * [" +k+","+j+"]");
                result[i][j] += a[i][k] * b[k][j];
                c++;
            }
            //to see all cells of each iteration
            System.out.println(result[i][j]);
        }
    }
    //To see results of 1st cell of each iteration
    System.out.println("Counts: " + result[0][0]);
    return result;
}

Testing

Just simply change one of the array values from 0.02 to 0.5 or 1 and you will see that it reaches Infinity a lot sooner. Nothing weird about the code. Your test case was just a funny edge case that dances around 1 a lot.

Your outputs

Sometimes results with 40 or 42 iterations seemed identical.
That was because the sum of columns was increasing so slowly that it wasn't visible in the first 4 positions behind point.

Community
  • 1
  • 1
Mike B
  • 2,756
  • 2
  • 16
  • 28