-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.java
More file actions
167 lines (114 loc) · 3.25 KB
/
Copy pathFunction.java
File metadata and controls
167 lines (114 loc) · 3.25 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
166
167
import java.util.ArrayList;
public class Function{
private ArrayList<ArrayList<Integer>> pyramid;
private double firstX;
private int firstVal;
private ArrayList<Monomial> expression;
public Function(ArrayList<Integer> vals)
{
firstX =(vals.size() - 1)/(double)2;
pyramid = arrPyramid(vals);
firstVal = findFirstVal();
expression = new ArrayList<Monomial>();
expression.add(new Monomial(firstVal));
}
/**
* Method that takes an ArrayList of numbers and returns an ArrayList of number that are the distances between each of the terms
* Exe: Takes [0, 1, 3, 6, 10, 15], and returns [1, 2, 3, 4, 5], since those are the distance betwen 0 and 1, 1 and 3, 3 and 6, etc
* @param prevArrayList the origional ArrayList from which to find the distances
* @return An ArrayList of the distances between each of the terms in the og AL
*/
public ArrayList<Integer> arrDeriv(ArrayList<Integer> prevArrayList)
{
ArrayList<Integer> derived = new ArrayList<Integer>();
for(int i = 0; i < prevArrayList.size()-1; i++)
{
derived.add(prevArrayList.get(i+1) - prevArrayList.get(i));
}
return derived;
}
/**
*
* @param ogArrayList
* @return
*/
public ArrayList<ArrayList<Integer>> arrPyramid(ArrayList<Integer> ogArrayList)
{
int ogSize = ogArrayList.size();
ArrayList<ArrayList<Integer>> incPyramid = new ArrayList<ArrayList<Integer>>();
incPyramid.add(ogArrayList);
for(int i = 0; i < ogSize - 1; i++)
{
ogArrayList = arrDeriv(ogArrayList);
incPyramid.add(ogArrayList);
}
return incPyramid;
}
public int findFirstVal()
{
return pyramid.get(pyramid.size() - 1).get(0);
}
//integrates the expression by frirst integrating every existing monomial, and then adding a new one that represents C, wh
//which will be solved for using anouther method
public void integrate()
{
for(int i = 0; i < expression.size(); i++)
{
expression.get(i).integrate();
}
expression.add(new Monomial(findC()));//change so it dont add for c = 0
}
public double sumExpression(double x)
{
double sum = 0;
for(Monomial m : expression)
{
sum += m.findSum(x);
}
return sum;
}
//Returns the value of C
//WORK IN PROGRESS
public double findC()
{
//finds value of c for the expression at any power
int exp = expression.get(0).getExponent();
int row = pyramid.size() - exp - 1;
ArrayList<Integer> corrRow = pyramid.get(row);
int valueC = corrRow.get(corrRow.size() - 1);
//finds the value of x of the expression at any power
double valX = firstX + 0.5 * exp;
double sum = sumExpression(valX);
return valueC - sum;
}
public void findExpression()
{
for(int i = 0; i < pyramid.size() - 1; i++)
{
integrate();
}
}
public void printExpression()
{
String printExp = new String();
for(Monomial bob : expression)
{
if(bob.getCoeff() != 0)
{
printExp = printExp + "+" + bob.toString();
}
}
System.out.println(printExp);
}
public void printPyramid()
{
for(int i = 0; i < pyramid.size(); i++)
{
for(int j = 0; j < pyramid.get(i).size(); j++)
{
System.out.print(pyramid.get(i).get(j) + " , ");
}
System.out.print("\n");
}
}
}