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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 139 - (view) (download)

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

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