KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > polynomials > ComplexPolynomial


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 /**
10  * A Polynomial over the complex field.
11  * For a description of the methods
12  * @see JSci.maths.polynomials.RealPolynomial
13  *
14  * @author b.dietrich
15  */

16 public class ComplexPolynomial implements Polynomial {
17     private Complex[] _coeff;
18
19     /** Creates a new instance of ComplexPolynomial */
20     public ComplexPolynomial( Complex[] coeff ) {
21         if ( coeff == null ) {
22             throw new NullPointerException JavaDoc("Coefficients cannot be null");
23         }
24         _coeff = normalise(coeff);
25     }
26
27         /**
28         * Normalises the coefficient array.
29         * Trims off any leading (high degree) zero terms.
30         */

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     /**
47      * Creates a new ComplexPolynomial object.
48      *
49      * @param f
50      */

51     public ComplexPolynomial( Field.Member[] f ) {
52         _coeff = normalise(ComplexPolynomialRing.toComplex( f ));
53     }
54
55     /**
56      *
57      * @param n
58      */

59     public Field.Member getCoefficient( int n ) {
60         return getCoefficientAsComplex( n );
61     }
62
63     /**
64      *
65      * @param n
66      */

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     /**
76      */

77     public Field.Member[] getCoefficients() {
78         return getCoefficientsAsComplexes();
79     }
80
81     /**
82      * Return the coefficients as an array of complex numbers.
83      */

84     public Complex[] getCoefficientsAsComplexes() {
85         return _coeff;
86     }
87
88         /**
89         * Evaluates this polynomial.
90         */

91         public Complex map(Complex z) {
92                 return PolynomialMath.evalPolynomial(this, z);
93         }
94         /**
95         * Evaluates this polynomial.
96         */

97         public Complex map(double x, double y) {
98                 return map(new Complex(x, y));
99         }
100
101     /**
102      *
103      * @return the degree
104      */

105     public int degree() {
106         return _coeff.length-1;
107     }
108
109     public Object JavaDoc getSet() {
110         return ComplexPolynomialRing.getInstance();
111     }
112
113     /**
114      *
115      * @return true if this is equal to zero.
116      */

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     /**
128      *
129      * @return true if this is equal to one.
130      */

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     /** The group composition law.
145      * @param g a group member
146      *
147      */

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 JavaDoc("Member class not recognised by this method.");
160         }
161
162         return result;
163     }
164
165     /**
166      * Returns the division of this polynomial by a scalar.
167      * @param f
168      *
169      */

170     public Polynomial scalarDivide( Field.Member f ) {
171         if ( f instanceof Complex ) {
172             return scalarDivide( (Complex) f );
173         } else if ( f instanceof Number JavaDoc ) {
174             return scalarDivide( ( (Number JavaDoc) f ).doubleValue() );
175         } else {
176             throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
177         }
178     }
179
180     /**
181      * Returns the division of this polynomial by a scalar.
182      * @param a
183      */

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     /**
194      * Returns the division of this polynomial by a scalar.
195      * @param a
196      */

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     /**
207      *
208      * @param o
209      */

210     public boolean equals( Object JavaDoc 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     /**
228      *
229      */

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     /**
240      * Returns the multiplication of this polynomial by a scalar.
241      * @param f
242      */

243     public Polynomial scalarMultiply( Field.Member f ) {
244         if ( f instanceof Number JavaDoc ) {
245             double a = ( (Number JavaDoc) f ).doubleValue();
246             return scalarMultiply( a );
247         } else if ( f instanceof Complex ) {
248             return scalarMultiply( (Complex) f );
249         } else {
250             throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
251         }
252     }
253
254     /**
255      * Returns the multiplication of this polynomial by a scalar.
256      * @param a
257      */

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     /**
268      * Returns the multiplication of this polynomial by a scalar.
269      * @param a
270      */

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     /** The multiplication law.
281      * @param r a ring member
282      *
283      */

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 JavaDoc("Member class not recognised by this method.");
305         }
306     }
307
308     /** Returns the inverse member.
309      *
310      */

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     /** The group composition law with inverse.
321      * @param g a group member
322      *
323      */

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 JavaDoc("Member class not recognised by this method.");
335         }
336     }
337
338     /**
339      *
340      */

341     public String JavaDoc toString() {
342         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( "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