forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum.java
More file actions
149 lines (129 loc) · 3.59 KB
/
Sum.java
File metadata and controls
149 lines (129 loc) · 3.59 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
package symjava.symbolic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import symjava.symbolic.utils.AddList;
import symjava.symbolic.utils.Utils;
/**
* a+b+c+d+...
*/
public class Sum extends Expr {
public Expr summandTemplate;
public Expr indexExpr;
public int start;
public int end;
HashMap<Integer, Expr> cache = new HashMap<Integer, Expr>();
/**
* \Sigma_{i=start}^{end}{x_i}
*
* @param summandTemplate
* @param indexExpr
* @param start
* @param end
*/
public Sum(Expr summandTemplate, Expr indexExpr, int start, int end) {
this.summandTemplate = summandTemplate;
this.indexExpr = indexExpr;
label = "\\Sigma_{"+indexExpr+"="+start+"}^" + end + "{" + SymPrinting.addParenthsesIfNeeded(summandTemplate, new Add(Symbol.x, Symbol.y)) + "}";
this.start = start;
this.end = end;
sortKey = label;
}
public static Sum apply(Expr summandTemplate, Expr indexExpr, int start, int end) {
return new Sum(summandTemplate, indexExpr, start, end);
}
public Expr getSummand(int index) {
Expr s = cache.get(index);
if(s == null) {
if(indexExpr instanceof Symbol) {
s = summandTemplate.subs(indexExpr, index).simplify();
cache.put(index, s);
} else {
List<Expr> exprs = Utils.extractSymbols(indexExpr);
if(exprs.size() > 1) {
throw new RuntimeException("Please call getSummand(Expr indexSymbol, int index).");
} else if(exprs.size() == 1){
Expr indexSymbol = exprs.get(0);
s = summandTemplate.subs(indexExpr, indexExpr.subs(indexSymbol, index).simplify());
cache.put(index, s);
} else {
return this;
}
}
}
return s;
}
public Expr getSummand(Symbol indexSymbol, int index) {
Expr s = cache.get(index);
if(s == null) {
s = summandTemplate.subs(indexExpr, indexExpr.subs(indexSymbol, index).simplify());
cache.put(index, s);
}
return s;
}
@Override
public Expr subs(Expr from, Expr to) {
return new Sum(summandTemplate.subs(from, to).simplify(),
indexExpr.subs(from, to), start, end);
}
@Override
public Expr diff(Expr expr) {
AddList addList = new AddList();
for(int i=start; i<=end; i++) {
Expr summand = this.getSummand(i).diff(expr);
if(!Utils.symCompare(Symbol.C0, summand))
addList.add(summand);
}
if(addList.size() == 0)
addList.add(Symbol.C0);
return addList.toExpr().simplify();
}
@Override
public Expr simplify() {
List<Expr> ss = Utils.extractSymbols(this.summandTemplate);
if(indexExpr instanceof Symbol) {
if(!Utils.containSymbol(this.summandTemplate, (Symbol)indexExpr))
return summandTemplate.multiply(end-start+1);
}
if(ss.size() == 1) { //This should be indexSym
List<Expr> list = new ArrayList<Expr>();
for(int i=start; i<=end; i++) {
list.add(summandTemplate.subs(indexExpr, i));
}
return Utils.addListToExpr(list).simplify();
}
return new Sum(summandTemplate.simplify(), indexExpr, start, end);
}
@Override
public boolean symEquals(Expr other) {
if(other instanceof Sum) {
Sum o = (Sum)other;
if(summandTemplate.symEquals(o.summandTemplate) && indexExpr.symEquals(o.indexExpr) &&
start == o.start && end == o.end)
return true;
}
return false;
}
@Override
public void flattenAdd(List<Expr> outList) {
outList.add(this);
}
@Override
public void flattenMultiply(List<Expr> outList) {
outList.add(this);
}
@Override
public Expr[] args() {
// TODO Auto-generated method stub
return null;
}
@Override
public TypeInfo getTypeInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void updateLabel() {
// TODO Auto-generated method stub
}
}