KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > groupJep > values > Polynomial


1 /* @author rich
2  * Created on 09-Mar-2004
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.groupJep.values;
9 import org.lsmp.djep.groupJep.interfaces.*;
10 import org.lsmp.djep.groupJep.*;
11 import org.nfunk.jep.type.*;
12 /**
13  * The ring of polynomials over a ring R.
14  *
15  * @author Rich Morris
16  * Created on 09-Mar-2004
17  */

18 public class Polynomial extends Number JavaDoc {
19     private RingI baseRing;
20     private String JavaDoc symbol;
21     private Number JavaDoc coeffs[];
22     private int degree;
23     /**
24      *
25      */

26     public Polynomial(RingI baseRing,String JavaDoc symbol,Number JavaDoc coeffs[]) {
27         this.baseRing = baseRing;
28         this.symbol = symbol;
29         int deg=0;
30         for(int i=coeffs.length-1;i>0;--i)
31             if(!baseRing.equals(coeffs[i],baseRing.getZERO()))
32             {
33                 deg=i;
34                 break;
35             }
36         if(deg == coeffs.length-1)
37             this.coeffs = coeffs;
38         else
39         {
40             this.coeffs = new Number JavaDoc[deg+1];
41             System.arraycopy(coeffs,0,this.coeffs,0,deg+1);
42         }
43         this.degree = deg;
44     }
45
46     /** Sub classes can change the coefficients. Other methods
47      * should treat polynomials as imutable. */

48     protected void setCoeffs(Number JavaDoc coeffs[])
49     {
50         this.coeffs = coeffs;
51         this.degree = coeffs.length-1;
52     }
53     /** sub classes should overright this to make the correct type. */
54     protected Polynomial valueOf(Number JavaDoc lcoeffs[])
55     {
56         Polynomial p = new Polynomial(baseRing,symbol,lcoeffs);
57         return p;
58     }
59     public Polynomial add(Polynomial poly)
60     {
61         int deg = degree > poly.degree ? degree : poly.degree;
62         Number JavaDoc lcoeffs[] = new Number JavaDoc[deg+1];
63         for(int i=0;i<=deg;++i)
64         {
65             if(i<=degree && i <= poly.degree)
66                 lcoeffs[i] = baseRing.add(coeffs[i],poly.coeffs[i]);
67             else if(i<=degree)
68                 lcoeffs[i] = coeffs[i];
69             else
70                 lcoeffs[i] = poly.coeffs[i];
71         }
72         return valueOf(lcoeffs);
73     }
74
75     public Polynomial sub(Polynomial poly)
76     {
77         int deg = degree > poly.degree ? degree : poly.degree;
78         Number JavaDoc lcoeffs[] = new Number JavaDoc[deg+1];
79         for(int i=0;i<=deg;++i)
80         {
81             if(i<=degree && i <= poly.degree)
82                 lcoeffs[i] = baseRing.sub(coeffs[i],poly.coeffs[i]);
83             else if(i<=degree)
84                 lcoeffs[i] = coeffs[i];
85             else
86                 lcoeffs[i] = baseRing.getInverse(poly.coeffs[i]);
87         }
88         return valueOf(lcoeffs);
89     }
90     
91     public Polynomial mul(Polynomial poly)
92     {
93         int deg = degree + poly.degree;
94         Number JavaDoc lcoeffs[] = new Number JavaDoc[deg+1];
95         for(int i=0;i<=deg;++i)
96             lcoeffs[i] = baseRing.getZERO();
97
98         for(int i=0;i<=degree;++i)
99             for(int j=0;j<=poly.degree;++j)
100             {
101                 lcoeffs[i+j] = baseRing.add(lcoeffs[i+j],
102                     baseRing.mul(coeffs[i],poly.coeffs[j]));
103             }
104         return valueOf(lcoeffs);
105     }
106
107     private String JavaDoc stripBrackets(Number JavaDoc num)
108     {
109         String JavaDoc s = num.toString();
110         if(s.startsWith("<") && s.endsWith(">"))
111         {
112             if(s.indexOf('+')!=-1 || s.indexOf('-')>1) // is it <x+y> or <x-y>. <-x> is OK
113
return s;
114             else
115                 return s.substring(1,s.length()-1);
116         }
117         else return s;
118     }
119     public String JavaDoc toString()
120     {
121         if(degree==0) return "<"+stripBrackets(coeffs[0])+">";
122         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<");
123         for(int i=degree;i>=0;--i)
124         {
125             String JavaDoc s = stripBrackets(coeffs[i]);
126
127             // don't bother if a zero coeff
128
if(s.equals("0") ||
129               this.baseRing.equals(coeffs[i],baseRing.getZERO()))
130                 continue;
131
132             // apart from first add a + sign if positive
133
if(i!=degree && !s.startsWith("-")) sb.append("+");
134             
135             // always print the final coeff (if non zero)
136
if( i==0 ) {
137                 String JavaDoc s1 = coeffs[i].toString();
138                 if(s1.startsWith("<") && s1.endsWith(">"))
139                 {
140                         sb.append(s1.substring(1,s1.length()-1));
141                 }
142                 else sb.append(s1);
143                 break;
144             }
145             // if its -1 t^i just print -
146
if(s.equals("-1"))
147                 sb.append("-");
148             else if(s.equals("1") ||
149                 this.baseRing.equals(
150                     coeffs[i],
151                     baseRing.getONE()))
152                 {} // don't print 1
153
else {
154                 sb.append(stripBrackets(coeffs[i]));
155                 sb.append(" ");
156             }
157             if(i>=2) sb.append(symbol+"^"+i);
158             else if(i==1) sb.append(symbol);
159         }
160         sb.append(">");
161         return sb.toString();
162     }
163     
164     public int getDegree() { return degree; }
165     public String JavaDoc getSymbol() { return symbol; }
166     public Number JavaDoc[] getCoeffs() { return coeffs; }
167     public Number JavaDoc getCoeff(int i) { return coeffs[i]; }
168     public RingI getBaseRing() { return baseRing; }
169
170     /** value of constant coeff. */
171     public int intValue() {return coeffs[0].intValue(); }
172     /** value of constant coeff. */
173     public long longValue() {return coeffs[0].longValue(); }
174     /** value of constant coeff. */
175     public float floatValue() { return coeffs[0].floatValue(); }
176     /** value of constant coeff. */
177     public double doubleValue() {return coeffs[0].doubleValue(); }
178
179     public boolean equals(Polynomial n)
180     {
181         if(this.getDegree()!=n.getDegree()) return false;
182         for(int i=0;i<=this.getDegree();++i)
183             if(!baseRing.equals(this.getCoeff(i),n.getCoeff(i)))
184                 return false;
185         return true;
186     }
187
188     /** returns the complex value of this polynomial.
189      * Where the value of the symbol is replaced by rootVal.
190      */

191     public Complex calculateComplexValue(Complex rootVal) {
192         Number JavaDoc val = coeffs[this.getDegree()];
193         Complex cval = GroupJep.getComplexValue(val);
194         
195         for(int i=this.getDegree()-1;i>=0;--i)
196         {
197             Number JavaDoc val2 = coeffs[i];
198             Complex cval2 = GroupJep.getComplexValue(val2);
199             Complex prod = cval.mul(rootVal);
200             cval = prod.add(cval2);
201         }
202         return cval;
203     }
204
205 }
206
Popular Tags