public Polynomial mul(Polynomial poly) { int deg = degree + poly.degree; Number lcoeffs[] = new Number[deg + 1]; for (int i = 0; i <= deg; ++i) lcoeffs[i] = baseRing.getZERO(); for (int i = 0; i <= degree; ++i) for (int j = 0; j <= poly.degree; ++j) { lcoeffs[i + j] = baseRing.add(lcoeffs[i + j], baseRing.mul(coeffs[i], poly.coeffs[j])); } return valueOf(lcoeffs); }
/** * Construct a polynomial over a ring. In general the valueOf method should be used to construct a * new polynomial. * * @param baseRing the underlying ring of the polynomial. * @param symbol the symbol used to display the polynomial * @param coeffs an array of coefficients in the base ring coeff[0] is constant, coeff[1] is * coefficient of t etc. */ public Polynomial(RingI baseRing, String symbol, Number coeffs[]) { this.baseRing = baseRing; this.symbol = symbol; int deg = 0; for (int i = coeffs.length - 1; i > 0; --i) if (!baseRing.equals(coeffs[i], baseRing.getZERO())) { deg = i; break; } if (deg == coeffs.length - 1) this.coeffs = coeffs; else { this.coeffs = new Number[deg + 1]; System.arraycopy(coeffs, 0, this.coeffs, 0, deg + 1); } this.degree = deg; }
public String toString() { if (degree == 0) return coeffs[0].toString(); StringBuffer sb = new StringBuffer(""); for (int i = degree; i >= 0; --i) { String s = coeffs[i].toString(); // don't bother if a zero coeff if (s.equals("0") || this.baseRing.equals(coeffs[i], baseRing.getZERO())) continue; // apart from first add a + sign if positive if (i != degree && !s.startsWith("-")) sb.append("+"); // always print the final coeff (if non zero) if (i == 0) { String s1 = coeffs[i].toString(); sb.append(s1); // if(s1.startsWith("(") && s1.endsWith(")")) // { // sb.append(s1.substring(1,s1.length()-1)); // } // else sb.append(s1); break; } // if its -1 t^i just print - if (s.equals("-1")) sb.append("-"); else if (s.equals("1") || this.baseRing.equals(coeffs[i], baseRing.getONE())) { } // don't print 1 else { if (needsBrackets(coeffs[i].toString())) { sb.append("("); sb.append(coeffs[i].toString()); sb.append(")"); } else sb.append(coeffs[i].toString()); // sb.append(stripBrackets(coeffs[i])); sb.append(" "); } if (i >= 2) sb.append(symbol + "^" + i); else if (i == 1) sb.append(symbol); } sb.append(""); return sb.toString(); }