/** * Compare the computed sequence against randomly generated polynomials of different length */ @Test public void checkSequence_Random() { for( int i = 3; i < 20; i++ ) { Polynomial p = new Polynomial(i); for( int trial = 0; trial < 20; trial++ ) { for( int j = 0; j < p.size; j++ ) { p.c[j] = (rand.nextDouble()-0.5)*2; } double value = (rand.nextDouble()-0.5)*4; compareSequences( p, value); compareSequences( p, Double.POSITIVE_INFINITY); compareSequences( p, Double.NEGATIVE_INFINITY); } } }
private void compareSequences( Polynomial p, double value) { List<Double> expected = computeSturm(p,value); SturmSequence alg = new SturmSequence(p.size); alg.initialize(p); alg.computeFunctions(value); assertEquals(expected.size(),alg.sequenceLength); for( int j = 0; j < expected.size(); j++ ) { if( Double.isInfinite(expected.get(j)) ) { assertTrue(expected.get(j) == alg.f[j]); } else { assertEquals(expected.get(j),alg.f[j],Math.abs(alg.f[j])*1e-6); } } }
@Test public void countRealRoots() { Polynomial poly = Polynomial.wrap(-1,0,3,1); assertEquals(1, countRealRoots(poly, -3, -2)); assertEquals(0, countRealRoots(poly, 2, 3)); assertEquals(3, countRealRoots(poly, -3, 3)); assertEquals(3, countRealRoots(poly, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)); // see if it handles small polynomials correctly assertEquals(0, countRealRoots(Polynomial.wrap(2), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)); assertEquals(1, countRealRoots(Polynomial.wrap(2, 3), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)); assertEquals(0, countRealRoots(Polynomial.wrap(2, 3), 0, Double.POSITIVE_INFINITY)); assertEquals(0, countRealRoots(Polynomial.wrap(2, 3), Double.NEGATIVE_INFINITY, -10)); assertEquals(1, countRealRoots(Polynomial.wrap(2, 3), -2, -0.5)); assertEquals(0, countRealRoots(Polynomial.wrap(2, 3, 4), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)); assertEquals(2, countRealRoots(Polynomial.wrap(2, -1, -4), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)); // more difficult case where special code is required for infinite bounds poly = Polynomial.wrap(-1.322309e+02 , 3.713984e+02 , -5.007874e+02 , 3.744386e+02 ,-1.714667e+02 , 4.865014e+01 ,-1.059870e+01 , 1.642273e+00 ,-2.304341e-01,2.112391e-03,-2.273737e-13); assertEquals(2, countRealRoots(poly, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)); }
/** * Check sequence against a hand selected sequence */ @Test public void checkSequence() { Polynomial poly = Polynomial.wrap(-1,0,3,1,5,-3); SturmSequence alg = new SturmSequence(10); alg.initialize(poly); alg.computeFunctions(-3); List<Double> expected = computeSturm(poly,-3); assertEquals(expected.size(),alg.sequenceLength); for( int i = 0; i < expected.size(); i++ ) { assertEquals(expected.get(i),alg.f[i],1e-8); } }
/** * Examine a case which was found to cause problems */ @Test public void checkSpecificPoly01() { Polynomial poly = Polynomial.wrap(-41.118263303597175,-120.95384505825373,-417.8477600492497,-634.5308297409192, -347.7885168491812,6.771313016808563,79.70258790927392,31.68212813610444,5.0248961592587875, 0.2879701466217739,0.0); // Compare computed sequence to the standard compareSequences( poly, -500); compareSequences( poly, Double.NEGATIVE_INFINITY); SturmSequence alg = new SturmSequence(poly.size); alg.initialize(poly); int N = alg.countRealRoots(Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY); int M = alg.countRealRoots(-500,500); assertTrue(M <= N); }