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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 342 - (view) (download)

1 : jhr 342 (* partials.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * A symbolic representation of partial derivative operators.
7 :     *)
8 :    
9 :     structure Partials :> sig
10 :    
11 :     eqtype axis (* abstract representation of axis *)
12 :     val axisToString : axis -> string (* string representation of axis name *)
13 :     val axis : int -> axis (* return ith axis name (0 base) *)
14 :     val index : axis -> int (* axis index *)
15 :    
16 :     (* representation of a partial derivative operator, where the length of the
17 :     * list defines the dimension of the space and each element specifies the
18 :     * number of levels of differentiation along the corresponding axis.
19 :     * For example, the operator d/dx in 2D space is represented by D[1, 0]
20 :     * and the operator d/dxdz in 3D is represented by D[1, 0, 1].
21 :     *)
22 :     datatype partial = D of int list
23 :    
24 :     val partialToString : partial -> string
25 :    
26 :     (* the identity for the given dimension *)
27 :     val ident : int -> partial
28 :    
29 :     (* derivative for the given dimension and axis *)
30 :     val del : int -> axis -> partial
31 :    
32 :     (* multiply two partial derivative operators *)
33 :     val multiply : (partial * partial) -> partial
34 :    
35 :     (* the partial derivative operator for a given dimension. Specifically,
36 :     *
37 :     * partial d index
38 :     *
39 :     * returns the partial derivative in the tensor of partial derivatives that
40 :     * is generated by differentiating k times (k = length index). For example,
41 :     *
42 :     * partial 2 [] = ID
43 :     * partial 2 [0] = d/dx
44 :     * partial 2 [1] = d/dy
45 :     * partial 2 [0, 0] = d/dx^2
46 :     * partial 2 [0, 1] = d/dxdy
47 :     * partial 2 [1, 0] = d/dxdy
48 :     * partial 2 [1, 1] = d/dy^2
49 :     *)
50 :     val partial : int -> axis list -> partial
51 :    
52 :     end = struct
53 :    
54 :     type axis = int
55 :    
56 :     val maxAxis = 3 (* upto 4 dimensions supported *)
57 :    
58 :     fun axisToString i = String.substring("xyzw", i, 1)
59 :    
60 :     fun axis i = if (i < 0) orelse (maxAxis < i) then raise Size else i
61 :    
62 :     fun index (i : axis) = i
63 :    
64 :     datatype partial = D of int list
65 :    
66 :     fun partialToString (D l) = let
67 :     fun f (a, []) = []
68 :     | f (a, 0::r) = f(a+1, r)
69 :     | f (a, 1::r) = "d" :: axisToString(axis a) :: f(a+1, r)
70 :     | f (a, i::r) = "d" :: axisToString(axis a) :: Int.toString i :: f(a+1, r)
71 :     in
72 :     case f (0, l)
73 :     of [] => "ID"
74 :     | l => String.concat("d/" :: l)
75 :     (* end case *)
76 :     end
77 :    
78 :     fun uncheckedIdent d = D(List.tabulate(d, fn _ => 0))
79 :     fun uncheckedDel d axis = D(List.tabulate(d, fn i => if i = axis then 1 else 0))
80 :    
81 :     fun ident d = if (d < 0) orelse (maxAxis < d)
82 :     then raise Size
83 :     else uncheckedIdent d
84 :    
85 :     fun del d = if (d < 0) orelse (maxAxis < d)
86 :     then raise Size
87 :     else fn axis => if (d < axis)
88 :     then raise Subscript
89 :     else uncheckedDel d axis
90 :    
91 :     (* multiply two partial derivative operators *)
92 :     fun multiply (D l1, D l2) = D(ListPair.mapEq (fn (i, j) => (i+j)) (l1, l2))
93 :    
94 :     fun partial dim = let
95 :     val del = uncheckedDel dim
96 :     fun loop ([], dd) = dd
97 :     | loop (a::r, dd) = loop (r, multiply(del a, dd))
98 :     in
99 :     if (dim < 0) orelse (maxAxis < dim)
100 :     then raise Size
101 :     else fn index => loop (index, uncheckedIdent dim)
102 :     end
103 :    
104 :     end

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