1 8 package org.lsmp.djep.groupJep.values; 9 import org.lsmp.djep.groupJep.interfaces.*; 10 import org.lsmp.djep.groupJep.*; 11 import org.nfunk.jep.type.*; 12 18 public class Polynomial extends Number { 19 private RingI baseRing; 20 private String symbol; 21 private Number coeffs[]; 22 private int degree; 23 26 public Polynomial(RingI baseRing,String symbol,Number coeffs[]) { 27 this.baseRing = baseRing; 28 this.symbol = symbol; 29 int deg=0; 30 for(int i=coeffs.length-1;i>0;--i) 31 if(!baseRing.equals(coeffs[i],baseRing.getZERO())) 32 { 33 deg=i; 34 break; 35 } 36 if(deg == coeffs.length-1) 37 this.coeffs = coeffs; 38 else 39 { 40 this.coeffs = new Number [deg+1]; 41 System.arraycopy(coeffs,0,this.coeffs,0,deg+1); 42 } 43 this.degree = deg; 44 } 45 46 48 protected void setCoeffs(Number coeffs[]) 49 { 50 this.coeffs = coeffs; 51 this.degree = coeffs.length-1; 52 } 53 54 protected Polynomial valueOf(Number lcoeffs[]) 55 { 56 Polynomial p = new Polynomial(baseRing,symbol,lcoeffs); 57 return p; 58 } 59 public Polynomial add(Polynomial poly) 60 { 61 int deg = degree > poly.degree ? degree : poly.degree; 62 Number lcoeffs[] = new Number [deg+1]; 63 for(int i=0;i<=deg;++i) 64 { 65 if(i<=degree && i <= poly.degree) 66 lcoeffs[i] = baseRing.add(coeffs[i],poly.coeffs[i]); 67 else if(i<=degree) 68 lcoeffs[i] = coeffs[i]; 69 else 70 lcoeffs[i] = poly.coeffs[i]; 71 } 72 return valueOf(lcoeffs); 73 } 74 75 public Polynomial sub(Polynomial poly) 76 { 77 int deg = degree > poly.degree ? degree : poly.degree; 78 Number lcoeffs[] = new Number [deg+1]; 79 for(int i=0;i<=deg;++i) 80 { 81 if(i<=degree && i <= poly.degree) 82 lcoeffs[i] = baseRing.sub(coeffs[i],poly.coeffs[i]); 83 else if(i<=degree) 84 lcoeffs[i] = coeffs[i]; 85 else 86 lcoeffs[i] = baseRing.getInverse(poly.coeffs[i]); 87 } 88 return valueOf(lcoeffs); 89 } 90 91 public Polynomial mul(Polynomial poly) 92 { 93 int deg = degree + poly.degree; 94 Number lcoeffs[] = new Number [deg+1]; 95 for(int i=0;i<=deg;++i) 96 lcoeffs[i] = baseRing.getZERO(); 97 98 for(int i=0;i<=degree;++i) 99 for(int j=0;j<=poly.degree;++j) 100 { 101 lcoeffs[i+j] = baseRing.add(lcoeffs[i+j], 102 baseRing.mul(coeffs[i],poly.coeffs[j])); 103 } 104 return valueOf(lcoeffs); 105 } 106 107 private String stripBrackets(Number num) 108 { 109 String s = num.toString(); 110 if(s.startsWith("<") && s.endsWith(">")) 111 { 112 if(s.indexOf('+')!=-1 || s.indexOf('-')>1) return s; 114 else 115 return s.substring(1,s.length()-1); 116 } 117 else return s; 118 } 119 public String toString() 120 { 121 if(degree==0) return "<"+stripBrackets(coeffs[0])+">"; 122 StringBuffer sb = new StringBuffer ("<"); 123 for(int i=degree;i>=0;--i) 124 { 125 String s = stripBrackets(coeffs[i]); 126 127 if(s.equals("0") || 129 this.baseRing.equals(coeffs[i],baseRing.getZERO())) 130 continue; 131 132 if(i!=degree && !s.startsWith("-")) sb.append("+"); 134 135 if( i==0 ) { 137 String s1 = coeffs[i].toString(); 138 if(s1.startsWith("<") && s1.endsWith(">")) 139 { 140 sb.append(s1.substring(1,s1.length()-1)); 141 } 142 else sb.append(s1); 143 break; 144 } 145 if(s.equals("-1")) 147 sb.append("-"); 148 else if(s.equals("1") || 149 this.baseRing.equals( 150 coeffs[i], 151 baseRing.getONE())) 152 {} else { 154 sb.append(stripBrackets(coeffs[i])); 155 sb.append(" "); 156 } 157 if(i>=2) sb.append(symbol+"^"+i); 158 else if(i==1) sb.append(symbol); 159 } 160 sb.append(">"); 161 return sb.toString(); 162 } 163 164 public int getDegree() { return degree; } 165 public String getSymbol() { return symbol; } 166 public Number [] getCoeffs() { return coeffs; } 167 public Number getCoeff(int i) { return coeffs[i]; } 168 public RingI getBaseRing() { return baseRing; } 169 170 171 public int intValue() {return coeffs[0].intValue(); } 172 173 public long longValue() {return coeffs[0].longValue(); } 174 175 public float floatValue() { return coeffs[0].floatValue(); } 176 177 public double doubleValue() {return coeffs[0].doubleValue(); } 178 179 public boolean equals(Polynomial n) 180 { 181 if(this.getDegree()!=n.getDegree()) return false; 182 for(int i=0;i<=this.getDegree();++i) 183 if(!baseRing.equals(this.getCoeff(i),n.getCoeff(i))) 184 return false; 185 return true; 186 } 187 188 191 public Complex calculateComplexValue(Complex rootVal) { 192 Number val = coeffs[this.getDegree()]; 193 Complex cval = GroupJep.getComplexValue(val); 194 195 for(int i=this.getDegree()-1;i>=0;--i) 196 { 197 Number val2 = coeffs[i]; 198 Complex cval2 = GroupJep.getComplexValue(val2); 199 Complex prod = cval.mul(rootVal); 200 cval = prod.add(cval2); 201 } 202 return cval; 203 } 204 205 } 206 | Popular Tags |