类 Accumulator
- java.lang.Object
-
- edu.princeton.cs.algs4.Accumulator
-
public class Accumulator extends java.lang.ObjectTheAccumulatorclass is a data type for computing the running mean, sample standard deviation, and sample variance of a stream of real numbers. It provides an example of a mutable data type and a streaming algorithm.This implementation uses a one-pass algorithm that is less susceptible to floating-point roundoff error than the more straightforward implementation based on saving the sum of the squares of the numbers. This technique is due to B. P. Welford. Each operation takes constant time in the worst case. The amount of memory is constant - the data values are not stored.
For additional documentation, see Section 1.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
构造器概要
构造器 构造器 说明 Accumulator()Initializes an accumulator.
-
方法概要
修饰符和类型 方法 说明 voidaddDataValue(double x)Adds the specified data value to the accumulator.intcount()Returns the number of data values.static voidmain(java.lang.String[] args)Unit tests theAccumulatordata type.doublemean()Returns the mean of the data values.doublestddev()Returns the sample standard deviation of the data values.java.lang.StringtoString()Returns a string representation of this accumulator.doublevar()Returns the sample variance of the data values.
-
-
-
方法详细资料
-
addDataValue
public void addDataValue(double x)
Adds the specified data value to the accumulator.- 参数:
x- the data value
-
mean
public double mean()
Returns the mean of the data values.- 返回:
- the mean of the data values
-
var
public double var()
Returns the sample variance of the data values.- 返回:
- the sample variance of the data values
-
stddev
public double stddev()
Returns the sample standard deviation of the data values.- 返回:
- the sample standard deviation of the data values
-
count
public int count()
Returns the number of data values.- 返回:
- the number of data values
-
toString
public java.lang.String toString()
Returns a string representation of this accumulator.- 覆盖:
toString在类中java.lang.Object- 返回:
- a string representation of this accumulator
-
main
public static void main(java.lang.String[] args)
Unit tests theAccumulatordata type. Reads in a stream of real number from standard input; adds them to the accumulator; and prints the mean, sample standard deviation, and sample variance to standard output.- 参数:
args- the command-line arguments
-
-