// This class does the Newton Interpolation import java.lang.Math; class Interpolator { // Constructor for no points public Interpolator() { pts = 0; alloced = 1; x = new double[1]; ddt = new double[1][]; } // Copy constructor of sorts, with a twist. This twist is used for // the copy-and-add constructor. public Interpolator(Interpolator frm, int extras) { int i,j; pts = frm.pts; if (extras < 0) alloced = frm.pts; else alloced = frm.pts + extras; x = new double[alloced]; ddt = new double[alloced][]; for (i=0;i=0;i--) d = d*(xval-x[i]) + getA(i); return d; } // Handy-dandy function for doing the homework public double[] MultiEval(double xval[]) { double y[] = new double[xval.length]; int i; for (i=0;i 0. public double[] MiddlePoints(double a, double b, int m) { if (m <= 0) return null; double x[] = new double[m+1]; double d = (b-a)/((double)m); int i; x[0] = a; x[m] = b; for (i=1;i 0.5) { while (myStuff[head-1] == '9') head--; if (myStuff[head-1] == '.') { head--; if (myStuff[head-1] == '9') { myStuff[head-1] = '1'; actpow++; absActPow = Math.abs(actpow); } else { myStuff[head-1] = (char)(myStuff[head-1] + 1); } } else { myStuff[head-1] = (char)(myStuff[head-1] + 1); } } else { // Purge trailing zeros while (myStuff[head-1] == '0') head--; // If all that's left is the decimal point, purge that too if (myStuff[head-1] == '.') head--; } if (actpow != 0) { // Do an exponent field myStuff[head++] = 'e'; if (actpow < 0) myStuff[head++] = '-'; if (absActPow >= 1000) myStuff[head++] = (char) ((absActPow/1000) + '0'); if (absActPow >= 100) myStuff[head++] = (char) (((absActPow/100)%10) + '0'); if (absActPow >= 10) myStuff[head++] = (char) (((absActPow/10)%10) + '0'); myStuff[head++] = (char) ((absActPow%100) + '0'); } // Finally, done! return new String(myStuff,0,head); } // Reallocate storage: need more. Synchronized for paranoid reasons. protected synchronized void ReAlloc() { double tx[] = new double[alloced]; double tddt[][] = new double[alloced][]; // Backup x, reallocate x, and restore the backup System.arraycopy(x, 0, tx, 0, alloced); x = new double[alloced*2]; System.arraycopy(tx, 0, x, 0, alloced); // Backup ddt, reallocate ddt, and restore the backup System.arraycopy(ddt, 0, tddt, 0, alloced); ddt = new double[alloced*2][]; System.arraycopy(tddt, 0, ddt, 0, alloced); alloced *= 2; } protected double x[]; protected double ddt[][]; protected int pts; // How many points are we interpolating protected int alloced; // How much room have we allocated }