Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

BasicHist.java

00001 /*
00002  *  Copyright 2006-2007 Columbia University.
00003  *
00004  *  This file is part of MEAPsoft.
00005  *
00006  *  MEAPsoft is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License version 2 as
00008  *  published by the Free Software Foundation.
00009  *
00010  *  MEAPsoft is distributed in the hope that it will be useful, but
00011  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with MEAPsoft; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00018  *  02110-1301 USA
00019  *
00020  *  See the file "COPYING" for the text of the license.
00021  */
00022 
00023 package com.meapsoft;
00024 
00025 
00029 public class BasicHist
00030 {
00031   int [] bins;
00032   int numBins;
00033   int underflows;
00034   int overflows;
00035 
00036   double lo;
00037   double hi;
00038   double range;
00039 
00044   public BasicHist (int numBins, double lo, double hi) {
00045     this.numBins = numBins;
00046 
00047     bins = new int[numBins];
00048 
00049     this.lo = lo;
00050     this.hi = hi;
00051     range = hi - lo;
00052   }
00053 
00059   public void add(double x) {
00060     if( x >= hi) overflows++;
00061     else if( x < lo) underflows++;
00062     else {
00063       double val = x - lo;
00064 
00065       // Casting to int will round off to lower
00066       // integer value.
00067      int bin = (int)(numBins * (val/range) );
00068 
00069       // Increment the corresponding bin.
00070       bins[bin]++;
00071     }
00072   }
00073 
00075   public void clear() {
00076     for (int i=0; i < numBins; i++) {
00077         bins[i] = 0;
00078         overflows = 0;
00079         underflows= 0;
00080     }
00081   }
00082 
00084   public int getValue(int bin) {
00085     if (bin < 0)
00086         return underflows;
00087     else if( bin >= numBins)
00088         return overflows;
00089     else
00090         return bins[bin];
00091   }
00092 }

Generated on Tue Feb 6 19:02:24 2007 for MEAPsoft by doxygen1.2.18