forked from cstrahan/aduni
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoly.java
More file actions
152 lines (136 loc) · 3.2 KB
/
Poly.java
File metadata and controls
152 lines (136 loc) · 3.2 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
/**
* Implements <b>immutable</b> polynomials with integer coefficients
*/
public class Poly extends RFunc implements Function{
private int coefs[] = {0};
/**
* Constructor from coefficient array
*/
public Poly(int[] coefs){
if(coefs != null){
this.coefs = new int[coefs.length];
for(int i=0;i<coefs.length;i++){
this.coefs[i] = coefs[i];
}
}
}
/**
* Return a new polynomial with coefficients scaled by s.
*/
public Poly scale(int s){
Poly p = new Poly(coefs);
for(int i=0;i<coefs.length;i++){
p.coefs[i] = s * coefs[i];
}
return(p);
}
private double evaluate(double[] coefs, double x){
double val = 0;
for(int i=coefs.length-1;i>=0;i--){
val = (val * x) + coefs[i];
}
return(val);
}
/**
* Return the definate integral using anti-derivative.
*/
public double defIntegral(double a, double b, int N){
double[] newcoefs = new double[coefs.length + 1];
newcoefs[0] = 0;
for(int i=0;i<coefs.length;i++){
newcoefs[i+1] = coefs[i]/((double)(i+1));
}
return(evaluate(newcoefs,b) - evaluate(newcoefs,a));
}
/**
* Return the degree of the polynomial.
*/
public int degree(){
int i;
int degree = 0;;
for(i=0;i<coefs.length;i++){
if(coefs[i] != 0) degree = i;
}
return(degree);
}
/**
* Return a string representation of the polynomial.
*/
public String toString(){
int i;
String buf = "";
for(i=coefs.length-1;i>=0;i--){
if(coefs[i] == 0){
if(degree() > 0 || i > 0) continue;
}
if(i > 0){
buf += coefs[i] + " x^" + i;
buf += " + ";
}
else
buf += coefs[i];
}
return(buf);
}
/**
* Evaluate the polynomial at x.
*/
public double evaluate(double x){
int i;
double val = 0;
for(i=coefs.length-1;i>=0;i--){
val = (val * x) + coefs[i];
}
return(val);
}
/**
* Returns the sum of two polynomials.
*/
public Poly add(Poly a){
int len = coefs.length;
if(a.coefs.length > len)
len = a.coefs.length;
int[] newcoefs = new int[len];
for(int i=0;i<len;i++){
newcoefs[i] = 0;
if(i<coefs.length)
newcoefs[i] += coefs[i];
if(i<a.coefs.length)
newcoefs[i] += a.coefs[i];
}
return(new Poly(newcoefs));
}
/**
* Static routine to sum two polynomials.
*/
public static Poly add(Poly a, Poly b){
int len = b.coefs.length;
if(a.coefs.length > len)
len = a.coefs.length;
int[] newcoefs = new int[len];
for(int i=0;i<len;i++){
newcoefs[i] = 0;
if(i<b.coefs.length)
newcoefs[i] += b.coefs[i];
if(i<a.coefs.length)
newcoefs[i] += a.coefs[i];
}
return(new Poly(newcoefs));
}
/**
* Returns the product of two polynomials.
*/
public Poly mul(Poly a){
int i;
int len = coefs.length;
int lena = a.coefs.length;
int[] newcoefs = new int[len + lena - 1];
for(i=0;i<len+lena-1;i++) newcoefs[i] = 0;
for(i=0;i<len;i++){
for(int j=0;j<lena;j++){
newcoefs[i+j] += coefs[i] * a.coefs[j];
}
}
return(new Poly(newcoefs));
}
}