1 package JSci.maths.polynomials; 2 3 import JSci.GlobalSettings; 4 import JSci.maths.Complex; 5 import JSci.maths.fields.*; 6 import JSci.maths.groups.*; 7 8 9 16 public class ComplexPolynomial implements Polynomial { 17 private Complex[] _coeff; 18 19 20 public ComplexPolynomial( Complex[] coeff ) { 21 if ( coeff == null ) { 22 throw new NullPointerException ("Coefficients cannot be null"); 23 } 24 _coeff = normalise(coeff); 25 } 26 27 31 private static Complex[] normalise(Complex[] c) { 32 int i = c.length-1; 33 while(i >= 0 && c[i].norm() <= 2.0*GlobalSettings.ZERO_TOL) 34 i--; 35 if(i < 0) { 36 return new Complex[] {Complex.ZERO}; 37 } else if(i < c.length-1) { 38 Complex[] arr = new Complex[i+1]; 39 System.arraycopy(c, 0, arr, 0, arr.length); 40 return arr; 41 } else { 42 return c; 43 } 44 } 45 46 51 public ComplexPolynomial( Field.Member[] f ) { 52 _coeff = normalise(ComplexPolynomialRing.toComplex( f )); 53 } 54 55 59 public Field.Member getCoefficient( int n ) { 60 return getCoefficientAsComplex( n ); 61 } 62 63 67 public Complex getCoefficientAsComplex( int n ) { 68 if ( n >= _coeff.length ) { 69 return Complex.ZERO; 70 } else { 71 return _coeff[n]; 72 } 73 } 74 75 77 public Field.Member[] getCoefficients() { 78 return getCoefficientsAsComplexes(); 79 } 80 81 84 public Complex[] getCoefficientsAsComplexes() { 85 return _coeff; 86 } 87 88 91 public Complex map(Complex z) { 92 return PolynomialMath.evalPolynomial(this, z); 93 } 94 97 public Complex map(double x, double y) { 98 return map(new Complex(x, y)); 99 } 100 101 105 public int degree() { 106 return _coeff.length-1; 107 } 108 109 public Object getSet() { 110 return ComplexPolynomialRing.getInstance(); 111 } 112 113 117 public boolean isZero() { 118 for ( int k = 0; k < _coeff.length; k++ ) { 119 if ( _coeff[k].norm() > ( GlobalSettings.ZERO_TOL * 2.0 ) ) { 120 return false; 121 } 122 } 123 124 return true; 125 } 126 127 131 public boolean isOne() { 132 if(_coeff[0].subtract( Complex.ONE ).norm() > ( GlobalSettings.ZERO_TOL * 2.0 )) 133 return false; 134 135 for ( int k = 1; k < _coeff.length; k++ ) { 136 if ( _coeff[k].norm() > ( 2.0 * GlobalSettings.ZERO_TOL ) ) { 137 return false; 138 } 139 } 140 141 return true; 142 } 143 144 148 public AbelianGroup.Member add( AbelianGroup.Member g ) { 149 AbelianGroup.Member result = null; 150 if ( g instanceof ComplexPolynomial ) { 151 ComplexPolynomial p = (ComplexPolynomial) g; 152 int maxgrade = PolynomialMath.maxDegree( this, p ); 153 Complex[] c = new Complex[maxgrade+1]; 154 for ( int k = 0; k < c.length; k++ ) { 155 c[k] = getCoefficientAsComplex( k ).add( p.getCoefficientAsComplex( k ) ); 156 } 157 result = new ComplexPolynomial( c ); 158 } else { 159 throw new IllegalArgumentException ("Member class not recognised by this method."); 160 } 161 162 return result; 163 } 164 165 170 public Polynomial scalarDivide( Field.Member f ) { 171 if ( f instanceof Complex ) { 172 return scalarDivide( (Complex) f ); 173 } else if ( f instanceof Number ) { 174 return scalarDivide( ( (Number ) f ).doubleValue() ); 175 } else { 176 throw new IllegalArgumentException ("Member class not recognised by this method."); 177 } 178 } 179 180 184 public ComplexPolynomial scalarDivide( Complex a ) { 185 Complex[] c = new Complex[_coeff.length]; 186 for ( int k = 0; k < c.length; k++ ) { 187 c[k] = _coeff[k].divide( a ); 188 } 189 190 return new ComplexPolynomial( c ); 191 } 192 193 197 public ComplexPolynomial scalarDivide( double a ) { 198 Complex[] c = new Complex[_coeff.length]; 199 for ( int k = 0; k < c.length; k++ ) { 200 c[k] = _coeff[k].divide( a ); 201 } 202 203 return new ComplexPolynomial( c ); 204 } 205 206 210 public boolean equals( Object o ) { 211 if ( o == this ) { 212 return true; 213 } else if ( o instanceof ComplexPolynomial ) { 214 ComplexPolynomial p = (ComplexPolynomial) o; 215 int maxgrade = PolynomialMath.maxDegree( this, p ); 216 for ( int k = 0; k<=maxgrade; k++ ) { 217 if ( p.getCoefficientAsComplex(k).subtract( getCoefficientAsComplex(k) ).norm() > ( 2 * GlobalSettings.ZERO_TOL ) ) { 218 return false; 219 } 220 } 221 return true; 222 } 223 224 return false; 225 } 226 227 230 public int hashCode() { 231 int res = 0; 232 for ( int k = 0; k < _coeff.length; k++ ) { 233 res += (int) ( _coeff[k].norm() * 10.0 ); 234 } 235 236 return res; 237 } 238 239 243 public Polynomial scalarMultiply( Field.Member f ) { 244 if ( f instanceof Number ) { 245 double a = ( (Number ) f ).doubleValue(); 246 return scalarMultiply( a ); 247 } else if ( f instanceof Complex ) { 248 return scalarMultiply( (Complex) f ); 249 } else { 250 throw new IllegalArgumentException ("Member class not recognised by this method."); 251 } 252 } 253 254 258 public ComplexPolynomial scalarMultiply( double a ) { 259 Complex[] c = new Complex[_coeff.length]; 260 for ( int k = 0; k < c.length; k++ ) { 261 c[k] = _coeff[k].multiply( a ); 262 } 263 264 return new ComplexPolynomial( c ); 265 } 266 267 271 public ComplexPolynomial scalarMultiply( Complex a ) { 272 Complex[] c = new Complex[_coeff.length]; 273 for ( int k = 0; k < c.length; k++ ) { 274 c[k] = _coeff[k].multiply( a ); 275 } 276 277 return new ComplexPolynomial( c ); 278 } 279 280 284 public Ring.Member multiply( Ring.Member r ) { 285 if ( r instanceof ComplexPolynomial ) { 286 ComplexPolynomial p = (ComplexPolynomial) r; 287 int maxgrade = PolynomialMath.maxDegree( this, p ); 288 int mingrade = PolynomialMath.minDegree( this, p ); 289 int destgrade = maxgrade + mingrade; 290 Complex[] n = new Complex[destgrade+1]; 291 for ( int k = 0; k < n.length; k++ ) { 292 n[k] = Complex.ZERO; 293 } 294 for ( int k = 0; k < _coeff.length; k++ ) { 295 Complex tis = _coeff[k]; 296 for ( int j = 0; j < p._coeff.length; j++ ) { 297 Complex tat = p._coeff[j]; 298 n[k + j] = n[k + j].add( tis.multiply( tat ) ); 299 } 300 } 301 302 return new ComplexPolynomial( n ); 303 } else { 304 throw new IllegalArgumentException ("Member class not recognised by this method."); 305 } 306 } 307 308 311 public AbelianGroup.Member negate() { 312 Complex[] c = new Complex[_coeff.length]; 313 for ( int k = 0; k < c.length; k++ ) { 314 c[k] = (Complex) _coeff[k].negate(); 315 } 316 317 return new ComplexPolynomial( c ); 318 } 319 320 324 public AbelianGroup.Member subtract( AbelianGroup.Member g ) { 325 if ( g instanceof ComplexPolynomial ) { 326 ComplexPolynomial p = (ComplexPolynomial) g; 327 int maxgrade = PolynomialMath.maxDegree( this, p ); 328 Complex[] c = new Complex[maxgrade+1]; 329 for ( int k = 0; k < c.length; k++ ) { 330 c[k] = getCoefficientAsComplex( k ).subtract( p.getCoefficientAsComplex( k ) ); 331 } 332 return new ComplexPolynomial( c ); 333 } else { 334 throw new IllegalArgumentException ("Member class not recognised by this method."); 335 } 336 } 337 338 341 public String toString() { 342 StringBuffer sb = new StringBuffer ( "P(z) = " ); 343 344 for ( int k = degree(); k > 0; k-- ) { 345 sb.append( _coeff[k] ).append( "z^" ).append( k ).append( " + " ); 346 } 347 sb.append( _coeff[0] ); 348 349 return sb.toString(); 350 } 351 } 352 353 | Popular Tags |