-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMyPolynomial.java
More file actions
133 lines (120 loc) · 3.34 KB
/
MyPolynomial.java
File metadata and controls
133 lines (120 loc) · 3.34 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
package src;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class MyPolynomial {
private double[] coeffs;
public MyPolynomial(double... coeffs) {
this.coeffs = coeffs;
}
/**
* Constructor that takes coefficients from a file (of the given filename),
* having this format:
*
* Degree-n(int)
* c0(double)
* c1(double)
* ......
* ......
* cn-1(double)
* cn(double)
* (end-of-file)
*
* @param filename [description]
*/
public MyPolynomial(String filename)
{
Scanner in = null;
try {
in = new Scanner(new File(filename)); // open file
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int degree = Integer.parseInt(in.nextLine()); // read the degree
coeffs = new double[degree+1]; // allocate the array
for (int i = 0; i < coeffs.length; ++i) {
coeffs[i] = Double.parseDouble(in.nextLine());
}
}
public double[] getCoeffs() {
return coeffs;
}
public int getDegree() {
return coeffs.length-1;
}
/**
* [evaluate description]
* @param x [description]
* @return [description]
*/
public double evaluate(double x)
{
double sum = 0.0;
double item = 1.0;
for (int i = 0; i < coeffs.length; i++) {
item *= (i == 0 ? 1.0 : x);
sum += item * coeffs[i] ;
}
return sum;
}
/**
* [add description]
* @param another [description]
* @return [description]
*/
public MyPolynomial add(MyPolynomial another)
{
MyPolynomial result = this;
if (result.getDegree() < another.getDegree()) {
result = another;
another = this;
}
double[] resultCoeffs = result.getCoeffs();
double[] anotherCoeffs = another.getCoeffs();
for (int i = 0; i < resultCoeffs.length; i++)
{
if (i > anotherCoeffs.length-1) {
continue;
}
resultCoeffs[i] += anotherCoeffs[i];
}
return new MyPolynomial(resultCoeffs);
}
/**
* [multiply description]
* @param another [description]
* @return [description]
*/
public MyPolynomial multiply(MyPolynomial another)
{
double[] resultCoeffs = new double[this.getDegree() + another.getDegree()+1];
double[] anotherCoeffs = another.getCoeffs();
for (int i =0; i < resultCoeffs.length; i++)
{
double tmp = 0.0;
for (int j = 0; j <= i; j++)
{
if (j > coeffs.length-1) {
continue;
}
if (i-j > anotherCoeffs.length-1) {
continue;
}
tmp += coeffs[j] * anotherCoeffs[i-j];
}
resultCoeffs[i] = tmp;
}
return new MyPolynomial(resultCoeffs);
}
/**
* [toString description]
* @return [description]
*/
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = coeffs.length-1; i >= 0; i--) {
sb.append(coeffs[i] + (i != 0 ? "*x^"+i+" + ":""));
}
return sb.toString();
}
}