Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a more efficient way of multiplying polynomials?
    primarykey
    data
    text
    <p>Here is my method for multiplying two polynomials of the form <code>an*x^n + an-1*x^n-1 + ... + a1*x + a0</code>. Each <code>Term</code> object has two fields: <code>double coefficient</code> and <code>int power</code>. <code>Polynomial</code> represents a polynomial by storing the terms in an <code>ArrayList&lt;Term&gt;</code>. This current implementation of multiply is O(n^2). Any ideas or hints on how to make it faster?</p> <pre><code>public Polynomial multiply(Polynomial P2) { PolynomialImp result = new PolynomialImp(); for (Term currentThisTerm : this.terms) { for (Term currentP2Term : ((PolynomialImp) P2).terms) { result.addTerm(new TermImp(currentThisTerm.getCoefficient()*currentP2Term.getCoefficient(), currentThisTerm.getExponent() + currentP2Term.getExponent())); } } //Sort polynomial in decreasing exponent order return result.sort(); } </code></pre> <p>Below is the addTerm method if needed:</p> <pre><code>private void addTerm(Term nextTerm) { for (int i = 0; i &lt; this.terms.size(); i++) { if (this.terms.get(i).getExponent() == nextTerm.getExponent()) { //Add the coefficients if the current term has the same exponent as a term that is already in the polynomial. //This preserves the sorting of the polynomial except during multiply. this.terms.set(i, new TermImp(this.terms.get(i).getCoefficient() + nextTerm.getCoefficient(), this.terms.get(i).getExponent())); return; } } //Avoid adding zeros to the polynomial. if (nextTerm.getCoefficient() != 0) this.terms.add(nextTerm); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload