Crout Decomposition #52

Merged
merged 16 commits into from Jul 2, 2013

Projects

None yet

3 participants

@yuronew
Contributor
yuronew commented Jun 29, 2013

my first pull request)

@vkostyukov
Owner

Hi Yuriy @yuronew,

Thank you for the contribution! I have few questions:

  1. Is Crout method faster then triangularize().product(), which is used currently?
  2. Is your implementation faster then call to existed LU decompositor: decompose(Matrices.LU_DECOMPOSITOR)[0].product()?

So, if yes, we need replace current implementation of determinant method with your Crout or default LU. The only line should be replaced is triangularize().product().

Please, try to collect some performance data for this.

yuronew added some commits Jun 30, 2013
@yuronew yuronew added CroutDecompositor to org.la4j.decomposition
changed determinant searching, now CroutDecompositor is used
added Crout_DECOMPOSITOR instance to Matrices class
added test case for CroutDecompositor
36eb6a9
@yuronew yuronew smth ed0e06b
@vkostyukov
Owner

@yuronew, could you please remove "bin" and other files from pull-request? You can do it by changing the last commit git commit --amend.

@vkostyukov vkostyukov commented on an outdated diff Jun 30, 2013
src/main/java/org/la4j/matrix/Matrices.java
@@ -902,4 +903,10 @@ public static MatrixAccumulator asProductFunctionAccumulator(double neutral,
return new FunctionMatrixAccumulator(new ProductMatrixAccumulator(
neutral), function);
}
+ /**
+ * The {@link CroutDecompositor} singleton instance.
+ */
+ public final static MatrixDecompositor Crout_DECOMPOSITOR =
@vkostyukov
vkostyukov Jun 30, 2013 Owner

It should be CROUT_DECOMPOSITOR.

