-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.java
More file actions
205 lines (197 loc) · 8.63 KB
/
Copy pathPolynomial.java
File metadata and controls
205 lines (197 loc) · 8.63 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.util.*;
//Author: Mpho Moses
//A class with a number of methods for performing operations on a polynomial
public class Polynomial{
private int exp;
private double coefficient;
private ArrayList<Double> coefficients_list;
public Polynomial()
{
coefficients_list = new ArrayList<>();
exp = 0;
coefficient = 0;
}
//set the coefficient value andb add it to the list of coefficients
public void setCoefficient(int exp, double coefficient)
{
this.exp = exp;
this.coefficient = coefficient;
coefficients_list.add(coefficient);
}
public double getCoefficient(int exp)
{
for(int i = 0; i <= coefficients_list.size()-1; i++){
if(i==exp)
return coefficients_list.get(i);
}
return 0; //return 0 if the coefficient isn't in the list of coefficients
}
public void evaluate(double x_value)
{
double result = 0;
for(int i = 1; i<= coefficients_list.size()-1;i++)
{
result+=getCoefficient(i)*Math.pow(x_value,i); //multiply each coefficient by x_value raised to exponent i and add to result
}
result+=getCoefficient(0); //add the constant term at position 0 to the result
System.out.println("The value of the function "+ this.toString()+ " at " +x_value+" is "+result);
}
public void createPolynomial()
{
System.out.println("==============Please create a polynomial=================");
System.out.println("\nThe polynomial will have the structure C0 + C1*x + C2*x^2 + C3*x^3 + ... + Cn*x^n\n");
Scanner scanner = new Scanner(System.in);
while(true){
System.out.print("Enter coefficient "+exp+" (press enter if done): ");
String input = scanner.nextLine();
if(input.equals((""))) { //Exit program if user pressed enter with no input
return;
}
coefficient = Double.parseDouble(input);
setCoefficient(exp, coefficient);
exp++;
}
}
//method that multiplies a polynomial by a scalar
public void scale(double scalar)
{
//create a temporary arraylist of coefficients and multiply each coefficient by a scalar
//then point the old list to the temp list
ArrayList<Double> temp = new ArrayList<>();
for (int j = 0; j <= coefficients_list.size() - 1; j++) {
temp.add(scalar * coefficients_list.get(j));
}
coefficients_list = temp;
}
public Polynomial addPolynomial(Polynomial otherPolynomial)
{
Polynomial newPolynomial = new Polynomial(); //Take in new polynomial as a parameter and instantiate it
double constantTerm = otherPolynomial.getCoefficient(0)+this.getCoefficient(0);
newPolynomial.coefficients_list.add(constantTerm); //add the constant term of the current polynomial to the new polynomial
//assuming there is at least one term in the polynomial
int curPolynomialSize = this.coefficients_list.size(); //where curPolynomialSize refers to the size of the calling object
int otherPolynomialSize = otherPolynomial.coefficients_list.size();
//Cater for cases where the number of terms in each of the polynomials may be different
if(curPolynomialSize < otherPolynomialSize)
{
for (int i = 1; i <= curPolynomialSize; i++) //add like terms, end at the end of the smaller coefficient list
{
double sum = this.getCoefficient(i)+otherPolynomial.getCoefficient(i);
if(sum!=0)
newPolynomial.coefficients_list.add(sum);
}
for(int j=curPolynomialSize;j < otherPolynomialSize;j++) //take the remaining coefficients in the larger list and add to the new polynomial
{
newPolynomial.coefficients_list.add(otherPolynomial.getCoefficient(j));
}
}
else if(otherPolynomialSize < curPolynomialSize)
{
for(int x = 1; x <= otherPolynomialSize;x++){ //add like terms, end at the end of the smaller coefficient list
double sum = this.getCoefficient(x)+otherPolynomial.getCoefficient(x);
if(sum!=0)
newPolynomial.coefficients_list.add(this.getCoefficient(x)+otherPolynomial.getCoefficient(x));
}
for(int y = otherPolynomialSize + 1; y < curPolynomialSize;y++) //take the remaining coefficients in the larger list and add to the new polynomial
{
newPolynomial.coefficients_list.add(this.getCoefficient(y));
}
}
else if(curPolynomialSize==otherPolynomialSize)
{
for(int o = 1; o < curPolynomialSize; o++){
double sum = this.getCoefficient(o)+ otherPolynomial.getCoefficient(o);
if(sum!=0)
newPolynomial.coefficients_list.add(sum);
}
}
return newPolynomial;
}
public Polynomial derivative()
{
Polynomial derivative = new Polynomial();
//Multiply each coefficient by the exponent i, excluding the coefficient at position 0 since it will always be a constant
if(coefficients_list.size()==1){
derivative.coefficients_list.add(0.0); //return a derivative of 0 if the coefficient is constant
}
for(int i = 1; i <= coefficients_list.size()-1; i++)
{
derivative.coefficients_list.add(getCoefficient(i) * (i));
}
if(derivative == null) System.out.println("The derivative does not exist!");
return derivative;
}
public String toString()
{
String result = "";
if(coefficients_list.size()==1) //if the polynomial has one term, return the term
return result+coefficients_list.ge);
if(coefficients_list.get(0)==0)
result+="";
else
result+=coefficients_list.get(0);
for(int i = 1; i < coefficients_list.size()-1; i++){
double term=getCoefficient(i);
if(i==1){ //consider the constant term and the term with power 1 separately, to avoid printing their exponents
//term = coefficients_list.get(i);
if(term < 0){
term = Math.abs(term);
if(coefficients_list.get(0)==0)
result+=term+"x";
else
result+=" - "+term+"x";
}else if(term==0){
result+="";
}
else{
if(coefficients_list.get(0)==0) //avoid printing positive sign
result+=term+"x";
else
result+=" + "+term+"x";
}
}
else if(i==2){
//term = coefficients_list.get(i);
if(term < 0){
term = Math.abs(term);
result+=" - "+term+"x^"+i;
}else if(term==0){
result+="";
}
else{
if(getCoefficient(i)==0)
result+=term+"x^"+i;
else
result += " + "+term+"x^"+i;
}
}
else{
//term = coefficients_list.get(i);
if(term < 0){
term = Math.abs(term);
result+=" - "+term+"x^"+ i;
}else if(term==0){
result+="";
}
else{
result +=" + "+term+"x^" + i;
}
}
}
if(getCoefficient(coefficients_list.size()-1)<0) {
double term = getCoefficient(coefficients_list.size()-1);//coefficients_list.get(coefficients_list.size()-1);
term = Math.abs(term);
result += " - " + term + "x^" + (coefficients_list.size() - 1);
}
else {
double sum = 0;
for(int i = 0; i<=coefficients_list.size()-2;i++)
sum+=coefficients_list.get(i);
if(sum==0 && sum+getCoefficient(coefficients_list.size()-1)!=0)
result+=getCoefficient(coefficients_list.size() - 1) + "x^" + (coefficients_list.size() - 1);
else
result+=" + "+getCoefficient(coefficients_list.size() - 1) + "x^" + (coefficients_list.size() - 1);
}
return result;
}
}