-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDerivative.java
More file actions
324 lines (300 loc) · 11.2 KB
/
Copy pathDerivative.java
File metadata and controls
324 lines (300 loc) · 11.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import java.util.ArrayList;
public class Derivative
{
String equation;
char dx;
ArrayList<String> variables = new ArrayList<String>();
public Derivative(Equation eq, String dx)
{
equation = eq.getEqNoEquals();
this.dx = 'x';
variables = eq.getRequiredVars();
for(String v : variables)
{
if(v.equals(dx)) this.dx = (char)(variables.indexOf(v)+74);
equation = equation.replace(v, (char)(variables.indexOf(v)+74)+"");
}
}
public String getEquation()
{
return equation;
}
public char getTerm()
{
return dx;
}
public void setEquation(String eq)
{
equation = eq;
}
public void setTerm(char dx)
{
this.dx = dx;
}
public String getDerivative()
{
String derek = getDerivative(equation, dx);
String tmp = "";
for(Character c : derek.toCharArray())
{
if(c > 73 && c < 100)
{
tmp += variables.get(c-74);
}
else
tmp += c;
}
return tmp;
}
/**
* Gets derivative of any function by splitting it up and relaying parts to different methods
* @param function The original function to be passed in
* @return the derivative of function
*/
public String getDerivative(String function, char dx)
{
// Step 1 - Replace all "-" with "+-1*"
function = function.replace("+-1*", "-");
function = function.replace("+(-1)*", "-");
function = function.replace("-", "+-1*");
String f1 = "";
// Step 2 - Split by "+" outside of parenthesis
ArrayList<String> subequations = new ArrayList<String>();
int parenthesisDepth = 0;
String tmp = "";
for(Character c : function.toCharArray())
{
if(c == '(' || c == '[') parenthesisDepth++;
else if(c == ')' || c == ']') parenthesisDepth--;
if(c == '+' && parenthesisDepth == 0){ subequations.add(tmp); tmp = ""; }
else tmp += c;
}
subequations.add(tmp);
// Step 3 - Group everything into parenthesis
for(String currentPart : subequations)
{
int bracketDepth = 0;
String pNp = "";
char nextC=' ';
if(currentPart.length() == 1) subequations.set(subequations.indexOf(currentPart), "("+currentPart+")");
else
{
for(int i = 1; i < currentPart.length()-1; i++)
{
char c = currentPart.charAt(i);
char prevC = currentPart.charAt(i-1);
nextC = currentPart.charAt(i+1);
if(pNp.equals("")) pNp += prevC;
if(i == 1 && prevC == '[') bracketDepth++;
if(i == currentPart.length()-1 && nextC == ']') bracketDepth--;
if(c == '[') bracketDepth++;
else if(c == ']') bracketDepth--;
if((nextC != ')' && prevC != '(') && bracketDepth == 0 && (c == '*' || c == '/' || c == '+')) pNp += ")" + c + "(";
else
pNp += c;
}
pNp += nextC;
if(!pNp.substring(0,1).equals("(") || !pNp.substring(pNp.length()-1).equals(")"))pNp = "("+pNp+")";
subequations.set(subequations.indexOf(currentPart), pNp);
}
}
// Removes extra ()S
for(String sub : subequations)
{
parenthesisDepth = 0;
int index = subequations.indexOf(sub);
for(Character c : sub.toCharArray())
{
if(c == '(') parenthesisDepth++;
else if(c == ')') parenthesisDepth--;
if(parenthesisDepth < 0)
{
subequations.set(index, sub.replace(")*(", "*"));
}
}
}
// Step 4 - Group off subequations based on whether there is a "/" before the opening parenthesis.
for(String currentPart : subequations)
{
ArrayList<String> nums = new ArrayList<String>();
ArrayList<String> dens = new ArrayList<String>();
parenthesisDepth = 0;
boolean isNumerator = true;
tmp = "";
for(Character c : currentPart.toCharArray())
{
if(c == '(' || c == '[') parenthesisDepth++;
else if(c == ')' || c == ']') parenthesisDepth--;
if(parenthesisDepth == 0 && isNumerator && !tmp.equals(""))
{
if(tmp.substring(0,1).equals("*") || tmp.substring(0,1).equals("/")) tmp = tmp.substring(1);
nums.add(tmp+c); tmp = "";
}
else if(parenthesisDepth == 0 && !isNumerator && !tmp.equals(""))
{
if(tmp.substring(0,1).equals("*") || tmp.substring(0,1).equals("/")) tmp = tmp.substring(1);
dens.add(tmp+c); tmp = "";
}
else tmp += c;
if(c == '/' && parenthesisDepth == 0) isNumerator = false;
else if(c == '*' && parenthesisDepth == 0) isNumerator = true;
}
// Remove extra ()s
for(String n : nums)
{
if(n.substring(0,1).equals("(") && n.substring(n.length()-1).equals(")")){
n = n.substring(1,n.length()-1);
nums.set(nums.indexOf("("+n+")"), n);
}
}
for(String n : dens)
{
if(n.substring(0,1).equals("(") && n.substring(n.length()-1).equals(")")){
n = n.substring(1,n.length()-1);
dens.set(dens.indexOf("("+n+")"), n);
}
}
// Step 5a - If there are any subequation denominators
if(dens.size() > 0)
f1 += quotientRule(join(nums, "*"), join(dens, "*"), dx);
else
{
if(nums.size() == 1 && !nums.get(0).contains("[")) f1 += powerRule(nums.get(0), dx);
else if(nums.size() == 1)
{
f1 += deriveFunction(nums.get(0).substring(0,3), nums.get(0).substring(4,nums.get(0).lastIndexOf("]")), dx);
}
else
f1 += productRule(nums, dx);
}
}
return f1;
}
public int getAppearances(String str, char chr)
{
int counter = 0;
for(Character c : str.toCharArray())
{
if(c == chr)
{
counter++;
}
}
return counter;
}
/**
* Small utility method to join ArrayLists together with a delimiter
*/
public String join(ArrayList<String> arr, String s)
{
String tmp = "";
for(String str : arr)
{
tmp += str+s;
}
try{
return tmp.substring(0,tmp.lastIndexOf(s));
}catch(Exception e){return "";}
}
/**
* Derives the equation parts using the power rules.
* @param fx the polynomial equation
* @param dx the variable to derive in terms of
*/
public String powerRule(String fx, char dx)
{
String f1 = "";
double power = 0, constant = 0, constant1 = 1;
String constantString = "";
int pc = 0;
for(Character c : fx.toCharArray())
{
constantString = constant+"";
if(constantString.contains("E")) constantString = constantString.substring(0,constantString.indexOf("E")+1);
if(c == dx) power++;
else if(c == '.'){pc = constantString.length()-1;}
else if(Character.isDigit(c)){constant*=10; constant+=c-48;}
else if(c == '*' && constant != 0){if(pc != 0)constant /= Math.pow(10,constantString.length()-pc-1); constant1 *= constant; constant = 0;}
else if(c == '+' || c == '-')
{
constant /= Math.pow(10,constantString.length()-pc-1);
if(constant != 0)constant1 *= constant;
f1+=(constant1*power)+"*";
for(int i = 0; i < power-1; i++) f1+=dx+"*";
f1 = f1.substring(0,f1.length()-1);
power = 0;
constant1 = 1;
constant = 0;
pc = 0;
f1+=c;
}
}
constant /= Math.pow(10,constantString.length()-pc-1);
if(constant != 0)constant1 *= constant;
f1+=(constant1*power)+"*";
for(int i = 0; i < power-1; i++) f1+=dx+"*";
f1 = f1.substring(0,f1.length()-1);
if(f1.equals("")) return "0";
if(f1.substring(f1.length()-1).equals("+") || f1.substring(f1.length()-1).equals("-")) f1 = f1.substring(0,f1.length()-1);
return f1;
}
public String quotientRule(String hi, String lo, char dx)
{
return "("+lo+"*"+getDerivative(hi, dx)+"-"+hi+"*"+getDerivative(lo, dx)+")/("+lo+"*"+lo+")";
}
public String productRule(ArrayList<String> functions, char dx)
{
String tmp = "";
ArrayList<String> tmpArr = new ArrayList<String>();
ArrayList<String> outerArr = new ArrayList<String>();
for(int i = 0; i < functions.size(); i++)
{
String fx = functions.get(i);
tmpArr = new ArrayList<String>();
tmpArr.add(getDerivative(fx, dx));
for(int j = 0; j < functions.size(); j++)
{
String gx = functions.get(j);
if(i!=j) tmpArr.add(gx);
}
outerArr.add(join(tmpArr, "*"));
}
return "("+join(outerArr, "+")+")";
}
/**
* Derives equation parts in the form fx[gx] -> fx'[gx]*gx' given that fx is a function of gx
* @param fx the function in the form: "cos" or "lg3"
* @param gx the inner function
*/
public String deriveFunction(String fx, String gx, char dx)
{
String f1 = "";
if(fx.equals("cos"))
f1 = "-1*sin["+gx+"]*"+getDerivative(gx, dx);
else if(fx.equals("sin"))
f1 = "cos["+gx+"]*"+getDerivative(gx, dx);
else if(fx.equals("tan"))
f1 = "sec["+gx+"]*sec["+gx+"]*"+getDerivative(gx, dx);
else if(fx.equals("sec"))
f1 = "sec["+gx+"]*tan["+gx+"]*"+getDerivative(gx, dx);
else if(fx.equals("csc"))
f1 = "-1*csc["+gx+"]*cot["+gx+"]*"+getDerivative(gx, dx);
else if(fx.equals("cot"))
f1 = "-1*csc["+gx+"]*csc["+gx+"]*"+getDerivative(gx, dx);
else if(fx.equals("asn"))
f1 = getDerivative(gx, dx)+"/sqt[1-"+gx+"*"+gx+"]";
else if(fx.equals("acs"))
f1 = "-1*"+getDerivative(gx, dx)+"/sqt[1-"+gx+"*"+gx+"]";
else if(fx.equals("atn"))
f1 = getDerivative(gx, dx)+"/(1+"+gx+"*"+gx+")";
else if(fx.equals("log"))
f1 = getDerivative(gx, dx)+"/"+gx;
else if(fx.substring(0,2).equals("lg"))
f1 = getDerivative(gx, dx)+"/"+gx+"/log["+fx.substring(2)+"]";
else if(fx.equals("sqt"))
f1 = getDerivative(gx, dx)+"/(2*"+fx+"["+gx+"])";
else
f1 = "";
return f1;
}
}