1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.math.stat.descriptive.summary; 18 19 import java.io.Serializable; 20 21 import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic; 22 23 /** 24 * Returns the product of the available values. 25 * <p> 26 * If there are no values in the dataset, or any of the values are 27 * <code>NaN</code>, then <code>NaN</code> is returned.</p> 28 * <p> 29 * <strong>Note that this implementation is not synchronized.</strong> If 30 * multiple threads access an instance of this class concurrently, and at least 31 * one of the threads invokes the <code>increment()</code> or 32 * <code>clear()</code> method, it must be synchronized externally.</p> 33 * 34 * @version $Revision: 617953 $ $Date: 2008-02-02 22:54:00 -0700 (Sat, 02 Feb 2008) $ 35 */ 36 public class Product extends AbstractStorelessUnivariateStatistic implements Serializable { 37 38 /** Serializable version identifier */ 39 private static final long serialVersionUID = 2824226005990582538L; 40 41 /**The number of values that have been added */ 42 private long n; 43 44 /** 45 * The current Running Product. 46 */ 47 private double value; 48 49 /** 50 * Create a Product instance 51 */ 52 public Product() { 53 n = 0; 54 value = Double.NaN; 55 } 56 57 /** 58 * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double) 59 */ 60 public void increment(final double d) { 61 if (n == 0) { 62 value = d; 63 } else { 64 value *= d; 65 } 66 n++; 67 } 68 69 /** 70 * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult() 71 */ 72 public double getResult() { 73 return value; 74 } 75 76 /** 77 * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN() 78 */ 79 public long getN() { 80 return n; 81 } 82 83 /** 84 * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear() 85 */ 86 public void clear() { 87 value = Double.NaN; 88 n = 0; 89 } 90 91 /** 92 * Returns the product of the entries in the specified portion of 93 * the input array, or <code>Double.NaN</code> if the designated subarray 94 * is empty. 95 * <p> 96 * Throws <code>IllegalArgumentException</code> if the array is null.</p> 97 * 98 * @param values the input array 99 * @param begin index of the first array element to include 100 * @param length the number of elements to include 101 * @return the product of the values or Double.NaN if length = 0 102 * @throws IllegalArgumentException if the array is null or the array index 103 * parameters are not valid 104 */ 105 public double evaluate(final double[] values, final int begin, final int length) { 106 double product = Double.NaN; 107 if (test(values, begin, length)) { 108 product = 1.0; 109 for (int i = begin; i < begin + length; i++) { 110 product *= values[i]; 111 } 112 } 113 return product; 114 } 115 116 }