From f1a6746f3bc3540e5f50ec7d3eb9fa0dc65d2f45 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 28 Jul 2013 15:31:13 +0200 Subject: [PATCH] Integration: clean up old overly complicated formatting --- src/Numerics/Integrate.cs | 23 +- .../DoubleExponentialTransformation.cs | 255 +++++++++--------- .../Integration/NewtonCotesTrapeziumRule.cs | 28 +- src/Numerics/Integration/SimpsonRule.cs | 13 +- src/Numerics/Interpolate.cs | 16 +- 5 files changed, 142 insertions(+), 193 deletions(-) diff --git a/src/Numerics/Integrate.cs b/src/Numerics/Integrate.cs index 5b643adc..2974e37d 100644 --- a/src/Numerics/Integrate.cs +++ b/src/Numerics/Integrate.cs @@ -51,17 +51,9 @@ namespace MathNet.Numerics /// Where the interval stops, inclusive and finite. /// The expected relative accuracy of the approximation. /// Approximation of the finite integral in the given interval. - public static double OnClosedInterval( - Func f, - double intervalBegin, - double intervalEnd, - double targetAbsoluteError) + public static double OnClosedInterval(Func f, double intervalBegin, double intervalEnd, double targetAbsoluteError) { - return Det.Integrate( - f, - intervalBegin, - intervalEnd, - targetAbsoluteError); + return Det.Integrate(f, intervalBegin, intervalEnd, targetAbsoluteError); } /// @@ -71,16 +63,9 @@ namespace MathNet.Numerics /// Where the interval starts, inclusive and finite. /// Where the interval stops, inclusive and finite. /// Approximation of the finite integral in the given interval. - public static double OnClosedInterval( - Func f, - double intervalBegin, - double intervalEnd) + public static double OnClosedInterval(Func f, double intervalBegin, double intervalEnd) { - return Det.Integrate( - f, - intervalBegin, - intervalEnd, - 1e-8); + return Det.Integrate(f, intervalBegin, intervalEnd, 1e-8); } } } diff --git a/src/Numerics/Integration/DoubleExponentialTransformation.cs b/src/Numerics/Integration/DoubleExponentialTransformation.cs index da46e769..4ab6c256 100644 --- a/src/Numerics/Integration/DoubleExponentialTransformation.cs +++ b/src/Numerics/Integration/DoubleExponentialTransformation.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2010 Math.NET +// Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -39,6 +39,129 @@ namespace MathNet.Numerics.Integration /// public class DoubleExponentialTransformation { + /// + /// Maximum number of iterations, until the asked + /// maximum error is (likely to be) satisfied. + /// + const int NumberOfMaximumLevels = 10; + + /// + /// Abscissa vector per level provider. + /// + IEnumerable _levelAbcissas; + + /// + /// Weight vector per level provider. + /// + IEnumerable _levelWeights; + + /// + /// Approximate the integral by the double exponential transformation + /// + /// The analytic smooth function to integrate. + /// Where the interval starts, inclusive and finite. + /// Where the interval stops, inclusive and finite. + /// The expected relative accuracy of the approximation. + /// Approximation of the finite integral in the given interval. + public double Integrate(Func f, double intervalBegin, double intervalEnd, double targetRelativeError) + { + if (_levelAbcissas == null) + { + _levelAbcissas = ProvideLevelAbcissas(); + _levelWeights = ProvideLevelWeights(); + } + + return NewtonCotesTrapeziumRule.IntegrateAdaptiveTransformedOdd( + f, + intervalBegin, intervalEnd, + _levelAbcissas, _levelWeights, + 1, targetRelativeError); + } + + /// + /// Abscissa vector per level provider. + /// + /// Level Enumerator. + internal static IEnumerable ProvideLevelAbcissas() + { + for (int i = 0; i < NumberOfMaximumLevels; i++) + { + yield return EvaluateAbcissas(i); + } + } + + /// + /// Weight vector per level provider. + /// + /// Level Enumerator. + internal static IEnumerable ProvideLevelWeights() + { + for (int i = 0; i < NumberOfMaximumLevels; i++) + { + yield return EvaluateWeights(i); + } + } + + /// + /// Compute the abscissa vector for a single level. + /// + /// The level to evaluate the abscissa vector for. + /// Abscissa Vector. + static double[] EvaluateAbcissas(int level) + { + if (level < PrecomputedAbscissas.Length) + { + return PrecomputedAbscissas[level]; + } + + double step = level <= 1 ? 1.0 : (1.0/(2 << (level - 2))); + double offset = level == 0 ? 0.0 : (1.0/(2 << (level - 1))); + int length = level == 0 ? 4 : (3 << (level - 1)); + + double t = 0; + double[] abcissas = new double[length]; + for (int i = 0; i < abcissas.Length; i++) + { + double arg = offset + t; + t += step; + + abcissas[i] = Math.Tanh(Constants.PiOver2*Math.Sinh(arg)); + } + + return abcissas; + } + + /// + /// Compute the weight vector for a single level. + /// + /// The level to evaluate the weight vector for. + /// Weight Vector. + static double[] EvaluateWeights(int level) + { + if (level < PrecomputedWeights.Length) + { + return PrecomputedWeights[level]; + } + + double step = level <= 1 ? 1.0 : (1.0/(2 << (level - 2))); + double offset = level == 0 ? 0.0 : (1.0/(2 << (level - 1))); + int length = level == 0 ? 4 : (3 << (level - 1)); + + double t = 0; + double[] weights = new double[length]; + for (int i = 0; i < weights.Length; i++) + { + double arg = offset + t; + t += step; + + // TODO: reuse abcissas as computed in EvaluateAbcissas + double abcissa = Math.Tanh(Constants.PiOver2*Math.Sinh(arg)); + weights[i] = Constants.PiOver2*(1 - (abcissa*abcissa))*Math.Cosh(arg); + } + + return weights; + } + #region Precomputed Abcissas and Weights /// @@ -486,135 +609,5 @@ namespace MathNet.Numerics.Integration }; #endregion - - /// - /// Maximum number of iterations, until the asked - /// maximum error is (likely to be) satisfied. - /// - const int NumberOfMaximumLevels = 10; - - /// - /// Abscissa vector per level provider. - /// - IEnumerable _levelAbcissas; - - /// - /// Weight vector per level provider. - /// - IEnumerable _levelWeights; - - /// - /// Approximate the integral by the double exponential transformation - /// - /// The analytic smooth function to integrate. - /// Where the interval starts, inclusive and finite. - /// Where the interval stops, inclusive and finite. - /// The expected relative accuracy of the approximation. - /// Approximation of the finite integral in the given interval. - public double Integrate( - Func f, - double intervalBegin, - double intervalEnd, - double targetRelativeError) - { - if (_levelAbcissas == null) - { - _levelAbcissas = ProvideLevelAbcissas(); - _levelWeights = ProvideLevelWeights(); - } - - return NewtonCotesTrapeziumRule.IntegrateAdaptiveTransformedOdd( - f, - intervalBegin, - intervalEnd, - _levelAbcissas, - _levelWeights, - 1, - targetRelativeError); - } - - /// - /// Abscissa vector per level provider. - /// - /// Level Enumerator. - internal static IEnumerable ProvideLevelAbcissas() - { - for (int i = 0; i < NumberOfMaximumLevels; i++) - { - yield return EvaluateAbcissas(i); - } - } - - /// - /// Weight vector per level provider. - /// - /// Level Enumerator. - internal static IEnumerable ProvideLevelWeights() - { - for (int i = 0; i < NumberOfMaximumLevels; i++) - { - yield return EvaluateWeights(i); - } - } - - /// - /// Compute the abscissa vector for a single level. - /// - /// The level to evaluate the abscissa vector for. - /// Abscissa Vector. - static double[] EvaluateAbcissas(int level) - { - if (level < PrecomputedAbscissas.Length) - { - return PrecomputedAbscissas[level]; - } - - double step = level <= 1 ? 1.0 : (1.0/(2 << (level - 2))); - double offset = level == 0 ? 0.0 : (1.0/(2 << (level - 1))); - int length = level == 0 ? 4 : (3 << (level - 1)); - - double t = 0; - double[] abcissas = new double[length]; - for (int i = 0; i < abcissas.Length; i++) - { - double arg = offset + t; - t += step; - - abcissas[i] = Math.Tanh(Constants.PiOver2*Math.Sinh(arg)); - } - - return abcissas; - } - - /// - /// Compute the weight vector for a single level. - /// - /// The level to evaluate the weight vector for. - /// Weight Vector. - static double[] EvaluateWeights(int level) - { - if (level < PrecomputedWeights.Length) - { - return PrecomputedWeights[level]; - } - - double step = level <= 1 ? 1.0 : (1.0/(2 << (level - 2))); - double offset = level == 0 ? 0.0 : (1.0/(2 << (level - 1))); - int length = level == 0 ? 4 : (3 << (level - 1)); - - double t = 0; - double[] weights = new double[length]; - for (int i = 0; i < weights.Length; i++) - { - double arg = offset + t; - t += step; - - // TODO: reuse abcissas as computed in EvaluateAbcissas - double abcissa = Math.Tanh(Constants.PiOver2*Math.Sinh(arg)); - weights[i] = Constants.PiOver2*(1 - (abcissa*abcissa))*Math.Cosh(arg); - } - - return weights; - } } } diff --git a/src/Numerics/Integration/NewtonCotesTrapeziumRule.cs b/src/Numerics/Integration/NewtonCotesTrapeziumRule.cs index 35e4c6fd..59f18eb4 100644 --- a/src/Numerics/Integration/NewtonCotesTrapeziumRule.cs +++ b/src/Numerics/Integration/NewtonCotesTrapeziumRule.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2010 Math.NET +// Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -49,10 +49,7 @@ namespace MathNet.Numerics.Integration /// Where the interval starts, inclusive and finite. /// Where the interval stops, inclusive and finite. /// Approximation of the finite integral in the given interval. - public static double IntegrateTwoPoint( - Func f, - double intervalBegin, - double intervalEnd) + public static double IntegrateTwoPoint(Func f, double intervalBegin, double intervalEnd) { if (f == null) { @@ -70,11 +67,7 @@ namespace MathNet.Numerics.Integration /// Where the interval stops, inclusive and finite. /// Number of composite subdivision partitions. /// Approximation of the finite integral in the given interval. - public static double IntegrateComposite( - Func f, - double intervalBegin, - double intervalEnd, - int numberOfPartitions) + public static double IntegrateComposite(Func f, double intervalBegin, double intervalEnd, int numberOfPartitions) { if (f == null) { @@ -108,11 +101,7 @@ namespace MathNet.Numerics.Integration /// Where the interval stops, inclusive and finite. /// The expected accuracy of the approximation. /// Approximation of the finite integral in the given interval. - public static double IntegrateAdaptive( - Func f, - double intervalBegin, - double intervalEnd, - double targetError) + public static double IntegrateAdaptive(Func f, double intervalBegin, double intervalEnd, double targetError) { if (f == null) { @@ -157,12 +146,9 @@ namespace MathNet.Numerics.Integration /// Approximation of the finite integral in the given interval. public static double IntegrateAdaptiveTransformedOdd( Func f, - double intervalBegin, - double intervalEnd, - IEnumerable levelAbscissas, - IEnumerable levelWeights, - double levelOneStep, - double targetRelativeError) + double intervalBegin, double intervalEnd, + IEnumerable levelAbscissas, IEnumerable levelWeights, + double levelOneStep, double targetRelativeError) { if (f == null) { diff --git a/src/Numerics/Integration/SimpsonRule.cs b/src/Numerics/Integration/SimpsonRule.cs index 721cf37f..a1717184 100644 --- a/src/Numerics/Integration/SimpsonRule.cs +++ b/src/Numerics/Integration/SimpsonRule.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2010 Math.NET +// Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -46,10 +46,7 @@ namespace MathNet.Numerics.Integration /// Where the interval starts, inclusive and finite. /// Where the interval stops, inclusive and finite. /// Approximation of the finite integral in the given interval. - public static double IntegrateThreePoint( - Func f, - double intervalBegin, - double intervalEnd) + public static double IntegrateThreePoint(Func f, double intervalBegin, double intervalEnd) { if (f == null) { @@ -68,11 +65,7 @@ namespace MathNet.Numerics.Integration /// Where the interval stops, inclusive and finite. /// Even number of composite subdivision partitions. /// Approximation of the finite integral in the given interval. - public static double IntegrateComposite( - Func f, - double intervalBegin, - double intervalEnd, - int numberOfPartitions) + public static double IntegrateComposite(Func f, double intervalBegin, double intervalEnd, int numberOfPartitions) { if (f == null) { diff --git a/src/Numerics/Interpolate.cs b/src/Numerics/Interpolate.cs index c558526a..475aa423 100644 --- a/src/Numerics/Interpolate.cs +++ b/src/Numerics/Interpolate.cs @@ -48,9 +48,7 @@ namespace MathNet.Numerics /// which can then be used to compute interpolations and extrapolations /// on arbitrary points. /// - public static IInterpolation Common( - IList points, - IList values) + public static IInterpolation Common(IList points, IList values) { return RationalWithoutPoles(points, values); } @@ -65,9 +63,7 @@ namespace MathNet.Numerics /// which can then be used to compute interpolations and extrapolations /// on arbitrary points. /// - public static IInterpolation LinearBetweenPoints( - IList points, - IList values) + public static IInterpolation LinearBetweenPoints(IList points, IList values) { var method = new LinearSplineInterpolation(); method.Initialize(points, values); @@ -84,9 +80,7 @@ namespace MathNet.Numerics /// which can then be used to compute interpolations and extrapolations /// on arbitrary points. /// - public static IInterpolation RationalWithoutPoles( - IList points, - IList values) + public static IInterpolation RationalWithoutPoles(IList points, IList values) { var method = new FloaterHormannRationalInterpolation(); method.Initialize(points, values); @@ -103,9 +97,7 @@ namespace MathNet.Numerics /// which can then be used to compute interpolations and extrapolations /// on arbitrary points. /// - public static IInterpolation RationalWithPoles( - IList points, - IList values) + public static IInterpolation RationalWithPoles(IList points, IList values) { var method = new BulirschStoerRationalInterpolation(); method.Initialize(points, values);