blob: 2a1b28e0e8a96f1dcaf2c6fe8283f109d0c8620c (
plain) (
blame)
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
|
package polynomial;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
/**
* A skeleton class for representing Polynomials
*
* @author Sjaak Smetsers
* @date 10-03-2015
*/
public class Polynomial {
/**
* A polynomial is a sequence of terms here kept in an List
*/
List<Term> terms;
/**
* A constructor for creating the zero Polynomial zero is presented as an
* empty list of terms and not as a single term with 0 as a coefficient
*/
public Polynomial() {
terms = new ArrayList<>();
}
/**
* A Constructor creating a polynomial from the argument string.
*
* @param s a String representing a list of terms which is converted to a
* scanner and passed to scanTerm for reading each individual term
*/
public Polynomial( String s ) {
terms = new ArrayList<>();
Scanner scan = new Scanner(s);
for (Term t = Term.scanTerm(scan); t != null; t = Term.scanTerm(scan)) {
terms.add(t);
}
}
/**
* The copy constructor for making a deep copy
*
* @param p the copied polynomial
*
*/
public Polynomial( Polynomial p ) {
terms = new ArrayList<>(p.terms.size());
for (Term t : p.terms) {
terms.add(new Term(t));
}
}
/**
* A straightforward conversion of a Polynomial into a string based on the
* toString for terms
*
* @return a readable string representation of this
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
boolean not_first = false;
for (Term t : terms) {
if (not_first && t.getCoef() >= 0)
sb.append("+");
sb.append(t);
not_first = true;
}
return sb.toString();
}
public void plus(Polynomial b) {
for (Term t : b.terms)
plus(t);
}
public void plus(Term t) {
Iterator<Term> iter = terms.iterator();
while (iter.hasNext()) {
Term this_t = iter.next();
if (this_t.getExp() == t.getExp()) {
this_t.plus(t);
if (this_t.getCoef() == 0)
iter.remove();
return;
}
}
terms.add(t);
}
public void minus(Polynomial b) {
b.times(new Polynomial("-1 0"));
plus(b);
}
public void times(Polynomial b) {
Polynomial result = new Polynomial();
for (Term that_t : b.terms)
for (Term this_t : terms)
result.plus(new Polynomial((that_t.getCoef() * this_t.getCoef()) + " " + (that_t.getExp() + this_t.getExp())));
terms = result.terms;
}
public void divide(Polynomial b) {
}
@Override
public boolean equals(Object other_poly) {
Polynomial that = (Polynomial) other_poly;
if (terms.size() != that.terms.size())
return false;
for (Term this_t : terms) {
Iterator<Term> that_iter = that.terms.iterator();
boolean found = false;
while (that_iter.hasNext()) {
if (this_t.equals(that_iter.next())) {
found = true;
that_iter.remove();
break;
}
}
if (!found) {
return false;
}
}
return true;
}
public double apply(double x) {
double result = 0;
for (Term t : terms)
result += t.apply(x);
return result;
}
}
|