11package io .polypen ;
22
3- import org .apache .commons .numbers .fraction .Fraction ;
4-
53import java .util .ArrayList ;
64import java .util .List ;
75
8- import static io .polypen .Util .isAbsoluteOne ;
9-
106public 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 }
0 commit comments