From d57116f2282d05549f4bf2abd2769332f647ca19 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 27 Sep 2014 18:17:56 +0200 Subject: [PATCH] Generate: guard negative lengths --- src/Numerics/Generate.cs | 108 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 4 deletions(-) diff --git a/src/Numerics/Generate.cs b/src/Numerics/Generate.cs index ddb3d20c..bbbbf6f8 100644 --- a/src/Numerics/Generate.cs +++ b/src/Numerics/Generate.cs @@ -97,7 +97,12 @@ namespace MathNet.Numerics /// public static double[] LinearSpaced(int length, double start, double stop) { - if (length <= 0) return new double[0]; + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + + if (length == 0) return new double[0]; if (length == 1) return new[] { stop }; double step = (stop - start)/(length - 1); @@ -116,7 +121,12 @@ namespace MathNet.Numerics /// public static T[] LinearSpacedMap(int length, double start, double stop, Func map) { - if (length <= 0) return new T[0]; + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + + if (length == 0) return new T[0]; if (length == 1) return new[] { map(stop) }; double step = (stop - start)/(length - 1); @@ -136,7 +146,12 @@ namespace MathNet.Numerics /// public static double[] LogSpaced(int length, double startExponent, double stopExponent) { - if (length <= 0) return new double[0]; + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + + if (length == 0) return new double[0]; if (length == 1) return new[] { Math.Pow(10, stopExponent) }; double step = (stopExponent - startExponent)/(length - 1); @@ -155,7 +170,12 @@ namespace MathNet.Numerics /// public static T[] LogSpacedMap(int length, double startExponent, double stopExponent, Func map) { - if (length <= 0) return new T[0]; + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + + if (length == 0) return new T[0]; if (length == 1) return new[] { map(Math.Pow(10, stopExponent)) }; double step = (stopExponent - startExponent)/(length - 1); @@ -269,6 +289,11 @@ namespace MathNet.Numerics /// Optional delay, relative to the phase. public static double[] Periodic(int length, double samplingRate, double frequency, double amplitude = 1.0, double phase = 0.0, int delay = 0) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + double step = frequency/samplingRate*amplitude; phase = Euclid.Modulus(phase - delay*step, amplitude); @@ -300,6 +325,11 @@ namespace MathNet.Numerics /// Optional delay, relative to the phase. public static T[] PeriodicMap(int length, Func map, double samplingRate, double frequency, double amplitude = 1.0, double phase = 0.0, int delay = 0) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + double step = frequency/samplingRate*amplitude; phase = Euclid.Modulus(phase - delay*step, amplitude); @@ -388,6 +418,11 @@ namespace MathNet.Numerics /// Optional delay, relative to the phase. public static double[] Sinusoidal(int length, double samplingRate, double frequency, double amplitude, double mean = 0.0, double phase = 0.0, int delay = 0) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + double step = frequency/samplingRate*Constants.Pi2; phase = (phase - delay*step)%Constants.Pi2; @@ -521,6 +556,11 @@ namespace MathNet.Numerics /// The value that each field should be set to. public static T[] Repeat(int length, T value) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + var data = new T[length]; CommonParallel.For(0, data.Length, 4096, (a, b) => { @@ -552,6 +592,11 @@ namespace MathNet.Numerics /// Offset to the time axis. public static double[] Step(int length, double amplitude, int delay) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + var data = new double[length]; for (int i = Math.Max(0, delay); i < data.Length; i++) { @@ -586,6 +631,11 @@ namespace MathNet.Numerics /// Offset to the time axis. Zero or positive. public static double[] Impulse(int length, double amplitude, int delay) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + var data = new double[length]; if (delay >= 0 && delay < length) { @@ -626,6 +676,11 @@ namespace MathNet.Numerics /// Offset to the time axis. Zero or positive. public static double[] PeriodicImpulse(int length, int period, double amplitude, int delay) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + var data = new double[length]; delay = Euclid.Modulus(delay, period); while (delay < length) @@ -668,6 +723,11 @@ namespace MathNet.Numerics /// public static double[] Uniform(int length) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + return SystemRandomSource.FastDoubles(length); } @@ -687,6 +747,11 @@ namespace MathNet.Numerics /// public static T[] UniformMap(int length, Func map) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + var samples = SystemRandomSource.FastDoubles(length); return Map(samples, map); } @@ -706,6 +771,11 @@ namespace MathNet.Numerics /// public static T[] UniformMap2(int length, Func map) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + var samples1 = SystemRandomSource.FastDoubles(length); var samples2 = SystemRandomSource.FastDoubles(length); return Map2(samples1, samples2, map); @@ -735,6 +805,11 @@ namespace MathNet.Numerics /// public static double[] Gaussian(int length, double mean, double standardDeviation) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + var samples = new double[length]; Normal.Samples(SystemRandomSource.Default, samples, mean, standardDeviation); return samples; @@ -758,6 +833,11 @@ namespace MathNet.Numerics /// Location mu-parameter of the stable distribution public static double[] Stable(int length, double alpha, double beta, double scale, double location) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + var samples = new double[length]; Distributions.Stable.Samples(SystemRandomSource.Default, samples, alpha, beta, scale, location); return samples; @@ -780,6 +860,11 @@ namespace MathNet.Numerics /// public static double[] Random(int length, IContinuousDistribution distribution) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + var samples = new double[length]; distribution.Samples(samples); return samples; @@ -798,6 +883,11 @@ namespace MathNet.Numerics /// public static float[] RandomSingle(int length, IContinuousDistribution distribution) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + var samples = new double[length]; distribution.Samples(samples); return Map(samples, v => (float)v); @@ -848,6 +938,11 @@ namespace MathNet.Numerics /// public static T[] RandomMap(int length, IContinuousDistribution distribution, Func map) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + var samples = new double[length]; distribution.Samples(samples); return Map(samples, map); @@ -866,6 +961,11 @@ namespace MathNet.Numerics /// public static T[] RandomMap2(int length, IContinuousDistribution distribution, Func map) { + if (length < 0) + { + throw new ArgumentOutOfRangeException("length"); + } + var samples1 = new double[length]; var samples2 = new double[length]; distribution.Samples(samples1);