-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.java
206 lines (190 loc) · 5.09 KB
/
Log.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
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
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* * @author Nikita Kogan.
* Implementation of Log.
*/
public class Log extends BinaryExpression implements Expression {
/**
* Constructor.
*
* @param exp1 exp1
* @param exp2 exp2
*/
public Log(Expression exp1, Expression exp2) {
super(exp1, exp2);
}
/**
* Constructor.
*
* @param st1 st1
* @param st2 st2
*/
public Log(String st1, String st2) {
super(new Var(st1), new Var(st2));
}
/**
* Constructor.
*
* @param st st
* @param d d
*/
public Log(String st, double d) {
super(new Var(st), new Num(d));
}
/**
* Constructor.
*
* @param d d
* @param st st
*/
public Log(double d, String st) {
super(new Num(d), new Var(st));
}
/**
* Constructor.
*
* @param st st
* @param ex ex
*/
public Log(String st, Expression ex) {
super(new Var(st), ex);
}
/**
* Constructor.
*
* @param ex ex
* @param st st
*/
public Log(Expression ex, String st) {
super(ex, new Var(st));
}
/**
* Constructor.
*
* @param d d
* @param ex ex
*/
public Log(double d, Expression ex) {
super(new Num(d), ex);
}
/**
* Constructor.
*
* @param ex ex
* @param d d
*/
public Log(Expression ex, double d) {
super(ex, new Num(d));
}
/**
* Constructor.
*
* @param d1 d1
* @param d2 d2
*/
public Log(double d1, double d2) {
super(new Num(d1), new Num(d2));
}
/**
* evaluate.
*
* @param assignment assigment
* @return value
* @throws Exception Exception.
*/
public double evaluate(Map<String, Double> assignment) throws Exception {
if (super.getExp1().evaluate(assignment) < 0 || super.getExp2().evaluate(assignment) < 0) {
throw new Exception("negative log");
}
return (Math.log(super.getExp2().evaluate(assignment)) / Math.log(super.getExp1().evaluate(assignment)));
}
/**
* evaluate.
*
* @return value
* @throws Exception Exception
*/
public double evaluate() throws Exception {
if (super.getExp1().evaluate() == 1.0) {
throw new Exception("1 base");
}
if (super.getExp1().evaluate() < 0 || super.getExp2().evaluate() < 0) {
throw new Exception("negative log");
}
return (Math.log(super.getExp2().evaluate()) / (Math.log(super.getExp1().evaluate())));
}
/**
* returns a list of vars.
*
* @return list of vars.
*/
public List<String> getVariables() {
List<String> vars = new ArrayList<>();
List<String> variableList1 = super.getExp1().getVariables();
List<String> variableList2 = super.getExp2().getVariables();
if (variableList1 != null) {
for (int i = 0; i < variableList1.size(); i++) {
vars.add(variableList1.get(i));
}
}
if (variableList2 != null) {
for (int i = 0; i < variableList2.size(); i++) {
vars.add(variableList2.get(i));
}
}
return vars;
}
/**
* makes a string from expression.
*
* @return string form.
*/
public String toString() {
String plusStr = "log(" + super.getExp1().toString() + ", " + super.getExp2().toString() + ")";
return plusStr;
}
/**
* assign.
*
* @param var var.
* @param expression expression.
* @return ex.
*/
public Expression assign(String var, Expression expression) {
Expression ex = new Log(getExp1().assign(var, expression), getExp2().assign(var, expression));
return ex;
}
/**
* differentiate.
*
* @param var var.
* @return differentiate the expression.
*/
public Expression differentiate(String var) {
Expression exp1 = super.getExp1();
Expression exp2 = super.getExp2();
Expression exp2Differ = super.getExp2().differentiate(var);
return new Div(exp2Differ, new Mult(exp2, new Log(new Var("e"), exp1)));
}
/**
* simplify.
*
* @return simplifies the expression.
*/
public Expression simplify() {
if (super.getExp2().simplify().toString().equals(super.getExp1().simplify().toString())) {
return new Num(1).simplify();
}
List<String> list = this.getVariables();
if (list.isEmpty()) {
try {
return new Num(new Log(super.getExp1().evaluate(), super.getExp2().evaluate()).evaluate());
} catch (Exception e) {
e.printStackTrace();
}
}
return new Log(super.getExp1().simplify(), super.getExp2().simplify());
}
}