KickJava   Java API By Example, From Geeks To Geeks.

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


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

12 public class RealPolynomialRing implements JSci.maths.fields.Ring {
13     private static final RealPolynomial ZERO = new RealPolynomial( new double[] { 0.0 } );
14     private static final RealPolynomial ONE = new RealPolynomial( new double[] { 1.0 } );
15     private static final RealPolynomialRing _instance = new RealPolynomialRing();
16
17     /** Creates a new instance of PolynomialRing */
18     protected RealPolynomialRing() {
19     }
20
21     /**
22      * Singleton.
23      */

24     public static final RealPolynomialRing getInstance() {
25         return _instance;
26     }
27
28     /** Returns true if one member is the negative of the other.
29      * @param a a group member
30      * @param b a group member
31      *
32      */

33     public boolean isNegative( AbelianGroup.Member a, AbelianGroup.Member b ) {
34         if ( ( a instanceof RealPolynomial ) && ( b instanceof RealPolynomial ) ) {
35             RealPolynomial p1 = (RealPolynomial) a;
36             RealPolynomial p2 = (RealPolynomial) b;
37
38             return p1.add( p2 ).equals( ZERO );
39         } else {
40             throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
41         }
42     }
43
44     /** Returns true if the member is the unit element.
45      *
46      */

47     public boolean isOne( Ring.Member r ) {
48         if ( r instanceof RealPolynomial ) {
49             return ( (RealPolynomial) r ).isOne();
50         } else {
51             throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
52         }
53     }
54
55     /** Returns true if the member is the identity element of this group.
56      * @param g a group member
57      *
58      */

59     public boolean isZero( AbelianGroup.Member g ) {
60         if ( g instanceof RealPolynomial ) {
61             return ( (RealPolynomial) g ).isZero();
62         } else {
63             throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
64         }
65     }
66
67     /** Returns the unit element.
68      *
69      */

70     public Ring.Member one() {
71         return ONE;
72     }
73
74     /** Returns the identity element.
75      *
76      */

77     public AbelianGroup.Member zero() {
78         return ZERO;
79     }
80
81     /**
82      * internal method for safe typecast
83      */

84     protected static double[] toDouble( Field.Member[] f ) {
85         if ( f == null ) {
86             return null;
87         }
88
89         int dim = f.length;
90
91         double[] d = new double[dim];
92         for ( int k = 0; k < dim; k++ ) {
93             if ( f[k] instanceof MathDouble ) {
94                 d[k] = ( (MathDouble) f[k] ).value();
95             } else {
96                 throw new IllegalArgumentException JavaDoc( "Expected MathDouble. Got (" + k + ") " + f[k] );
97             }
98         }
99
100         return d;
101     }
102
103     /**
104      * internal method for safe typecast
105      */

106     protected static MathDouble[] toMathDouble( double[] d ) {
107         if ( d == null ) {
108             return null;
109         }
110
111         int dim = d.length;
112         MathDouble[] s = new MathDouble[dim];
113         for ( int k = 0; k < dim; k++ ) {
114             s[k] = new MathDouble( d[k] );
115         }
116
117         return s;
118     }
119 }
120
Popular Tags