Skip to content

Commit 9c459f2

Browse files
committed
no fractions
1 parent 3180e54 commit 9c459f2

File tree

7 files changed

+39
-54
lines changed

7 files changed

+39
-54
lines changed

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ tasks.withType(GenerateModuleMetadata).configureEach {
3434
}
3535

3636
dependencies {
37-
implementation 'org.apache.commons:commons-numbers-fraction:1.2'
3837
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
3938
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.0'
4039
}

src/main/java/io/polypen/Monomial.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package io.polypen;
22

3-
import org.apache.commons.numbers.fraction.Fraction;
4-
53
import java.util.ArrayList;
64
import java.util.List;
75

8-
public record Monomial(Fraction coefficient, int exponent) {
6+
public record Monomial(int coefficient, int exponent) {
97

10-
public static final Monomial ZERO = new Monomial(Fraction.ZERO, 0);
8+
public static final Monomial ZERO = new Monomial(0, 0);
119

1210
public Polynomial multiply(Polynomial p) {
13-
List<Fraction> result = new ArrayList<>(p.degree() + exponent + 1);
11+
List<Integer> result = new ArrayList<>(p.degree() + exponent + 1);
1412
for (int i = 0; i < exponent; i++) {
15-
result.add(Fraction.ZERO);
13+
result.add(0);
1614
}
1715
for (int i = 0; i <= p.degree(); i++) {
18-
result.add(coefficient.multiply(p.coefficient(i)));
16+
result.add(coefficient * (p.coefficient(i)));
1917
}
2018
return new Polynomial(result);
2119
}

src/main/java/io/polypen/Parser.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import io.polypen.Expressions.Literal;
55
import io.polypen.Expressions.Product;
66
import io.polypen.Expressions.Sum;
7-
import org.apache.commons.numbers.fraction.Fraction;
87

98
import java.util.ArrayList;
109
import java.util.List;
@@ -18,35 +17,36 @@
1817

1918
final class Parser {
2019

21-
static List<Fraction> parsePolynomial(String s) {
20+
static List<Integer> parsePolynomial(String s) {
2221
List<SignedString> strings = split(s.trim());
23-
TreeMap<Integer, Fraction> cof = new TreeMap<>();
22+
TreeMap<Integer, Integer> cof = new TreeMap<>();
2423
for (SignedString term : strings) {
2524
String[] tokens = term.token.split("[a-z]", 3);
2625
String coefficient = tokens[0].replace("*", "").trim();
2726
if (coefficient.isEmpty()) {
2827
coefficient = "1";
2928
}
29+
int signedCoefficient = Integer.parseInt(coefficient) * term.sign.factor;
3030
if (tokens.length == 1) {
31-
cof.put(0, Fraction.parse(coefficient).multiply(term.sign.factor));
31+
cof.put(0, signedCoefficient);
3232
} else {
3333
String rawExponent = tokens[1].replace("^", "").trim();
3434
if (rawExponent.isEmpty()) {
35-
cof.put(1, Fraction.parse(coefficient).multiply(term.sign.factor));
35+
cof.put(1, signedCoefficient);
3636
} else {
3737
int exponent = Integer.parseInt(rawExponent);
38-
cof.put(exponent, Fraction.parse(coefficient).multiply(term.sign.factor));
38+
cof.put(exponent, signedCoefficient);
3939
}
4040
}
4141
}
4242
Integer highestExponent = cof.lastKey();
43-
List<Fraction> result = new ArrayList<>(highestExponent);
43+
List<Integer> result = new ArrayList<>(highestExponent);
4444
for (int i = 0; i <= highestExponent; i++) {
45-
result.add(Fraction.ZERO);
45+
result.add(0);
4646
}
47-
for (Map.Entry<Integer, Fraction> e : cof.entrySet()) {
47+
for (Map.Entry<Integer, Integer> e : cof.entrySet()) {
4848
Integer exponent = e.getKey();
49-
Fraction coefficient = e.getValue();
49+
Integer coefficient = e.getValue();
5050
result.set(exponent, coefficient);
5151
}
5252
return result;

src/main/java/io/polypen/Polynomial.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package io.polypen;
22

3-
import org.apache.commons.numbers.fraction.Fraction;
4-
53
import java.util.ArrayList;
64
import java.util.List;
75

8-
import static io.polypen.Util.isAbsoluteOne;
9-
106
public final class Polynomial {
117

12-
public static final Polynomial ZERO = new Polynomial(List.of(Fraction.ZERO));
13-
public static final Polynomial ONE = new Polynomial(List.of(Fraction.ONE));
8+
public static final Polynomial ZERO = new Polynomial(List.of(0));
9+
public static final Polynomial ONE = new Polynomial(List.of(1));
1410

15-
private final List<Fraction> coefficients;
11+
private final List<Integer> coefficients;
1612

17-
Polynomial(List<Fraction> coefficients) {
13+
Polynomial(List<Integer> coefficients) {
1814
this.coefficients = coefficients;
1915
}
2016

@@ -72,9 +68,9 @@ private record NestingInfo(int sign, String term) {
7268

7369
public Polynomial add(Polynomial other) {
7470
int degree = Math.max(degree(), other.degree());
75-
List<Fraction> r = new ArrayList<>(degree + 1);
71+
List<Integer> r = new ArrayList<>(degree + 1);
7672
for (int i = 0; i <= degree; i++) {
77-
r.add(coefficient(i).add(other.coefficient(i)));
73+
r.add(coefficient(i) + other.coefficient(i));
7874
}
7975
return new Polynomial(r);
8076
}
@@ -94,9 +90,9 @@ public Polynomial multiply(Polynomial other) {
9490
}
9591

9692
public Polynomial multiply(int factor) {
97-
ArrayList<Fraction> newCoefficients = new ArrayList<>();
98-
for (Fraction coefficient : coefficients) {
99-
newCoefficients.add(coefficient.multiply(factor));
93+
List<Integer> newCoefficients = new ArrayList<>(coefficients.size());
94+
for (Integer coefficient : coefficients) {
95+
newCoefficients.add(coefficient * (factor));
10096
}
10197
return new Polynomial(newCoefficients);
10298
}
@@ -110,21 +106,23 @@ public String toString() {
110106
List<String> result = new ArrayList<>(coefficients.size());
111107
boolean firstCoefficient = true;
112108
for (int i = coefficients.size() - 1; i >= 0; i--) {
113-
Fraction coefficient = coefficients.get(i);
114-
if (coefficient.isZero()) {
109+
Integer coefficient = coefficients.get(i);
110+
if (coefficient == 0) {
115111
continue;
116112
}
117-
String plus = (i == coefficients.size() - 1 && coefficient.compareTo(Fraction.ZERO) > 0) ? "" : firstCoefficient ? "" : "+ ";
113+
String plus = i == coefficients.size() - 1 && coefficient > 0 ?
114+
"" :
115+
firstCoefficient ? "" : "+ ";
118116
firstCoefficient = false;
119-
String prettySign = coefficient.compareTo(Fraction.ZERO) < 0 ? "- " : plus;
117+
String prettySign = coefficient < 0 ? "- " : plus;
120118
if (i == 0) {
121-
result.add(prettySign + coefficient.abs());
119+
result.add(prettySign + Math.abs(coefficient));
122120
} else {
123121
String factor = i == 1 ? "x" : "x^" + i;
124-
if (isAbsoluteOne(coefficient)) {
122+
if (Math.abs(coefficient) == 1) {
125123
result.add(prettySign + factor);
126124
} else {
127-
result.add(prettySign + coefficient.abs() + factor);
125+
result.add(prettySign + Math.abs(coefficient) + factor);
128126
}
129127
}
130128
}
@@ -135,21 +133,20 @@ public String toString() {
135133
public boolean equals(Object o) {
136134
if (this == o) return true;
137135
if (o == null) return false;
138-
if (!(o instanceof Polynomial)) return false;
139-
Polynomial p = (Polynomial) o;
136+
if (!(o instanceof Polynomial p)) return false;
140137
int size = Math.min(coefficients.size(), p.coefficients.size());
141138
for (int i = 0; i < size; i++) {
142139
if (!coefficients.get(i).equals(p.coefficients.get(i))) {
143140
return false;
144141
}
145142
}
146143
for (int i = size; i < coefficients.size(); i++) {
147-
if (!coefficients.get(i).equals(Fraction.ZERO)) {
144+
if (coefficients.get(i) != 0) {
148145
return false;
149146
}
150147
}
151148
for (int i = size; i < p.coefficients.size(); i++) {
152-
if (!p.coefficients.get(i).equals(Fraction.ZERO)) {
149+
if (p.coefficients.get(i) != 0) {
153150
return false;
154151
}
155152
}
@@ -161,9 +158,9 @@ public int hashCode() {
161158
return coefficients.hashCode();
162159
}
163160

164-
public Fraction coefficient(int i) {
161+
public Integer coefficient(int i) {
165162
if (i >= coefficients.size()) {
166-
return Fraction.ZERO;
163+
return 0;
167164
}
168165
return coefficients.get(i);
169166
}

src/main/java/io/polypen/Util.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
package io.polypen;
22

3-
import org.apache.commons.numbers.fraction.Fraction;
4-
53
final class Util {
6-
private static final Fraction NEGATIVE_ONE = Fraction.ONE.negate();
7-
8-
static boolean isAbsoluteOne(Fraction fraction) {
9-
return fraction.isOne() || NEGATIVE_ONE.equals(fraction);
10-
}
114

125
private Util() {
136
}

src/main/java/module-info.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
module io.polypen {
2-
requires org.apache.commons.numbers.fraction;
32
}

src/test/java/io/polypen/PolynomialTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.polypen;
22

3-
import org.apache.commons.numbers.fraction.Fraction;
43
import org.junit.jupiter.api.Test;
54

65
import static io.polypen.Polynomial.parse;
@@ -32,7 +31,7 @@ void polynomialEquals() {
3231
@Test
3332
void monomialMultiplication() {
3433
assertEquals(parse("2x^6 - 4x^2 - 2x"),
35-
new Monomial(Fraction.of(2), 1).multiply(parse("x^5 - 2x - 1")));
34+
new Monomial(2, 1).multiply(parse("x^5 - 2x - 1")));
3635
}
3736

3837
@Test

0 commit comments

Comments
 (0)