-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
metric.cljc
330 lines (298 loc) · 10.2 KB
/
metric.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
;;
;; Copyright © 2021 Sam Ritchie.
;; This work is based on the Scmutils system of MIT/GNU Scheme:
;; Copyright © 2002 Massachusetts Institute of Technology
;;
;; This is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or (at
;; your option) any later version.
;;
;; This software is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this code; if not, see <http://www.gnu.org/licenses/>.
;;
(ns sicmutils.calculus.metric
(:require [sicmutils.calculus.basis :as b]
[sicmutils.calculus.coordinate :as coord]
[sicmutils.calculus.derivative :refer [D]]
[sicmutils.calculus.form-field :as ff]
[sicmutils.calculus.indexed :as ci]
[sicmutils.calculus.vector-field :as vf]
[sicmutils.calculus.manifold :as m]
[sicmutils.function :as f]
[sicmutils.generic :as g]
[sicmutils.matrix :as matrix]
[sicmutils.structure :as s :refer [up down]]
[sicmutils.value :as v]))
;; ## Metrics
;; A metric is a function that takes two vector fields and produces a function
;; on the manifold.
(defn embedding-map->metric-components
[n xi->rectangular]
(let [h (D xi->rectangular)
ref (fn [f k]
(f/compose #(get % k) f))]
(if (= n 1)
(down (down (g/dot-product h h)))
(s/generate
n ::s/down
(fn [i]
(s/generate
n ::s/down
(fn [j]
(g/dot-product (ref h i)
(ref h j)))))))))
(defn coordinate-system->metric-components [coordsys]
(let [n (:dimension (m/manifold coordsys))
;; assumes internal rectangular representation
xi->x (f/compose m/manifold-point-representation
(m/point coordsys))]
(embedding-map->metric-components n xi->x)))
(defn coordinate-system->metric [coordinate-system]
(let [basis (b/coordinate-system->basis coordinate-system)
oneform-basis (b/basis->oneform-basis basis)
->components (coordinate-system->metric-components
coordinate-system)
Chi (m/chart coordinate-system)]
(letfn [(the-metric [v1 v2]
(fn [m]
(let [gcoeffs (->components (Chi m))]
(g/* (g/* gcoeffs ((oneform-basis v1) m))
((oneform-basis v2) m)))))]
(ci/with-argument-types
the-metric
[::vf/vector-field
::vf/vector-field]))))
(defn coordinate-system->inverse-metric [coordinate-system]
(let [basis (b/coordinate-system->basis coordinate-system)
vector-basis (b/basis->vector-basis basis)
->components
(g// 1 (coordinate-system->metric-components
coordinate-system))
Chi (m/chart coordinate-system)]
(letfn [(the-inverse-metric [w1 w2]
(fn [m]
(let [gcoeffs (->components (Chi m))]
(g/* (g/* gcoeffs
(s/mapr (fn [e] ((w1 e) m))
vector-basis))
(s/mapr (fn [e] ((w2 e) m))
vector-basis)))))]
(ci/with-argument-types
the-inverse-metric
[::ff/oneform-field
::ff/oneform-field]))))
;; Symbolic metrics are often useful for testing.
(defn- make-metric [name coordinate-system]
(fn gij [i j]
(if (<= i j)
(m/literal-manifold-function
(symbol (str name "_" i j))
coordinate-system)
(gij j i))))
(defn literal-metric
"Flat coordinate systems here only."
[name coordinate-system]
(let [basis (b/coordinate-system->basis coordinate-system)
oneform-basis (b/basis->oneform-basis basis)
gij (make-metric name coordinate-system)
n (g/dimension oneform-basis)
gcoeffs (s/generate
n ::s/down
(fn [i]
(s/generate
n ::s/down
(fn [j]
(gij i j)))))]
(letfn [(the-metric [v1 v2]
(g/* (g/* gcoeffs (oneform-basis v1))
(oneform-basis v2)))]
(ci/with-argument-types
the-metric
[::vf/vector-field
::vf/vector-field]))))
(defn components->metric [components basis]
(let [oneform-basis (b/basis->oneform-basis basis)]
(fn the-metric [v1 v2]
(g/* (oneform-basis v1)
(g/* components (oneform-basis v2))))))
(defn metric->components [metric basis]
(let [vector-basis (b/basis->vector-basis basis)]
(s/mapr (fn [e_i]
(s/mapr (fn [e_j]
(metric e_i e_j))
vector-basis))
vector-basis)))
(defn metric->inverse-components
"Given a metric and a basis, computes the inverse metric."
[metric basis]
(fn the-coeffs [m]
(let [g_ij ((metric->components metric basis) m)
oneform-basis (b/basis->oneform-basis basis)
typical (s/typical-object oneform-basis)]
(matrix/s:inverse typical g_ij typical))))
(defn invert [metric basis]
(letfn [(the-inverse-metric [w1 w2]
(let [vector-basis (b/basis->vector-basis basis)
g-ij (metric->inverse-components metric basis)]
(g/* (g/* g-ij (s/mapr w1 vector-basis))
(s/mapr w2 vector-basis))))]
(ci/with-argument-types
the-inverse-metric
[::ff/oneform-field
::ff/oneform-field])))
;; Over a map...
(defn metric-over-map [mu:N->M g-on-M]
(letfn [(make-fake-vector-field [V-over-mu n]
(vf/procedure->vector-field
(fn [f]
(fn [_]
((V-over-mu f) n)))
`(~'make-fake-vector-field
~(v/freeze V-over-mu))))
(the-metric [v1 v2]
(fn [n]
((g-on-M
(make-fake-vector-field v1 n)
(make-fake-vector-field v2 n))
(mu:N->M n))))]
(ci/with-argument-types
the-metric
[::vf/vector-field
::vf/vector-field])))
;; ### Raising and lowering indices
(defn lower
"To make a vector field into a one-form field, ie, a (1,0) tensor into a (0,1)
tensor."
[metric]
(fn [u]
(letfn [(omega [v]
(metric v u))]
(ff/procedure->oneform-field
omega
`(~'lower
~(v/freeze u)
~(v/freeze metric))))))
(def ^{:doc "Alias for [[lower]]."}
vector-field->oneform-field
lower)
(def ^{:doc "Alias for [[lower]]."}
drop1
lower)
(defn raise
"To make a one-form field into a vector field, ie, a (0,1) tensor into a (1,0)
tensor."
[metric basis]
(let [gi (invert metric basis)]
(fn [omega]
(let [v (b/contract
(fn [vf-i ff-i]
(g/* (gi omega ff-i) vf-i))
basis)]
(vf/procedure->vector-field
v
`(~'raise
~(v/freeze omega)
~(v/freeze metric)))))))
(def ^{:doc "Alias for [[raise]]."}
oneform-field->vector-field
raise)
(def ^{:doc "Alias for [[raise]]."}
raise1
raise)
(defn drop2
"For making a (2,0) tensor into a (0,2) tensor."
[metric-tensor basis]
(fn [tensor]
(letfn [(omega [v1 v2]
(b/contract
(fn [e1 w1]
(b/contract
(fn [e2 w2]
(g/* (metric-tensor v1 e1)
(tensor w1 w2)
(metric-tensor e2 v2)))
basis))
basis))]
(ci/with-argument-types
omega
[::vf/vector-field
::vf/vector-field]))))
(defn raise2
"For making a (0,2) tensor into a (2,0) tensor."
[metric-tensor basis]
(let [gi (invert metric-tensor basis)]
(fn [tensor02]
(letfn[(v2 [omega1 omega2]
(b/contract
(fn [e1 w1]
(b/contract
(fn [e2 w2]
(g/* (gi omega1 w1)
(tensor02 e1 e2)
(gi w2 omega2)))
basis))
basis))]
(ci/with-argument-types
v2
[::ff/oneform-field
::ff/oneform-field])))))
(defn trace2down
"Computes the trace of a (0,2) tensor."
[metric-tensor basis]
(let [inverse-metric-tensor (invert metric-tensor basis)]
(fn [tensor02]
(let [f (b/contract
(fn [e1 w1]
(b/contract
(fn [e2 w2]
(g/* (inverse-metric-tensor w1 w2)
(tensor02 e1 e2)))
basis))
basis)]
(ci/with-argument-types
f
[::v/function])))))
(defn trace2up
"Computes the trace of a (2,0) tensor"
[metric-tensor basis]
(fn [tensor20]
(let [f (b/contract
(fn [e1 w1]
(b/contract
(fn [e2 w2]
(g/* (metric-tensor e1 e2)
(tensor20 w1 w2)))
basis))
basis)]
(ci/with-argument-types
f
[::v/function]))))
;; Unfortunately raise is very expensive because the matrix is
;; inverted for each manifold point.
(defn sharpen [metric basis m]
(let [g-ij ((metric->inverse-components metric basis) m)
vector-basis (b/basis->vector-basis basis)
oneform-basis (b/basis->oneform-basis basis)]
(fn sharp [oneform-field]
(let [oneform-coeffs
(s/mapr (fn [ei] ((oneform-field ei) m))
vector-basis)
vector-coeffs (g/* g-ij oneform-coeffs)]
(s/sumr g/* vector-coeffs vector-basis)))))
;; ## Useful metrics
(def S2-metric
(let [[theta phi] (coord/coordinate-functions m/S2-spherical)
[dtheta dphi] (ff/coordinate-system->oneform-basis m/S2-spherical)]
(-> (fn the-metric [v1 v2]
(g/+ (g/* (dtheta v1) (dtheta v2))
(g/* (g/expt (g/sin theta) 2)
(dphi v1) (dphi v2))))
(ci/with-argument-types
[::vf/vector-field
::vf/vector-field]))))