forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymPrinting.java
More file actions
47 lines (43 loc) · 1.31 KB
/
Copy pathSymPrinting.java
File metadata and controls
47 lines (43 loc) · 1.31 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
package symjava.symbolic;
public class SymPrinting {
public static int getPrecedence(Expr expr) {
if(expr instanceof Sum)
return 5;
else if(expr instanceof Add || expr instanceof Subtract)
return 10;
if(expr instanceof Multiply || expr instanceof Divide || expr instanceof Negate)
return 20;
if( expr instanceof Reciprocal )
return 25;
if(expr instanceof Pow)
return 30;
if(expr instanceof Func) {
Func f = (Func)expr;
if(f.isAbstract())
return 90;
else
return getPrecedence(f.getExpr());
}
if(expr instanceof Symbol || expr instanceof Symbols || expr instanceof SymReal<?>)
return 100;
return 1000; //default to atomic
}
public static String addParenthsesIfNeeded(Expr toMe, Expr forOperation) {
if(getPrecedence(toMe) < getPrecedence(forOperation))
return "(" + toMe.toString() + ")";
return toMe.toString();
}
public static String addParenthsesIfNeeded2(Expr toMe, Expr forOperation) {
if(getPrecedence(toMe) <= getPrecedence(forOperation))
return "(" + toMe.toString() + ")";
return toMe.toString();
}
public static String join(Expr[] exprs,String deliminator) {
StringBuilder sb = new StringBuilder();
for(Expr e : exprs) {
sb.append(e.toString());
sb.append(deliminator);
}
return sb.substring(0, sb.length()-deliminator.length());
}
}