commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Luc Maisonobe <Luc.Maison...@free.fr>
Subject Re: svn commit: r1488914 - in /commons/proper/math/trunk: ./ src/changes/ src/main/java/org/apache/commons/math3/analysis/integration/ src/test/java/org/apache/commons/math3/analysis/integration/
Date Mon, 03 Jun 2013 12:14:59 GMT
Le 03/06/2013 12:19, Gilles a écrit :
> Hello.

Hi Gilles,

> 
> On Mon, 03 Jun 2013 09:04:40 -0000, luc@apache.org wrote:
>> Author: luc
>> Date: Mon Jun  3 09:04:40 2013
>> New Revision: 1488914
>>
>> URL: http://svn.apache.org/r1488914
>> Log:
>> Added midpoint integration method.
>>
>> Patch contributed by Oleksandr Kornieiev.
>>
>> JIRA: MATH-967
> 
>> [...]
> 
>> +    private double stage(final int n)
>> +        throws TooManyEvaluationsException {
>> +
>> +        final double max = getMax();
>> +        final double min = getMin();
>> +
>> +        if (n == 0) {
>> +            final double midPoint = 0.5 * (max - min);
>                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> Is this correct?

No, it was wrong!

The fact it the problem was not detected by the tests cases is because
the contribution of this single wrong initial point is completely
negectible after just a few iteration (each iteration adds 2^n new points).

Solving the error was trivial, but finding a test case that triggers it
was difficult! It involved setting the integration parameters to very
low accuracy, so very few iterations are performed.

Thanks for spotting this.

Luc

> 
>> +            s = (max - min) * computeObjectiveValue(midPoint);
>> +            return s;
>> +        } else {
>> +            final long np = 1L << (n - 1);           // number of
>> new points in this stage
>> +            double sum = 0;
>> +            // spacing between adjacent new points
>> +            final double spacing = (max - min) / np;
>> +            double x = min + 0.5 * spacing;    // the first new point
>> +            for (long i = 0; i < np; i++) {
>> +                sum += computeObjectiveValue(x);
>> +                x += spacing;
>> +            }
>> +            // add the new sum to previously calculated result
>> +            s = 0.5 * (s + sum * spacing);
>> +            return s;
>> +        }
>> +    }
>> +
>> +    /** {@inheritDoc} */
>> +    protected double doIntegrate()
>> +        throws MathIllegalArgumentException,
>> TooManyEvaluationsException, MaxCountExceededException {
>> +
>> +        double oldt = stage(0);
>> +        iterations.incrementCount();
>> +        while (true) {
>> +            final int i = iterations.getCount();
>> +            final double t = stage(i);
>> +            if (i >= getMinimalIterationCount()) {
>> +                final double delta = FastMath.abs(t - oldt);
>> +                final double rLimit =
>> +                        getRelativeAccuracy() * (FastMath.abs(oldt)
>> + FastMath.abs(t)) * 0.5;
>> +                if ((delta <= rLimit) || (delta <=
>> getAbsoluteAccuracy())) {
>> +                    return t;
>> +                }
>> +            }
>> +            oldt = t;
>> +            iterations.incrementCount();
>> +        }
>> +
>> +    }
>> +
>> +}
>>
>> [...]
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message