-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
function.cljc
322 lines (285 loc) · 12.2 KB
/
function.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
;;
;; Copyright © 2020 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.abstract.function
"Implementation of a [[literal-function]] constructor. Literal functions can be
applied to structures and numeric inputs, and differentiated.
The namespace also contains an implementation of a small language for
declaring the input and output types of [[literal-function]] instances."
(:refer-clojure :exclude [name])
(:require [sicmutils.abstract.number :as an]
[sicmutils.differential :as d]
[sicmutils.expression :as x]
[sicmutils.function :as f]
[sicmutils.generic :as g]
[sicmutils.matrix :as m]
[sicmutils.numsymb :as sym]
[sicmutils.polynomial]
[sicmutils.structure :as s]
[sicmutils.util :as u]
[sicmutils.value :as v])
#?(:clj
(:import (clojure.lang IFn))))
;; ## Abstract Function
;;
;; This namespace declares an abstract function type, along with the support
;; structure to process the scmutils domain/range language.
(declare literal-apply f:=)
;; This derivation allows `::function` to take advantage of all generic
;; operations installed via [[sicmutils.function]].
(derive ::function ::v/function)
(defn ^:private sicm-set->exemplar
"Convert a SICM-style set (e.g., Real or (UP Real Real)) to
an exemplar (an instance of the relevant type)."
[s]
(cond
(= s 'Real) 0
(sequential? s)
(let [[constructor & args] s]
(case constructor
X (mapv sicm-set->exemplar args)
UP (apply s/up (map sicm-set->exemplar args))
DOWN (apply s/down (map sicm-set->exemplar args))
UP* (apply s/up (repeat (second args) (sicm-set->exemplar (first args))))
DOWN* (apply s/down (repeat (second args) (sicm-set->exemplar (first args))))
X* (into [] (repeat (second args) (sicm-set->exemplar (first args))))))))
(defn ^:no-doc sicm-signature->domain-range
"Convert a SICM-style literal function signature (e.g.,
'(-> Real (X Real Real)) ) to our 'exemplar' format."
[[arrow domain range]]
(when-not (and (= '-> arrow) domain range)
(u/illegal (str "A SICM signature is of the form '(-> domain range), got: " arrow domain range)))
[(let [d (sicm-set->exemplar domain)]
(if (vector? d) d [d]))
(sicm-set->exemplar range)])
(deftype Function [name arity domain range]
v/Value
(zero? [_] false)
(one? [_] false)
(identity? [_] false)
(zero-like [_] (fn [& _] (v/zero-like range)))
(one-like [_] (fn [& _] (v/one-like range)))
(identity-like [_]
(let [meta {:arity arity :from :identity-like}]
(with-meta identity meta)))
(exact? [f] (f/compose v/exact? f))
(freeze [_] (v/freeze name))
(kind [_] ::function)
f/IArity
(arity [_] arity)
Object
(toString [_] (str name))
#?(:clj (equals [a b] (f:= a b)))
#?@(:clj
[IFn
(invoke [this x] (literal-apply this [x]))
(invoke [this x y] (literal-apply this [x y]))
(invoke [this x y z] (literal-apply this [x y z]))
(invoke [this w x y z] (literal-apply this [w x y z]))
(applyTo [this xs] (literal-apply this xs))]
:cljs
[IEquiv
(-equiv [a b] (f:= a b))
IPrintWithWriter
(-pr-writer [x writer _]
(write-all writer (.toString x)))
IFn
(-invoke [this a]
(literal-apply this [a]))
(-invoke [this a b]
(literal-apply this [a b]))
(-invoke [this a b c]
(literal-apply this [a b c]))
(-invoke [this a b c d]
(literal-apply this [a b c d]))
(-invoke [this a b c d e]
(literal-apply this [a b c d e]))
(-invoke [this a b c d e f]
(literal-apply this [a b c d e f]))
(-invoke [this a b c d e f g]
(literal-apply this [a b c d e f g]))
(-invoke [this a b c d e f g h]
(literal-apply this [a b c d e f g h]))
(-invoke [this a b c d e f g h i]
(literal-apply this [a b c d e f g h i]))
(-invoke [this a b c d e f g h i j]
(literal-apply this [a b c d e f g h i j]))
(-invoke [this a b c d e f g h i j k]
(literal-apply this [a b c d e f g h i j k]))
(-invoke [this a b c d e f g h i j k l]
(literal-apply this [a b c d e f g h i j k l]))
(-invoke [this a b c d e f g h i j k l m]
(literal-apply this [a b c d e f g h i j k l m]))
(-invoke [this a b c d e f g h i j k l m n]
(literal-apply this [a b c d e f g h i j k l m n]))
(-invoke [this a b c d e f g h i j k l m n o]
(literal-apply this [a b c d e f g h i j k l m n o]))
(-invoke [this a b c d e f g h i j k l m n o p]
(literal-apply this [a b c d e f g h i j k l m n o p]))
(-invoke [this a b c d e f g h i j k l m n o p q]
(literal-apply this [a b c d e f g h i j k l m n o p q]))
(-invoke [this a b c d e f g h i j k l m n o p q r]
(literal-apply this [a b c d e f g h i j k l m n o p q r]))
(-invoke [this a b c d e f g h i j k l m n o p q r s]
(literal-apply this [a b c d e f g h i j k l m n o p q r s]))
(-invoke [this a b c d e f g h i j k l m n o p q r s t]
(literal-apply this [a b c d e f g h i j k l m n o p q r s t]))
(-invoke [this a b c d e f g h i j k l m n o p q r s t rest]
(literal-apply this (concat [a b c d e f g h i j k l m n o p q r s t] rest)))]))
#?(:clj
(defmethod print-method Function [^Function f ^java.io.Writer w]
(.write w (.toString f))))
(defn literal-function?
"Returns true if the supplied object is an instance of [[Function]], false
otherwise."
[f]
(instance? Function f))
(defn- name
"Returns the `-name` field of the supplied [[Function]] object. Errors if any
other type is supplied."
[f]
{:pre [(literal-function? f)]}
(.-name ^Function f))
(defn- domain-types
"Returns the `-domain` field of the supplied [[Function]] object. Errors if any
other type is supplied."
[f]
{:pre [(literal-function? f)]}
(.-domain ^Function f))
(defn- range-type
"Returns the `-range` field of the supplied [[Function]] object. Errors if any
other type is supplied."
[f]
{:pre [(literal-function? f)]}
(.-range ^Function f))
(defn- f:=
"Returns true if the function `a` equals `b`, false otherwise."
[a b]
(and (literal-function? b)
(= (name a) (name b))
(= (domain-types a) (domain-types b))
(= (range-type a) (range-type b))))
(defn literal-function
([f] (->Function f [:exactly 1] [0] 0))
([f signature]
(let [[domain range] (sicm-signature->domain-range signature)]
(literal-function f domain range)))
([f domain range]
(cond (number? range)
(let [arity (if (vector? domain)
(count domain)
1)]
(->Function f [:exactly arity]
(if (vector? domain) domain [domain])
range))
(s/structure? range)
(let [n (count range)
orientation (s/orientation range)
template (s/literal f n orientation)]
(s/mapr #(literal-function %1 domain %2)
template
range))
:else
(u/illegal (str "WTF range" range)))))
(defn ^:no-doc binding-pairs [litfns]
(letfn [(extract-sym [entry]
(if (symbol? entry) entry (first entry)))
(entry->fn [entry]
(cond (symbol? entry) `(literal-function (quote ~entry))
(and (sequential? entry) (= (count entry) 3))
(let [[sym domain range] entry]
`(literal-function (quote ~sym) ~domain ~range))
:else (u/illegal (str "unknown literal function type" entry))))]
(mapv (fn [entry]
[(extract-sym entry)
(entry->fn entry)])
litfns)))
(defmacro with-literal-functions [litfns & body]
(let [pairs (binding-pairs litfns)
bindings (into [] cat pairs)]
`(let ~bindings ~@body)))
;; ## Differentiation of literal functions
(defn- literal-partial [f path]
(let [fexp (if (= (f/arity f) [:exactly 1]) ;; univariate
(if (= (first path) 0)
(if (= (count path) 1)
;; Special-case the single argument case, or a unary function
;; that's provided with a structure of a single entry.
(sym/derivative (name f))
`((~'partial ~@(next path)) ~(name f)))
(u/illegal "wrong indices"))
;; If the function takes multiple arguments we DO need to index
;; into that first layer. (else the first layer is added.)
`((~'partial ~@path) ~(name f)))]
(->Function
fexp (f/arity f) (domain-types f) (range-type f))))
(defn- literal-derivative
"Takes a literal function `f` and a sequence of arguments `xs`, and generates an
expanded `((D f) xs)` by applying the chain rule and summing the partial
derivatives for each differential argument in the input structure."
[f xs]
(let [v (m/seq-> xs)
flat-v (flatten v)
tag (apply d/max-order-tag flat-v)
ve (s/mapr #(d/primal-part % tag) v)
partials (s/map-chain
(fn [x path _]
(let [dx (d/tangent-part x tag)]
(if (v/zero? dx)
0
(d/d:* (literal-apply
(literal-partial f path) ve)
dx))))
v)]
(apply d/d:+ (apply f ve) (flatten partials))))
(defn- check-argument-type
"Check that the argument provided at index i has the same type as
the exemplar expected."
[f provided expected indexes]
(cond (number? expected)
(when-not (v/numerical? provided)
(u/illegal (str "expected numerical quantity in argument " indexes
" of function call " f
" but got " provided)))
(s/structure? expected)
(do (when-not (and (or (s/structure? provided) (sequential? provided))
(= (s/orientation provided) (s/orientation expected))
(= (count provided) (count expected)))
(u/illegal (str "expected structure matching " expected
" but got " provided )))
(doseq [[provided expected sub-index] (map list provided expected (range))]
(check-argument-type f provided expected (conj indexes sub-index))))
(keyword? expected) ;; a keyword has to match the argument's kind
(when-not (= (v/kind provided) expected)
(u/illegal (str "expected argument of type " expected " but got " (v/kind provided)
" in call to function " f)))
:else (u/illegal (str "unexpected argument example. got " provided " want " expected))))
(defn- literal-apply [f xs]
(check-argument-type f xs (domain-types f) [0])
(if (some d/perturbed? xs)
(literal-derivative f xs)
(an/literal-number `(~(name f) ~@(map v/freeze xs)))))
;; ## Specific Generics
;;
;; We can install one more method - [[sicmutils.generic/simplify]] returns its
;; argument with the internally captured name simplified.
(defmethod g/simplify [::function] [f]
(->Function (g/simplify (name f))
(f/arity f)
(domain-types f)
(range-type f)))