-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.java
More file actions
executable file
·111 lines (95 loc) · 3.29 KB
/
Polynomial.java
File metadata and controls
executable file
·111 lines (95 loc) · 3.29 KB
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
import java.util.*;
public class Polynomial implements Comparable <Polynomial> {
private int[] coefficients;
private int degree ;
public Polynomial(int[] coefficients){
for(degree = coefficients.length-1; degree > 0 && coefficients[degree] == 0 ; degree--);
this.coefficients = new int[degree+1];
for(int i = 0 ; i <= degree ; i++){
this.coefficients[i] = coefficients[i];
}
}
public int getDegree(){
return this.coefficients.length-1;
}
public int getCoefficient(int k){
if(k >= 0 && k < this.coefficients.length){
return this.coefficients[k];
}
return 0;
}
public long evaluate(int x){
long sum = 0;
for (int i = 0; i < this.coefficients.length; i++) {
sum += this.coefficients[i] * Math.pow(x,i);
}
return sum;
}
/*
@Override public String toString(){
int[] p = {42,-7,0,5};
Polynomial polynomial = new Polynomial(p);
System.out.println("Degree: " + polynomial.getDegree());
System.out.println("Coefficient at order 0 : " + polynomial.getCoefficient(0));
System.out.println("Sum when x = 2: " + polynomial.evaluate(2));
return "";
}
*/
static int max(int m, int n) {
return (m > n) ? m : n;
}
public Polynomial add(Polynomial other){
int size = max(this.coefficients.length, other.coefficients.length);
int sum[] = new int[size];
for (int i = 0; i < this.coefficients.length; i++) {
sum[i] = this.coefficients[i];
}
for (int i = 0; i < other.coefficients.length; i++) {
sum[i] += other.coefficients[i];
}
return new Polynomial(sum);
}
public Polynomial multiply(Polynomial other){
int[] prod = new int[this.coefficients.length + other.coefficients.length - 1];
for (int i = 0; i < this.coefficients.length + other.coefficients.length - 1; i++){
prod[i] = 0;
}
for (int i = 0; i < this.coefficients.length; i++) {
for (int j = 0; j < other.coefficients.length; j++) {
prod[i + j] += this.coefficients[i] * other.coefficients[j];
}
}
return new Polynomial(prod);
}
@Override public boolean equals(Object other){
if (!(other instanceof Polynomial)) {
return false;
}
if (this.compareTo((Polynomial) other) == 0 ){
return true;
}
return false;
}
@Override public int hashCode(){
return Arrays.hashCode(this.coefficients);
}
public int compareTo(Polynomial other){
if (this.getDegree() > other.getDegree()){
return 1;
}
else if (this.getDegree() < other.getDegree()){
return -1;
}
else if (this.getDegree() == other.getDegree()){
for (int i = this.getDegree(); i >= 0; i--){
if (coefficients[i] > other.getCoefficient(i)){
return 1;
}
if (coefficients[i] < other.getCoefficient(i)){
return -1;
}
}
}
return 0;
}
}