12 |
* to the instantiated meta variables mvs) to a list of SSA assignments in |
* to the instantiated meta variables mvs) to a list of SSA assignments in |
13 |
* reverse order. |
* reverse order. |
14 |
*) |
*) |
15 |
val translate : (HighIL.var * Var.var * Types.meta_var list * HighIL.var list) |
val translate : (HighIL.var * Var.var * SimpleTypes.meta_arg list * HighIL.var list) |
16 |
-> HighIL.assignment list |
-> HighIL.assignment list |
17 |
|
|
18 |
end = struct |
end = struct |
21 |
structure IL = HighIL |
structure IL = HighIL |
22 |
structure DstTy = HighILTypes |
structure DstTy = HighILTypes |
23 |
structure Op = HighOps |
structure Op = HighOps |
24 |
structure Ty = Types |
structure Ty = SimpleTypes |
|
structure TU = TypeUtil |
|
|
structure MV = MetaVar |
|
25 |
structure VTbl = Var.Tbl |
structure VTbl = Var.Tbl |
26 |
structure EinOp= Examples |
structure mk= mkOperators |
27 |
|
|
28 |
fun pruneTy tv = (case TU.prune(MV.toType tv) |
fun trType (Ty.TY ty) = TranslateTy.tr ty |
29 |
of (ty as Ty.T_Var _) => raise Fail("unresolved type " ^ TU.toString ty) |
| trType _ = raise Fail "expected type" |
30 |
| ty => TranslateTy.tr ty |
fun dimVarToInt (Ty.DIM d) = d |
31 |
(* end case *)) |
| dimVarToInt _ = raise Fail "expected dim" |
|
|
|
|
fun pruneDim d = (case TU.pruneDim d |
|
|
of (Ty.DimConst n) => n |
|
|
| d => raise Fail("unresolved dimension " ^ TU.dimToString d) |
|
|
(* end case *)) |
|
|
|
|
|
fun pruneShape sv = (case TU.pruneShape(MV.toShape sv) |
|
|
of Ty.Shape dd => DstTy.tensorTy(List.map pruneDim dd) |
|
|
| shp => raise Fail("unresolved shape " ^ TU.shapeToString shp) |
|
|
(* end case *)) |
|
|
|
|
|
fun dimVarToInt dv = pruneDim(MV.toDim dv) |
|
32 |
fun dimVarToTensor dv = DstTy.tensorTy[dimVarToInt dv] |
fun dimVarToTensor dv = DstTy.tensorTy[dimVarToInt dv] |
33 |
fun dimVarToMatrix dv = let |
fun dimVarToMatrix dv = let |
34 |
val d = dimVarToInt dv |
val d = dimVarToInt dv |
35 |
in |
in |
36 |
DstTy.tensorTy[d, d] (* square matrix type *) |
DstTy.tensorTy[d, d] (* square matrix type *) |
37 |
end |
end |
38 |
fun shapeVarToTensor sv = pruneShape sv |
fun shapeVarToTensor (Ty.SHAPE shp) = DstTy.tensorTy shp |
39 |
|
| shapeVarToTensor _ = raise Fail "expected shape" |
40 |
|
|
41 |
fun assign (y, rator, xs) = [IL.ASSGN(y, IL.OP(rator, xs))] |
fun assign (y, rator, xs) = [IL.ASSGN(y, IL.OP(rator, xs))] |
42 |
|
|
50 |
|
|
51 |
fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), []) |
fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), []) |
52 |
|
|
53 |
|
val cnt = ref 0 |
54 |
|
fun genName prefix = let |
55 |
|
val n = !cnt |
56 |
|
in |
57 |
|
cnt := n+1; |
58 |
|
String.concat[prefix, "_", Int.toString n] |
59 |
|
end |
60 |
(* utility functions for synthesizing eigenvector/eigenvalue code *) |
(* utility functions for synthesizing eigenvector/eigenvalue code *) |
61 |
fun eigenVec (rator, dim) = let |
fun eigenVec (rator, dim) = let |
62 |
val ty = DstTy.SeqTy(DstTy.realTy, dim) |
val ty = DstTy.SeqTy(DstTy.realTy, dim) |
77 |
end |
end |
78 |
end |
end |
79 |
|
|
80 |
fun assignEin(*hmm*) |
fun assignEin (y, rator, xs) = IL.ASSGN(y, IL.EINAPP(rator, xs)) |
|
|
|
81 |
|
|
82 |
fun createTenEin(operator, sv)=S.transform(operator,[len(sv), sv]) |
fun simpleEOp rator (y, _,xs) = [assignEin(y, rator, xs)] |
|
fun createFldEin(operator, f)=S.transform(operator,[ ]) |
|
83 |
|
|
84 |
fun ScaGenToEin rator(y, [],xs)= assignEin (y,S.transform(rator,[]), xs) |
(* shape is an int list, DIM is in int|variable, k-level of differntiation *) |
|
fun GenToEin rator(y,[sv],xs)= assignEin(y,createTenEin(rator,sv),xs) |
|
85 |
|
|
86 |
(* build a table that maps Basis variables to their translation functions *) |
(* build a table that maps Basis variables to their translation functions *) |
87 |
val tbl : ((IL.var * Ty.meta_var list * IL.var list) -> IL.assignment list) VTbl.hash_table = let |
val tbl : ((IL.var * Ty.meta_arg list * IL.var list) -> IL.assignment list) VTbl.hash_table = let |
88 |
val tbl = VTbl.mkTable (128, Fail "Translate table") |
val tbl = VTbl.mkTable (128, Fail "Translate table") |
89 |
val insert = VTbl.insert tbl |
val insert = VTbl.insert tbl |
90 |
in |
in |
105 |
(BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)), |
(BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)), |
106 |
(BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)), |
(BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)), |
107 |
(BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))), |
(BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))), |
108 |
|
(BV.add_ii, simpleOp Op.IAdd), |
109 |
(* Changed*) |
(BV.add_tt, fn (y, [shp], xs) => let |
110 |
|
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp |
111 |
(BV.add_ii, ScaGenToEin(EinOp.addScalars)), |
val rator =mk.addTT(dd1) |
112 |
(BV.add_tt, GenToEin(EinOp.addTensors)), |
in |
113 |
|
[assignEin(y, rator,xs)] |
114 |
(*Ein*) |
end), |
115 |
(BV.add_ff, fn (y, _, [f, g]) |
(BV.add_ff, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
116 |
=>assignEin(y, createFldEin(EinOp.addField,f ), [f, g])), |
[assignEin(y, mk.addFF(d, dd),xs)]), |
117 |
(BV.add_fr, fn (y, _, [f, s]) |
|
118 |
=> assignEin(y, createFldEin(EinOp.addTenField,f ), [f, s])), |
(BV.add_ft, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [f,s]) => |
119 |
(BV.add_rf, fn (y, _, [s, f]) |
[assignEin(y, mk.addTF(d,dd),[s,f])]), |
120 |
=> assignEin(y, createFldEin(EinOp.addTenField,f ), [f, s])), |
|
121 |
|
(BV.add_tf, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
122 |
(BV.sub_ii, ScaGenToEin(EinOp.subScalars)), (*DstTy.IntTy*) |
[assignEin(y, mk.addTF(d,dd),xs)]), |
123 |
(BV.sub_tt, GenToEin(EinOp.subTensor)), |
(BV.sub_ii, simpleOp Op.ISub), |
124 |
(BV.sub_ff, fn (y, _, [f, g]) |
(BV.sub_tt, fn (y, [shp], xs) => let |
125 |
=> assignEin(y, createFldEin(EinOp.subField,f), [f, g])), |
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp |
126 |
|
val rator =mk.subTT dd1 |
127 |
(* UnChanged*) |
in |
128 |
(BV.sub_fr, fn (y, _, [f, s]) => let |
[assignEin(y, rator,xs)] |
129 |
val s' = IL.Var.copy s |
end), |
130 |
in [ |
(BV.sub_ff, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
131 |
IL.ASSGN(s', IL.OP(Op.Neg DstTy.realTy, [s])), |
[assignEin(y, mk.subFF(d, dd),xs)]), |
132 |
IL.ASSGN(y, IL.OP(Op.OffsetField, [f, s'])) |
(BV.sub_ft, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [x1,x2]) => |
133 |
] end), |
[assignEin(y, mk.subFT(d,dd),[x2,x1])]), |
134 |
|
(BV.sub_tf, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
135 |
(BV.sub_rf, fn (y, _, [s, f]) => let |
[assignEin(y, mk.subTF(d,dd),xs)]), |
136 |
val f' = IL.Var.copy f |
(BV.mul_ii, simpleOp Op.IMul), |
137 |
in [ |
(BV.mul_rr, fn (y,_,args) => [assignEin(y, mk.mulRR,args)]), |
138 |
IL.ASSGN(f', IL.OP(Op.NegField, [f])), |
(BV.mul_rt, fn (y, [shp], xs) => let |
139 |
IL.ASSGN(y, IL.OP(Op.OffsetField, [f', s])) |
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp |
140 |
] end), |
val rator =mk.mulRT(dd1) |
141 |
(* Changed*) |
in |
142 |
(BV.mul_ii, ScaGenToEin(EinOp.scalarxscalar)), |
[assignEin(y, rator,xs)] |
143 |
(BV.mul_rr, ScaGenToEin(EinOp.scalarxscalar)), |
end), |
144 |
(BV.mul_rt, GenToEin(EinOp.scaleTensor)), (*tensorOp Op.Scale),*) |
(BV.mul_tr, fn (y, [shp], [t, r]) => let |
145 |
|
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp |
146 |
(BV.mul_tr, fn (y, sv, [t, r]) |
val rator = mk.mulRT dd1 |
147 |
=>assignEin(y,createTenEin(EinOp.scaleTensor, sv), [r,t]) |
in |
148 |
|
[assignEin(y, rator,[r,t])] |
149 |
|
end), |
150 |
(BV.mul_rf, fn (y, _, [s, f]) => assignEin(y, createFldEin(EinOp.scaleField,f), [s, f])), |
(BV.mul_rf, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
151 |
(BV.mul_fr, fn (y, _, [f, s]) => assignEin(y, createFldEin(EinOp.scaleField,f), [s, f])), |
[assignEin(y, mk.mulRF(d, dd),xs)]), |
152 |
|
(BV.mul_fr, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [f, s]) => |
153 |
(* UnChanged*) |
[assignEin(y, mk.mulRF(d, dd),[s,f])]), |
154 |
|
(BV.mul_ss, fn (y, [_,Ty.DIM d], xs) => |
155 |
(BV.div_ii, simpleOp(Op.Div DstTy.IntTy)), |
[assignEin(y, mk.mulSS d,xs)]), |
156 |
(BV.div_rr, simpleOp(Op.Div DstTy.realTy)), |
(BV.mul_sf, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
157 |
(BV.div_tr, fn (y, [sv], [x, s]) => let |
[assignEin(y, mk.mulSF(d,dd),xs)]), |
158 |
val one = IL.Var.new("one", DstTy.realTy) |
(BV.mul_fs, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], [s,f]) => |
159 |
val s' = IL.Var.new("sInv", DstTy.realTy) |
[assignEin(y, mk.mulSF(d,dd),[f,s])]), |
160 |
in [ |
(BV.div_ii, simpleOp Op.IDiv), |
161 |
IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))), |
(BV.div_rr, fn (y,_,args) => [assignEin(y, mk.divRR,args)]), |
162 |
IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])), |
(BV.div_tr, fn (y, [shp], xs) => let |
163 |
IL.ASSGN(y, IL.OP(Op.Scale(shapeVarToTensor sv), [s', x])) |
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp |
164 |
] end), |
val rator =mk.divTR dd1 |
165 |
(BV.div_fr, fn (y, _, [f, s]) => let |
in |
166 |
val one = IL.Var.new("one", DstTy.realTy) |
[assignEin(y, rator,xs)] |
167 |
val s' = IL.Var.new("sInv", DstTy.realTy) |
end), |
168 |
in [ |
(BV.div_fr, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
169 |
IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))), |
[assignEin(y, mk.divFR(d, dd),xs)]), |
170 |
IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])), |
(BV.div_fs, fn (y, [_,_,Ty.DIM d, Ty.SHAPE dd], xs) => |
171 |
IL.ASSGN(y, IL.OP(Op.ScaleField, [s', f])) |
[assignEin(y, mk.divFS(d, dd),xs)]), |
172 |
] end), |
(BV.div_ss, fn (y, [_,Ty.DIM d], xs) => |
173 |
|
[assignEin(y, mk.divSS d,xs)]), |
174 |
(BV.exp_ri, simpleOp(Op.Power)), |
(BV.exp_ri, simpleOp(Op.Power)), |
175 |
(BV.exp_rr, basisFn MathFuns.pow), |
(BV.exp_rr, basisFn MathFuns.pow), |
176 |
|
(BV.curl2D, simpleEOp mk.curl2d), |
177 |
(* Changed*) |
(BV.curl3D, simpleEOp mk.curl3d), |
178 |
(BV.curl2D, fn (y, _, xs) => assignEin(y, EinOp.Curl2d, xs)), |
(BV.convolve_vk, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) => |
179 |
(BV.curl3D, fn (y, _, xs) => assignEin(y, EinOp.Curl3d, xs)), |
[assignEin(y, mk.conv(d, dd),xs)]), |
180 |
|
(BV.convolve_kv, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [k, v]) => |
181 |
|
[assignEin(y, mk.conv(d, dd),[v,k])]), |
182 |
(* UnChanged*) |
(BV.neg_i, simpleOp Op.INeg), |
183 |
(BV.convolve_vk, fn (y, [_, dv, _], xs) => |
(BV.neg_t, fn (y, [shp], xs) => let |
184 |
assign(y, Op.Field(pruneDim(MV.toDim dv)), xs)), |
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp |
185 |
(BV.convolve_kv, fn (y, [_, dv, _], [k, v]) => |
val rator =mk.negTT dd1 |
186 |
assign(y, Op.Field(pruneDim(MV.toDim dv)), [v, k])), |
in |
187 |
|
[assignEin(y, rator,xs)] |
188 |
|
end), |
189 |
(BV.neg_i, simpleOp(Op.Neg DstTy.IntTy)), |
(BV.neg_f, fn (y, [_,Ty.DIM d, Ty.SHAPE dd], xs) => |
190 |
(BV.neg_t, tensorOp Op.Neg), |
[assignEin(y, mk.negFF(d, dd),xs)]), |
191 |
(BV.neg_f, fn (y, _, xs) => assign(y, Op.NegField, xs)), |
(BV.op_probe, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) => |
192 |
(BV.op_probe, fn (y, [_, dv, sv], xs) => |
[assignEin(y, (mk.probe(dd,d)),xs)]), |
193 |
assign(y, Op.Probe(dimVarToTensor dv, shapeVarToTensor sv), xs)), |
(BV.op_D, fn (y, [_, Ty.DIM d], xs) => (case d |
194 |
|
of 2=>[assignEin(y, mk.grad([d]),xs)] |
195 |
(* Changed*) |
| 3=>[assignEin(y, mk.grad([d]),xs)] |
196 |
(BV.op_D, fn (y, _, xs) => assignEin(y, createFldEin(EinOp.Grad,xs), xs)), |
| _ => raise Fail "unsupported gradient" |
197 |
(BV.op_Dotimes, fn (y, _, xs) => assignEin(y, createFldEin(EinOp.Divergence,xs), xs)), |
(*end case*))), |
198 |
|
(BV.op_Dotimes, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) => |
199 |
(* UnChanged*) |
[assignEin(y, mk.dotimes(d1, dd@[d2]),xs)]), |
200 |
(BV.op_norm, fn (y, [sv], xs) => (case shapeVarToTensor sv |
(BV.op_Ddot, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) => |
201 |
|
[assignEin(y, mk.divergence(d1, dd),xs)] ), |
202 |
|
(BV.op_norm_t, fn (y, [sv], xs) => let |
203 |
|
val ty=shapeVarToTensor sv |
204 |
|
in (case shapeVarToTensor sv |
205 |
of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs) |
of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs) |
206 |
| ty => assign(y, Op.Norm ty, xs) |
| DstTy.TensorTy [_] => assign(y, Op.Norm ty, xs) |
207 |
|
| DstTy.TensorTy[_,_] => assign(y, Op.Norm ty, xs) |
208 |
|
| DstTy.TensorTy[_,_,_] => assign(y, Op.Norm ty, xs) |
209 |
|
| _ => raise Fail"unsupported norm of TensorTy" |
210 |
|
(* end case *)) |
211 |
|
end), |
212 |
|
(BV.op_norm_f, fn (y, [ _,Ty.DIM d1, Ty.SHAPE dd], xs) => (case dd |
213 |
|
of [] => [assignEin(y, mk.magnitudeFF(d1, dd),xs)] |
214 |
|
| [_] => [assignEin(y, mk.magnitudeFF(d1, dd),xs@xs)] |
215 |
|
| [_,_] => [assignEin(y, mk.magnitudeFF(d1, dd),xs@xs)] |
216 |
|
| _ => raise Fail"unsupported norm of fieldTy" |
217 |
(* end case *))), |
(* end case *))), |
218 |
(BV.op_not, simpleOp Op.Not), |
(BV.op_not, simpleOp Op.Not), |
219 |
|
(BV.op_cross3_tt, simpleEOp mk.cross3TT), |
220 |
|
(BV.op_cross2_tt, simpleEOp mk.cross2TT), |
221 |
|
(BV.op_cross2_ff, simpleEOp mk.cross2FF), |
222 |
|
(BV.op_cross3_ff, simpleEOp mk.cross3FF), |
223 |
|
(BV.op_outer_tt, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) => |
224 |
|
[assignEin(y, (mk.outerTT(d1, d2)), xs)]), |
225 |
|
(BV.op_outer_ff, fn (y, [_, Ty.DIM d1,Ty.DIM i,Ty.DIM j], xs)=> |
226 |
|
[assignEin(y, mk.outerFF(d1,i,j), xs)]), |
227 |
|
(BV.op_inner_tt, fn (y, [sh1, sh2, _], xs) => let |
228 |
|
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1 |
229 |
|
val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2 |
230 |
|
in |
231 |
|
[assignEin(y, (mk.innerTT(dd1,dd2)),xs)] |
232 |
|
end), |
233 |
|
|
234 |
(* Changed*) |
(BV.op_inner_tf, fn (y, [_,Ty.DIM d,sh1,Ty.SHAPE dd2,_], xs) =>let |
235 |
(BV.op_cross, fn(y, _ , xs )=> assignEin(y,EinOp.crossProductE ,xs) |
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1 |
236 |
(BV.op_outer, fn (y, [dv1, dv2], xs) => let |
in |
237 |
val d1 = pruneDim(MV.toDim dv1) |
[assignEin(y, mk.innerTF(dd1,d,dd2),xs)] |
238 |
val d2 = pruneDim(MV.toDim dv2) |
end ), |
239 |
in assignEin( y, S.transform(EinOp.outerProduct,[(length(d1),d1), (length(d2),d2)]),xs) |
(BV.op_inner_ft, fn (y, [_,Ty.DIM d,Ty.SHAPE dd1,sh2,_], xs) =>let |
240 |
end |
val ty1 as DstTy.TensorTy dd2 = shapeVarToTensor sh2 |
241 |
(*Here, d1, d2 is an int, but we want a list.*) |
in |
242 |
|
[assignEin(y, mk.innerFT(dd1,d,dd2),xs)] |
243 |
|
end ), |
244 |
|
|
245 |
|
(BV.op_inner_ff, fn (y, [_,_,Ty.DIM d,Ty.SHAPE dd1,Ty.SHAPE dd2,_], xs) => |
246 |
|
[assignEin(y, mk.innerFF(dd1,d,dd2),xs)]), |
247 |
|
|
248 |
(* Assuming dd1, ddd2 are lists of dimensions*) |
(BV.op_colon_tt, fn (y, [sh1, sh2, _], xs) => let |
249 |
(BV.op_inner, fn (y, [sh1, sh2, _], xs) => let |
val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1 |
250 |
val ty1 as DstTy.TensorTy dd1 = pruneShape sh1 |
val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2 |
251 |
val ty2 as DstTy.TensorTy dd2 = pruneShape sh2 |
in |
252 |
val ilist= [(length(dd1)-1, List.take(dd1, length(dd1)-1) ) , |
[assignEin(y, (mk.colonTT(dd1,dd2)),xs)] |
253 |
( length(dd2)-1, tl(dd2)), |
end), |
254 |
( 0, [hd(dd2)]) ] |
(BV.op_colon_ff, fn (y, [_,Ty.SHAPE dd1,_,Ty.SHAPE dd2, _], xs) => |
255 |
in assignEin(y, S.tranform(EinOp.innerProduct, ilist),xs) end), |
[assignEin(y, (mk.colonFF(dd1,dd2)),xs)]), |
256 |
|
(BV.fn_inside, fn (y, [_, Ty.DIM d, _], xs) => assign(y, Op.Inside d, xs)), |
|
|
|
|
(BV.op_colon, fn (y, [sh1, sh2, _], xs) => let |
|
|
val ty1 as DstTy.TensorTy dd1 = pruneShape sh1 |
|
|
val ty2 as DstTy.TensorTy dd2 = pruneShape sh2 |
|
|
val ilist= [ (length(dd1)-2 , List.take(q, length(q)-2)) , |
|
|
( length(dd2)-2 , tl(tl(dd2))), |
|
|
(_, List.take(dd2,2))] |
|
|
in assignEin(y, S.transform(EinOp.doubleDot, ilist),xs) end), |
|
|
|
|
|
|
|
|
(* UnChanged*) |
|
|
(BV.fn_inside, fn (y, [_, dv, _], xs) => |
|
|
assign(y, Op.Inside(pruneDim(MV.toDim dv)), xs)), |
|
257 |
(BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)), |
(BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)), |
258 |
(BV.clamp_vvv, vectorOp Op.Clamp), |
(BV.clamp_vvv, vectorOp Op.Clamp), |
259 |
(BV.lerp3, tensorOp Op.Lerp), |
(BV.lerp3, tensorOp Op.Lerp), |
262 |
val t2 = IL.Var.new("t2", DstTy.realTy) |
val t2 = IL.Var.new("t2", DstTy.realTy) |
263 |
val t3 = IL.Var.new("t3", DstTy.realTy) |
val t3 = IL.Var.new("t3", DstTy.realTy) |
264 |
in [ |
in [ |
265 |
IL.ASSGN(t1, IL.OP(Op.Sub DstTy.realTy, [x, x0])), |
assignEin(t1, mk.subRR,[x,x0]), |
266 |
IL.ASSGN(t2, IL.OP(Op.Sub DstTy.realTy, [x1, x0])), |
assignEin(t2, mk.subRR,[x1,x0]), |
267 |
IL.ASSGN(t3, IL.OP(Op.Div DstTy.realTy, [t1, t2])), |
assignEin(t3, mk.divRR,[t1,t2]), |
268 |
IL.ASSGN(y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3])) |
IL.ASSGN(y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3])) |
269 |
] end), |
] end), |
270 |
(BV.evals2x2, eigenVal (Op.Eigen2x2, 2)), |
(BV.evals2x2, eigenVal (Op.Eigen2x2, 2)), |
271 |
(BV.evals3x3, eigenVal (Op.Eigen3x3, 3)), |
(BV.evals3x3, eigenVal (Op.Eigen3x3, 3)), |
272 |
(BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)), |
(BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)), |
273 |
(BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)), |
(BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)), |
274 |
|
|
275 |
|
|
276 |
(BV.fn_max, simpleOp Op.Max), |
(BV.fn_max, simpleOp Op.Max), |
277 |
(BV.fn_min, simpleOp Op.Min), |
(BV.fn_min, simpleOp Op.Min), |
|
(BV.fn_modulate, vectorOp Op.Mul), |
|
|
(BV.fn_normalize, vectorOp Op.Normalize), |
|
|
(BV.fn_principleEvec, vectorOp Op.PrincipleEvec), |
|
278 |
|
|
279 |
(* Changed*) |
(* modulate is vector * vector pointwise multiplication *) |
280 |
(BV.fn_trace, fn (y, [dv], xs) => (let |
(BV.fn_modulate, fn (y,[Ty.DIM dd1], xs) => |
281 |
val i=pruneDim(MV.toDim dv) (* (dimVarToMatrix dv)*) |
[assignEin(y, (mk.modulate dd1),xs)]), |
282 |
(*we need i to be the dimension n of an n xn matrix*) |
(BV.fn_normalize_t, vectorOp Op.Normalize), |
283 |
in assignEin(y,S.transform(EinOp.trace, [1,i]),xs) end) |
(BV.fn_normalize_f, fn (y, [ _,Ty.DIM d1, Ty.SHAPE dd], xs) => |
284 |
|
(case dd |
285 |
(* UnChanged*) |
of [] => [assignEin(y, mk.normalizeFF(d1, []),xs)] |
286 |
(BV.fn_transpose, fn (y, [dv1, dv2], xs) => |
| [i] => [assignEin(y, mk.normalizeFF(d1, [i]),xs@xs)] |
287 |
assign(y, Op.Transpose(dimVarToInt dv1, dimVarToInt dv2), xs)), |
| _ => raise Fail"unsupported normalized of fieldTy" |
288 |
|
(* end case *))), |
289 |
|
(BV.fn_principleEvec, vectorOp Op.PrincipleEvec), |
290 |
|
(BV.fn_trace_t, fn (y, [Ty.DIM d], xs) => |
291 |
|
[assignEin(y,(mk.traceT d), xs)]), |
292 |
|
(BV.fn_trace_f, fn (y, [_,Ty.DIM d,Ty.SHAPE dd], xs) => |
293 |
|
[assignEin(y,mk.traceF(d,dd), xs)]), |
294 |
|
(BV.fn_transpose_t, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) => |
295 |
|
[assignEin(y, (mk.transposeT [d1,d2]), xs)]), |
296 |
|
(BV.fn_transpose_f, fn (y, [_,Ty.DIM d1, Ty.DIM d2,Ty.DIM d3], xs) => |
297 |
|
[assignEin(y, (mk.transposeF (d1,d2,d3)), xs)]), |
298 |
(BV.kn_bspln3, kernel Kernel.bspln3), |
(BV.kn_bspln3, kernel Kernel.bspln3), |
299 |
(BV.kn_bspln5, kernel Kernel.bspln5), |
(BV.kn_bspln5, kernel Kernel.bspln5), |
300 |
(BV.kn_ctmr, kernel Kernel.ctmr), |
(BV.kn_ctmr, kernel Kernel.ctmr), |
303 |
(BV.kn_tent, kernel Kernel.tent), |
(BV.kn_tent, kernel Kernel.tent), |
304 |
(BV.kn_c1tent, kernel Kernel.tent), |
(BV.kn_c1tent, kernel Kernel.tent), |
305 |
(BV.i2r, simpleOp Op.IntToReal), |
(BV.i2r, simpleOp Op.IntToReal), |
306 |
|
(BV.identity, fn (y, [Ty.DIM d], xs) => |
307 |
(* Changed*) |
[assignEin(y, (mk.identity d), xs)]), |
|
(BV.identity, fn (y, [dv], []) =>(let |
|
|
val i=pruneDim(MV.toDim dv) |
|
|
(*we need i to be the dimension n of an n xn matrix*) |
|
|
in assignEin(y, S.transform(EinOp.identity,[(1,i),(1,i)]),[]) end)), |
|
|
|
|
|
(* UnChanged*) |
|
308 |
(BV.zero, fn (y, [sv], []) => |
(BV.zero, fn (y, [sv], []) => |
309 |
assign(y, Op.Zero(shapeVarToTensor sv), [])), |
assign(y, Op.Zero(shapeVarToTensor sv), [])), |
310 |
(BV.subscript, fn (y, [tv, dv], args) => |
(BV.subscript, fn (y, [ty, Ty.DIM d], xs) => |
311 |
assign(y, |
assign (y, Op.SeqSub(DstTy.SeqTy(trType ty, d)), xs)) |
312 |
Op.SeqSub(DstTy.SeqTy(pruneTy tv, pruneDim(MV.toDim dv))), |
(*, |
313 |
args)) |
(BV.dynSubscript, fn (y, [tv], args) => |
314 |
|
assign(y, Op.SeqSub(DstTy.DynSeqTy(pruneTy tv)), args)) |
315 |
|
*) |
316 |
]; |
]; |
317 |
(* add C math functions *) |
(* add C math functions *) |
318 |
List.app (fn (n, x) => insert(x, basisFn n)) BV.mathFns; |
List.app (fn (n, x) => insert(x, basisFn n)) BV.mathFns; |