StdDraw的VisualAccumlator
StdDraw的VisualAccumlator

StdDraw的VisualAccumlator

VisualAccumulator类

import edu.princeton.cs.algs4.Accumulator;
import edu.princeton.cs.algs4.StdDraw;

public class VisualAccumulator extends Accumulator {
    private double total;
    private int N;
    public VisualAccumulator(int trails,double max){
        StdDraw.setXscale(0,trails);
        StdDraw.setYscale(-1,max);
        StdDraw.setPenRadius(.005);
    }
    public void addDataValue(double val){
        N++;
        total+=val;
        StdDraw.setPenColor(StdDraw.DARK_GRAY);
        StdDraw.point(N,val);
        StdDraw.setPenColor(StdDraw.RED);
        StdDraw.point(N,total/N);
    }
    public double mean() {
        return total / N;
    }
}

TestVisualAccumulator类(类似于Unit Test?)

import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

public class TestVisualAccumulator {
    public static void main(String[] args){
        int T = Integer.parseInt(args[0]);
        VisualAccumulator a = new VisualAccumulator(T,1.0);
        for (int t=0;t<T;t++)
            a.addDataValue(StdRandom.gaussian());
        StdOut.println(a);
    }
}

结果

Uniform

Gaussian

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据