aboutsummaryrefslogtreecommitdiff
path: root/Week7
diff options
context:
space:
mode:
authorCamil Staps2015-03-17 21:57:28 +0100
committerCamil Staps2015-03-17 21:57:28 +0100
commitb0646c93d7a2aece16b099ab805af2d58753ac79 (patch)
treee204fb1265a2deed188e115647116e4c8a23226f /Week7
parentAdded comment about ordering on exponent (diff)
Various small enhancements
Diffstat (limited to 'Week7')
-rw-r--r--Week7/src/polynomial/Polynomial.java11
-rw-r--r--Week7/src/polynomial/Term.java10
-rw-r--r--Week7/test/polynomial/PolynomialTest.java72
3 files changed, 79 insertions, 14 deletions
diff --git a/Week7/src/polynomial/Polynomial.java b/Week7/src/polynomial/Polynomial.java
index f7b1194..f7f9860 100644
--- a/Week7/src/polynomial/Polynomial.java
+++ b/Week7/src/polynomial/Polynomial.java
@@ -116,7 +116,7 @@ public class Polynomial {
return;
}
}
- terms.add(t);
+ terms.add(new Term(t));
}
/**
@@ -124,8 +124,9 @@ public class Polynomial {
* @param p the polynomial to subtract
*/
public void minus(Polynomial p) {
- p.times(new Polynomial("-1 0"));
- plus(p);
+ Polynomial temp = new Polynomial(p);
+ temp.times(new Polynomial("-1 0"));
+ plus(temp);
}
/**
@@ -136,7 +137,7 @@ public class Polynomial {
Polynomial result = new Polynomial();
for (Term that_t : p.terms)
for (Term this_t : terms)
- result.plus(new Polynomial((that_t.getCoef() * this_t.getCoef()) + " " + (that_t.getExp() + this_t.getExp())));
+ result.plus(new Term(that_t.getCoef() * this_t.getCoef(), that_t.getExp() + this_t.getExp()));
terms = result.terms;
}
@@ -153,7 +154,7 @@ public class Polynomial {
if (other_poly == null || other_poly.getClass() != getClass())
return false;
- Polynomial that = (Polynomial) other_poly;
+ Polynomial that = new Polynomial((Polynomial) other_poly); // We need to copy because later we'll remove elements
if (terms.size() != that.terms.size())
return false;
for (Term this_t : terms) {
diff --git a/Week7/src/polynomial/Term.java b/Week7/src/polynomial/Term.java
index 7cde927..60c6381 100644
--- a/Week7/src/polynomial/Term.java
+++ b/Week7/src/polynomial/Term.java
@@ -81,17 +81,21 @@ public class Term {
/**
* Converts a term into a readable representation the standard format is
* %fx^%d
+ * I changed the implementation to remove trailing zeroes from the coefficient.
*
* @return the string representing the term
*/
@Override
public String toString() {
+ String nice_coefficient = Double.toString(coefficient);
+ if (Math.round(coefficient) == coefficient)
+ nice_coefficient = Integer.toString((int) coefficient);
if (exponent == 0) {
- return String.format("%f", coefficient, exponent);
+ return String.format("%s", nice_coefficient, exponent);
} else if (exponent == 1) {
- return String.format("%fx", coefficient, exponent);
+ return String.format("%sx", nice_coefficient, exponent);
} else {
- return String.format("%fx^%d", coefficient, exponent);
+ return String.format("%sx^%d", nice_coefficient, exponent);
}
}
diff --git a/Week7/test/polynomial/PolynomialTest.java b/Week7/test/polynomial/PolynomialTest.java
index c0f9c4b..8560dfb 100644
--- a/Week7/test/polynomial/PolynomialTest.java
+++ b/Week7/test/polynomial/PolynomialTest.java
@@ -1,8 +1,3 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
package polynomial;
import org.junit.After;
@@ -44,7 +39,7 @@ public class PolynomialTest {
public void testToString() {
System.out.println("toString");
// Test exp=0, exp=1, exp>1, positive & negative integer & floating point coefficients all at once
- assertEquals("3.000000x^5-4.000000x+2.500000-3.400000x^-7", new Polynomial("3 5 -4 1 2.5 0 -3.4 -7").toString());
+ assertEquals("3x^5-4x+2.5-3.4x^-7", new Polynomial("3 5 -4 1 2.5 0 -3.4 -7").toString());
// Test zero polynomial
assertEquals("", new Polynomial().toString());
}
@@ -59,6 +54,33 @@ public class PolynomialTest {
Polynomial instance = new Polynomial("7 4 -4 2 3 1 5 0"); // 7x^4 - 4x^2 + 3x + 5
instance.minus(new Polynomial("5 3 4 2 3 1 10 0")); // 5x^3 + 4x^2 + 3x + 10
assertEquals(new Polynomial("7 4 -5 3 -8 2 -5 0"), instance); // 7x^4 - 5x^3 - 8x^2 - 5
+
+ // Test minus = plus (times -1) on many small 2-term polynomials
+ for (float c1 = -10; c1 < 10; c1 += 2.3)
+ for (int exp1 = 5; exp1 < 10; exp1++)
+ for (float c2 = -10; c2 < 10; c2 += 2.3)
+ for (int exp2 = 5; exp2 < 10; exp2++) {
+ Polynomial fst = new Polynomial(c1 + " " + exp1 + " " + c2 + " " + exp2);
+ Polynomial snd = new Polynomial(c1 + " " + exp2 + " " + c2 + " " + exp1);
+ Polynomial fst_2 = new Polynomial(fst);
+ Polynomial snd_2 = new Polynomial(snd);
+ fst.minus(snd);
+ snd_2.times(new Polynomial("-1 0"));
+ fst_2.plus(snd_2);
+ assertEquals(true, fst.equals(fst_2));
+ }
+
+ // Test associativity for some random polynomials
+ Polynomial a = new Polynomial("5 4 2 5 20 8 4 0");
+ Polynomial b = new Polynomial("9 2 48 2 10 8 28 2");
+ Polynomial c = new Polynomial("3 8 4 20 8 2 4 5 0 0");
+ Polynomial a_bc = new Polynomial(a);
+ a_bc.minus(b);
+ a_bc.minus(c);
+ Polynomial a_cb = new Polynomial(a);
+ a_cb.minus(c);
+ a_cb.minus(b);
+ assertEquals(true, a_bc.equals(a_cb));
}
/**
@@ -82,6 +104,25 @@ public class PolynomialTest {
instance = new Polynomial("4 4 2 3"); // 4x^4 + 2x^3
instance.times(new Polynomial("2 2 -4 3")); // 2x^2 - 4x^3
assertEquals(new Polynomial("4 5 -16 7"), instance); // 4x^5 - 16x^7 (exponent 6 cancels out)
+
+ // Test associativity for some random polynomials
+ Polynomial a = new Polynomial("5 4 2 5 20 8 4 0");
+ Polynomial b = new Polynomial("9 2 48 2 10 8 28 2");
+ Polynomial c = new Polynomial("3 8 4 20 8 2 4 5 0 0");
+ Polynomial a_bc = new Polynomial(a);
+ a_bc.times(b);
+ a_bc.times(c);
+ Polynomial a_cb = new Polynomial(a);
+ a_cb.times(c);
+ a_cb.times(b);
+ assertEquals(true, a_bc.equals(a_cb));
+
+ // Test commutativity
+ Polynomial a_b = new Polynomial(a);
+ a_b.times(b);
+ Polynomial b_a = new Polynomial(b);
+ b_a.times(a);
+ assertEquals(true, a_b.equals(b_a));
}
/**
@@ -127,6 +168,25 @@ public class PolynomialTest {
Polynomial instance = new Polynomial("7 4 -4 2 7 1 5 0"); // 7x^4 - 4x^2 + 7x + 5
instance.plus(new Polynomial("5 3 4 2 3 1 10 0")); // 5x^3 + 4x^2 + 3x + 10
assertEquals(new Polynomial("7 4 5 3 10 1 15 0"), instance); // 7x^4 + 5x^3 + 10x + 15
+
+ // Test associativity for some random polynomials
+ Polynomial a = new Polynomial("5 4 2 5 20 8 4 0");
+ Polynomial b = new Polynomial("9 2 7 5 10 8 28 2");
+ Polynomial c = new Polynomial("3 8 4 20 8 2 4 5 0 0");
+ Polynomial a_bc = new Polynomial(a);
+ a_bc.plus(b);
+ a_bc.plus(c);
+ Polynomial a_cb = new Polynomial(a);
+ a_cb.plus(c);
+ a_cb.plus(b);
+ assertEquals(true, a_bc.equals(a_cb));
+
+ // Test commutativity
+ Polynomial a_b = new Polynomial(a);
+ a_b.plus(b);
+ Polynomial b_a = new Polynomial(b);
+ b_a.plus(a);
+ assertEquals(true, a_b.equals(b_a));
}
/**