//****************************************************************************** // Lab.java: Applet // CS 20c lab 2 // Brian Frazier // frazier@cs.caltech.edu // This applet implements both fast polynomial multiplication and // LU decomposition of matrices // //****************************************************************************** import java.applet.*; import java.awt.*; import java.util.*; import java.lang.Math; /* * * Complex * */ //The class Complex encapsulates all the necessary //functions of a Complex variable class Complex { public double Re; //Real part of complex variable public double Im; //Imaginery part of the complex variable // Complex() method constructs a complex and initializes its values public Complex() { Re=0.0; Im=0.0; } // add method adds two complexs together public void add(Complex a, Complex b) { Re = (a.Re + b.Re); Im = (b.Im + a.Im); } // subtract method subtracts two complexs public void subtract(Complex a,Complex b) { Re = a.Re - b.Re; Im = a.Im - b.Im; } // multiply method multiplies two complexs together public void multiply(Complex a,Complex b) { Re = (a.Re * b.Re) - (a.Im * b.Im); Im = (a.Re * b.Im) + (b.Re * a.Im); } // divide method creates an inverse of the functions in the denominator // then multiplies the numerator by the inverse of the denominator public void divide(Complex a,Complex b) { Complex c = new Complex(); Re = (b.Re / ((b.Re * b.Re)+(b.Im * b.Im))); Im = (((-1.0)*b.Im) / ((b.Re * b.Re)+(b.Im * b.Im))); c.Re = Re; c.Im = Im; multiply(a,c); } // rootun method returns the root of unity of Omega(k,n) public void rootun(double k,double n) { Re = Math.cos((2*k*Math.PI)/n); Im = Math.sin((2*k*Math.PI)/n); } } // // // Matrix // // // Matrix is a class that stores a matrix in a 2 dimensional // array with the first index representing the rows and the // second index representing the columns class Matrix { int rows,columns,flag; double mat[][]; public Matrix(int r, int c) { rows = r; columns = c; flag=0; mat = new double [rows][columns]; } } // // // MatrixOp // // // MatrixOp is a class that contains functions to operate // on matrices. All the function take input of the class Matrix // and return matrices of the type Matrix class MatrixOp { // Matrix add is a method that simply add two matrices // a and b together by adding each component a(x,y) to // b(x,y). A matrix of the proper size is returned // with the result public Matrix add(Matrix a, Matrix b) { Matrix c = new Matrix(a.rows,a.columns); for(int x=0;x=1;i--) { l = i - 1; scaler=0.0; h=0.0; if(l > 0) { for(int x=0;x<=l;x++) { scaler = scaler + Math.abs(a.mat[i][x]); } if(scaler == 0.0) ret.mat[1][i] = a.mat[i][l]; else { for(int y=0;y<=l;y++) { a.mat[i][y] = a.mat[i][y] / scaler; h = h + a.mat[i][y]*a.mat[i][y]; } f=a.mat[i][l]; if(f >= 0.0) au = Math.sqrt(h); else au = Math.sqrt(h * -1); ret.mat[1][i]=scaler * au; h = h - f*au; a.mat[i][l]=f-au; f=0.0; for(int z=0;z<=l;z++) { au=0.0; for(int c=0;c<=z;c++) au = au + a.mat[z][c]*a.mat[i][c]; for(int c=z+1;c<=l;c++) au = au + a.mat[c][z]*a.mat[i][c]; ret.mat[1][z]=au/h; f = f + ret.mat[1][z]*a.mat[i][z]; } k=f/(h+h); for(int z=0;z<=l;z++) { f=a.mat[i][z]; ret.mat[1][z]=au=ret.mat[1][z] - k*f; for(int c=0;c<=z;c++) a.mat[z][c] = a.mat[z][c] - (f * ret.mat[1][c] + au*a.mat[i][c]); } } } else ret.mat[1][i]=a.mat[i][l]; ret.mat[0][i]=h; } ret.mat[1][0]=0.0; for(int i=0;i 1) { while(aa<(tempr)) aa = aa * 2; } // get coefficients of second polynomial in2 = this.input2.getText(); tok2 = new StringTokenizer(in2); temp2 = tok2.countTokens(); tempr2= temp2; // find the next highest order or 2 in which // the second polynomial can fit in if((tempr2) != bb && (tempr2) > 1) { while(bb<(tempr2)) bb = bb * 2; } // create two vectors that are twice the // size of the largest of the two polynomial vectors if(aa < bb) { a = new Matrix(2,(2 * bb)); b = new Matrix(2,(2 * bb)); d = new Matrix(2,(2 * bb)); } else { a = new Matrix(2,(2 * aa)); b = new Matrix(2,(2 * aa)); d = new Matrix(2,(2 * aa)); } // copy elements to the vectors for processing for(int x=0;x<(temp);x++) { a.mat[0][x]= (Double.valueOf(tok.nextToken())).doubleValue(); a.mat[1][x]= 0.0; } for(int x=0;x<(temp2);x++) { b.mat[0][x]= (Double.valueOf(tok2.nextToken())).doubleValue(); b.mat[1][x]= 0.0; } //Fast Fourier Transform both polynomials at = k.FFT(a); bt = k.FFT(b); // multiple respective elements of each transform // together to form the final fourier orders for(int i=0;i-1.0e-08) c.mat[0][x]=0.0; if(c.mat[1][x]<1.0e-08 && c.mat[1][x]>-1.0e-08) c.mat[1][x]=0.0; } // set flag to draw the result flag=3; repaint(); return true; } return false; } // TODO: Place additional applet code here }