@vkostyukov vkostyukov commented on an outdated diff Jun 30, 2013
src/main/java/org/la4j/matrix/Matrix.java
@@ -681,4 +681,15 @@ Matrix slice(int fromRow, int fromColumn, int untilRow, int untilColumn,
* Wraps this matrix with unsafe interface
*/
Matrix unsafe();
+
+ /**
+ * Returns the "determinant" of this matrix, computed with Crout LU-decomposition method.
+ * <p>
+ * See <a href="http://math.fullerton.edu/mathews/n2003/CholeskyMod.html">
+ * http://math.fullerton.edu/mathews/n2003/CholeskyMod.html</a> for more details.
+ * </p>
+ *
+ * @return the "determinant" of this matrix
+ */
+ double crout();
@vkostyukov
vkostyukov Jun 30, 2013 Owner

We don't need having this method in Matrix interface. It is enough to have determinant().

@vkostyukov vkostyukov commented on an outdated diff Jun 30, 2013
...in/java/org/la4j/decomposition/CroutDecompositor.java
+ *
+ * @param matrix
+ * @param factory
+ * @return { L, U }
+ */
+ @Override
+ public Matrix[] decompose(Matrix matrix, Factory factory) {
+
+ if (matrix.rows() != matrix.columns()) {
+ throw new IllegalArgumentException("Wrong matrix size: "
+ + "rows != columns");
+ }
+
+ Matrix l = factory.createMatrix(matrix.rows(), matrix.columns());
+ Matrix u = factory.createIdentityMatrix(matrix.rows());
+ double s=0;
@vkostyukov
vkostyukov Jun 30, 2013 Owner

Please, declare variable just before using.

@vkostyukov vkostyukov commented on an outdated diff Jun 30, 2013
...in/java/org/la4j/decomposition/CroutDecompositor.java
+ * @return { L, U }
+ */
+ @Override
+ public Matrix[] decompose(Matrix matrix, Factory factory) {
+
+ if (matrix.rows() != matrix.columns()) {
+ throw new IllegalArgumentException("Wrong matrix size: "
+ + "rows != columns");
+ }
+
+ Matrix l = factory.createMatrix(matrix.rows(), matrix.columns());
+ Matrix u = factory.createIdentityMatrix(matrix.rows());
+ double s=0;
+ for (int j = 0; j < l.columns(); j++){
+ for (int i = j; i < l.rows(); i++){
+ s=0;
@vkostyukov
vkostyukov Jun 30, 2013 Owner

Here double s = 0.0;

@vkostyukov vkostyukov commented on an outdated diff Jun 30, 2013
...in/java/org/la4j/decomposition/CroutDecompositor.java
+ }
+
+ Matrix l = factory.createMatrix(matrix.rows(), matrix.columns());
+ Matrix u = factory.createIdentityMatrix(matrix.rows());
+ double s=0;
+ for (int j = 0; j < l.columns(); j++){
+ for (int i = j; i < l.rows(); i++){
+ s=0;
+ for (int k = 0; k < j; k++){
+ s = s + l.get(i, k)*u.get(k, j);
+ }
+ l.set(i,j,matrix.get(i, j) - s);
+ }
+
+ for (int i = j; i < l.rows(); i++){
+ s=0;
@vkostyukov
vkostyukov Jun 30, 2013 Owner

Declare a new double variable here.

@vkostyukov vkostyukov commented on an outdated diff Jun 30, 2013
...in/java/org/la4j/decomposition/CroutDecompositor.java
+ double s=0;
+ for (int j = 0; j < l.columns(); j++){
+ for (int i = j; i < l.rows(); i++){
+ s=0;
+ for (int k = 0; k < j; k++){
+ s = s + l.get(i, k)*u.get(k, j);
+ }
+ l.set(i,j,matrix.get(i, j) - s);
+ }
+
+ for (int i = j; i < l.rows(); i++){
+ s=0;
+ for (int k = 0; k < j; k++){
+ s = s + l.get(j, k)*u.get(k, i);
+ }
+ if (l.get(j, j) == 0) throw new IllegalArgumentException("Singular matrix!");
@vkostyukov
vkostyukov Jun 30, 2013 Owner

We can't compare double with 0.0 - it is unsafe. Please, use the following code:

if (Math.abs(value) < Matrices.EPS) {
  // close to zero
}
@vkostyukov
vkostyukov Jun 30, 2013 Owner

Also, add brackets after condition.

@vkostyukov vkostyukov commented on an outdated diff Jun 30, 2013
src/main/java/org/la4j/matrix/AbstractMatrix.java
@@ -953,4 +954,38 @@ protected void ensureDimensionsAreNotNegative(int rows, int columns) {
+ rows + "x" + columns);
}
}
+
+ @Override
+ public double crout(){
@vkostyukov
vkostyukov Jun 30, 2013 Owner

We don't need this method.

@vkostyukov
Owner

@yuronew,

I've came through the patch. Please, make sure that you use spaces instead of tabs. Configure your IDE and try to figure out why it doesn't look like the rest of la4j's code.

Also, please follow the code convetion (by Oracle). There is no much spaces in you code. In statemens like:

a.set(i,j,5+4) -> a.set(i, j, 5 + 4);

Please, also follow my comments to improve your patch.

@vkostyukov vkostyukov commented on an outdated diff Jun 30, 2013
src/main/java/org/la4j/matrix/AbstractSafeMatrix.java
@@ -511,4 +511,9 @@ protected void ensureIndexInColumns(int i) {
+ i);
}
}
+
@vkostyukov
vkostyukov Jun 30, 2013 Owner

There is a compile error. This method no longer exsits.

@vkostyukov
Owner

Hi @yuronew,

Just one thing. Please remove crout method. It raises compile error.

@vkostyukov
Owner

Hi @yuronew,

There is still a failed test:

Running org.la4j.decomposition.CroutDecompositorTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.012 sec <<< FAILURE!

Please, run mvn test to make sure all tests are passed.

@vkostyukov
Owner

Thanks Yuriy!

@vkostyukov vkostyukov merged commit 135ca3c into vkostyukov:master Jul 2, 2013

1 check passed

default The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment