View Javadoc
1   import cern.colt.matrix.tfloat.FloatMatrix2D;
2   import cern.colt.matrix.tfloat.algo.DenseFloatAlgebra;
3   import cern.colt.matrix.tfloat.algo.SparseFloatAlgebra;
4   import cern.colt.matrix.tfloat.algo.decomposition.DenseFloatLUDecomposition;
5   import cern.colt.matrix.tfloat.algo.decomposition.SparseFloatLUDecomposition;
6   import cern.colt.matrix.tfloat.impl.DenseFloatMatrix2D;
7   import cern.colt.matrix.tfloat.impl.SparseCCFloatMatrix2D;
8   
9   /**
10   * Demonstrate the determinant has wrong sign problem.
11   * <p>
12   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
14   * <p>
15   * $LastChangedDate: 2015-08-30 00:16:51 +0200 (Sun, 30 Aug 2015) $, @version $Revision: 1329 $, by $Author: averbraeck $,
16   * initial version mrt. 2015 <br>
17   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
18   */
19  public final class DeterminantProblem
20  {
21      /**
22       * This class should never be instantiated.
23       */
24      private DeterminantProblem()
25      {
26          // This class should never be instantiated.
27      }
28  
29      /**
30       * Execute the demo.
31       * @param args String[]; not used
32       */
33      public static void main(final String[] args)
34      {
35          float[][] values = {{1, 2, 3}, {3, 5, 7}, {5, 10, 0}};
36  
37          FloatMatrix2D sparseFloatMatrix2D = new SparseCCFloatMatrix2D(values.length, values[0].length);
38          FloatMatrix2D denseFloatMatrix2D = new DenseFloatMatrix2D(values.length, values[0].length);
39  
40          for (int row = 0; row < values.length; row++)
41          {
42              for (int column = 0; column < values[row].length; column++)
43              {
44                  sparseFloatMatrix2D.set(row, column, values[row][column]);
45                  denseFloatMatrix2D.set(row, column, values[row][column]);
46              }
47          }
48          System.out.println("sparse matrix: " + sparseFloatMatrix2D.toString());
49          float sparseDeterminant = new SparseFloatAlgebra().det(sparseFloatMatrix2D);
50          System.out.println("determinant returned by det() is " + sparseDeterminant
51              + " (prints -15.0; correct value is 15.0)");
52          System.out.println("");
53          System.out.println("dense matrix: " + denseFloatMatrix2D.toString());
54          float denseDeterminant = new DenseFloatAlgebra().det(denseFloatMatrix2D);
55          System.out.println("determinant returned by det() is " + denseDeterminant + " (prints 15.0 which is OK)");
56  
57          System.out.println("");
58          System.out.println("The L and U matrices of the LU decompositions look fine and are identical (except for "
59              + "one being sparse and the other dense).");
60          SparseFloatLUDecomposition sparseLU = new SparseFloatLUDecomposition(sparseFloatMatrix2D, 0, true);
61          DenseFloatLUDecomposition denseLU = new DenseFloatLUDecomposition(denseFloatMatrix2D);
62  
63          System.out.println("sparse L: " + sparseLU.getL());
64          System.out.println("dense L: " + denseLU.getL());
65          System.out.println("");
66          System.out.println("sparse U: " + sparseLU.getU());
67          System.out.println("dense U: " + denseLU.getU());
68  
69          sparseLU.det();
70          denseLU.det();
71      }
72  
73  }