// the drivers private static IntegrationResult Integrate_Adaptive(IAdaptiveIntegrator integrator, EvaluationSettings s) { LinkedList<IAdaptiveIntegrator> list = new LinkedList<IAdaptiveIntegrator>(); list.AddFirst(integrator); int n = integrator.EvaluationCount; while (true) { // go through the intervals, adding estimates (and errors) // and noting which contributes the most error // keep track of the total value and uncertainty UncertainValue vTotal = new UncertainValue(); //double v = 0.0; //double u = 0.0; // keep track of which node contributes the most error LinkedListNode<IAdaptiveIntegrator> maxNode = null; double maxError = 0.0; LinkedListNode<IAdaptiveIntegrator> node = list.First; while (node != null) { IAdaptiveIntegrator i = node.Value; UncertainValue v = i.Estimate; vTotal += v; //UncertainValue uv = i.Estimate; //v += uv.Value; //u += uv.Uncertainty; if (v.Uncertainty > maxError) { maxNode = node; maxError = v.Uncertainty; } node = node.Next; } // if our error is small enough, return if ((vTotal.Uncertainty <= Math.Abs(vTotal.Value) * s.RelativePrecision) || (vTotal.Uncertainty <= s.AbsolutePrecision)) { return (new IntegrationResult(vTotal, n, s)); } //if ((vTotal.Uncertainty <= Math.Abs(vTotal.Value) * s.RelativePrecision) || (vTotal.Uncertainty <= s.AbsolutePrecision)) { // return (new IntegrationResult(vTotal.Value, n)); //} // if our evaluation count is too big, throw if (n > s.EvaluationBudget) throw new NonconvergenceException(); // subdivide the interval with the largest error IEnumerable<IAdaptiveIntegrator> divisions = maxNode.Value.Divide(); foreach (IAdaptiveIntegrator division in divisions) { list.AddBefore(maxNode, division); n += division.EvaluationCount; //v2 += division.Estimate; } list.Remove(maxNode); } }
// the drivers private static IntegrationResult Integrate_Adaptive(IAdaptiveIntegrator integrator, EvaluationSettings s) { LinkedList <IAdaptiveIntegrator> list = new LinkedList <IAdaptiveIntegrator>(); list.AddFirst(integrator); int n = integrator.EvaluationCount; while (true) { // go through the intervals, adding estimates (and errors) // and noting which contributes the most error // keep track of the total value and uncertainty UncertainValue vTotal = new UncertainValue(); //double v = 0.0; //double u = 0.0; // keep track of which node contributes the most error LinkedListNode <IAdaptiveIntegrator> maxNode = null; double maxError = 0.0; LinkedListNode <IAdaptiveIntegrator> node = list.First; while (node != null) { IAdaptiveIntegrator i = node.Value; UncertainValue v = i.Estimate; vTotal += v; //UncertainValue uv = i.Estimate; //v += uv.Value; //u += uv.Uncertainty; if (v.Uncertainty > maxError) { maxNode = node; maxError = v.Uncertainty; } node = node.Next; } // if our error is small enough, return if ((vTotal.Uncertainty <= Math.Abs(vTotal.Value) * s.RelativePrecision) || (vTotal.Uncertainty <= s.AbsolutePrecision)) { return(new IntegrationResult(vTotal, n, s)); } //if ((vTotal.Uncertainty <= Math.Abs(vTotal.Value) * s.RelativePrecision) || (vTotal.Uncertainty <= s.AbsolutePrecision)) { // return (new IntegrationResult(vTotal.Value, n)); //} // if our evaluation count is too big, throw if (n > s.EvaluationBudget) { throw new NonconvergenceException(); } // subdivide the interval with the largest error IEnumerable <IAdaptiveIntegrator> divisions = maxNode.Value.Divide(); foreach (IAdaptiveIntegrator division in divisions) { list.AddBefore(maxNode, division); n += division.EvaluationCount; //v2 += division.Estimate; } list.Remove(maxNode); } }
// the drivers private static IntegrationResult Integrate_Adaptive(IAdaptiveIntegrator integrator, IntegrationSettings settings) { Debug.Assert(integrator != null); Debug.Assert(settings != null); LinkedList <IAdaptiveIntegrator> list = new LinkedList <IAdaptiveIntegrator>(); list.AddFirst(integrator); int n = integrator.EvaluationCount; while (true) { // go through the intervals, adding estimates (and errors) // and noting which contributes the most error // keep track of the total value and uncertainty UncertainValue vTotal = new UncertainValue(); // keep track of which node contributes the most error LinkedListNode <IAdaptiveIntegrator> maxNode = null; double maxError = 0.0; LinkedListNode <IAdaptiveIntegrator> node = list.First; while (node != null) { IAdaptiveIntegrator i = node.Value; UncertainValue v = i.Estimate; vTotal += v; if (v.Uncertainty > maxError) { maxNode = node; maxError = v.Uncertainty; } node = node.Next; } // Inform listeners of our latest result. if (settings.Listener != null) { settings.Listener(new IntegrationResult(vTotal, n, settings)); } // if our error is small enough, return double tol = settings.ComputePrecision(vTotal.Value); if (vTotal.Uncertainty <= tol) { // Don't claim uncertainty significantly less than tol. if (vTotal.Uncertainty < tol / 2.0) { vTotal = new UncertainValue(vTotal.Value, tol / 2.0); } return(new IntegrationResult(vTotal, n, settings)); } // if our evaluation count is too big, throw if (n > settings.EvaluationBudget) { throw new NonconvergenceException(); } // Subdivide the interval with the largest error IEnumerable <IAdaptiveIntegrator> divisions = maxNode.Value.Divide(); foreach (IAdaptiveIntegrator division in divisions) { list.AddBefore(maxNode, division); n += division.EvaluationCount; } list.Remove(maxNode); } }