-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynom.java
186 lines (135 loc) · 6.25 KB
/
Polynom.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import javax.swing.JOptionPane;
public class Polynom {
//we will have a dynamic array of polynom instance and consider them to a sum.
private ArrayList<PolynomInstance> PolynomPresentation;
//Costume constructor setting a polynom by a given degrees and coefficients arrays.
public Polynom(int[] degrees, double[] coefficients) {
try{
this.PolynomPresentation = new ArrayList<PolynomInstance>(0);
for(int i = 0 ; i<degrees.length || i<coefficients.length; i++)
this.PolynomPresentation.add(new PolynomInstance(degrees[i],coefficients[i]));
}catch(ArrayIndexOutOfBoundsException e){
// if(degrees.length != coefficients.length)
JOptionPane.showMessageDialog(null, "\nError: \nNumber of coefficients and degrees do not match.\n");
throw e; //pop the exception to the main for indication.
}
}
// copy constructor
public Polynom(Polynom c) {
this.PolynomPresentation = new ArrayList<PolynomInstance>(0);
for(int i=0; i < c.getPolynomParameters().size(); i++)
this.PolynomPresentation.add(new PolynomInstance(c.getPolynomParameters().get(i)));
}
//unite similar degrees
//this is a private method supporting public sort method, the dependency on a previuos sorting (for efficiency) must be protected.
private void uniteSimilarDegree() {
int i=0;
//to be efficient and unite in O(n) we will check only the next instance in a polynom(and delete it if we sum it) based on a previuos sort function that gerenteed descending order of degrees.
while( i<this.PolynomPresentation.size() )
{
while( i+1<this.PolynomPresentation.size() && this.PolynomPresentation.get(i+1).getDegree()==this.PolynomPresentation.get(i).getDegree() )
{
this.PolynomPresentation.get(i).setCoefficient(this.PolynomPresentation.get(i).getCoefficient() + this.PolynomPresentation.get(i+1).getCoefficient() );
this.PolynomPresentation.remove(i+1);
}//end of internal loop
if(this.PolynomPresentation.get(i).getCoefficient()==0)
this.PolynomPresentation.remove(i);
else i++; //if we removed index i, all the sub array shifted left anyway.
}//end of loop
}//end of function.
// Sorting the polynom by degree order.
//O(nlogn) complexity.
public void sort() {
Comparator<PolynomInstance> compareByDegree = new Comparator<PolynomInstance>() {
@Override
public int compare(PolynomInstance o1, PolynomInstance o2) {
return Integer.compare(o1.getDegree(),o2.getDegree());
}
};
Collections.sort(this.PolynomPresentation, Collections.reverseOrder(compareByDegree)); //by descending order
this.uniteSimilarDegree(); //O(n).
}
//added clones of the polynoms and return a polynom(so the source will not be harmed)
public Polynom plus(Polynom inputParameter) {
Polynom tempPolynomSource = new Polynom(this);
Polynom tempInputParameterr = new Polynom(inputParameter);
for(int i=0; i<tempInputParameterr.getPolynomParameters().size() ; i++)
tempPolynomSource.getPolynomParameters().add(tempInputParameterr.getPolynomParameters().get(i));
tempPolynomSource.sort();
return tempPolynomSource;
}
//substructing 2 polynoms, again not operating on the source itself
public Polynom minus(Polynom sourceParameter) {
Polynom temporaryPolynom = new Polynom(sourceParameter);
//we basicly reverse the sign of each coefficient and repeat the function plus.
for(int i=0; i<temporaryPolynom.getPolynomParameters().size() ; i++)
temporaryPolynom.getPolynomParameters().get(i).setCoefficient(temporaryPolynom.getPolynomParameters().get(i).getCoefficient() * (-1));
return this.plus(temporaryPolynom);
}
//derivative by applying on each instance the derivative formal method
public Polynom derivative() {
Polynom temporaryPolynom = new Polynom(this);
temporaryPolynom.sort();
for(int i=0; i<temporaryPolynom.getPolynomParameters().size() ; i++) {
temporaryPolynom.getPolynomParameters().get(i).setCoefficient(temporaryPolynom.getPolynomParameters().get(i).getCoefficient() * temporaryPolynom.getPolynomParameters().get(i).getDegree());
temporaryPolynom.getPolynomParameters().get(i).setDegree(temporaryPolynom.getPolynomParameters().get(i).getDegree()-1);
}
temporaryPolynom.sort();
return temporaryPolynom;
}
//comparing each polynom instance and return true or false
public boolean equals(Polynom sourceParameter) {
Polynom tempPolynomSource = new Polynom(this);
Polynom tempInputParameterr = new Polynom(sourceParameter);
tempPolynomSource.sort();
tempInputParameterr.sort();
for(int i=0; i<tempPolynomSource.getPolynomParameters().size() && i<tempInputParameterr.getPolynomParameters().size(); i++) {
if(!tempPolynomSource.getPolynomParameters().get(i).isEqualTo(tempInputParameterr.getPolynomParameters().get(i)))
return false;
}
return true;
}
//return 100 points represent the polynom from x=-50 to x=50
public ArrayList<Point> polynomToPoints() {
ArrayList<Point> points = new ArrayList<Point>(100);
double coefficient, degree, sum;
//coff*x^degree
System.out.println("Generating points of a polynom:");
for(int i = 0 ; i<100 ; i++) {
sum=0;
points.add(new Point());
points.get(i).setX(i - 50); //x=-50, -49, ....0,1...49,50
for(int j=0; j< this.PolynomPresentation.size(); j++)
{
coefficient = this.PolynomPresentation.get(j).getCoefficient();
degree = this.PolynomPresentation.get(j).getDegree();
sum += coefficient * (Math.pow(points.get(i).getX(), degree) );
}
points.get(i).setY(sum);
System.out.println("Point " + (i+1) + " ->\tX: " +points.get(i).getX()+ "\tY: " +points.get(i).getY());
}
return points;
}
//getter
public ArrayList<PolynomInstance> getPolynomParameters() {
return this.PolynomPresentation;
}
//string the polynom expression
public String toString()
{
String PolynomExpression = new String();
int polynomIndex = 0;
while(polynomIndex < PolynomPresentation.size())
{
PolynomExpression += PolynomPresentation.get(polynomIndex);
polynomIndex++;
if(polynomIndex < PolynomPresentation.size())
if(PolynomPresentation.get(polynomIndex).getCoefficient() >= 0)
PolynomExpression += "+";
}
return PolynomExpression;
}
}