[Datastructure] Another usage of list: Polynomial

Source: Internet
Author: User

Statements: This blog was written by me, but most of content is quoted from book [Data Structure with Java hubbard]

[Description]

Apolympus nomialis a mathematical function of the form:

P (x) = a0xn + a1xn-1 + a2xn-2 +??? + An-1x + an the greatest exponent, N, is called the degreeof the polynomial. for example, p (x) = 7x4-2 is abpolynomial of degree 4. the simplest polynomials are constant polynomialssuch as p (x) = 6 (degree 0) and linear polynomialssuch as p (x) = 9x + 6 (degree 1 ). the unique zero polynomial p (x) = 0 is defined to have degree-1. in this section we present a polynomialclass whose instances represent mathematical polynomials and which supports the usual algebraic operations on polynomials. A Polynomial can be regarded as a sum of distinct terms. A Termis a mathematical function of the form T (x) = cxe, where CIS any real number and EIS any nonnegative integer. the number ciscalled the coefficient, and the number EIS called the exponent. to define a class whose objects represent polynomials, we use a linked list of termobjects. for example, the polynomial p (x) = 3x2-2x + 5 cocould be represented as a list of three elements, where the first element represents the term 3x2, the second element represents the term-2x, andthe third element represents the (constant) term 5.

[Implement]

package com.albertshao.ds.polynomial;//  Data Structures with Java, Second Edition//  by John R. Hubbard//  Copyright 2007 by McGraw-Hillimport java.util.*;public class Polynomial {  private List<Term> list = new LinkedList<Term>();  public static final Polynomial ZERO = new Polynomial();  private Polynomial() {  }  public Polynomial(double coef, int exp) {    if (coef != 0.0) {      list.add(new Term(coef, exp));    }  }  public Polynomial(double... a) {    for (int i=0; i<a.length; i++) {      if (a[i] != 0.0) {        list.add(new Term(a[i], i));      }    }  }  public Polynomial(Polynomial p) {  // copy constructor    for (Term term : p.list) {      this.list.add(new Term(term));    }  }  public int degree() {    if (list.isEmpty()) {      return -1;    } else {      return list.get(list.size()-1).exp;    }  }  public boolean isZero() {    return list.isEmpty();  }  public Polynomial plus(Polynomial p) {    if (this.isZero()) {      return new Polynomial(p);    }    if (p.isZero()) {      return new Polynomial(this);    }    Polynomial q = new Polynomial();    ListIterator<Term> it = list.listIterator();    ListIterator<Term> itp = p.list.listIterator();    while (it.hasNext() && itp.hasNext()) {      Term term = it.next();      Term pTerm = itp.next();      if (term.exp < pTerm.exp) {        q.list.add(new Term(term));        itp.previous();      } else if (term.exp == pTerm.exp) {        q.list.add(new Term(term.coef + pTerm.coef, term.exp));      } else {  // (term.exp > pTerm.exp)         q.list.add(new Term(pTerm));        it.previous();      }    }    while (it.hasNext()) {      q.list.add(new Term(it.next()));    }    while (itp.hasNext()) {      q.list.add(new Term(itp.next()));    }    return q;  }  public String toString() {    if (this.isZero()) {      return "0";    }    Iterator<Term> it = list.iterator();    StringBuilder buf = new StringBuilder();    boolean isFirstTerm = true;    while (it.hasNext()) {      Term term = it.next();      double c = term.coef;      int e = term.exp;      if (isFirstTerm) {        buf.append(String.format("%.2f", c));        isFirstTerm = false;      } else {        if (term.coef < 0) {          buf.append(String.format(" - %.2f", -c));        } else {          buf.append(String.format(" + %.2f", c));        }      }      if (e == 1) {        buf.append("x");      } else if (e > 1) {        buf.append("x^" + e);      }    }    return buf.toString();  }  private class Term {    private double coef;    private int exp;    public Term(double coef, int exp) {      if (coef == 0.0 || exp < 0) {        throw new IllegalArgumentException();      }      this.coef = coef;      this.exp = exp;    }    public Term(Term that) {  // copy constructor      this(that.coef, that.exp);    }  }}

//  Data Structures with Java, Second Edition//  by John R. Hubbard//  Copyright 2007 by McGraw-Hillpackage com.albertshao.ds.polynomial;public class TestPolynomial {public static void main(String[] args) {Polynomial p = new Polynomial(3, -8, 0, 0, 2, 1);Polynomial q = new Polynomial(0, 5, 6, 9);System.out.println("p: " + p);System.out.println("p.degree(): " + p.degree());System.out.println("q: " + q);System.out.println("q.degree(): " + q.degree());System.out.println("p.plus(q): " + p.plus(q));System.out.println("q.plus(p): " + q.plus(p));System.out.println("p.plus(q).degree(): " + p.plus(q).degree());Polynomial z = new Polynomial(0);System.out.println("z: " + z);System.out.println("z.degree(): " + z.degree());System.out.println("p.plus(z): " + p.plus(z));System.out.println("z.plus(p): " + z.plus(p));System.out.println("p: " + p);Polynomial t = new Polynomial(8.88, 44);System.out.println("t: " + t);System.out.println("t.degree(): " + t.degree());}}

[Result]
p: 3.00 - 8.00x + 2.00x^4 + 1.00x^5p.degree(): 5q: 5.00x + 6.00x^2 + 9.00x^3q.degree(): 3p.plus(q): 3.00 - 3.00x + 6.00x^2 + 9.00x^3 + 2.00x^4 + 1.00x^5q.plus(p): 3.00 - 3.00x + 6.00x^2 + 9.00x^3 + 2.00x^4 + 1.00x^5p.plus(q).degree(): 5z: 0z.degree(): -1p.plus(z): 3.00 - 8.00x + 2.00x^4 + 1.00x^5z.plus(p): 3.00 - 8.00x + 2.00x^4 + 1.00x^5p: 3.00 - 8.00x + 2.00x^4 + 1.00x^5t: 8.88x^44t.degree(): 44



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.