LUDecomposition { public RealMatrix getP() { if ((cachedP == null) && !singular) { final int m = pivot.length; cachedP = MatrixUtils.createRealMatrix(m, m); for (int i = 0; i < m; ++i) { cachedP.setEntry(i, pivot[i], 1.0); } } return cachedP; } LUDecomposition(RealMatrix matrix);  LUDecomposition(RealMatrix matrix, double singularityThreshold); RealMatrix getL(); RealMatrix getU(); RealMatrix getP(); int[] getPivot(); double getDeterminant(); DecompositionSolver getSolver();  }
@Test public void testPPermutation() { RealMatrix matrix = MatrixUtils.createRealMatrix(testData); RealMatrix p = new LUDecomposition(matrix).getP(); RealMatrix ppT = p.multiply(p.transpose()); RealMatrix id = MatrixUtils.createRealIdentityMatrix(p.getRowDimension()); Assert.assertEquals(0, ppT.subtract(id).getNorm(), normTolerance); for (int i = 0; i < p.getRowDimension(); i++) { int zeroCount = 0; int oneCount = 0; int otherCount = 0; for (int j = 0; j < p.getColumnDimension(); j++) { final double e = p.getEntry(i, j); if (e == 0) { ++zeroCount; } else if (e == 1) { ++oneCount; } else { ++otherCount; } } Assert.assertEquals(p.getColumnDimension() - 1, zeroCount); Assert.assertEquals(1, oneCount); Assert.assertEquals(0, otherCount); } for (int j = 0; j < p.getColumnDimension(); j++) { int zeroCount = 0; int oneCount = 0; int otherCount = 0; for (int i = 0; i < p.getRowDimension(); i++) { final double e = p.getEntry(i, j); if (e == 0) { ++zeroCount; } else if (e == 1) { ++oneCount; } else { ++otherCount; } } Assert.assertEquals(p.getRowDimension() - 1, zeroCount); Assert.assertEquals(1, oneCount); Assert.assertEquals(0, otherCount); } }