src/Metaprogramming.jl

Wed, 22 Dec 2021 11:13:38 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 22 Dec 2021 11:13:38 +0200
changeset 37
f8be66557e0f
parent 35
d881275c6564
permissions
-rw-r--r--

Planar finite elements, simple linear solvers for fixed dimensions

35
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 ##################
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 # Metaprogramming
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 ##################
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 """
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 `module AlgTools.MetaProgramming`
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 This module implements helpers for metaprogramming, used, e.g., by `Loops`.
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 It includes the macros `@foldl`, `@all`, and `@map` and the metaprogramming
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 functions `foldl_exprs`, `all_exprs`, `map_exprs`, `lift_exprs`, and `sequence_exprs`.
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 """
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 module Metaprogramming
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 ##############
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 # Our exports
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 ##############
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 export @foldl,
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 @all,
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 @map,
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 foldl_exprs,
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 all_exprs,
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 map_exprs,
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 lift_exprs,
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 sequence_exprs
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 ###########################################
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 # Support functions for macro construction
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 ###########################################
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 """"
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 `foldl_exprs(f, a)`
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 Does `foldl` on the `Expr`s in `a` with the `f`, a `Function` or another `Expr`
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 representing a function. Returns new `Expr` with the expanded `foldl` operation.
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 """
37
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 35
diff changeset
38 function foldl_exprs(f :: Union{Expr,Symbol}, a)
35
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 return foldl((b, c) -> :($f($b, $c)), a)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 end
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 function foldl_exprs(f :: Function, a)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 return foldl((b, c) -> :($(f(b, c))), a)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 end
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 """"
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 `all_exprs(a...)`
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 Does `all` on the `Expr`s in `a`.
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 Returns new `Expr` with the expanded `all` operation.
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 """
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 function all_exprs(a)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 return foldl((b, c) -> :( $b && $c ), a)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 end
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 """"
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 `map_exprs(f, a...)`
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 Does `map` on the `Expr`s in `a` with the `f`, a `Function` or another `Expr`
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 representing a function. Returns a new `NTuple` with `Expr`s for the result of
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 applying a to all elements of `a`.
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 """
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63 function map_exprs(f :: Expr , a)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 return ([ :( $f($b) ) for b ∈ a ]...,)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65 end
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
66
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 function map_exprs(f :: Function , a)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68 return ([ :( $(f(b)) ) for b ∈ a ]...,)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 end
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
70
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
71 """"
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
72 `lift_exprs(a)`
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
73
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
74 Turns a `Tuple` `a` of `Expr`s into an `Expr` containing `Tuple`.
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
75 """
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76 function lift_exprs(a)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
77 return :( ($(a...),) )
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
78 end
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
79
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
80 """
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
81 `sequence_exprs(a)`
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
82
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83 Turns a `Tuple` `a` of `Expr`s into an `Expr` with each original expr on its own line (separated by `;`).
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84 """
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
85 function sequence_exprs(a)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
86 return foldl_exprs((b, c) -> :( $b; $c ), a)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
87 end
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
88
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
89
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
90 #########
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
91 # Macros
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
92 #########
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
93
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
94 """"
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
95 `@foldl(f, a...)`
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
96
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
97 A basic macro version of `foldl`.
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
98 """
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
99 macro foldl(f, a...)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
100 return foldl_exprs(f, a)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
101 end
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
102
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
103 """"
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
104 `@all(a...)`
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
105
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
106 A macro version of `all`.
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
107 """
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
108 macro all(a...)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
109 # Implementation based on @foldl:
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
110 #return :( @foldl((b, c) -> b && c, $(a...)) )
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
111 # Direct implementation produces prettier generated code:
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
112 return all_exprs(a)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
113 end
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
114
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
115 """"
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
116 `map(f, a...)`
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
117
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
118 A macro version of `map`.
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
119 """
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
120 macro map(f, a...)
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
121 return lift_exprs(map_exprs(f, a))
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
122 end
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
123
d881275c6564 Add metaprogramming tools and fast multidimensional loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
124 end # module

mercurial