-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolynomial.java
54 lines (43 loc) · 1.27 KB
/
Polynomial.java
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
import java.util.Arrays;
public class Polynomial {
double[] coefficients;
public Polynomial() {
coefficients = new double[] {0};
}
public Polynomial(double[] inputPolynomial) {
coefficients = inputPolynomial.clone();
}
public Polynomial add(Polynomial another_polynomial) {
int length1 = coefficients.length;
int length2 = another_polynomial.coefficients.length;
//take the minimum of the 2 lengths cuz we only need the shorter one to run
//the for loop
int shorter_length = Math.min(length1, length2);
Polynomial sum_polynomial = new Polynomial();
if (length1 == shorter_length)
{
sum_polynomial = new Polynomial(another_polynomial.coefficients);
}
else
{
sum_polynomial = new Polynomial(coefficients);
}
for (int i = 0; i < shorter_length; i++){
sum_polynomial.coefficients[i] = coefficients[i] + another_polynomial.coefficients[i];
}
return sum_polynomial;
}
public double evaluate(double x_value) {
double solution = 0;
for (int i = 0; i < coefficients.length; i++) {
solution += coefficients[i]*(Math.pow(x_value, i));
}
return solution;
}
public boolean hasRoot(double potential_root) {
if (evaluate(potential_root) == 0) {
return true;
}
return false;
}
}