1 package JSci.maths.polynomials; 2 3 import JSci.GlobalSettings; 4 import JSci.maths.MathDouble; 5 import JSci.maths.analysis.RealFunction; 6 import JSci.maths.fields.*; 7 import JSci.maths.fields.Field; 8 import JSci.maths.groups.AbelianGroup; 9 10 11 15 public class RealPolynomial extends RealFunction implements Polynomial { 16 17 private double[] _coeff; 18 19 20 public RealPolynomial( double[] coeff ) { 21 if ( coeff == null ) { 22 throw new NullPointerException ("Coefficients cannot be null"); 23 } 24 _coeff = normalise(coeff); 25 } 26 30 private static double[] normalise(double[] c) { 31 int i = c.length-1; 32 while(i >= 0 && Math.abs(c[i]) <= GlobalSettings.ZERO_TOL) 33 i--; 34 if(i < 0) { 35 return new double[] {0.0}; 36 } else if(i < c.length-1) { 37 double[] arr = new double[i+1]; 38 System.arraycopy(c, 0, arr, 0, arr.length); 39 return arr; 40 } else { 41 return c; 42 } 43 } 44 45 50 public RealPolynomial( Field.Member[] f ) { 51 if ( f == null ) { 52 throw new NullPointerException ("Coefficients cannot be null"); 53 } 54 _coeff = normalise(toDoubleArray(f)); 55 } 56 57 private static double[] toDoubleArray(Field.Member[] f) { 58 double[] arr = new double[f.length]; 59 for(int i=0; i<arr.length; i++) { 60 if(f[i] instanceof MathDouble) 61 arr[i] = ((MathDouble)f[i]).value(); 62 else 63 throw new IllegalArgumentException ("Different fields. Argument was " + f[i].getClass()); 64 } 65 return arr; 66 } 67 68 73 public Field.Member getCoefficient( int k ) { 74 return new MathDouble( getCoefficientAsDouble( k ) ); 75 } 76 77 82 public double getCoefficientAsDouble( int k ) { 83 if ( k >= _coeff.length ) { 84 return 0.0; 85 } else { 86 return _coeff[k]; 87 } 88 } 89 90 93 public Field.Member[] getCoefficients() { 94 return RealPolynomialRing.toMathDouble( getCoefficientsAsDoubles() ); 95 } 96 97 100 public double[] getCoefficientsAsDoubles() { 101 return _coeff; 102 } 103 104 107 public double map(double x) { 108 return PolynomialMath.evalPolynomial(this, x); 109 } 110 111 114 public int degree() { 115 return _coeff.length-1; 116 } 117 118 public Object getSet() { 119 return RealPolynomialRing.getInstance(); 120 } 121 122 127 public boolean isZero() { 128 for ( int k = 0; k < _coeff.length; k++ ) { 129 if ( Math.abs( _coeff[k] ) > GlobalSettings.ZERO_TOL) { 130 return false; 131 } 132 } 133 134 return true; 135 } 136 137 143 public boolean isOne() { 144 if(Math.abs( _coeff[0] - 1.0 ) > GlobalSettings.ZERO_TOL) 145 return false; 146 147 for ( int k = 1; k < _coeff.length; k++ ) { 148 if ( Math.abs( _coeff[k] ) > GlobalSettings.ZERO_TOL ) { 149 return false; 150 } 151 } 152 153 return true; 154 } 155 156 160 public RealFunction add( RealFunction g ) { 161 if ( g instanceof RealPolynomial ) { 162 RealPolynomial p = (RealPolynomial) g; 163 int maxgrade = PolynomialMath.maxDegree( this, p ); 164 double[] c = new double[maxgrade+1]; 165 for ( int k = 0; k < c.length; k++ ) { 166 c[k] = getCoefficientAsDouble( k ) + p.getCoefficientAsDouble( k ); 167 } 168 return new RealPolynomial( c ); 169 } else { 170 return super.add(g); 171 } 172 } 173 174 180 public RealFunction differentiate() { 181 if ( degree() == 0 ) { 182 return (RealPolynomial) RealPolynomialRing.getInstance().zero(); 183 } else { 184 double[] dn = new double[degree()]; 185 for ( int k = 0; k < dn.length; k++ ) { 186 dn[k] = getCoefficientAsDouble( k+1 ) * (k+1); 187 } 188 189 return new RealPolynomial( dn ); 190 } 191 } 192 193 197 public Polynomial scalarDivide( Field.Member f ) { 198 if ( f instanceof Number ) { 199 double a = ( (Number ) f ).doubleValue(); 200 201 return scalarDivide( a ); 202 } else { 203 throw new IllegalArgumentException ("Member class not recognised by this method."); 204 } 205 } 206 207 211 public RealPolynomial scalarDivide( double a ) { 212 double[] c = new double[_coeff.length]; 213 for ( int k = 0; k < c.length; k++ ) { 214 c[k] = _coeff[k] / a; 215 } 216 217 return new RealPolynomial( c ); 218 } 219 220 226 public boolean equals( Object o ) { 227 if ( o == this ) { 228 return true; 229 } else if ( o instanceof RealPolynomial ) { 230 RealPolynomial p = (RealPolynomial) o; 231 232 return ( (RealPolynomial) this.subtract( p ) ).isZero(); 233 } 234 235 return false; 236 } 237 238 242 public int hashCode() { 243 int res = 0; 244 for ( int k = 0; k < _coeff.length; k++ ) { 245 res += (int) ( _coeff[k] * 10.0 ); 246 } 247 248 return res; 249 } 250 251 255 public RealPolynomial integrate() { 256 double[] dn = new double[_coeff.length+1]; 257 for ( int k = 1; k < dn.length; k++ ) { 258 dn[k] = getCoefficientAsDouble( k-1 ) / k; 259 } 260 261 return new RealPolynomial( dn ); 262 } 263 264 268 public Polynomial scalarMultiply( Field.Member f ) { 269 if ( f instanceof Number ) { 270 double a = ( (Number ) f ).doubleValue(); 271 return scalarMultiply( a ); 272 } else { 273 throw new IllegalArgumentException ("Member class not recognised by this method."); 274 } 275 } 276 277 282 public RealPolynomial scalarMultiply( double a ) { 283 double[] c = new double[_coeff.length]; 284 for ( int k = 0; k < c.length; k++ ) { 285 c[k] = _coeff[k] * a; 286 } 287 288 return new RealPolynomial( c ); 289 } 290 291 296 public RealFunction multiply( RealFunction r ) { 297 if ( r instanceof RealPolynomial ) { 298 RealPolynomial p = (RealPolynomial) r; 299 int maxgrade = PolynomialMath.maxDegree( this, p ); 300 int mingrade = PolynomialMath.minDegree( this, p ); 301 int destgrade = maxgrade + mingrade; 302 double[] n = new double[destgrade+1]; 303 for ( int k = 0; k < _coeff.length; k++ ) { 304 for ( int j = 0; j < p._coeff.length; j++ ) { 305 n[k + j] += ( _coeff[k] * p._coeff[j] ); 306 } 307 } 308 309 return new RealPolynomial( n ); 310 } else { 311 throw new IllegalArgumentException ("Member class not recognised by this method."); 312 } 313 } 314 315 318 public AbelianGroup.Member negate() { 319 double[] c = new double[_coeff.length]; 320 for ( int k = 0; k < c.length; k++ ) { 321 c[k] = -_coeff[k]; 322 } 323 324 return new RealPolynomial( c ); 325 } 326 327 331 public RealFunction subtract( RealFunction g ) { 332 if ( g instanceof RealPolynomial ) { 333 RealPolynomial p = (RealPolynomial) g; 334 int maxgrade = PolynomialMath.maxDegree( this, p ); 335 double[] c = new double[maxgrade+1]; 336 for ( int k = 0; k < c.length; k++ ) { 337 c[k] = getCoefficientAsDouble( k ) - p.getCoefficientAsDouble( k ); 338 } 339 return new RealPolynomial( c ); 340 } else { 341 return super.subtract(g); 342 } 343 } 344 345 349 public String toString() { 350 StringBuffer sb = new StringBuffer ( "P(x) = " ); 351 if ( _coeff[degree()] < 0.0 ) { 352 sb.append( "-" ); 353 } else { 354 sb.append( " " ); 355 } 356 for ( int k = degree(); k > 0; k-- ) { 357 sb.append( Math.abs( _coeff[k] ) ).append( "x^" ).append( k ) 358 .append( ( _coeff[k-1] >= 0.0 ) ? " + " : " - " ); 359 } 360 sb.append( Math.abs(_coeff[0]) ); 361 362 return sb.toString(); 363 } 364 } 365 366 | Popular Tags |