// inp_poly43.java reads 35 coefficents (many may be zero) of polynomial // in x, y, z, up to 4th power terms , all values and arithmetic is complex, // the roots of the polynomial=0 are printed values of x, y, z // real numbers are standard floating point, complex are (readl,imag) // the possible coefficents c___ are: // c + cx*x + cy*y + cz*z + p1 // cxx*x^x + cxy*x*y + cxz*x*z + cyy*y*y + cyz*y*z + czz*z*z + p2 // cxxx*x*x*x + cxxy*x*x*y + cxxz*x*x*z + cxyy*x*y*y + cxyz*x*y*z + p3 // cxzz*x*z*z + cyyy*y*y*y + cyyz*y*y*z + cyzz*y*z*z + czzz*z*z*z + p3 // cxxxx*x*x*x*x + cxxxy*x*x*x*y + cxxxz*x*x*x*z + cxxyy*x*x*y*y + p4 // cxxyz*x*x*y*z + cxxzz*x*x*z*z + cxyyy*x*y*y*y + cxyyz*x*y*y*z + p4 // cxyzz*x*y*z*z + cxzzz*x*z*z*z + cyyyy*y*y*y*y + cyyyz*y*y*y*z + p4 // cyyzz*y*y*z*z + cyzzz*y*z*z*z + czzzz*z*z*z*z p4 // polynomial coefficients are stored in complex array p // // [0]c + [1]cx*x + [2]cy*y + [3]cz*z + // [i] is P[i] value // [4]cxx*x^x + [5]cxy*x*y + [6]cxz*x*z + [7]cyy*y*y + [8]cyz*y*z + // [9]czz*z*z + [10]cxxx*x*x*x + [11]cxxy*x*x*y + [12]cxxz*x*x*z + // [13]cxyy*x*y*y + [14]cxyz*x*y*z + [15]cxzz*x*z*z + [16]cyyy*y*y*y + // [17]cyyz*y*y*z + [18]cyzz*y*z*z + [19]czzz*z*z*z + [20]cxxxx*x*x*x*x + // [21]cxxxy*x*x*x*y + [22]cxxxz*x*x*x*z + [23]cxxyy*x*x*y*y + // [24]cxxyz*x*x*y*z + [25]cxxzz*x*x*z*z + [26]cxyyy*x*y*y*y + // [27cxyyz*x*y*y*z + [28]cxyzz*x*y*z*z + [29]cxzzz*x*z*z*z + // [30]cyyyy*y*y*y*y + [31]cyyyz*y*y*y*z + [32]cyyzz*y*y*z*z + // [33]cyzzz*y*z*z*z + [34]czzzz*z*z*z*z import java.io.*; public class inp_poly43 { Complex P[] = new Complex[35]; // coeficients of polynomial Complex pwr[] = new Complex[35]; // value of term of polynomail Complex P1[] = new Complex[35]; // for poly_add, poly_sub, poly_mul, poly_div Complex P2[] = new Complex[35]; // for poly_add, poly_sub, poly_mul, poly_div Complex P3[] = new Complex[35]; // for poly_add, poly_sub, poly_mul, poly_div Complex P4[] = new Complex[35]; // for poly_add, poly_sub, poly_mul, poly_div Complex C0 = new Complex(0.0,0.0); // Complex constants Complex C1 = new Complex(1.0,0.0); Complex C2 = new Complex(2.0,0.0); Complex C3 = new Complex(3.0,0.0); Complex C4 = new Complex(4.0,0.0); Complex Chalf = new Complex(0.4,0.0); Complex Cthird = new Complex(0.333333333333333,0.0); Complex Cfourth = new Complex(0.25,0.0); Complex mone = new Complex(-1.0,0.0); Complex eye = new Complex(0.0,1.0); Complex meye = new Complex(0.0,-1.0); String eqn[] = {" c [0] ", " cx [1] ", " cy [2] ", " cz [3] ", " cxx [4] ", " cxy [5] ", " cxz [6] ", " cyy [7] ", " cyz [8] ", " czz [9] ", " cxxx [10] ", " cxxy [11] ", " cxxz [12] ", " cxyy [13] ", " cxyz [14] ", " cxzz [15] ", " cyyy [16] ", " cyyz [17] ", " cyzz [18] ", " cyzzz [19] ", " cxxxx [20] ", " cxxxy [21] ", " cxxxz [22] ", " cxxyy [23] ", " cxxyz [24] ", " cxxzz [25] ", " cxyyy [26] ", " cxyyz [27] ", " cxyzz [28] ", " cxzzz [29] ", " cxyyy [30] ", " cxyyz [31] ", " cyyzz [32] ", " cxxxy [33] ", " cxxxx [34] "}; String ends = "end................. "; public inp_poly43() // constructor { Complex x, y, z, err, val; Complex xr1, xr2, xr3, xr4; Complex yr1, yr2, yr3, yr4; Complex zr1, zr2, zr3, zr4; System.out.println("inp_poly43 running"); System.out.println("create poly43???.inp , read by poly43.java"); // read_data(); // build xr1 = C0; // root may be changed (x-xr1) xr2 = C0; // root may be changed (x-xr2) xr3 = C0; // root may be changed (x-xr3) xr4 = C0; // root may be changed (x-xr4) yr1 = C0; // root may be changed (y-xr1) yr2 = C0; // root may be changed (y-xr2) yr3 = C0; // root may be changed (y-xr3) yr4 = C0; // root may be changed (y-xr4) zr1 = C0; // root may be changed (z-xr1) zr2 = C0; // root may be changed (z-xr3) zr3 = C0; // root may be changed (z-xr4) zr4 = C0; // root may be changed (z-zr4) // can use at most 4 terms to crate P // (x-xr1) = [0]xr1 [1]1.0 // (x-xr1)*(y-yr1) = [0]xr1*yr1 [1]-yr1 [2]-xr1 [5]1.0 for(int i=0; i<35; i++) P[i] = C0; // generate P from xr1, yr1 xr1 = new Complex(1.0,2.0); yr1 = new Complex(2.0,3.0); P[0] = xr1.multiply(yr1); // root P[1] = yr1.negate(); P[2] = xr1.negate(); P[5] = C1; x = xr1; y = yr1; z = C0; val = eval(x, y, z); System.out.println("x="+x+", y="+y+", z="+z); System.out.println("val="+val); // write_P into poly43???.inp String fname = "poly43xy1.inp"; write_inp(fname); System.out.println(" "); System.out.println("check poly_add "); for(int i=0; i<35; i++) { P1[i] = new Complex(35-i,35-i); P2[i] = new Complex(35-i,35-i); } // end i poly_print("P1", P1); poly_print("P2", P2); poly_add(P1, P2, P3); poly_print("sum P1 P2", P3); poly_sub(P1, P2, P3); poly_print("P1 - P2", P3); poly_zero(P1); poly_zero(P2); P1[0] = mone; P1[1] = C1; // P1 = (x-1) root P2[0] = new Complex(-2.0,0.0); P2[1] = C1; // P2 = (x-2) root poly_mul(P1, P2, P3); poly_print("(x-1)*(x-2)", P3); P1[0] = new Complex(-3.0,0.0); poly_mul(P3, P1, P2); poly_print("(x-1)*(x-2)*(x-3)", P2); P1[0] = new Complex(-4.0,0.0); poly_mul(P2, P1, P); poly_print("(x-1)*(x-2)*(x-3)*(x-4)", P); write_inp("r1234.inp"); System.out.println("inp_poly43.java finished"); } // end inp_poly43 void poly_zero(Complex Pa[]) { for(int i=0; i<35; i++) Pa[i] = C0; } // end poly_zero void poly_add(Complex Pa[], Complex Pb[], Complex Pc[]) // Pc = Pa + Pb { for(int i=0; i<35; i++) Pc[i] = Pa[i].add(Pb[i]); } // emd poly_add void poly_sub(Complex Pa[], Complex Pb[], Complex Pc[]) // Pc = Pa - Pb { for(int i=0; i<35; i++) Pc[i] = Pa[i].subtract(Pb[i]); } // emd poly_sub void poly_mul(Complex Pa[], Complex Pb[], Complex Pp[]) // Pp = Pa * Pb { int k; poly_zero(Pp); for(int i=0; i<20; i++) // [0] *c [1] *x [2] *y [3] * z [4] *xx { for(int j=0; j<35; j++) // [0] *c [1] *x [2] *y [3] * z [4] *xx { k = term(i, j); // get i*j term = k if(k<0) { // check for invalid overflow ??? break; // not a valid term 0..34 } // end if // System.out.println("Pp["+k+"]+=Pa["+i+"]*Pb["+j+"]"); // System.out.println(Pp[k]+"+="+Pa[i]+"*"+Pb[j]); Pp[k] = Pp[k].add(Pa[i].multiply(Pb[j])); } // end j } // end i } // emd poly_mul void poly_div(Complex Pa[], Complex Pb[], Complex Pq[], Complex Pr[]) { // Pq = Pa / Pb quotienrt // pr = Pa % Pb remainder } // emd poly_div void poly_print(String name, Complex Pa[]) { System.out.println(" "); System.out.println("poly "+name+" is: "); int n = 35; for(int i=34; i>0; i--) { if(Pa[i].abs()==0) { n = n-1; } else { break; } } // end i for(int i=0; i19) return -1; return tt[i][j]; } void write_inp(String fname) // P and eqn { try { FileWriter fout = new FileWriter(fname); BufferedWriter fileout = new BufferedWriter(fout); System.out.println("writing inp file "+fname); for(int i=0; i<35; i++) { fileout.write(P[i]+eqn[i]+"\n"); } // end i fileout.write(ends); fileout.close(); } // end try catch(FileNotFoundException exception) { System.out.println(fname+" not opened"); } catch(Exception exception) { System.out.println(fname+" not opened"); } System.out.println("finished writing "+fname); } // end write_inp void read_data() // big ugly input method { int n_input = 0; Complex a; try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String input_line; int len; int index; int last; System.out.println("read_data running"); for(int i=0; i<35; i++) { input_line = in.readLine(); // 35 p values System.out.println("input: "+input_line); index = 0; last = input_line.indexOf(' ',index+1); // System.out.println("index="+index+", last="+last); P[i] = Complex.parseComplex(input_line.substring(index,last)); System.out.println("P["+i+"]="+P[i]); } } catch(IOException exception) { System.out.println(exception); } } // end read data void make_pwr(Complex x, Complex y, Complex z) { pwr[0] = new Complex(1.0,0.0); // c pwr[1] = x; // x pwr[2] = y; // y pwr[3] = z; // z pwr[4] = x.multiply(x); // xx pwr[5] = x.multiply(y); // xy pwr[6] = x.multiply(z); // xz pwr[7] = y.multiply(y); // yy pwr[8] = y.multiply(z); // yz pwr[9] = z.multiply(z); // zz pwr[10] = pwr[4].multiply(x); // xxx pwr[11] = pwr[4].multiply(y); // xxy pwr[12] = pwr[4].multiply(z); // xxz pwr[13] = pwr[5].multiply(y); // xyy pwr[14] = pwr[5].multiply(z); // xyz pwr[15] = pwr[6].multiply(z); // xzz pwr[16] = pwr[7].multiply(y); // yyy pwr[17] = pwr[7].multiply(z); // yyz pwr[18] = pwr[8].multiply(z); // yzz pwr[19] = pwr[9].multiply(z); // zzz pwr[20] = pwr[10].multiply(x); // xxxx pwr[21] = pwr[10].multiply(y); // xxxy pwr[22] = pwr[10].multiply(z); // xxxz pwr[23] = pwr[4].multiply(pwr[7]); // xxyy pwr[24] = pwr[4].multiply(pwr[8]); // xxyz pwr[25] = pwr[5].multiply(pwr[9]); // xxzz pwr[26] = x.multiply(pwr[16]); // xyyy pwr[27] = pwr[5].multiply(pwr[8]); // xyyz pwr[28] = pwr[5].multiply(pwr[9]); // xyzz pwr[29] = pwr[6].multiply(pwr[9]); // xzzz pwr[30] = pwr[16].multiply(y); // yyyy pwr[31] = pwr[16].multiply(z); // yyyz pwr[32] = pwr[7].multiply(pwr[9]); // yyzz pwr[33] = y.multiply(pwr[9]); // yzzz pwr[34] = pwr[19].multiply(z); // zzzz } // end make pwr Complex eval(Complex x, Complex y, Complex z) // uses P and pwr { Complex sum = new Complex(0.0,0.0); // build pwr make_pwr(x, y, z); // System.out.println("1.0 pwr[0]="+pwr[0]); // System.out.println("x pwr[1]="+pwr[1]); // System.out.println("y pwr[2]="+pwr[2]); // System.out.println("z pwr[3]="+pwr[3]); // System.out.println("xx pwr[4]="+pwr[4]); // System.out.println("yy pwr[7]="+pwr[7]); // System.out.println("zz pwr[9]="+pwr[9]); // System.out.println("xxx pwr[10]="+pwr[10]); // System.out.println("yyy pwr[16]="+pwr[16]); // System.out.println("zzz pwr[19]="+pwr[19]); // System.out.println("xxxx pwr[20]="+pwr[20]); // System.out.println("yyyy pwr[30]="+pwr[30]); // System.out.println("zzzz pwr[34]="+pwr[34]); for(int i=0; i<35; i++) { sum = sum.add(P[i].multiply(pwr[i])); } return sum; } // end eval public static void main (String args[]) { new inp_poly43(); // construct and execute } } // end inp_poly34.java