public
Created

Polynomial Data Structure

  • Download Gist
PolynomialTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/*
* Question: Create a data structure to represent polynomial addition and substraction
* Run the code online: http://ideone.com/s5qjU
*/
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.*;
 
public class PolynomialTest
{
public static class Term
implements Cloneable
{
int coefficient;
int power;
public Term(int coefficient, int power) {
this.coefficient = coefficient;
this.power = power;
}
public String toString() {
StringBuffer display = new StringBuffer();
if (power == 0) {
display.append(coefficient);
}
else if (power > 0) {
if (coefficient != 1) display.append(coefficient);
display.append('x');
if (power > 1) display.append('^').append(power);
}
return display.toString();
}
 
public Term add(Term other) {
if (this.power == other.power) this.coefficient += other.coefficient;
return this;
}
protected Object clone() {
Term term = new Term(this.coefficient,this.power);
return term;
}
public boolean equals(Object o) {
if (!(o instanceof Term)) return false;
Term other = (Term)o;
return this.coefficient == other.coefficient && this.power == other.power;
}
}
 
public static class Polynomial
{
Map<Integer,Term> terms;
public Polynomial() {
this.terms = new TreeMap<Integer,Term>();
}
 
public Polynomial(Collection<Term> list) {
this();
for (Term term : list) this.add((Term)term.clone());
}
 
public Polynomial add(Term other) {
Term term = terms.get(other.power);
terms.put(other.power,(term == null) ? other : term.add(other));
return this;
}
 
public Polynomial add(int coefficient, int power) {
add(new Term(coefficient,power));
return this;
}
public Polynomial add(Polynomial other) {
Polynomial poly = new Polynomial(getTerms());
for (Term term : other.getTerms()) poly.add(term);
return poly;
}
 
public void plus(Polynomial other) {
for (Term term : other.getTerms()) this.add(term);
}
public Polynomial multiply(Polynomial other) {
Polynomial poly = new Polynomial();
for (Term first : this.getTerms()) {
Polynomial current = new Polynomial();
for (Term second : other.getTerms()) {
Term product = new Term(first.coefficient*second.coefficient,first.power+second.power);
current.add(product);
}
poly.plus(current);
}
return poly;
}
public String toString() {
StringBuffer display = new StringBuffer();
int count = 0;
for (Term term : getTerms()) {
display.append(term.toString());
if (count<terms.size()-1) display.append('+');
count++;
}
return display.toString();
}
public Collection<Term> getTerms() {
return this.terms.values();
}
public boolean equals(Object o) {
if (!(o instanceof Polynomial)) return false;
Polynomial other = (Polynomial)o;
return this.terms.equals(other.terms);
}
}
// x^2 + 2x
// + 1x + 2
//----------------
// x^2 + 3x + 2
@Test public void testPolynomialAddition1()
{
System.out.println();
Polynomial first = new Polynomial().add(new Term(1,2)).add(new Term(2,1));
Polynomial second = new Polynomial().add(new Term(1,1)).add(new Term(2,0));
Polynomial expect = new Polynomial().add(new Term(1,2)).add(new Term(3,1)).add(new Term(2,0));
Polynomial output = first.add(second);
System.out.println("first = "+first);
System.out.println("second = "+second);
System.out.println("sum = "+output);
assertEquals(expect,output);
}
// x + 1
// + x + 2
//---------------
// 2x + 3
@Test public void testPolynomialAddition2()
{
System.out.println();
Polynomial first = new Polynomial().add(new Term(1,1)).add(new Term(1,0));
Polynomial second = new Polynomial().add(new Term(1,1)).add(new Term(2,0));
Polynomial expect = new Polynomial().add(new Term(2,1)).add(new Term(3,0));
Polynomial output = first.add(second);
System.out.println("first = "+first);
System.out.println("second = "+second);
System.out.println("sum = "+output);
assertEquals(expect,output);
}
 
// x + 1
// * x + 2
// -------------------------
// x*(x + 2) + 1*(x + 2)
// x^2 + 2x + x + 2
// x^2 + 3x + 2
@Test public void testPolynomialMultiply()
{
System.out.println();
Polynomial first = new Polynomial().add(new Term(1,1)).add(new Term(1,0));
Polynomial second = new Polynomial().add(new Term(1,1)).add(new Term(2,0));
Polynomial output = first.multiply(second);
Polynomial expect = new Polynomial().add(new Term(1,2)).add(new Term(3,1)).add(new Term(2,0));
System.out.println("first = "+first);
System.out.println("second = "+second);
System.out.println("product = "+output);
assertEquals(expect,output);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.