forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymReal.java
More file actions
155 lines (127 loc) · 3.11 KB
/
SymReal.java
File metadata and controls
155 lines (127 loc) · 3.11 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
package symjava.symbolic;
import java.util.Map;
import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
import com.sun.org.apache.bcel.internal.generic.InstructionFactory;
import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
import com.sun.org.apache.bcel.internal.generic.InstructionList;
import com.sun.org.apache.bcel.internal.generic.MethodGen;
import com.sun.org.apache.bcel.internal.generic.PUSH;
import symjava.symbolic.utils.Utils;
/**
* A SymReal object is a symbolic representation of a real number
*
* @param <T>
*/
public class SymReal<T extends Number> extends Expr {
protected T value;
public SymReal(T val) {
this.value = val;
isSimplified = true;
updateLabel();
}
public T getValue() {
return value;
}
public double getDoubleValue() {
return value.doubleValue();
}
public int getIntValue() {
return value.intValue();
}
public long getLongValue() {
return value.longValue();
}
public float getFloatValue() {
return value.floatValue();
}
public boolean isInteger() {
double dval = value.doubleValue();
double remain = dval - Math.floor(dval);
if(remain == 0.0) {
return true;
}
return false;
}
public boolean isPositive () {
return value.doubleValue() > 0.0;
}
public boolean isNonPositive () {
return value.doubleValue() <= 0.0;
}
public boolean isNegative () {
return value.doubleValue() < 0.0;
}
public boolean isNonNegative () {
return value.doubleValue() >= 0.0;
}
public boolean isZero () {
return value.doubleValue() == 0.0;
}
public boolean isOne () {
return value.doubleValue() == 1.0;
}
public boolean isNegativeOne () {
return value.doubleValue() == -1.0;
}
public static <T extends Number> SymReal<T> valueOf(T val) {
return new SymReal<T>(val);
}
@Override
public Expr diff(Expr expr) {
return Symbol.C0;
}
@Override
public Expr subs(Expr from, Expr to) {
if(Utils.symCompare(this, from))
return to;
return this;
}
@Override
public Expr simplify() {
return this;
}
@Override
public boolean symEquals(Expr other) {
if(this == other)
return true;
else if(other instanceof SymReal<?>) {
SymReal<?> o = (SymReal<?>)other;
Number t1 = (Number)value;
Number t2 = (Number)o.getValue();
//if(t1.equals(t2))
// return true;
if(t1.doubleValue() == t2.doubleValue())
return true;
}
return false;
}
@Override
public InstructionHandle bytecodeGen(String clsName, MethodGen mg,
ConstantPoolGen cp, InstructionFactory factory,
InstructionList il, Map<String, Integer> argsMap, int argsStartPos,
Map<Expr, Integer> funcRefsMap) {
return il.append(new PUSH(cp, value));
}
@Override
public TypeInfo getTypeInfo() {
if(value instanceof Double)
return TypeInfo.tiDouble;
else if(value instanceof Integer)
return TypeInfo.tiInt;
else if(value instanceof Long)
return TypeInfo.tiLong;
else if(value instanceof Float)
return TypeInfo.tiFloat;
else
throw new RuntimeException();
}
@Override
public Expr[] args() {
return new Expr[0];
}
@Override
public void updateLabel() {
label = String.valueOf(value);
sortKey = label;
}
}