Skip to content

Commit

Permalink
Fixes #66
Browse files Browse the repository at this point in the history
  • Loading branch information
hisohito committed Jul 24, 2013
1 parent 4f30b75 commit f948d78
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/main/java/org/la4j/matrix/AbstractMatrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,16 @@ public double diagonalProduct() {
return result;
}

@Override
public double product() {
return fold(Matrices.asProductAccumulator(1));
}

@Override
public double sum() {
return fold(Matrices.asSumAccumulator(0));
}

@Override
public Matrix triangularize() {
return triangularize(factory);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/la4j/matrix/AbstractSafeMatrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ public double diagonalProduct() {
return self.diagonalProduct();
}

@Override
public double product() {
return self.product();
}

@Override
public double sum() {
return self.sum();
}

@Override
public double determinant() {
return self.determinant();
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/la4j/matrix/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,20 @@ public interface Matrix extends Externalizable {
*/
double diagonalProduct();

/**
* Productizes up all elements of the matrix
*
* @return the product of all elements of the matrix
*/
double product();

/**
* Summarizes up all elements of the matrix
*
* @return the sum of all elements of the matrix
*/
double sum();

/**
* Returns the "determinant" of this matrix.
* <p>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/la4j/vector/AbstractSafeVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ public Vector divide(double value, Factory factory) {
return self.divide(value, factory);
}

@Override
public double product() {
return self.product();
}

@Override
public double sum() {
return self.sum();
}

@Override
public double innerProduct(Vector vector) {
return self.innerProduct(vector);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/la4j/vector/AbstractVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ public Vector divide(double value, Factory factory) {
return multiply(1.0 / value, factory);
}

@Override
public double product() {
return fold(Vectors.asProductAccumulator(1));
}

@Override
public double sum() {
return fold(Vectors.asSumAccumulator(0));
}

@Override
public double innerProduct(Vector vector) {

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/la4j/vector/Vector.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@ public interface Vector extends Externalizable {
*/
Vector divide(double value, Factory factory);

/**
* Productizes all elements of the vector
*
* @return product of all vector elements
*/
double product();

/**
* Summarizes all elements of the vector
*
* @return sum of all elements of the vector
*/
double sum();

/**
* Calculates the inner product of this vector and given {@code vector}.
*
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/org/la4j/matrix/AbstractMatrixTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ public void testTrace_3x3() {
assertTrue(Math.abs(a.trace() - 15.0) < Matrices.EPS);
}

public void testProduct_3x3() {
public void testDiagonalProduct_3x3() {

Matrix a = factory().createMatrix(new double[][] {
{ 1.0, 0.0, 0.0 },
Expand All @@ -767,6 +767,28 @@ public void testProduct_3x3() {
assertTrue(Math.abs(a.diagonalProduct() - 45.0) < Matrices.EPS);
}

public void testProduct_3x3() {

Matrix a = factory().createMatrix(new double[][] {
{1.0, 1.0, 1.0},
{1.0, 5.0, 1.0},
{1.0, 1.0, 9.0}
});

assertTrue(Math.abs(a.product() - 45.0) < Matrices.EPS);
}

public void testSum_3x3() {

Matrix a = factory().createMatrix(new double[][] {
{ 1.0, 0.0, 0.0 },
{ 0.0, 5.0, 0.0 },
{ 0.0, 0.0, 9.0 }
});

assertTrue(Math.abs(a.sum() - 15.0) < Matrices.EPS);
}

public void testDeterminant_3x3() {

Matrix a = factory().createMatrix(new double[][] {
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/la4j/vector/AbstractVectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,24 @@ public void testMultiply_3_3x1() {
assertEquals(c, a.multiply(b));
}

public void testProduct_3() {

Vector a = factory().createVector(new double[]
{2.0, 4.0, 6.0}
);

assertTrue(Math.abs(a.product() - 48.0) < Vectors.EPS);
}

public void testSum_3() {

Vector a = factory().createVector(new double[]
{2.0, 4.0, 6.0}
);

assertTrue(Math.abs(a.sum() - 12.0) < Vectors.EPS);
}

public void testDivide_3() {

Vector a = factory().createVector(new double[]
Expand Down

0 comments on commit f948d78

Please sign in to comment.