Author: celestin Date: Sat Jun 2 05:34:40 2012 New Revision: 1345443 URL: http://svn.apache.org/viewvc?rev=1345443&view=rev Log: MATH-795: factored out testBasicFunctions().
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java?rev=1345443&r1=1345442&r2=1345443&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java Sat Jun 2 05:34:40 2012 @@ -510,6 +510,7 @@ public class ArrayRealVectorTest extends @Test public void testBasicFunctions() { + super.testBasicFunctions(); ArrayRealVector v1 = new ArrayRealVector(vec1); ArrayRealVector v2 = new ArrayRealVector(vec2); ArrayRealVector v5 = new ArrayRealVector(vec5); @@ -517,141 +518,6 @@ public class ArrayRealVectorTest extends RealVectorTestImpl v2_t = new RealVectorTestImpl(vec2); - // emacs calc: [-4, 0, 3, 1, -6, 3] A --> 8.4261497731763586307 - double d_getNorm = v5.getNorm(); - Assert.assertEquals("compare values ", 8.4261497731763586307, d_getNorm, normTolerance); - - // emacs calc: [-4, 0, 3, 1, -6, 3] vN --> 17 - double d_getL1Norm = v5.getL1Norm(); - Assert.assertEquals("compare values ", 17.0, d_getL1Norm, normTolerance); - - // emacs calc: [-4, 0, 3, 1, -6, 3] vn --> 6 - double d_getLInfNorm = v5.getLInfNorm(); - Assert.assertEquals("compare values ", 6.0, d_getLInfNorm, normTolerance); - - - //octave = sqrt(sumsq(v1-v2)) - double dist = v1.getDistance(v2); - Assert.assertEquals("compare values ",v1.subtract(v2).getNorm(), dist, normTolerance); - - //octave = sqrt(sumsq(v1-v2)) - double dist_2 = v1.getDistance(v2_t); - Assert.assertEquals("compare values ", v1.subtract(v2).getNorm(),dist_2, normTolerance); - - //octave = sqrt(sumsq(v1-v2)) - double dist_3 = v1.getDistance(v2); - Assert.assertEquals("compare values ", v1.subtract(v2).getNorm(),dist_3, normTolerance); - - //octave = ??? - double d_getL1Distance = v1. getL1Distance(v2); - Assert.assertEquals("compare values ", 9d, d_getL1Distance, normTolerance); - - double d_getL1Distance_2 = v1.getL1Distance(v2_t); - Assert.assertEquals("compare values ", 9d, d_getL1Distance_2, normTolerance); - - double d_getL1Distance_3 = v1.getL1Distance(v2); - Assert.assertEquals("compare values ", 9d, d_getL1Distance_3, normTolerance); - - //octave = ??? - double d_getLInfDistance = v1.getLInfDistance(v2); - Assert.assertEquals("compare values ", 3d, d_getLInfDistance, normTolerance); - - double d_getLInfDistance_2 = v1. getLInfDistance(v2_t); - Assert.assertEquals("compare values ", 3d, d_getLInfDistance_2, normTolerance); - - double d_getLInfDistance_3 = v1. getLInfDistance(v2); - Assert.assertEquals("compare values ", 3d, d_getLInfDistance_3, normTolerance); - - //octave = v1 + v2 - ArrayRealVector v_add = v1.add(v2); - double[] result_add = {5d, 7d, 9d}; - assertClose("compare vect" ,v_add.toArray(), result_add, normTolerance); - - RealVectorTestImpl vt2 = new RealVectorTestImpl(vec2); - RealVector v_add_i = v1.add(vt2); - double[] result_add_i = {5d, 7d, 9d}; - assertClose("compare vect" ,v_add_i.toArray(),result_add_i,normTolerance); - - //octave = v1 - v2 - ArrayRealVector v_subtract = v1.subtract(v2); - double[] result_subtract = {-3d, -3d, -3d}; - assertClose("compare vect" ,v_subtract.toArray(),result_subtract,normTolerance); - - RealVector v_subtract_i = v1.subtract(vt2); - double[] result_subtract_i = {-3d, -3d, -3d}; - assertClose("compare vect" ,v_subtract_i.toArray(),result_subtract_i,normTolerance); - - // octave v1 .* v2 - ArrayRealVector v_ebeMultiply = v1.ebeMultiply(v2); - double[] result_ebeMultiply = {4d, 10d, 18d}; - assertClose("compare vect" ,v_ebeMultiply.toArray(),result_ebeMultiply,normTolerance); - - RealVector v_ebeMultiply_2 = v1.ebeMultiply(v2_t); - double[] result_ebeMultiply_2 = {4d, 10d, 18d}; - assertClose("compare vect" ,v_ebeMultiply_2.toArray(),result_ebeMultiply_2,normTolerance); - - RealVector v_ebeMultiply_3 = v1.ebeMultiply(v2); - double[] result_ebeMultiply_3 = {4d, 10d, 18d}; - assertClose("compare vect" ,v_ebeMultiply_3.toArray(),result_ebeMultiply_3,normTolerance); - - // octave v1 ./ v2 - ArrayRealVector v_ebeDivide = v1.ebeDivide(v2); - double[] result_ebeDivide = {0.25d, 0.4d, 0.5d}; - assertClose("compare vect" ,v_ebeDivide.toArray(),result_ebeDivide,normTolerance); - - RealVector v_ebeDivide_2 = v1.ebeDivide(v2_t); - double[] result_ebeDivide_2 = {0.25d, 0.4d, 0.5d}; - assertClose("compare vect" ,v_ebeDivide_2.toArray(),result_ebeDivide_2,normTolerance); - - RealVector v_ebeDivide_3 = v1.ebeDivide(v2); - double[] result_ebeDivide_3 = {0.25d, 0.4d, 0.5d}; - assertClose("compare vect" ,v_ebeDivide_3.toArray(),result_ebeDivide_3,normTolerance); - - // octave dot(v1,v2) - double dot = v1.dotProduct(v2); - Assert.assertEquals("compare val ",32d, dot, normTolerance); - - // octave dot(v1,v2_t) - double dot_2 = v1.dotProduct(v2_t); - Assert.assertEquals("compare val ",32d, dot_2, normTolerance); - - RealMatrix m_outerProduct = v1.outerProduct(v2); - Assert.assertEquals("compare val ",4d, m_outerProduct.getEntry(0,0), normTolerance); - - RealMatrix m_outerProduct_2 = v1.outerProduct(v2_t); - Assert.assertEquals("compare val ",4d, m_outerProduct_2.getEntry(0,0), normTolerance); - - RealMatrix m_outerProduct_3 = v1.outerProduct(v2); - Assert.assertEquals("compare val ",4d, m_outerProduct_3.getEntry(0,0), normTolerance); - - RealVector v_unitVector = v1.unitVector(); - RealVector v_unitVector_2 = v1.mapDivide(v1.getNorm()); - assertClose("compare vect" ,v_unitVector.toArray(),v_unitVector_2.toArray(),normTolerance); - - try { - v_null.unitVector(); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - // expected behavior - } - - ArrayRealVector v_unitize = v1.copy(); - v_unitize.unitize(); - assertClose("compare vect" ,v_unitVector_2.toArray(),v_unitize.toArray(),normTolerance); - try { - v_null.unitize(); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - // expected behavior - } - - RealVector v_projection = v1.projection(v2); - double[] result_projection = {1.662337662337662, 2.0779220779220777, 2.493506493506493}; - assertClose("compare vect", v_projection.toArray(), result_projection, normTolerance); - - RealVector v_projection_2 = v1.projection(v2_t); - double[] result_projection_2 = {1.662337662337662, 2.0779220779220777, 2.493506493506493}; - assertClose("compare vect", v_projection_2.toArray(), result_projection_2, normTolerance); } @Test Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java?rev=1345443&r1=1345442&r2=1345443&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java Sat Jun 2 05:34:40 2012 @@ -44,8 +44,10 @@ import org.apache.commons.math3.analysis import org.apache.commons.math3.analysis.function.Tan; import org.apache.commons.math3.analysis.function.Tanh; import org.apache.commons.math3.analysis.function.Ulp; +import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; +import org.apache.commons.math3.linear.ArrayRealVectorTest.RealVectorTestImpl; import org.junit.Test; @@ -506,6 +508,165 @@ public abstract class RealVectorAbstract assertClose("compare vectors" ,result_mapUlpToSelf,v_mapUlpToSelf.toArray(),normTolerance); } + @Test + public void testBasicFunctions() { + final RealVector v1 = create(vec1); + final RealVector v2 = create(vec2); + final RealVector v5 = create(vec5); + final RealVector v_null = create(vec_null); + + final RealVector v2_t = createAlien(vec2); + + // emacs calc: [-4, 0, 3, 1, -6, 3] A --> 8.4261497731763586307 + double d_getNorm = v5.getNorm(); + Assert.assertEquals("compare values ", 8.4261497731763586307, + d_getNorm, normTolerance); + + // emacs calc: [-4, 0, 3, 1, -6, 3] vN --> 17 + double d_getL1Norm = v5.getL1Norm(); + Assert.assertEquals("compare values ", 17.0, d_getL1Norm, + normTolerance); + + // emacs calc: [-4, 0, 3, 1, -6, 3] vn --> 6 + double d_getLInfNorm = v5.getLInfNorm(); + Assert.assertEquals("compare values ", 6.0, d_getLInfNorm, + normTolerance); + + // octave = sqrt(sumsq(v1-v2)) + double dist = v1.getDistance(v2); + Assert.assertEquals("compare values ", v1.subtract(v2).getNorm(), + dist, normTolerance); + + // octave = sqrt(sumsq(v1-v2)) + double dist_2 = v1.getDistance(v2_t); + Assert.assertEquals("compare values ", v1.subtract(v2).getNorm(), + dist_2, normTolerance); + + // octave = ??? + double d_getL1Distance = v1.getL1Distance(v2); + Assert.assertEquals("compare values ", 9d, d_getL1Distance, + normTolerance); + + double d_getL1Distance_2 = v1.getL1Distance(v2_t); + Assert.assertEquals("compare values ", 9d, d_getL1Distance_2, + normTolerance); + + // octave = ??? + double d_getLInfDistance = v1.getLInfDistance(v2); + Assert.assertEquals("compare values ", 3d, d_getLInfDistance, + normTolerance); + + double d_getLInfDistance_2 = v1.getLInfDistance(v2_t); + Assert.assertEquals("compare values ", 3d, d_getLInfDistance_2, + normTolerance); + + // octave = v1 + v2 + final RealVector v_add = v1.add(v2); + double[] result_add = { + 5d, 7d, 9d + }; + assertClose("compare vect", v_add.toArray(), result_add, normTolerance); + + final RealVector vt2 = createAlien(vec2); + RealVector v_add_i = v1.add(vt2); + double[] result_add_i = { + 5d, 7d, 9d + }; + assertClose("compare vect", v_add_i.toArray(), result_add_i, + normTolerance); + + // octave = v1 - v2 + final RealVector v_subtract = v1.subtract(v2); + double[] result_subtract = { + -3d, -3d, -3d + }; + assertClose("compare vect", v_subtract.toArray(), result_subtract, + normTolerance); + + final RealVector v_subtract_i = v1.subtract(vt2); + double[] result_subtract_i = { + -3d, -3d, -3d + }; + assertClose("compare vect", v_subtract_i.toArray(), result_subtract_i, + normTolerance); + + // octave v1 .* v2 + final RealVector v_ebeMultiply = v1.ebeMultiply(v2); + double[] result_ebeMultiply = { + 4d, 10d, 18d + }; + assertClose("compare vect", v_ebeMultiply.toArray(), + result_ebeMultiply, normTolerance); + + final RealVector v_ebeMultiply_2 = v1.ebeMultiply(v2_t); + double[] result_ebeMultiply_2 = { + 4d, 10d, 18d + }; + assertClose("compare vect", v_ebeMultiply_2.toArray(), + result_ebeMultiply_2, normTolerance); + + // octave v1 ./ v2 + final RealVector v_ebeDivide = v1.ebeDivide(v2); + double[] result_ebeDivide = { + 0.25d, 0.4d, 0.5d + }; + assertClose("compare vect", v_ebeDivide.toArray(), result_ebeDivide, + normTolerance); + + final RealVector v_ebeDivide_2 = v1.ebeDivide(v2_t); + double[] result_ebeDivide_2 = { + 0.25d, 0.4d, 0.5d + }; + assertClose("compare vect", v_ebeDivide_2.toArray(), + result_ebeDivide_2, normTolerance); + + // octave dot(v1,v2) + double dot = v1.dotProduct(v2); + Assert.assertEquals("compare val ", 32d, dot, normTolerance); + + // octave dot(v1,v2_t) + double dot_2 = v1.dotProduct(v2_t); + Assert.assertEquals("compare val ", 32d, dot_2, normTolerance); + + RealMatrix m_outerProduct = v1.outerProduct(v2); + Assert.assertEquals("compare val ", 4d, m_outerProduct.getEntry(0, 0), + normTolerance); + + RealMatrix m_outerProduct_2 = v1.outerProduct(v2_t); + Assert.assertEquals("compare val ", 4d, + m_outerProduct_2.getEntry(0, 0), normTolerance); + + RealVector v_unitVector = v1.unitVector(); + RealVector v_unitVector_2 = v1.mapDivide(v1.getNorm()); + assertClose("compare vect", v_unitVector.toArray(), + v_unitVector_2.toArray(), normTolerance); + + try { + v_null.unitVector(); + Assert.fail("Expecting MathArithmeticException"); + } catch (MathArithmeticException ex) { + // expected behavior + } + + RealVector v_unitize = v1.copy(); + v_unitize.unitize(); + assertClose("compare vect" ,v_unitVector_2.toArray(),v_unitize.toArray(),normTolerance); + try { + v_null.unitize(); + Assert.fail("Expecting MathArithmeticException"); + } catch (MathArithmeticException ex) { + // expected behavior + } + + RealVector v_projection = v1.projection(v2); + double[] result_projection = {1.662337662337662, 2.0779220779220777, 2.493506493506493}; + assertClose("compare vect", v_projection.toArray(), result_projection, normTolerance); + + RealVector v_projection_2 = v1.projection(v2_t); + double[] result_projection_2 = {1.662337662337662, 2.0779220779220777, 2.493506493506493}; + assertClose("compare vect", v_projection_2.toArray(), result_projection_2, normTolerance); + } + /* * TESTS OF THE VISITOR PATTERN */ Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java?rev=1345443&r1=1345442&r2=1345443&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java Sat Jun 2 05:34:40 2012 @@ -183,6 +183,19 @@ public class RealVectorTest extends Real } } + @Override + public void testBasicFunctions() { + /* + * TODO this test is huge, and some of the methods being tested are not + * supported by TestVectorImpl. For the time being, this test is skipped + * (testBasicFunctions() is overriden, ommitting the @Test anotation). + * + * What should really be done: split testBasicFunctions() in many + * smaller unit tests, and skip only those tests which are not meaningfull + * for RealVector. + */ + } + @Test public void testMap() throws Exception { double[] vec1Squared = { 1d, 4d, 9d, 16d, 25d }; Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java?rev=1345443&r1=1345442&r2=1345443&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java Sat Jun 2 05:34:40 2012 @@ -324,131 +324,6 @@ public class SparseRealVectorTest extend } @Test - public void testBasicFunctions() { - OpenMapRealVector v1 = new OpenMapRealVector(vec1); - OpenMapRealVector v2 = new OpenMapRealVector(vec2); - OpenMapRealVector v5 = new OpenMapRealVector(vec5); - OpenMapRealVector v_null = new OpenMapRealVector(vec_null); - - SparseRealVectorTestImpl v2_t = new SparseRealVectorTestImpl(vec2); - - // emacs calc: [-4, 0, 3, 1, -6, 3] A --> 8.4261497731763586307 - double d_getNorm = v5.getNorm(); - Assert.assertEquals("compare values ", 8.4261497731763586307, d_getNorm, normTolerance); - - // emacs calc: [-4, 0, 3, 1, -6, 3] vN --> 17 - double d_getL1Norm = v5.getL1Norm(); - Assert.assertEquals("compare values ", 17.0, d_getL1Norm, normTolerance); - - // emacs calc: [-4, 0, 3, 1, -6, 3] vn --> 6 - double d_getLInfNorm = v5.getLInfNorm(); - Assert.assertEquals("compare values ", 6.0, d_getLInfNorm, normTolerance); - - //octave = sqrt(sumsq(v1-v2)) - double dist = v1.getDistance(v2); - Assert.assertEquals("compare values ",v1.subtract(v2).getNorm(), dist, normTolerance); - - //octave = sqrt(sumsq(v1-v2)) - double dist_2 = v1.getDistance(v2_t); - Assert.assertEquals("compare values ", v1.subtract(v2).getNorm(),dist_2, normTolerance); - - //octave = ??? - double d_getL1Distance = v1. getL1Distance(v2); - Assert.assertEquals("compare values ", 9d, d_getL1Distance, normTolerance); - - double d_getL1Distance_2 = v1. getL1Distance(v2_t); - Assert.assertEquals("compare values ", 9d, d_getL1Distance_2, normTolerance); - - //octave = ??? - double d_getLInfDistance = v1. getLInfDistance(v2); - Assert.assertEquals("compare values ", 3d, d_getLInfDistance, normTolerance); - - double d_getLInfDistance_2 = v1. getLInfDistance(v2_t); - Assert.assertEquals("compare values ", 3d, d_getLInfDistance_2, normTolerance); - - //octave = v1 + v2 - OpenMapRealVector v_add = v1.add(v2); - double[] result_add = {5d, 7d, 9d}; - assertClose("compare vect" ,v_add.toArray(),result_add,normTolerance); - - SparseRealVectorTestImpl vt2 = new SparseRealVectorTestImpl(vec2); - RealVector v_add_i = v1.add(vt2); - double[] result_add_i = {5d, 7d, 9d}; - assertClose("compare vect" ,v_add_i.toArray(),result_add_i,normTolerance); - - //octave = v1 - v2 - OpenMapRealVector v_subtract = v1.subtract(v2); - double[] result_subtract = {-3d, -3d, -3d}; - assertClose("compare vect" ,v_subtract.toArray(),result_subtract,normTolerance); - - RealVector v_subtract_i = v1.subtract(vt2); - double[] result_subtract_i = {-3d, -3d, -3d}; - assertClose("compare vect" ,v_subtract_i.toArray(),result_subtract_i,normTolerance); - - // octave v1 .* v2 - RealVector v_ebeMultiply = v1.ebeMultiply(v2); - double[] result_ebeMultiply = {4d, 10d, 18d}; - assertClose("compare vect" ,v_ebeMultiply.toArray(),result_ebeMultiply,normTolerance); - - RealVector v_ebeMultiply_2 = v1.ebeMultiply(v2_t); - double[] result_ebeMultiply_2 = {4d, 10d, 18d}; - assertClose("compare vect" ,v_ebeMultiply_2.toArray(),result_ebeMultiply_2,normTolerance); - - // octave v1 ./ v2 - RealVector v_ebeDivide = v1.ebeDivide(v2); - double[] result_ebeDivide = {0.25d, 0.4d, 0.5d}; - assertClose("compare vect" ,v_ebeDivide.toArray(),result_ebeDivide,normTolerance); - - RealVector v_ebeDivide_2 = v1.ebeDivide(v2_t); - double[] result_ebeDivide_2 = {0.25d, 0.4d, 0.5d}; - assertClose("compare vect" ,v_ebeDivide_2.toArray(),result_ebeDivide_2,normTolerance); - - // octave dot(v1,v2) - double dot = v1.dotProduct(v2); - Assert.assertEquals("compare val ",32d, dot, normTolerance); - - // octave dot(v1,v2_t) - double dot_2 = v1.dotProduct(v2_t); - Assert.assertEquals("compare val ",32d, dot_2, normTolerance); - - RealMatrix m_outerProduct = v1.outerProduct(v2); - Assert.assertEquals("compare val ",4d, m_outerProduct.getEntry(0,0), normTolerance); - - RealMatrix m_outerProduct_2 = v1.outerProduct(v2_t); - Assert.assertEquals("compare val ",4d, m_outerProduct_2.getEntry(0,0), normTolerance); - - RealVector v_unitVector = v1.unitVector(); - RealVector v_unitVector_2 = v1.mapDivide(v1.getNorm()); - assertClose("compare vect" ,v_unitVector.toArray(),v_unitVector_2.toArray(),normTolerance); - - try { - v_null.unitVector(); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - // expected behavior - } - - OpenMapRealVector v_unitize = v1.copy(); - v_unitize.unitize(); - assertClose("compare vect" ,v_unitVector_2.toArray(),v_unitize.toArray(),normTolerance); - try { - v_null.unitize(); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - // expected behavior - } - - RealVector v_projection = v1.projection(v2); - double[] result_projection = {1.662337662337662, 2.0779220779220777, 2.493506493506493}; - assertClose("compare vect", v_projection.toArray(), result_projection, normTolerance); - - RealVector v_projection_2 = v1.projection(v2_t); - double[] result_projection_2 = {1.662337662337662, 2.0779220779220777, 2.493506493506493}; - assertClose("compare vect", v_projection_2.toArray(), result_projection_2, normTolerance); - - } - - @Test public void testOuterProduct() { final OpenMapRealVector u = new OpenMapRealVector(new double[] {1, 2, -3}); final OpenMapRealVector v = new OpenMapRealVector(new double[] {4, -2});