forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
158 lines (135 loc) · 5.02 KB
/
Matrix.java
File metadata and controls
158 lines (135 loc) · 5.02 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
156
157
158
package symjava.symbolic;
import java.util.Map;
import com.sun.org.apache.bcel.internal.Constants;
import com.sun.org.apache.bcel.internal.generic.ALOAD;
import com.sun.org.apache.bcel.internal.generic.ASTORE;
import com.sun.org.apache.bcel.internal.generic.ArrayType;
import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
import com.sun.org.apache.bcel.internal.generic.DSTORE;
import com.sun.org.apache.bcel.internal.generic.InstructionConstants;
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.LocalVariableGen;
import com.sun.org.apache.bcel.internal.generic.MethodGen;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.bcel.internal.generic.ObjectType;
import com.sun.org.apache.bcel.internal.generic.PUSH;
import com.sun.org.apache.bcel.internal.generic.Type;
import symjava.matrix.ExprMatrix;
import symjava.symbolic.Expr.TYPE;
public class Matrix extends Tensor {
public int nRowStart;
public int nColStart;
public int nRow;
public int nCol;
public Matrix parent;
protected int indexLVT = -1;
public Matrix(String name, int nRow, int nCol) {
super(name);
nRowStart = 0;
nColStart = 0;
this.nRow = nRow;
this.nCol = nCol;
}
public Matrix(Matrix parent, int nRowStart, int nColStart, int nRow, int nCol) {
super(parent.label+"_"+nRowStart+"_"+nColStart+"_"+nRow+"_"+nCol);
this.nRowStart = nRowStart;
this.nColStart = nColStart;
this.nRow = nRow;
this.nCol = nCol;
this.parent = parent;
}
public Matrix(Matrix parent, String name, int nRowStart, int nColStart, int nRow, int nCol) {
super(name);
this.nRowStart = nRowStart;
this.nColStart = nColStart;
this.nRow = nRow;
this.nCol = nCol;
this.parent = parent;
}
public ExprMatrix split(int nRowBlock, int nColBlock) {
int m = nRow/nRowBlock;
if(nRow%nRowBlock > 0)
m = (nRow+(nRowBlock-nRow%nRowBlock))/nRowBlock;
int n = nCol/nColBlock;
if(nCol%nColBlock > 0)
n = (nCol+(nColBlock-nCol%nColBlock))/nColBlock;
int last_m = nRow%m==0?m:nRow%m;
int last_n = nCol%n==0?n:nCol%n;
/* System.out.println(m);
System.out.println(n);
System.out.println(last_m);
System.out.println(last_n);
*/ Expr[][] items = new Expr[nRowBlock][nColBlock];
for(int i=0; i<nRowBlock; i++) {
int nr = m;
if(i == nRowBlock-1) nr = last_m;
for(int j=0; j<nColBlock; j++) {
int nc = n;
if(j == nColBlock-1) nc = last_n;
items[i][j] = new Matrix(this, this.label+"_"+i+"_"+j, i*m, j*n, nr, nc);
}
}
return new ExprMatrix(items);
}
@Override
public InstructionHandle bytecodeGen(String clsName, MethodGen mg,
ConstantPoolGen cp, InstructionFactory factory,
InstructionList il, Map<String, Integer> argsMap, int argsStartPos,
Map<Expr, Integer> funcRefsMap) {
// Create an instance of Jama.Matrix and store it to local variable index with this.indexLVT
if(indexLVT == -1) {
//jama.Matrix l_m = null;
LocalVariableGen lg = mg.addLocalVariable("l_"+getLabel(),
new ObjectType("Jama.Matrix"), null, null);
indexLVT = lg.getIndex();
// il.append(InstructionConstants.ACONST_NULL);
// lg.setStart(il.append(new DSTORE(idx)));
// First time touch the matrix, declare a local reference of Java.Matrix
il.append(new NEW(cp.addClass("Jama.Matrix")));
il.append(InstructionConstants.DUP);
//prepare argument: double[] vals
//il.append(new ALOAD(argsStartPos));
//il.append(new PUSH(cp, argsMap.get(this.label)));
//il.append(InstructionConstants.DALOAD); //Load double from array
//////////////il.append(new ALOAD(argsMap.get(this.label))); //Load reference from local variable (from function arguments)
//il.append(new ALOAD(1)); //Load reference from local variable (from function arguments)
il.append(new ALOAD(argsStartPos));
il.append(new PUSH(cp, argsMap.get(this.label)));
il.append(InstructionConstants.AALOAD); //Load double from array
//prepare argument: double m - number of rows
il.append(new PUSH(cp, nRow));
il.append(factory.createInvoke("Jama.Matrix", "<init>",
Type.VOID, new Type[] { new ArrayType(Type.DOUBLE, 1), Type.INT },
Constants.INVOKESPECIAL));
//jama.Matrix l_m = new jama.Matrix(args[], nRow);
lg.setStart(il.append(new ASTORE(indexLVT)));
}
// Retrun the local reference of the matrix
return il.append(new ALOAD(indexLVT));
//For test purpose
//il.append(new ALOAD(indexLVT));
//il.append(new PUSH(cp, 1.0));
//return il.append(InstructionConstants.DRETURN);
}
@Override
public TypeInfo getTypeInfo() {
TypeInfo ti = new TypeInfo(TYPE.MATRIX, new int[2]);
ti.dim[0] = this.nRow;
ti.dim[1] = this.nCol;
return ti;
}
public void bytecodeGenReset() {
this.indexLVT = -1;
}
@Override
public Expr getParent() {
return this.parent;
}
public static void main(String[] args) {
Matrix m = new Matrix("A",6,8);
ExprMatrix sm = m.split(3, 2);
System.out.println(sm);
}
}