001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.math;
020
021import icy.type.DataIterator;
022import icy.type.DataIteratorUtil;
023
024/**
025 * Math utilities for {@link DataIterator} classes.
026 * 
027 * @author Stephane
028 */
029public class DataIteratorMath
030{
031    /**
032     * @deprecated Use {@link DataIteratorUtil#count(DataIterator)} instead.
033     */
034    @Deprecated
035    public static long count(DataIterator it)
036    {
037        return DataIteratorUtil.count(it);
038    }
039
040    /**
041     * Returns the sum of all values contained in the specified {@link DataIterator}.
042     * Returns <code>0</code> if no value in <code>DataIterator</code>.
043     */
044    public static double sum(DataIterator it)
045    {
046        double result = 0;
047
048        it.reset();
049
050        while (!it.done())
051        {
052            result += it.get();
053            it.next();
054        }
055
056        return result;
057    }
058
059    /**
060     * Returns the minimum value found in the specified {@link DataIterator}.
061     * Returns <code>Double.MAX_VALUE</code> if no value in <code>DataIterator</code>.
062     */
063    public static double min(DataIterator it)
064    {
065        double result = Double.MAX_VALUE;
066
067        it.reset();
068
069        while (!it.done())
070        {
071            final double value = it.get();
072            if (value < result)
073                result = value;
074            it.next();
075        }
076
077        return result;
078    }
079
080    /**
081     * Returns the maximum value found in the specified {@link DataIterator}.
082     * Returns <code>Double.MIN_VALUE</code> if no value in <code>DataIterator</code>.
083     */
084    public static double max(DataIterator it)
085    {
086        double result = -Double.MAX_VALUE;
087
088        it.reset();
089
090        while (!it.done())
091        {
092            final double value = it.get();
093            if (value > result)
094                result = value;
095            it.next();
096        }
097
098        return result;
099    }
100
101    /**
102     * Returns the mean value found in the specified {@link DataIterator}.
103     * Returns <code>0</code> if no value in <code>DataIterator</code>.
104     */
105    public static double mean(DataIterator it)
106    {
107        double result = 0;
108        long numSample = 0;
109
110        it.reset();
111
112        while (!it.done())
113        {
114            result += it.get();
115            numSample++;
116            it.next();
117        }
118
119        return result / numSample;
120    }
121}