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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
| import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/***************************************
* Created By HuJiekang on 2019/12/12. *
***************************************/
public class Polynomial {
/**
* 此方法用于快速创建一个ArrayList对象
* @param elements 为整个整数的每一位数字
* @return ArrayList<Double>
*/
public static ArrayList<Double> CreateArrayList(double ... elements) {
ArrayList<Double> list = new ArrayList<>();
for (Double element : elements) {
list.add(element);
}
return list;
}
/**
* 此方法用于比较两个数的大小。若第一个数大于等于第二个数则返回true,否则返回false
* @params num1, num2 为要比较的数
* @return 返回比较结果 boolean
*/
public static boolean isBigger(ArrayList<Double> num1, ArrayList<Double> num2) {
if(num1.size() < num2.size()) return false;
else if(num1.size() > num2.size()) return true;
else {
for (int i = num1.size()-1; i >=0; i--){
if(num1.get(i)<num2.get(i)) return false;
else if(num1.get(i)>num2.get(i)) return true;
}
}
return true;
}
/**
* 此方法用于将一个多项式格式化输出为∑aix^i的形式
* @param d 要输出的多项式
* @return void
*/
public static void DisplayPolynomial(ArrayList<Double> d){
for(int i = 0;i<d.size();i++){
if(d.get(i)==0.0) continue;
if(i==0) System.out.print(d.get(i));
else if(i==1) System.out.print(d.get(i)+" * x");
else{
System.out.print(d.get(i)+" * x^"+i);
}
if(i==d.size()-1) break;
else System.out.print(" + ");
}
}
/**
* 此方法实现了多项式的加法计算
* @params pol1,pol2 要进行运算的两个多项式
* @return ArrayList<Double>
*/
public static ArrayList<Double> PolynomialPlus(ArrayList<Double> pol1, ArrayList<Double> pol2){
ArrayList<Double> result = new ArrayList<>();
int i;
for(i = 0;i<Math.min(pol1.size(), pol2.size());i++){
result.add(i, pol1.get(i)+pol2.get(i));
}
for(;i<Math.max(pol1.size(), pol2.size());i++){
result.add(i, pol1.size()>=pol2.size()?pol1.get(i):pol2.get(i));
}
while(result.get(result.size()-1)==0) {
result.remove(result.size() - 1);
}
return result;
}
/**
* 此方法实现了多项式的减法计算
* @params pol1,pol2 要进行运算的两个多项式
* @return ArrayList<Double>
*/
public static ArrayList<Double> PolynomialSub(ArrayList<Double> pol1, ArrayList<Double> pol2){
ArrayList<Double> result = new ArrayList<>();
int i;
for(i = 0;i<Math.min(pol1.size(), pol2.size());i++){
result.add(i, pol1.get(i)-pol2.get(i));
}
for(;i<Math.max(pol1.size(), pol2.size());i++){
result.add(i, pol1.size()>=pol2.size()?pol1.get(i):pol2.get(i)*-1);
}
while(result.get(result.size()-1)==0) {
result.remove(result.size() - 1);
}
return result;
}
/**
* 此方法实现了多项式的乘法计算
* @params pol1,pol2 要进行运算的两个多项式
* @return ArrayList<Double>
*/
public static ArrayList<Double> PolynomialMult(ArrayList<Double> pol1, ArrayList<Double> pol2){
ArrayList<Double> result = new ArrayList<>();
for(int i = 0;i<=pol1.size()-1+pol2.size()-1;i++){
result.add(0.0);
}
for(int i = 0;i<pol1.size();i++){
for(int j = 0;j<pol2.size();j++){
result.set(i+j, result.get(i+j)+pol1.get(i)*pol2.get(j));
}
}
return result;
}
/**
* 此方法实现了多项式的带余除法计算
* @params pol1,pol2 要进行运算的两个多项式
* @return 包含结果的List
*/
public static List<ArrayList<Double>> PolynomialDiv(ArrayList<Double> pol1, ArrayList<Double> pol2){
if(pol2==null||(pol2.size()==1&&pol2.get(0)==0.0)) throw new ArithmeticException("/ by zero");
if(pol1 == pol2) return Arrays.asList(CreateArrayList(1.0),CreateArrayList(0.0));
if(pol1==null||(pol1.size()==1&&pol1.get(0)==0.0)) return Arrays.asList(CreateArrayList(0.0), CreateArrayList(0.0));
if(!isBigger(pol1, pol2)) return Arrays.asList(CreateArrayList(0.0), pol1);
ArrayList<Double> result = new ArrayList<>();
for (int i=0;i<pol1.size();i++){
result.add(i,0.0);
}
ArrayList<Double> pol2_duiqi = (ArrayList<Double>) pol2.clone();
int mult_times = 0;
double times;
while(pol2_duiqi.size()!=pol1.size()){
pol2_duiqi.add(0, 0.0 );
mult_times++;
}
while(true) {
if(pol1.size()==0) break;
if(pol1.size()==pol2_duiqi.size()){
times = pol1.get(pol1.size()-1)/pol2_duiqi.get(pol1.size()-1);
for(int i = 0;i<pol1.size();i++){
pol1.set(i, pol1.get(i)-pol2_duiqi.get(i)*times);
}
pol1.set(pol1.size()-1, 0.0);
}else{
times = 0.0;
}
if(pol1.size()!=0){
while(pol1.get(pol1.size()-1)==0){
pol1.remove(pol1.size()-1);
if(pol1.isEmpty()){
break;
}
}
}
result.set(mult_times, times);
if (pol2_duiqi.get(0) == 0 && pol2_duiqi.size() > pol2.size()){
pol2_duiqi.remove(0);
mult_times--;
}
else{
break;
}
}
while(result.get(result.size()-1)==0){
result.remove(result.size()-1);
}
if(pol1.size()==0){
return Arrays.asList(result,CreateArrayList(0));
}
return Arrays.asList(result,pol1);
}
/**
* 此方法实现了扩展的欧几里得算法,用于计算多项式的逆元
* @params pol1,pol2 要进行运算的两个多项式
* @return 包含结果的List
*/
public static List<ArrayList<Double>> Euclid(ArrayList<Double> pol1, ArrayList<Double> pol2){
if(pol2==null||(pol2.size()==1&&pol2.get(0)==0.0)) return Arrays.asList(pol1, CreateArrayList(1.0), CreateArrayList(0.0));
ArrayList<Double> u1x = CreateArrayList(0.0);
ArrayList<Double> u2x = CreateArrayList(1.0);
ArrayList<Double> v1x = CreateArrayList(1.0);
ArrayList<Double> v2x = CreateArrayList(0.0);
ArrayList<Double> qx;
ArrayList<Double> rx;
ArrayList<Double> ux;
ArrayList<Double> vx;
List tmp;
while(!(pol2.size() == 1 && pol2.get(0) == 0.0)){
tmp = PolynomialDiv(pol1, pol2);
qx = (ArrayList<Double>) tmp.get(0);
rx = (ArrayList<Double>) tmp.get(1);
ux = PolynomialSub(u2x, PolynomialMult(qx, u1x));
vx = PolynomialSub(v2x, PolynomialMult(qx, v1x));
pol1 = pol2;
pol2 = rx;
u2x = u1x;
u1x = ux;
v2x = v1x;
v1x = vx;
}
return Arrays.asList(pol1, u2x, v2x);
}
}
|