1 package JSci.maths.polynomials; 2 3 import JSci.maths.MathDouble; 4 import JSci.maths.fields.*; 5 import JSci.maths.groups.*; 6 7 8 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 18 protected RealPolynomialRing() { 19 } 20 21 24 public static final RealPolynomialRing getInstance() { 25 return _instance; 26 } 27 28 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 ("Member class not recognised by this method."); 41 } 42 } 43 44 47 public boolean isOne( Ring.Member r ) { 48 if ( r instanceof RealPolynomial ) { 49 return ( (RealPolynomial) r ).isOne(); 50 } else { 51 throw new IllegalArgumentException ("Member class not recognised by this method."); 52 } 53 } 54 55 59 public boolean isZero( AbelianGroup.Member g ) { 60 if ( g instanceof RealPolynomial ) { 61 return ( (RealPolynomial) g ).isZero(); 62 } else { 63 throw new IllegalArgumentException ("Member class not recognised by this method."); 64 } 65 } 66 67 70 public Ring.Member one() { 71 return ONE; 72 } 73 74 77 public AbelianGroup.Member zero() { 78 return ZERO; 79 } 80 81 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 ( "Expected MathDouble. Got (" + k + ") " + f[k] ); 97 } 98 } 99 100 return d; 101 } 102 103 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 |