KickJava   Java API By Example, From Geeks To Geeks.

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


1 package JSci.maths.polynomials;
2
3 import JSci.maths.Complex;
4 import JSci.maths.MathDouble;
5 import JSci.maths.fields.Field;
6 import JSci.maths.fields.Ring;
7 import JSci.maths.groups.AbelianGroup;
8
9
10 /**
11  *
12  * @author b.dietrich
13  */

14 public class ComplexPolynomialRing implements JSci.maths.fields.Ring {
15     private static final ComplexPolynomial ZERO =
16             new ComplexPolynomial( new Complex[] { Complex.ZERO } );
17     private static final ComplexPolynomial ONE =
18             new ComplexPolynomial( new Complex[] { Complex.ONE } );
19     private static final ComplexPolynomialRing _instance = new ComplexPolynomialRing();
20
21     /** Creates a new instance of ComplexPolynomialRing */
22     protected ComplexPolynomialRing() {
23     }
24
25     /**
26      * Singleton.
27      */

28     public static final ComplexPolynomialRing getInstance() {
29         return _instance;
30     }
31
32     /** Returns true if one member is the negative of the other.
33      * @param a a group member
34      * @param b a group member
35      *
36      */

37     public boolean isNegative( AbelianGroup.Member a, AbelianGroup.Member b ) {
38         return a.add( b ).equals( ZERO );
39     }
40
41     /** Returns true if the member is the unit element.
42      *
43      */

44     public boolean isOne( Ring.Member r ) {
45         return r.equals( ONE );
46     }
47
48     /** Returns true if the member is the identity element of this group.
49      * @param g a group member
50      *
51      */

52     public boolean isZero( AbelianGroup.Member g ) {
53         return g.equals( ZERO );
54     }
55
56     /** Returns the unit element.
57      *
58      */

59     public Ring.Member one() {
60         return ONE;
61     }
62
63     /** Returns the identity element.
64      *
65      */

66     public AbelianGroup.Member zero() {
67         return ZERO;
68     }
69
70     /**
71      * Internal method for typesafe cast
72      *
73      */

74     protected static Complex[] toComplex( Field.Member[] f ) {
75         Complex[] _c = null;
76         if ( f == null ) {
77             _c = new Complex[] { Complex.ZERO };
78         }
79         if ( f.length == 0 ) {
80             _c = new Complex[] { Complex.ZERO };
81         } else {
82             _c = new Complex[f.length];
83             for ( int k = 0; k < _c.length; k++ ) {
84                 if ( f[k] instanceof Complex ) {
85                     _c[k] = (Complex) f[k];
86                 } else if ( f[k] instanceof MathDouble ) {
87                     _c[k] = new Complex( ( (MathDouble) f[k] ).value(), 0. );
88                 } else {
89                     throw new IllegalArgumentException JavaDoc( "Different fields. Argument was " + f[k] );
90                 }
91             }
92         }
93
94         return _c;
95     }
96 }
97
Popular Tags