Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Charting/Histogram.cs @ 563

Last change on this file since 563 was 560, checked in by gkronber, 16 years ago
  • added strong name key to CEDMA.Charting.
  • moved classes related to charting from CEDMA.Core to CEDMA.Charting

#268

File size: 2.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
25using System.Text;
26using HeuristicLab.Core;
27using System.Xml;
28using HeuristicLab.CEDMA.DB.Interfaces;
29
30namespace HeuristicLab.CEDMA.Charting {
31  public class Histogram {
32    private int buckets;
33    public int Buckets {
34      get { return buckets; }
35      set {
36        buckets = value;
37        ResetBucketBounds();
38      }
39    }
40
41    private double[] limit;
42
43    private List<double> values;
44
45    public Histogram(int buckets) {
46      this.buckets = buckets;
47      values = new List<double>();
48    }
49
50    public double LowerValue(int bucketIndex) {
51      return limit[bucketIndex];
52    }
53
54    public double UpperValue(int bucketIndex) {
55      return limit[bucketIndex+1];
56    }
57
58    public int Frequency(int bucketIndex) {
59      return values.Count(x => x >= LowerValue(bucketIndex) && x < UpperValue(bucketIndex));
60    }
61
62    public void AddValues(IEnumerable<double> xs) {
63      values.AddRange(xs.Where(x=>
64        !double.IsInfinity(x) && !double.IsNaN(x) && double.MaxValue!=x && double.MinValue !=x));
65      ResetBucketBounds();
66    }
67
68    private void ResetBucketBounds() {
69      limit = new double[buckets+1];
70      values.Sort();
71      double min = values[10];
72      double max = values[values.Count-10];
73
74      double step = (max - min) / buckets;
75      double cur = min;
76      for(int i = 0; i < buckets+1; i++) {
77        limit[i] = cur;
78        cur += step;
79      }
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.