FieldLUDecomposition { public FieldMatrix<T> getP() { if ((cachedP == null) && !singular) { final int m = pivot.length; cachedP = new Array2DRowFieldMatrix<T>(field, m, m); for (int i = 0; i < m; ++i) { cachedP.setEntry(i, pivot[i], field.getOne()); } } return cachedP; } FieldLUDecomposition(FieldMatrix<T> matrix); FieldMatrix<T> getL(); FieldMatrix<T> getU(); FieldMatrix<T> getP(); int[] getPivot(); T getDeterminant(); FieldDecompositionSolver<T> getSolver();  }
@Test public void testPPermutation() { FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<Fraction>(FractionField.getInstance(), testData); FieldMatrix<Fraction> p = new FieldLUDecomposition<Fraction>(matrix).getP(); FieldMatrix<Fraction> ppT = p.multiply(p.transpose()); FieldMatrix<Fraction> id = new Array2DRowFieldMatrix<Fraction>(FractionField.getInstance(), p.getRowDimension(), p.getRowDimension()); for (int i = 0; i < id.getRowDimension(); ++i) { id.setEntry(i, i, Fraction.ONE); } TestUtils.assertEquals(id, ppT); 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 Fraction e = p.getEntry(i, j); if (e.equals(Fraction.ZERO)) { ++zeroCount; } else if (e.equals(Fraction.ONE)) { ++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 Fraction e = p.getEntry(i, j); if (e.equals(Fraction.ZERO)) { ++zeroCount; } else if (e.equals(Fraction.ONE)) { ++oneCount; } else { ++otherCount; } } Assert.assertEquals(p.getRowDimension() - 1, zeroCount); Assert.assertEquals(1, oneCount); Assert.assertEquals(0, otherCount); } }