/* * LeastSquares.java * * (C) 2008, Alex S. */ import java.awt.*; import java.util.*; /** * very basic least squares */ public class LeastSquares extends BufferedApplet { private int width,height; // points are read into this thing. private Vector points = new Vector(); // matrices to easily move from model to screen and vice versa. private pMatrix toscreen = null; private pMatrix tomodel = null; /** * initialize things */ public void init(){ super.init(); width = bounds().width; height = bounds().height; tomodel = pMatrix.reflecty3d().mult(pMatrix.translate3d(-width/2,-height/2,0)); toscreen = tomodel.inv3d(); } /** * given all points in `points' vector; use * the ``Least Squares'' method to get a line. */ public double[] solve(){ // make the X (samples) matrix. pMatrix X = new pMatrix(points.size(),2); pMatrix Y = new pMatrix(points.size(),1); for(int i=0;i