-
Notifications
You must be signed in to change notification settings - Fork 0
/
Equation.java
83 lines (66 loc) · 3.22 KB
/
Equation.java
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
import java.util.*;
public class Equation {
private int numTerms;
private ArrayList<ArrayList<Integer>> listTerms;
public Equation(ArrayList<ArrayList <Integer>> list) {
this.listTerms = list;
this.numTerms = this.listTerms.size();
}
public double equationAtPoint(double x) {
double result = 0;
for (int i = 0; i < this.numTerms; i++) {
if(this.listTerms.get(i).get(0) == 0){
Polynomial polynomial = new Polynomial(this.listTerms.get(i).get(1), this.listTerms.get(i).get(2));
result += polynomial.calculatePoly(x);
} else if(this.listTerms.get(i).get(0) == 1){
Sin sin = new Sin(this.listTerms.get(i).get(1), this.listTerms.get(i).get(2), this.listTerms.get(i).get(3));
result += sin.calculateSin(x);
} else if (this.listTerms.get(i).get(0) == 2){
Cos cos = new Cos(this.listTerms.get(i).get(1), this.listTerms.get(i).get(2), this.listTerms.get(i).get(3));
result += cos.calculateCos(x);
} else if(this.listTerms.get(i).get(0) == 3) {
Constant constant = new Constant(this.listTerms.get(i).get(1));
result += constant.getConstant();
}
}
return result;
}
public Equation equationDerivative() {
ArrayList<ArrayList<Integer>> equation = new ArrayList<ArrayList <Integer>>();
ArrayList<Integer> term = new ArrayList<Integer>();
for (int i = 0; i < this.numTerms; i++) {
term.removeAll(term);
if(this.listTerms.get(i).get(0) == 0){
Polynomial polynomial = new Polynomial(this.listTerms.get(i).get(1), this.listTerms.get(i).get(2));
polynomial = polynomial.polynomialDerivative();
if(polynomial.getExponent() == 0) {
term.add(3);
term.add((int) polynomial.getCoefficient());
} else {
term.add(0);
term.add((int) polynomial.getCoefficient());
term.add((int) polynomial.getExponent());
}
equation.add(term);
} else if(this.listTerms.get(i).get(0) == 1) {
Sin sin = new Sin(this.listTerms.get(i).get(1), this.listTerms.get(i).get(2), this.listTerms.get(i).get(3));
Cos cos = sin.sinDerivative();
term.add(2);
term.add((int) cos.getCoefficient());
term.add((int) cos.getPeriod());
term.add((int) cos.getExponent());
equation.add(term);
} else if(this.listTerms.get(i).get(0) == 2) {
Cos cos = new Cos(this.listTerms.get(i).get(1), this.listTerms.get(i).get(2), this.listTerms.get(i).get(3));
Sin sin = cos.cosDerivative();
term.add(1);
term.add((int) sin.getCoefficient());
term.add((int) sin.getPeriod());
term.add((int) sin.getExponent());
} else if(this.listTerms.get(i).get(0) == 3) {
}
}
Equation equation1 = new Equation(equation);
return equation1;
}
}