Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
155 lines (142 loc) · 4.2 KB

File metadata and controls

155 lines (142 loc) · 4.2 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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.HashMap;
import java.util.List;
import symjava.symbolic.utils.Utils;
/**
* An object of Symbols can represents a list of symbols
* For example:
* Symbols ss = new Symbols("x"); //x_i, i=...,1,2,3...
* Symbol x1 = ss.get(1); //x_1
* Symbol x8 = ss.get(8); //x_8
*
* This can be used to generate a list of symbols in summation
* or as a parameter list in an equation.
*
* @author yuemingliu
*
*/
public class Symbols extends Expr {
Expr namePrefix;
Expr indexExpr;
HashMap<Integer, Symbol> cache = new HashMap<Integer, Symbol>();
/**
* Create symbols like:
* x_i, i=...,1,2,3,...
* @param namePrefix
*/
public Symbols(String namePrefix) {
this.namePrefix = new Symbol(namePrefix);
this.indexExpr = new Symbol("i");
this.label = namePrefix + "_" +indexExpr;
sortKey = label;
}
/**
* Create more complicated symbols like
* x_{i+j}, i=...,1,2,3,...
* x_{i-1}, i=...,1,2,3,...
*
* @param namePrefix
* @param indexExpr
*/
public Symbols(String namePrefix, Expr indexExpr) {
this.namePrefix = new Symbol(namePrefix);
this.indexExpr = indexExpr;
if(indexExpr instanceof Symbol)
this.label = namePrefix + "_" + indexExpr;
else
this.label = namePrefix + "_{" + indexExpr+"}";
sortKey = label;
}
public Symbol get(int index) {
Symbol s = cache.get(index);
if(s == null) {
if(indexExpr instanceof Symbols) {
s = new Symbol(namePrefix+"_"+index);
cache.put(index, s);
} else {
List<Expr> exprs = Utils.extractSymbols(indexExpr);
if(exprs.size() > 1) {
throw new RuntimeException("Please call get(Expr indexSymbol, int index).");
} else if(exprs.size() == 1){
Expr indexSymbol = exprs.get(0);
Expr simpliedIndexExpr = indexExpr.subs(indexSymbol, index).simplify();
if(simpliedIndexExpr instanceof Symbol || simpliedIndexExpr instanceof SymReal)
s = new Symbol(namePrefix+"_"+simpliedIndexExpr.getLabel());
else
s = new Symbol(namePrefix+"_{"+simpliedIndexExpr.getLabel()+"}");
cache.put(index, s);
} else {
throw new RuntimeException("No indexes found!");
}
}
}
return s;
}
public Symbol get(Symbol indexSymbol, int index) {
Symbol s = cache.get(index);
if(s == null) {
Expr simpliedIndexExpr = indexExpr.subs(indexSymbol, index).simplify();
if(simpliedIndexExpr instanceof Symbol || simpliedIndexExpr instanceof SymReal)
s = new Symbol(namePrefix.getLabel() + "_"+simpliedIndexExpr.getLabel());
else
s = new Symbol(namePrefix.getLabel() + "_{"+simpliedIndexExpr.getLabel()+"}");
cache.put(index, s);
}
return s;
}
/**
* Return an array of symbols: [startIdx, endIdx]
* which include startIdx and endIdx
* @param startIdx
* @param endIdx
* @return
*/
public Expr[] get(int startIdx, int endIdx) {
Expr[] rlt = new Expr[endIdx-startIdx+1];
for(int i=startIdx; i<=endIdx; i++)
rlt[i-startIdx] = get(i);
return rlt;
}
/**
* The substitution of symbols will return a symbol
* which behaves different from other class in SymJava
*/
@Override
public Expr subs(Expr from, Expr to) {
if(Utils.symCompare(this, from)) {
return to;
} else if(Utils.symCompare(indexExpr,from)) {
if(to instanceof SymInteger) {
SymInteger index = (SymInteger)to;
Symbol s = this.get(index.getValue());
//case: symbols.subs(i,3): x_i => x_3
return s;
}
//case: symbols.subs(i,j): x_i => x_j
return new Symbol(namePrefix.getLabel() + "_{" + to + "}");
}
Expr simpliedIndexExpr = indexExpr.subs(from, to).simplify();
if(simpliedIndexExpr instanceof Symbol || simpliedIndexExpr instanceof SymReal)
return new Symbol(this.namePrefix.getLabel() + "_" + simpliedIndexExpr.getLabel());
else
return new Symbol(this.namePrefix.getLabel() + "_{" + simpliedIndexExpr.getLabel() + "}");
}
@Override
public Expr diff(Expr expr) {
if(Utils.symCompare(this, expr))
return Symbol.C1;
return Symbol.C0;
}
@Override
public Expr simplify() {
return this;
}
@Override
public boolean symEquals(Expr other) {
if(other instanceof Symbols) {
return this.namePrefix.equals(((Symbols) other).namePrefix) &&
Utils.symCompare(this.indexExpr, ((Symbols) other).indexExpr);
}
return false;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.