Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Common/3.3/SequenceGenerator.cs @ 18078

Last change on this file since 18078 was 17209, checked in by gkronber, 5 years ago

#2994: merged r17132:17198 from trunk to branch

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25
26namespace HeuristicLab.Common {
27  public static class SequenceGenerator {
28    /// <summary>
29    /// Generates a sequence of evenly spaced points by returning the start value and adding the stepwidth until the end is reached or surpassed.
30    /// </summary>
31    /// <param name="start">The smallest and first value of the sequence.</param>
32    /// <param name="end">The largest and last value of the sequence.</param>
33    /// <param name="stepWidth">The step size between subsequent values.</param>
34    /// <param name="includeEnd">Determines if the end should be included in the sequence regardless if the end is divisible by the stepwidth.</param>
35    /// <returns>A sequence of values from start to end (inclusive)</returns>
36    [Obsolete("It is recommended to use the decimal overload to achieve a higher numerical accuracy.")]
37    public static IEnumerable<double> GenerateSteps(double start, double end, double stepWidth, bool includeEnd = false) {
38      //mkommend: IEnumerable.Cast fails due to boxing and unboxing of the involved types
39      // http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs#27bb217a6d5457ec
40      // http://blogs.msdn.com/b/ericlippert/archive/2009/03/19/representation-and-identity.aspx     
41
42      return GenerateSteps((decimal)start, (decimal)end, (decimal)stepWidth, includeEnd).Select<decimal, double>(x => (double)x);
43    }
44
45    /// <summary>
46    /// Generates a sequence of evenly spaced points by returning the start value and adding the stepwidth until the end is reached or surpassed.
47    /// </summary>
48    /// <param name="start">The smallest and first value of the sequence.</param>
49    /// <param name="end">The largest and last value of the sequence.</param>
50    /// <param name="stepWidth">The step size between subsequent values.</param>
51    /// /// <param name="includeEnd">Determines if the end should be included in the sequence regardless if the end is divisible by the stepwidth.</param>
52    /// <returns>A sequence of values from start to end</returns>
53    public static IEnumerable<decimal> GenerateSteps(decimal start, decimal end, decimal stepWidth, bool includeEnd = false) {
54      if (stepWidth == 0)
55        throw new ArgumentException("The step width cannot be zero.");
56      if (start < end && stepWidth < 0)
57        throw new ArgumentException("The step width must be larger than zero for increasing sequences (start < end).");
58      if (start > end && stepWidth > 0)
59        throw new ArgumentException("The step width must be smaller than zero for decreasing sequences (start > end).");
60
61      decimal x = start;
62      while (x <= end) {
63        yield return x;
64        x += stepWidth;
65      }
66      if (x - stepWidth < end && includeEnd) yield return end;
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.