New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some iterating methods to Matrix (issue #57) #79

Merged
merged 18 commits into from Aug 10, 2013

Conversation

Projects
None yet
2 participants
@SamoylovMD
Contributor

SamoylovMD commented Aug 7, 2013

Added: each, eachInRow, eachInColumn methods;
Renamed each method to eachNonZero.

@vkostyukov

This comment has been minimized.

Show comment
Hide comment
@vkostyukov

vkostyukov Aug 8, 2013

Owner

Test are failed, since there is a usage if each in MatrixMarketStream. So, we need to change it to new eachNonZero call.

Owner

vkostyukov commented Aug 8, 2013

Test are failed, since there is a usage if each in MatrixMarketStream. So, we need to change it to new eachNonZero call.

@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractSafeMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractSafeMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/Matrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/Matrix.java Outdated
@@ -286,6 +286,52 @@ public void each(MatrixProcedure procedure) {
}
@Override
public void each(MatrixProcedure procedure) {

This comment has been minimized.

@vkostyukov

vkostyukov Aug 8, 2013

Owner

We don't need this. There is each method in AbstractMatrix class that has the same logic. And we can't do it better than there.

@vkostyukov

vkostyukov Aug 8, 2013

Owner

We don't need this. There is each method in AbstractMatrix class that has the same logic. And we can't do it better than there.

@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/sparse/CCSMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/sparse/CCSMatrix.java Outdated
@@ -360,6 +360,52 @@ public void each(MatrixProcedure procedure) {
}
@Override
public void each(MatrixProcedure procedure) {

This comment has been minimized.

@vkostyukov

vkostyukov Aug 8, 2013

Owner

We don't need a separate implementation of each method for CRS matrix. We already have it in AbstractMatrix.

@vkostyukov

vkostyukov Aug 8, 2013

Owner

We don't need a separate implementation of each method for CRS matrix. We already have it in AbstractMatrix.

This comment has been minimized.

@SamoylovMD

SamoylovMD Aug 9, 2013

Contributor

I thought that implementation of getting an element in CRS and CCS matrices works longer than O(1). So, my implementation works for O(rows*columns) which is faster than method inherited from AbstractMatrix.

@SamoylovMD

SamoylovMD Aug 9, 2013

Contributor

I thought that implementation of getting an element in CRS and CCS matrices works longer than O(1). So, my implementation works for O(rows*columns) which is faster than method inherited from AbstractMatrix.

This comment has been minimized.

@vkostyukov

vkostyukov Aug 9, 2013

Owner

Right. The get/set works in O(log rows) for CCS and in O(log columns) for CRS. Probably, you're right. We might need each methods for CRS/CCS.

@vkostyukov

vkostyukov Aug 9, 2013

Owner

Right. The get/set works in O(log rows) for CCS and in O(log columns) for CRS. Probably, you're right. We might need each methods for CRS/CCS.

@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/sparse/CRSMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/sparse/CRSMatrix.java Outdated
@vkostyukov

This comment has been minimized.

Show comment
Hide comment
@vkostyukov

vkostyukov Aug 8, 2013

Owner

Hi Maxim @SamoylovMD,

Please also add eachNonZeroInRow() and eachNonZeroInColumn() implementations. And we can provide more efficient implementation of eachNonZeroInRow() for CRS matrix and eachNonZeroInColumn for CCS matrix. For other cases, it would be enough to have general implenetation in AbstractMatrix class.

Please, also follow my coments to improve the code.

Owner

vkostyukov commented Aug 8, 2013

Hi Maxim @SamoylovMD,

Please also add eachNonZeroInRow() and eachNonZeroInColumn() implementations. And we can provide more efficient implementation of eachNonZeroInRow() for CRS matrix and eachNonZeroInColumn for CCS matrix. For other cases, it would be enough to have general implenetation in AbstractMatrix class.

Please, also follow my coments to improve the code.

@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/sparse/CRSMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractSafeMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/AbstractSafeMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/sparse/CCSMatrix.java Outdated
}
@Override
public void eachNonZeroInColumn(int j, MatrixProcedure procedure) {

This comment has been minimized.

@vkostyukov

vkostyukov Aug 10, 2013

Owner

What about having a simple for loop here. Like:

for (int i = columnPointers[k]; i < columnPointers[j + 1]; i++) {
    procedure.apply(rowIndices[i], j, values[i]);
}

There is something wrong with your code. First, i = rowIndicies[k]. And then you're getting row by rowIndicies[i]. This is strange. I guarantee it will fail tests.

@vkostyukov

vkostyukov Aug 10, 2013

Owner

What about having a simple for loop here. Like:

for (int i = columnPointers[k]; i < columnPointers[j + 1]; i++) {
    procedure.apply(rowIndices[i], j, values[i]);
}

There is something wrong with your code. First, i = rowIndicies[k]. And then you're getting row by rowIndicies[i]. This is strange. I guarantee it will fail tests.

This comment has been minimized.

@SamoylovMD

SamoylovMD Aug 10, 2013

Contributor

But this loop

for (int i = columnPointers[k]; i < columnPointers[j + 1]; i++) {
    procedure.apply(rowIndices[i], j, values[i]);
}

will apply procedure to non-zero values column only, won't it? Your implementation looks like mine in CCSMatrix.eachNonZeroInColumn

@SamoylovMD

SamoylovMD Aug 10, 2013

Contributor

But this loop

for (int i = columnPointers[k]; i < columnPointers[j + 1]; i++) {
    procedure.apply(rowIndices[i], j, values[i]);
}

will apply procedure to non-zero values column only, won't it? Your implementation looks like mine in CCSMatrix.eachNonZeroInColumn

@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/sparse/CRSMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/sparse/CRSMatrix.java Outdated
@vkostyukov

This comment has been minimized.

Show comment
Hide comment
@vkostyukov

vkostyukov Aug 10, 2013

Owner

So, I believe the best way to make sure that all implementations are correct - is to write tests. Could you please write simple tess for all the methods (esp. eachIn.. and eachNonZeroIn...) in AbstractMatrixTest class.

Here is some ideas how to test it. We can write simple matrix procedure that dumps row into double array and then check it. Like this:

public static class RowDumper implements MatrixProcedure {
  double data[];
  public Dumper(int n) {
    data = new double[n];
  }

  public apply(int i, int j, double) {
    data[j] = value;
  }
}

Matrix a = new CRSMatrix(...);
RowDumper d = new RowDumper(a.columns());
a.eachInRow(0, d);
// check whether the d.data is correct
a.eachNonZeroInRow(1, d);
// check whether the d.data is correct
Owner

vkostyukov commented Aug 10, 2013

So, I believe the best way to make sure that all implementations are correct - is to write tests. Could you please write simple tess for all the methods (esp. eachIn.. and eachNonZeroIn...) in AbstractMatrixTest class.

Here is some ideas how to test it. We can write simple matrix procedure that dumps row into double array and then check it. Like this:

public static class RowDumper implements MatrixProcedure {
  double data[];
  public Dumper(int n) {
    data = new double[n];
  }

  public apply(int i, int j, double) {
    data[j] = value;
  }
}

Matrix a = new CRSMatrix(...);
RowDumper d = new RowDumper(a.columns());
a.eachInRow(0, d);
// check whether the d.data is correct
a.eachNonZeroInRow(1, d);
// check whether the d.data is correct
@SamoylovMD

This comment has been minimized.

Show comment
Hide comment
@SamoylovMD

SamoylovMD Aug 10, 2013

Contributor

EachInRow works correct, EachNonZeroInRow needs correction.

Contributor

SamoylovMD commented Aug 10, 2013

EachInRow works correct, EachNonZeroInRow needs correction.

SamoylovMD added some commits Aug 10, 2013

@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/sparse/CRSMatrix.java Outdated
@vkostyukov

View changes

Show outdated Hide outdated src/main/java/org/la4j/matrix/sparse/CCSMatrix.java Outdated

SamoylovMD added some commits Aug 10, 2013

Update CCSMatrix.java
Refactored eachNonZeroInColumn method
Update CRSMatrix.java
Refactored eachNonZeroInRow method
@vkostyukov

This comment has been minimized.

Show comment
Hide comment
@vkostyukov

vkostyukov Aug 10, 2013

Owner

Thank you Maxim @SamoylovMD!

Owner

vkostyukov commented Aug 10, 2013

Thank you Maxim @SamoylovMD!

vkostyukov added a commit that referenced this pull request Aug 10, 2013

Merge pull request #79 from SamoylovMD/master
Added some iterating methods to Matrix (fixes #57)

@vkostyukov vkostyukov merged commit c7b3843 into vkostyukov:master Aug 10, 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