remove spacing from head of polynomial string (#1032)

This commit is contained in:
MakKi (makki_d) 2018-06-20 02:53:30 +09:00 committed by Sean Owen
parent fc057169cd
commit 15b09aeda5
2 changed files with 9 additions and 3 deletions

View file

@ -225,15 +225,19 @@ final class GenericGFPoly {
@Override
public String toString() {
if (getDegree() == 0) {
return Integer.toString(getCoefficient(0));
if (isZero()) {
return "0";
}
StringBuilder result = new StringBuilder(8 * getDegree());
for (int degree = getDegree(); degree >= 0; degree--) {
int coefficient = getCoefficient(degree);
if (coefficient != 0) {
if (coefficient < 0) {
result.append(" - ");
if (degree == getDegree()) {
result.append("-");
} else {
result.append(" - ");
}
coefficient = -coefficient;
} else {
if (result.length() > 0) {

View file

@ -32,6 +32,8 @@ public final class GenericGFPolyTestCase extends Assert {
assertEquals("-1", FIELD.buildMonomial(0, -1).toString());
GenericGFPoly p = new GenericGFPoly(FIELD, new int[] {3, 0, -2, 1, 1});
assertEquals("a^25x^4 - ax^2 + x + 1", p.toString());
p = new GenericGFPoly(FIELD, new int[] {3});
assertEquals("a^25", p.toString());
}
@Test