-
Notifications
You must be signed in to change notification settings - Fork 3
/
Function.java
52 lines (46 loc) · 1.38 KB
/
Function.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
public class Function{
String variable;
String expression;
double value;
FunctionExpression compose=null;
public Function(String expression, String variable, double value) {
this.variable = variable;
this.expression = expression;
this.value=value;
}
public Function(String expression){
this.expression=expression;
}
public Function copy(){
Function temp=new Function(expression);
temp.value=value;
temp.variable=variable;
if(compose!=null)temp.compose=compose.copy();
return temp;
}
public void compose(FunctionExpression f){
compose=f;
}
public void print(){
System.out.println("Function: "+expression);
System.out.println("Variable: "+variable);
System.out.println("Value: "+value);
if(compose!=null)System.out.println("Compose: "+compose.function.expression);
}
//add arbitrary function "genericFunction" by appending
//if(expression.equals("genericFunction") return mapValue();
public double evaluate(){
double value;
if(variable.equals("/"))value=this.value;
else value=Calculus.getValue(variable);
if(compose!=null){
value= Calculus.compute(compose);
}
if(expression.equals("const")) return this.value;
if(expression.equals("sin")) return Math.sin(value);
if(expression.equals("cos")) return Math.cos(value);
if(expression.equals("ln")) return Math.log(value);
if(expression.equals(variable)) return value;
return 0;
}
}