Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /trunk/src/compiler/fields/kernel.sml
ViewVC logotype

Annotation of /trunk/src/compiler/fields/kernel.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 197 - (view) (download)
Original Path: trunk/src/compiler/IL/kernel.sml

1 : jhr 108 (* kernel.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 : jhr 117 *
6 :     * QUESTION: should we
7 : jhr 108 *)
8 :    
9 :     structure Kernel : sig
10 :    
11 : jhr 117 type coefficient = Rational.rat
12 : jhr 108
13 :     (* polynomial represented as list of coefficients, where ith element is
14 :     * coefficient for x^i.
15 :     *)
16 :     type polynomial = coefficient list
17 :    
18 : jhr 139 type kernel
19 : jhr 108
20 : jhr 139 (* kernel name *)
21 :     val name : kernel -> string
22 : jhr 195 val toString : kernel -> string
23 : jhr 108
24 : jhr 165 (* are two kernels the same *)
25 :     val same : kernel * kernel -> bool
26 :    
27 :     (* hash value *)
28 :     val hash : kernel -> word
29 :    
30 : jhr 139 (* kernel support *)
31 :     val support : kernel -> int
32 :    
33 :     (* representation of i'th derivative of the kernel *)
34 :     val curve : kernel * int -> {
35 :     isOdd : bool,
36 :     isCont : bool,
37 :     segs : polynomial list (* piece-wise polynomial that defines *)
38 :     (* the curve over the positive support *)
39 :     }
40 :    
41 : jhr 117 val evaluate : polynomial * int -> Rational.rat
42 : jhr 108
43 : jhr 153 (* some standard kernels *)
44 :     val tent : kernel (* linear interpolation *)
45 :     val ctmr : kernel (* Catmull-Rom interpolation *)
46 : jhr 197 val bspln3 : kernel (* cubic bspline reconstruction, doesn't interpolate *)
47 :     val bspln5 : kernel (* quintic bspline reconstruction, doesn't interpolate *)
48 : jhr 153
49 : jhr 108 end = struct
50 :    
51 : jhr 117 structure R = Rational
52 : jhr 139 structure A = Array
53 : jhr 108
54 : jhr 139 val maxDiffLevels = 15 (* support upto 15 levels of differentiation *)
55 :    
56 : jhr 117 type coefficient = R.rat
57 : jhr 108
58 : jhr 153 val zero = R.zero
59 : jhr 117 val one = R.fromInt 1
60 :    
61 : jhr 108 (* polynomial represented as list of coefficients, where ith element is
62 :     * coefficient for x^i.
63 :     *)
64 :     type polynomial = coefficient list
65 :    
66 :     fun differentiate [] = raise Fail "invalid polynomial"
67 : jhr 117 | differentiate [_] = [zero]
68 : jhr 108 | differentiate (_::coeffs) = let
69 :     fun lp (_, []) = []
70 : jhr 139 | lp (i, c::r) = R.*(R.fromInt i, c) :: lp(i+1, r)
71 : jhr 108 in
72 :     lp (1, coeffs)
73 :     end
74 :    
75 :     (* evaluate a polynomial at an integer coordinate (used to test continuity) *)
76 :     fun evaluate (poly, x) = let
77 : jhr 139 val x = R.fromInt x
78 : jhr 108 fun eval (sum, [], xn) = sum
79 : jhr 139 | eval (sum, c::r, xn) = eval(R.+(sum, R.*(c, xn)), r, R.*(x, xn))
80 : jhr 108 in
81 : jhr 117 eval (zero, poly, one)
82 : jhr 108 end
83 :    
84 : jhr 139 type curve = {
85 :     isOdd : bool,
86 :     isCont : bool,
87 :     segs : polynomial list (* piece-wise polynomial that defines *)
88 :     (* the curve over the positive support *)
89 :     }
90 :    
91 :     datatype kernel = K of {
92 :     name : string,
93 : jhr 165 id : Stamp.stamp, (* unique ID *)
94 : jhr 139 support : int, (* number of samples to left/right *)
95 :     curves : curve option array (* cache of curves indexed by differentiation level *)
96 :     }
97 :    
98 :     (* determine if a list of polynomials represents a continuous piece-wise polynomial *)
99 : jhr 153 fun isContinuous [_] = true
100 :     | isContinuous (f0::r) = let
101 : jhr 139 fun chk (i, f_i, []) = (R.zero = evaluate(f_i, i))
102 :     | chk (i, f_i, f_i1::r) = let
103 :     val y_i = evaluate(f_i, i)
104 :     val y_i1 = evaluate(f_i1, i)
105 :     in
106 : jhr 155 (y_i = y_i1) andalso chk(i+1, f_i1, r)
107 : jhr 139 end
108 :     in
109 : jhr 153 chk (1, f0, r)
110 : jhr 139 end
111 :    
112 :     (* kernel name *)
113 :     fun name (K{name, ...}) = name
114 : jhr 195 val toString = name
115 : jhr 139
116 :     (* kernel support *)
117 :     fun support (K{support, ...}) = support
118 :    
119 : jhr 165 fun hash (K{id, ...}) = Stamp.hash id
120 :     fun same (K{id=a, ...}, K{id=b, ...}) = Stamp.same (a, b)
121 :    
122 : jhr 139 (* representation of i'th derivative of the kernel *)
123 :     fun curve (K{curves, ...}, k) = (case A.sub(curves, k)
124 :     of SOME curve => curve
125 :     | NONE => let
126 :     (* compute the (k+1)'th derivative, given the k'th *)
127 :     fun diff (k, {isOdd, isCont, segs}) = let
128 :     val segs' = List.map differentiate segs
129 : jhr 153 val isOdd' = not isOdd
130 :     val isCont' = if isCont andalso isContinuous segs'
131 :     then (not isOdd') orelse (evaluate(List.hd segs', 0) = R.zero)
132 :     else false
133 : jhr 139 in {
134 : jhr 153 isOdd = isOdd',
135 :     isCont = isCont',
136 : jhr 139 segs = segs'
137 :     } end
138 :     fun lp (j, curve) = if (j < k)
139 :     then (case A.sub(curves, j+1)
140 :     of NONE => let
141 :     val curve' = diff(j+1, curve)
142 :     in
143 :     A.update(curves, j+1, SOME curve');
144 :     lp (j+1, curve')
145 :     end
146 :     | SOME curve' => lp(j+1, curve')
147 :     (* end case *))
148 :     else curve
149 :     in
150 :     lp (0, valOf(A.sub(curves, 0)))
151 :     end
152 :     (* end case *))
153 :    
154 : jhr 117 (* some standard kernels *)
155 :     local
156 :     val op / = R./
157 :     fun r i = R.fromInt i
158 : jhr 139 fun mkKernel {name, support, segs} = let
159 :     val curves = Array.array(maxDiffLevels+1, NONE)
160 :     val curve0 = {
161 :     isOdd = false,
162 :     isCont = isContinuous segs,
163 :     segs = segs
164 :     }
165 :     in
166 :     A.update (curves, 0, SOME curve0);
167 : jhr 165 K{name=name, id=Stamp.new(), support=support, curves=curves}
168 : jhr 139 end
169 : jhr 117 in
170 : jhr 139 val tent : kernel = mkKernel{ (* linear interpolation *)
171 : jhr 135 name = "tent",
172 : jhr 117 support = 1,
173 :     segs = [[r 1, r ~1]]
174 :     }
175 : jhr 139 val ctmr : kernel = mkKernel{ (* Catmull-Rom interpolation *)
176 : jhr 135 name = "ctmr",
177 : jhr 117 support = 2,
178 :     segs = [
179 :     [r 1, r 0, ~5/2, 3/2],
180 :     [r 2, r ~4, 5/2, ~1/2]
181 :     ]
182 :     }
183 : jhr 197 val bspln3 : kernel = mkKernel{ (* cubic bspline reconstruction, doesn't interpolate *)
184 :     name = "bspln3",
185 : jhr 117 support = 2,
186 :     segs = [
187 : jhr 122 [ 2/3, r 0, r ~1, 1/2 ],
188 : jhr 135 [ 4/3, r ~2, r 1, ~1/6 ]
189 : jhr 117 ]
190 :     }
191 : jhr 197 val bspln5 : kernel = mkKernel{ (* quintic bspline reconstruction, doesn't interpolate *)
192 :     name = "bspln5",
193 : jhr 117 support = 3,
194 :     segs = [
195 :     [ 11/20, r 0, ~1/2, r 0, 1/4, ~1/12 ],
196 : jhr 127 [ 17/40, 5/8, ~7/4, 5/4, ~3/8, 1/24 ],
197 : jhr 117 [ 81/40, ~27/8, 9/4, ~3/4, 1/8, ~1/120 ]
198 :     ]
199 :     }
200 :     end
201 :    
202 : jhr 108 end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0