Repository navigation
Expand file tree
/
Copy pathPolynomial_problem.java
More file actions
166 lines (147 loc) · 4.41 KB
/
Copy pathPolynomial_problem.java
File metadata and controls
166 lines (147 loc) · 4.41 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
/*public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int degree1[] = new int[n];
for(int i = 0; i < n; i++){
degree1[i] = s.nextInt();
}
int coeff1[] = new int[n];
for(int i = 0; i < n; i++){
coeff1[i] = s.nextInt();
}
Polynomial first = new Polynomial();
for(int i = 0; i < n; i++){
first.setCoefficient(degree1[i],coeff1[i]);
}
n = s.nextInt();
int degree2[] = new int[n];
for(int i = 0; i < n; i++){
degree2[i] = s.nextInt();
}
int coeff2[] = new int[n];
for(int i = 0; i < n; i++){
coeff2[i] = s.nextInt();
}
Polynomial second = new Polynomial();
for(int i = 0; i < n; i++){
second.setCoefficient(degree2[i],coeff2[i]);
}
int choice = s.nextInt();
Polynomial result;
switch(choice){
// Add
case 1:
result = first.add(second);
result.print();
break;
// Subtract
case 2 :
result = first.subtract(second);
result.print();
break;
// Multiply
case 3 :
result = first.multiply(second);
result.print();
break;
}
}
*/
public class Polynomial {
int array[];
Polynomial(){
array=new int[10];
}
/* This function sets coefficient for a particular degree value, if degree is not there in the polynomial
* then corresponding term(with specified degree and value is added int the polynomial. If the degree
* is already present in the polynomial then previous coefficient is replaced by
* new coefficient value passed as function argument
*/
public void setCoefficient(int degree, int coeff){
if (degree>this.array.length - 1){
// doubleCapacity();
int tempArray[] = this.array;
this.array = new int[degree + 1];
for(int i = 0; i < tempArray.length; i++) {
this.array[i] = tempArray[i];
}
}
this.array[degree]=coeff;
}
// private void doubleCapacity() {
// int temp[]=array;
// array=new int[2*temp.length];
// for(int i=0;i<temp.length;i++) {
// array[i]=temp[i];
// }
// Prints all the terms(only terms with non zero coefficients are to be printed) in increasing order of degree.
public void print(){
for(int i=0;i<this.array.length;i++){
if(this.array[i]!=0){
System.out.print(this.array[i]+"x"+ i + " ");
}
}
}
// Adds two polynomials and returns a new polynomial which has result
public Polynomial add(Polynomial p){
Polynomial result = new Polynomial();
int size=Math.min(this.array.length, p.array.length);
int i;
for(i=0; i<size; i++){
// array[i]=array[i]+p[i];
result.setCoefficient(i, this.array[i] + p.array[i]);
}
while(i < this.array.length) {
result.setCoefficient(i, this.array[i]);
i++;
}
while(i < p.array.length) {
result.setCoefficient(i, p.array[i]);
i++;
}
return result;
}
// Subtracts two polynomials and returns a new polynomial which has result
public Polynomial subtract(Polynomial p){
Polynomial result = new Polynomial();
int size=Math.min(this.array.length, p.array.length);
int i;
for(i=0; i<size; i++){
// array[i]=array[i]-p[i];
result.setCoefficient(i, this.array[i] - p.array[i]);
}
while(i < this.array.length) {
result.setCoefficient(i, this.array[i]);
i++;
}
while(i < p.array.length) {
result.setCoefficient(i, -p.array[i]);
i++;
}
return result;
}
public int getCoeff(int degree){
if(degree < this.array.length){
return array[degree];
}
return 0;
}
// Multiply two polynomials and returns a new polynomial which has result
public Polynomial multiply(Polynomial p){
// if(degree>array.length){
// doubleCapacity();
// }
// int mul[]=new int[array.length];
// mul[]=array;
Polynomial mul = new Polynomial();
for(int i=0;i< this.array.length;i++){
for(int j=0;j<p.array.length;j++){
int deg = i + j;
int coeff = this.array[i] * p.array[j];
int oldCoeff = mul.getCoeff(deg);
mul.setCoefficient(deg, coeff + oldCoeff);
}
}
return mul;
}
}