rlm@2
|
1 <?xml version="1.0" encoding="utf-8"?>
|
rlm@2
|
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
rlm@2
|
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
rlm@2
|
4 <html xmlns="http://www.w3.org/1999/xhtml"
|
rlm@2
|
5 lang="en" xml:lang="en">
|
rlm@2
|
6 <head>
|
rlm@2
|
7 <title>Building a Classical Mechanics Library in Clojure</title>
|
rlm@2
|
8 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
|
rlm@2
|
9 <meta name="generator" content="Org-mode"/>
|
rlm@2
|
10 <meta name="generated" content="2011-08-11 04:10:36 EDT"/>
|
rlm@2
|
11 <meta name="author" content="Dylan Holmes"/>
|
rlm@2
|
12 <meta name="description" content=""/>
|
rlm@2
|
13 <meta name="keywords" content=""/>
|
rlm@2
|
14 <style type="text/css">
|
rlm@2
|
15 <!--/*--><![CDATA[/*><!--*/
|
rlm@2
|
16 html { font-family: Times, serif; font-size: 12pt; }
|
rlm@2
|
17 .title { text-align: center; }
|
rlm@2
|
18 .todo { color: red; }
|
rlm@2
|
19 .done { color: green; }
|
rlm@2
|
20 .tag { background-color: #add8e6; font-weight:normal }
|
rlm@2
|
21 .target { }
|
rlm@2
|
22 .timestamp { color: #bebebe; }
|
rlm@2
|
23 .timestamp-kwd { color: #5f9ea0; }
|
rlm@2
|
24 .right {margin-left:auto; margin-right:0px; text-align:right;}
|
rlm@2
|
25 .left {margin-left:0px; margin-right:auto; text-align:left;}
|
rlm@2
|
26 .center {margin-left:auto; margin-right:auto; text-align:center;}
|
rlm@2
|
27 p.verse { margin-left: 3% }
|
rlm@2
|
28 pre {
|
rlm@2
|
29 border: 1pt solid #AEBDCC;
|
rlm@2
|
30 background-color: #F3F5F7;
|
rlm@2
|
31 padding: 5pt;
|
rlm@2
|
32 font-family: courier, monospace;
|
rlm@2
|
33 font-size: 90%;
|
rlm@2
|
34 overflow:auto;
|
rlm@2
|
35 }
|
rlm@2
|
36 table { border-collapse: collapse; }
|
rlm@2
|
37 td, th { vertical-align: top; }
|
rlm@2
|
38 th.right { text-align:center; }
|
rlm@2
|
39 th.left { text-align:center; }
|
rlm@2
|
40 th.center { text-align:center; }
|
rlm@2
|
41 td.right { text-align:right; }
|
rlm@2
|
42 td.left { text-align:left; }
|
rlm@2
|
43 td.center { text-align:center; }
|
rlm@2
|
44 dt { font-weight: bold; }
|
rlm@2
|
45 div.figure { padding: 0.5em; }
|
rlm@2
|
46 div.figure p { text-align: center; }
|
rlm@2
|
47 textarea { overflow-x: auto; }
|
rlm@2
|
48 .linenr { font-size:smaller }
|
rlm@2
|
49 .code-highlighted {background-color:#ffff00;}
|
rlm@2
|
50 .org-info-js_info-navigation { border-style:none; }
|
rlm@2
|
51 #org-info-js_console-label { font-size:10px; font-weight:bold;
|
rlm@2
|
52 white-space:nowrap; }
|
rlm@2
|
53 .org-info-js_search-highlight {background-color:#ffff00; color:#000000;
|
rlm@2
|
54 font-weight:bold; }
|
rlm@2
|
55 /*]]>*/-->
|
rlm@2
|
56 </style>
|
rlm@2
|
57 <link rel="stylesheet" type="text/css" href="../css/aurellem.css" />
|
rlm@2
|
58 <script type="text/javascript">
|
rlm@2
|
59 <!--/*--><![CDATA[/*><!--*/
|
rlm@2
|
60 function CodeHighlightOn(elem, id)
|
rlm@2
|
61 {
|
rlm@2
|
62 var target = document.getElementById(id);
|
rlm@2
|
63 if(null != target) {
|
rlm@2
|
64 elem.cacheClassElem = elem.className;
|
rlm@2
|
65 elem.cacheClassTarget = target.className;
|
rlm@2
|
66 target.className = "code-highlighted";
|
rlm@2
|
67 elem.className = "code-highlighted";
|
rlm@2
|
68 }
|
rlm@2
|
69 }
|
rlm@2
|
70 function CodeHighlightOff(elem, id)
|
rlm@2
|
71 {
|
rlm@2
|
72 var target = document.getElementById(id);
|
rlm@2
|
73 if(elem.cacheClassElem)
|
rlm@2
|
74 elem.className = elem.cacheClassElem;
|
rlm@2
|
75 if(elem.cacheClassTarget)
|
rlm@2
|
76 target.className = elem.cacheClassTarget;
|
rlm@2
|
77 }
|
rlm@2
|
78 /*]]>*///-->
|
rlm@2
|
79 </script>
|
rlm@2
|
80
|
rlm@2
|
81 </head>
|
rlm@2
|
82 <body>
|
rlm@2
|
83
|
rlm@2
|
84 <div id="content">
|
rlm@2
|
85
|
rlm@2
|
86
|
rlm@2
|
87
|
rlm@2
|
88 <div class="header">
|
rlm@2
|
89 <div class="float-right">
|
rlm@2
|
90 <!--
|
rlm@2
|
91 <form>
|
rlm@2
|
92 <input type="text"/><input type="submit" value="search the blog »"/>
|
rlm@2
|
93 </form>
|
rlm@2
|
94 -->
|
rlm@2
|
95 </div>
|
rlm@2
|
96
|
rlm@2
|
97 <h1>aurellem <em>☉</em></h1>
|
rlm@2
|
98 <ul class="nav">
|
rlm@2
|
99 <li><a href="/">read the blog »</a></li>
|
rlm@2
|
100 <!-- li><a href="#">learn about us »</a></li-->
|
rlm@2
|
101 </ul>
|
rlm@2
|
102 </div>
|
rlm@2
|
103
|
rlm@2
|
104 <h1 class="title">Building a Classical Mechanics Library in Clojure</h1>
|
rlm@2
|
105
|
rlm@2
|
106
|
rlm@2
|
107
|
rlm@2
|
108
|
rlm@2
|
109
|
rlm@2
|
110
|
rlm@2
|
111
|
rlm@2
|
112 <div id="table-of-contents">
|
rlm@2
|
113 <h2>Table of Contents</h2>
|
rlm@2
|
114 <div id="text-table-of-contents">
|
rlm@2
|
115 <ul>
|
rlm@2
|
116 <li><a href="#sec-1">1 Useful data types </a>
|
rlm@2
|
117 <ul>
|
rlm@2
|
118 <li><a href="#sec-1-1">1.1 Complex numbers </a></li>
|
rlm@2
|
119 <li><a href="#sec-1-2">1.2 Power series </a></li>
|
rlm@2
|
120 <li><a href="#sec-1-3">1.3 Tuples and tensors </a>
|
rlm@2
|
121 <ul>
|
rlm@2
|
122 <li><a href="#sec-1-3-1">1.3.1 Tuples are “sequences with spin” </a></li>
|
rlm@2
|
123 <li><a href="#sec-1-3-2">1.3.2 Matrices </a></li>
|
rlm@2
|
124 </ul>
|
rlm@2
|
125 </li>
|
rlm@2
|
126 <li><a href="#sec-1-4">1.4 Generic Operations </a></li>
|
rlm@2
|
127 </ul>
|
rlm@2
|
128 </li>
|
rlm@2
|
129 <li><a href="#sec-2">2 Operators and Differentiation </a>
|
rlm@2
|
130 <ul>
|
rlm@2
|
131 <li><a href="#sec-2-1">2.1 Operators </a></li>
|
rlm@2
|
132 <li><a href="#sec-2-2">2.2 Differential Terms and Sequences </a></li>
|
rlm@2
|
133 </ul>
|
rlm@2
|
134 </li>
|
rlm@2
|
135 </ul>
|
rlm@2
|
136 </div>
|
rlm@2
|
137 </div>
|
rlm@2
|
138
|
rlm@2
|
139 <div id="outline-container-1" class="outline-2">
|
rlm@2
|
140 <h2 id="sec-1"><span class="section-number-2">1</span> Useful data types </h2>
|
rlm@2
|
141 <div class="outline-text-2" id="text-1">
|
rlm@2
|
142
|
rlm@2
|
143
|
rlm@2
|
144
|
rlm@2
|
145 </div>
|
rlm@2
|
146
|
rlm@2
|
147 <div id="outline-container-1-1" class="outline-3">
|
rlm@2
|
148 <h3 id="sec-1-1"><span class="section-number-3">1.1</span> Complex numbers </h3>
|
rlm@2
|
149 <div class="outline-text-3" id="text-1-1">
|
rlm@2
|
150
|
rlm@2
|
151
|
rlm@2
|
152 </div>
|
rlm@2
|
153
|
rlm@2
|
154 </div>
|
rlm@2
|
155
|
rlm@2
|
156 <div id="outline-container-1-2" class="outline-3">
|
rlm@2
|
157 <h3 id="sec-1-2"><span class="section-number-3">1.2</span> Power series </h3>
|
rlm@2
|
158 <div class="outline-text-3" id="text-1-2">
|
rlm@2
|
159
|
rlm@2
|
160
|
rlm@2
|
161 </div>
|
rlm@2
|
162
|
rlm@2
|
163 </div>
|
rlm@2
|
164
|
rlm@2
|
165 <div id="outline-container-1-3" class="outline-3">
|
rlm@2
|
166 <h3 id="sec-1-3"><span class="section-number-3">1.3</span> Tuples and tensors </h3>
|
rlm@2
|
167 <div class="outline-text-3" id="text-1-3">
|
rlm@2
|
168
|
rlm@2
|
169
|
rlm@2
|
170
|
rlm@2
|
171 </div>
|
rlm@2
|
172
|
rlm@2
|
173 <div id="outline-container-1-3-1" class="outline-4">
|
rlm@2
|
174 <h4 id="sec-1-3-1"><span class="section-number-4">1.3.1</span> Tuples are “sequences with spin” </h4>
|
rlm@2
|
175 <div class="outline-text-4" id="text-1-3-1">
|
rlm@2
|
176
|
rlm@2
|
177
|
rlm@2
|
178
|
rlm@2
|
179
|
rlm@2
|
180
|
rlm@2
|
181 <pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
|
rlm@2
|
182
|
rlm@2
|
183 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">Let some objects have spin</span>
|
rlm@2
|
184
|
rlm@2
|
185 (<span style="color: #f0dfaf; font-weight: bold;">defprotocol</span> <span style="color: #f0dfaf;">Spinning</span>
|
rlm@2
|
186 (up? [this])
|
rlm@2
|
187 (down? [this]))
|
rlm@2
|
188
|
rlm@2
|
189 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">spin</span>
|
rlm@2
|
190 <span style="color: #8fb28f;">"Returns the spin of the Spinning s, either :up or :down"</span>
|
rlm@2
|
191 [<span style="color: #dfdfbf; font-weight: bold;">#^Spinning</span> s]
|
rlm@2
|
192 (<span style="color: #f0dfaf; font-weight: bold;">cond</span> (up? s) <span style="color: #8cd0d3;">:up</span> (down? s) <span style="color: #8cd0d3;">:down</span>))
|
rlm@2
|
193
|
rlm@2
|
194
|
rlm@2
|
195 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">DEFINITION: A tuple is a sequence with spin</span>
|
rlm@2
|
196
|
rlm@2
|
197 (<span style="color: #f0dfaf; font-weight: bold;">deftype</span> <span style="color: #f0dfaf;">Tuple</span>
|
rlm@2
|
198 [spin coll]
|
rlm@2
|
199
|
rlm@2
|
200 clojure.lang.Seqable
|
rlm@2
|
201 (<span style="color: #8cd0d3;">seq</span> [this] (<span style="color: #8cd0d3;">seq</span> (.coll this)))
|
rlm@2
|
202
|
rlm@2
|
203 clojure.lang.Counted
|
rlm@2
|
204 (<span style="color: #8cd0d3;">count</span> [this] (<span style="color: #8cd0d3;">count</span> (.coll this)))
|
rlm@2
|
205
|
rlm@2
|
206 Spinning
|
rlm@2
|
207 (up? [this] (<span style="color: #8cd0d3;">=</span> <span style="color: #8cd0d3;">::up</span> (.spin this)))
|
rlm@2
|
208 (down? [this] (<span style="color: #8cd0d3;">=</span> <span style="color: #8cd0d3;">::down</span> (.spin this))))
|
rlm@2
|
209
|
rlm@2
|
210 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">print-method</span> Tuple
|
rlm@2
|
211 [o w]
|
rlm@2
|
212 (<span style="color: #8cd0d3;">print-simple</span>
|
rlm@2
|
213 (<span style="color: #f0dfaf; font-weight: bold;">if</span> (up? o)
|
rlm@2
|
214 (<span style="color: #8cd0d3;">str</span> <span style="color: #cc9393;">"u"</span> (.coll o))
|
rlm@2
|
215 (<span style="color: #8cd0d3;">str</span> <span style="color: #cc9393;">"d"</span> (<span style="color: #8cd0d3;">vec</span>(.coll o))))
|
rlm@2
|
216 w))
|
rlm@2
|
217
|
rlm@2
|
218
|
rlm@2
|
219 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">CONSTRUCTORS</span>
|
rlm@2
|
220
|
rlm@2
|
221 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">up</span>
|
rlm@2
|
222 <span style="color: #8fb28f;">"Create a new up-tuple containing the contents of coll."</span>
|
rlm@2
|
223 [coll]
|
rlm@2
|
224 (Tuple. <span style="color: #8cd0d3;">::up</span> coll))
|
rlm@2
|
225
|
rlm@2
|
226 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">down</span>
|
rlm@2
|
227 <span style="color: #8fb28f;">"Create a new down-tuple containing the contents of coll."</span>
|
rlm@2
|
228 [coll]
|
rlm@2
|
229 (Tuple. <span style="color: #8cd0d3;">::down</span> coll))
|
rlm@2
|
230 </pre>
|
rlm@2
|
231
|
rlm@2
|
232
|
rlm@2
|
233
|
rlm@2
|
234
|
rlm@2
|
235
|
rlm@2
|
236
|
rlm@2
|
237 </div>
|
rlm@2
|
238
|
rlm@2
|
239 </div>
|
rlm@2
|
240
|
rlm@2
|
241 <div id="outline-container-1-3-2" class="outline-4">
|
rlm@2
|
242 <h4 id="sec-1-3-2"><span class="section-number-4">1.3.2</span> Matrices </h4>
|
rlm@2
|
243 <div class="outline-text-4" id="text-1-3-2">
|
rlm@2
|
244
|
rlm@2
|
245
|
rlm@2
|
246
|
rlm@2
|
247
|
rlm@2
|
248 <pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
|
rlm@2
|
249 (<span style="color: #8cd0d3;">require</span> 'incanter.core) <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">use incanter's fast matrices</span>
|
rlm@2
|
250
|
rlm@2
|
251
|
rlm@2
|
252 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">all-equal?</span> [coll]
|
rlm@2
|
253 (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">empty?</span> (<span style="color: #8cd0d3;">rest</span> coll)) true
|
rlm@2
|
254 (<span style="color: #f0dfaf; font-weight: bold;">and</span> (<span style="color: #8cd0d3;">=</span> (<span style="color: #8cd0d3;">first</span> coll) (<span style="color: #8cd0d3;">second</span> coll))
|
rlm@2
|
255 (<span style="color: #f0dfaf; font-weight: bold;">recur</span> (<span style="color: #8cd0d3;">rest</span> coll)))))
|
rlm@2
|
256
|
rlm@2
|
257
|
rlm@2
|
258 (<span style="color: #f0dfaf; font-weight: bold;">defprotocol</span> <span style="color: #f0dfaf;">Matrix</span>
|
rlm@2
|
259 (rows [matrix])
|
rlm@2
|
260 (cols [matrix])
|
rlm@2
|
261 (diagonal [matrix])
|
rlm@2
|
262 (trace [matrix])
|
rlm@2
|
263 (determinant [matrix])
|
rlm@2
|
264 (transpose [matrix])
|
rlm@2
|
265 (conjugate [matrix])
|
rlm@2
|
266 )
|
rlm@2
|
267
|
rlm@2
|
268 (<span style="color: #8cd0d3;">extend-protocol</span> Matrix incanter.Matrix
|
rlm@2
|
269 (rows [rs] (<span style="color: #8cd0d3;">map</span> down (<span style="color: #8cd0d3;">apply</span> map vector (<span style="color: #8cd0d3;">apply</span> map vector rs))))
|
rlm@2
|
270 (cols [rs] (<span style="color: #8cd0d3;">map</span> up (<span style="color: #8cd0d3;">apply</span> map vector rs)))
|
rlm@2
|
271 (diagonal [matrix] (incanter.core/diag matrix) )
|
rlm@2
|
272 (determinant [matrix] (incanter.core/det matrix))
|
rlm@2
|
273 (trace [matrix] (incanter.core/trace matrix))
|
rlm@2
|
274 (transpose [matrix] (incanter.core/trans matrix)))
|
rlm@2
|
275
|
rlm@2
|
276 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">count-rows</span> [matrix]
|
rlm@2
|
277 ((<span style="color: #8cd0d3;">comp</span> count rows) matrix))
|
rlm@2
|
278
|
rlm@2
|
279 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">count-cols</span> [matrix]
|
rlm@2
|
280 ((<span style="color: #8cd0d3;">comp</span> count cols) matrix))
|
rlm@2
|
281
|
rlm@2
|
282 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">square?</span> [matrix]
|
rlm@2
|
283 (<span style="color: #8cd0d3;">=</span> (count-rows matrix) (count-cols matrix)))
|
rlm@2
|
284
|
rlm@2
|
285 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">identity-matrix</span>
|
rlm@2
|
286 <span style="color: #8fb28f;">"Define a square matrix of size n-by-n with 1s along the diagonal and</span>
|
rlm@2
|
287 <span style="color: #8fb28f;"> 0s everywhere else."</span>
|
rlm@2
|
288 [n]
|
rlm@2
|
289 (incanter.core/identity-matrix n))
|
rlm@2
|
290
|
rlm@2
|
291
|
rlm@2
|
292 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">matrix-by-rows</span>
|
rlm@2
|
293 <span style="color: #8fb28f;">"Define a matrix by giving its rows."</span>
|
rlm@2
|
294 [& rows]
|
rlm@2
|
295 (<span style="color: #f0dfaf; font-weight: bold;">if</span>
|
rlm@2
|
296 (<span style="color: #8cd0d3;">not</span> (all-equal? (<span style="color: #8cd0d3;">map</span> count rows)))
|
rlm@2
|
297 (<span style="color: #f0dfaf; font-weight: bold;">throw</span> (Exception. <span style="color: #cc9393;">"All rows in a matrix must have the same number of elements."</span>))
|
rlm@2
|
298 (incanter.core/matrix (<span style="color: #8cd0d3;">vec</span> rows))))
|
rlm@2
|
299
|
rlm@2
|
300 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">matrix-by-cols</span>
|
rlm@2
|
301 <span style="color: #8fb28f;">"Define a matrix by giving its columns"</span>
|
rlm@2
|
302 [& cols]
|
rlm@2
|
303 (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">not</span> (all-equal? (<span style="color: #8cd0d3;">map</span> count cols)))
|
rlm@2
|
304 (<span style="color: #f0dfaf; font-weight: bold;">throw</span> (Exception. <span style="color: #cc9393;">"All columns in a matrix must have the same number of elements."</span>))
|
rlm@2
|
305 (incanter.core/matrix (<span style="color: #8cd0d3;">vec</span> (<span style="color: #8cd0d3;">apply</span> map vector cols)))))
|
rlm@2
|
306
|
rlm@2
|
307 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">identity-matrix</span>
|
rlm@2
|
308 <span style="color: #8fb28f;">"Define a square matrix of size n-by-n with 1s along the diagonal and</span>
|
rlm@2
|
309 <span style="color: #8fb28f;"> 0s everywhere else."</span>
|
rlm@2
|
310 [n]
|
rlm@2
|
311 (incanter.core/identity-matrix n))
|
rlm@2
|
312
|
rlm@2
|
313 </pre>
|
rlm@2
|
314
|
rlm@2
|
315
|
rlm@2
|
316
|
rlm@2
|
317
|
rlm@2
|
318 </div>
|
rlm@2
|
319 </div>
|
rlm@2
|
320
|
rlm@2
|
321 </div>
|
rlm@2
|
322
|
rlm@2
|
323 <div id="outline-container-1-4" class="outline-3">
|
rlm@2
|
324 <h3 id="sec-1-4"><span class="section-number-3">1.4</span> Generic Operations </h3>
|
rlm@2
|
325 <div class="outline-text-3" id="text-1-4">
|
rlm@2
|
326
|
rlm@2
|
327
|
rlm@2
|
328
|
rlm@2
|
329
|
rlm@2
|
330 <pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
|
rlm@2
|
331 (<span style="color: #8cd0d3;">use</span> 'clojure.contrib.generic.arithmetic
|
rlm@2
|
332 'clojure.contrib.generic.collection
|
rlm@2
|
333 'clojure.contrib.generic.functor
|
rlm@2
|
334 'clojure.contrib.generic.math-functions)
|
rlm@2
|
335
|
rlm@2
|
336 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">numbers?</span>
|
rlm@2
|
337 <span style="color: #8fb28f;">"Returns true if all arguments are numbers, else false."</span>
|
rlm@2
|
338 [& xs]
|
rlm@2
|
339 (<span style="color: #8cd0d3;">every?</span> number? xs))
|
rlm@2
|
340
|
rlm@2
|
341 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">tuple-surgery</span>
|
rlm@2
|
342 <span style="color: #8fb28f;">"Applies the function f to the items of tuple and the additional</span>
|
rlm@2
|
343 <span style="color: #8fb28f;"> arguments, if any. Returns a Tuple of the same type as tuple."</span>
|
rlm@2
|
344 [tuple f & xs]
|
rlm@2
|
345 ((<span style="color: #f0dfaf; font-weight: bold;">if</span> (up? tuple) up down)
|
rlm@2
|
346 (<span style="color: #8cd0d3;">apply</span> f (<span style="color: #8cd0d3;">seq</span> tuple) xs)))
|
rlm@2
|
347
|
rlm@2
|
348
|
rlm@2
|
349
|
rlm@2
|
350 <span style="color: #708070;">;;; </span><span style="color: #7f9f7f;">CONTRACTION collapses two compatible tuples into a number.</span>
|
rlm@2
|
351
|
rlm@2
|
352 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">contractible?</span>
|
rlm@2
|
353 <span style="color: #8fb28f;">"Returns true if the tuples a and b are compatible for contraction,</span>
|
rlm@2
|
354 <span style="color: #8fb28f;"> else false. Tuples are compatible if they have the same number of</span>
|
rlm@2
|
355 <span style="color: #8fb28f;"> components, they have opposite spins, and their elements are</span>
|
rlm@2
|
356 <span style="color: #8fb28f;"> pairwise-compatible."</span>
|
rlm@2
|
357 [a b]
|
rlm@2
|
358 (<span style="color: #f0dfaf; font-weight: bold;">and</span>
|
rlm@2
|
359 (<span style="color: #8cd0d3;">isa?</span> (<span style="color: #8cd0d3;">type</span> a) Tuple)
|
rlm@2
|
360 (<span style="color: #8cd0d3;">isa?</span> (<span style="color: #8cd0d3;">type</span> b) Tuple)
|
rlm@2
|
361 (<span style="color: #8cd0d3;">=</span> (<span style="color: #8cd0d3;">count</span> a) (<span style="color: #8cd0d3;">count</span> b))
|
rlm@2
|
362 (<span style="color: #8cd0d3;">not=</span> (spin a) (spin b))
|
rlm@2
|
363
|
rlm@2
|
364 (<span style="color: #8cd0d3;">not-any?</span> false?
|
rlm@2
|
365 (<span style="color: #8cd0d3;">map</span> #(<span style="color: #f0dfaf; font-weight: bold;">or</span>
|
rlm@2
|
366 (numbers? %1 %2)
|
rlm@2
|
367 (contractible? %1 %2))
|
rlm@2
|
368 a b))))
|
rlm@2
|
369
|
rlm@2
|
370 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">contract</span>
|
rlm@2
|
371 <span style="color: #8fb28f;">"Contracts two tuples, returning the sum of the</span>
|
rlm@2
|
372 <span style="color: #8fb28f;"> products of the corresponding items. Contraction is recursive on</span>
|
rlm@2
|
373 <span style="color: #8fb28f;"> nested tuples."</span>
|
rlm@2
|
374 [a b]
|
rlm@2
|
375 (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">not</span> (contractible? a b))
|
rlm@2
|
376 (<span style="color: #f0dfaf; font-weight: bold;">throw</span>
|
rlm@2
|
377 (Exception. <span style="color: #cc9393;">"Not compatible for contraction."</span>))
|
rlm@2
|
378 (<span style="color: #8cd0d3;">reduce</span> +
|
rlm@2
|
379 (<span style="color: #8cd0d3;">map</span>
|
rlm@2
|
380 (<span style="color: #8cd0d3;">fn</span> [x y]
|
rlm@2
|
381 (<span style="color: #f0dfaf; font-weight: bold;">if</span> (numbers? x y)
|
rlm@2
|
382 (<span style="color: #8cd0d3;">*</span> x y)
|
rlm@2
|
383 (contract x y)))
|
rlm@2
|
384 a b))))
|
rlm@2
|
385
|
rlm@2
|
386
|
rlm@2
|
387
|
rlm@2
|
388
|
rlm@2
|
389
|
rlm@2
|
390 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">conj</span> Tuple
|
rlm@2
|
391 [tuple & xs]
|
rlm@2
|
392 (tuple-surgery tuple #(<span style="color: #8cd0d3;">apply</span> conj % xs)))
|
rlm@2
|
393
|
rlm@2
|
394 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">fmap</span> Tuple
|
rlm@2
|
395 [f tuple]
|
rlm@2
|
396 (tuple-surgery tuple (<span style="color: #8cd0d3;">partial</span> map f)))
|
rlm@2
|
397
|
rlm@2
|
398
|
rlm@2
|
399
|
rlm@2
|
400 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">TODO: define Scalar, and add it to the hierarchy above Number and Complex</span>
|
rlm@2
|
401
|
rlm@2
|
402
|
rlm@2
|
403 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">*</span> [Tuple Tuple] <span style="color: #708070;">; </span><span style="color: #7f9f7f;">tuple*tuple</span>
|
rlm@2
|
404 [a b]
|
rlm@2
|
405 (<span style="color: #f0dfaf; font-weight: bold;">if</span> (contractible? a b)
|
rlm@2
|
406 (contract a b)
|
rlm@2
|
407 (<span style="color: #8cd0d3;">map</span> (<span style="color: #8cd0d3;">partial</span> * a) b)))
|
rlm@2
|
408
|
rlm@2
|
409
|
rlm@2
|
410 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">*</span> [java.lang.Number Tuple] <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">scalar * tuple</span>
|
rlm@2
|
411 [a x] (fmap (<span style="color: #8cd0d3;">partial</span> * a) x))
|
rlm@2
|
412
|
rlm@2
|
413 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">*</span> [Tuple java.lang.Number]
|
rlm@2
|
414 [x a] (<span style="color: #8cd0d3;">*</span> a x))
|
rlm@2
|
415
|
rlm@2
|
416 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">*</span> [java.lang.Number incanter.Matrix] <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">scalar * matrix</span>
|
rlm@2
|
417 [x M] (incanter.core/mult x M))
|
rlm@2
|
418
|
rlm@2
|
419 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">*</span> [incanter.Matrix java.lang.Number]
|
rlm@2
|
420 [M x] (<span style="color: #8cd0d3;">*</span> x M))
|
rlm@2
|
421
|
rlm@2
|
422 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">*</span> [incanter.Matrix incanter.Matrix] <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">matrix * matrix</span>
|
rlm@2
|
423 [M1 M2]
|
rlm@2
|
424 (incanter.core/mmult M1 M2))
|
rlm@2
|
425
|
rlm@2
|
426 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">*</span> [incanter.Matrix Tuple] <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">matrix * tuple</span>
|
rlm@2
|
427 [M v]
|
rlm@2
|
428 (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #f0dfaf; font-weight: bold;">and</span> (<span style="color: #8cd0d3;">apply</span> numbers? v) (up? v))
|
rlm@2
|
429 (<span style="color: #8cd0d3;">*</span> M (matrix-by-cols v))
|
rlm@2
|
430 (<span style="color: #f0dfaf; font-weight: bold;">throw</span> (Exception. <span style="color: #cc9393;">"Currently, you can only multiply a matrix by a tuple of *numbers*"</span>))
|
rlm@2
|
431 ))
|
rlm@2
|
432
|
rlm@2
|
433 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">*</span> [Tuple incanter.Matrix] <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">tuple * Matrix</span>
|
rlm@2
|
434 [v M]
|
rlm@2
|
435 (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #f0dfaf; font-weight: bold;">and</span> (<span style="color: #8cd0d3;">apply</span> numbers? v) (down? v))
|
rlm@2
|
436 (<span style="color: #8cd0d3;">*</span> (matrix-by-rows v) M)
|
rlm@2
|
437 (<span style="color: #f0dfaf; font-weight: bold;">throw</span> (Exception. <span style="color: #cc9393;">"Currently, you can only multiply a matrix by a tuple of *numbers*"</span>))
|
rlm@2
|
438 ))
|
rlm@2
|
439
|
rlm@2
|
440
|
rlm@2
|
441 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">exp</span> incanter.Matrix
|
rlm@2
|
442 [M]
|
rlm@2
|
443 (incanter.core/exp M))
|
rlm@2
|
444
|
rlm@2
|
445 </pre>
|
rlm@2
|
446
|
rlm@2
|
447
|
rlm@2
|
448
|
rlm@2
|
449
|
rlm@2
|
450 </div>
|
rlm@2
|
451 </div>
|
rlm@2
|
452
|
rlm@2
|
453 </div>
|
rlm@2
|
454
|
rlm@2
|
455 <div id="outline-container-2" class="outline-2">
|
rlm@2
|
456 <h2 id="sec-2"><span class="section-number-2">2</span> Operators and Differentiation </h2>
|
rlm@2
|
457 <div class="outline-text-2" id="text-2">
|
rlm@2
|
458
|
rlm@2
|
459
|
rlm@2
|
460
|
rlm@2
|
461
|
rlm@2
|
462 </div>
|
rlm@2
|
463
|
rlm@2
|
464 <div id="outline-container-2-1" class="outline-3">
|
rlm@2
|
465 <h3 id="sec-2-1"><span class="section-number-3">2.1</span> Operators </h3>
|
rlm@2
|
466 <div class="outline-text-3" id="text-2-1">
|
rlm@2
|
467
|
rlm@2
|
468
|
rlm@2
|
469 </div>
|
rlm@2
|
470
|
rlm@2
|
471 </div>
|
rlm@2
|
472
|
rlm@2
|
473 <div id="outline-container-2-2" class="outline-3">
|
rlm@2
|
474 <h3 id="sec-2-2"><span class="section-number-3">2.2</span> Differential Terms and Sequences </h3>
|
rlm@2
|
475 <div class="outline-text-3" id="text-2-2">
|
rlm@2
|
476
|
rlm@2
|
477
|
rlm@2
|
478
|
rlm@2
|
479
|
rlm@2
|
480 <pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
|
rlm@2
|
481 (<span style="color: #8cd0d3;">use</span> 'clojure.contrib.seq
|
rlm@2
|
482 'clojure.contrib.generic.arithmetic
|
rlm@2
|
483 'clojure.contrib.generic.collection
|
rlm@2
|
484 'clojure.contrib.generic.math-functions)
|
rlm@2
|
485
|
rlm@2
|
486 <span style="color: #708070;">;;</span><span style="color: #7f9f7f;">∂</span>
|
rlm@2
|
487
|
rlm@2
|
488 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">DEFINITION : Differential Term</span>
|
rlm@2
|
489
|
rlm@2
|
490 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">A quantity with infinitesimal components, e.g. x, dxdy, 4dydz. The</span>
|
rlm@2
|
491 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">coefficient of the quantity is returned by the 'coefficient' method,</span>
|
rlm@2
|
492 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">while the sequence of differential parameters is returned by the</span>
|
rlm@2
|
493 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">method 'partials'.</span>
|
rlm@2
|
494
|
rlm@2
|
495 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">Instead of using (potentially ambiguous) letters to denote</span>
|
rlm@2
|
496 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">differential parameters (dx,dy,dz), we use integers. So, dxdz becomes [0 2].</span>
|
rlm@2
|
497
|
rlm@2
|
498 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">The coefficient can be any arithmetic object; the</span>
|
rlm@2
|
499 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">partials must be a nonrepeating sorted sequence of nonnegative</span>
|
rlm@2
|
500 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">integers.</span>
|
rlm@2
|
501
|
rlm@2
|
502 (<span style="color: #f0dfaf; font-weight: bold;">deftype</span> <span style="color: #f0dfaf;">DifferentialTerm</span> [coefficient partials])
|
rlm@2
|
503
|
rlm@2
|
504 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">differential-term</span>
|
rlm@2
|
505 <span style="color: #8fb28f;">"Make a differential term given pairs of coefficients and partials."</span>
|
rlm@2
|
506 [coefficient partials]
|
rlm@2
|
507 (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #f0dfaf; font-weight: bold;">and</span> (<span style="color: #8cd0d3;">coll?</span> partials) (<span style="color: #8cd0d3;">every?</span> #(<span style="color: #f0dfaf; font-weight: bold;">and</span> (<span style="color: #8cd0d3;">integer?</span> %) (<span style="color: #8cd0d3;">not</span>(<span style="color: #8cd0d3;">neg?</span> %))) partials))
|
rlm@2
|
508 (DifferentialTerm. coefficient (<span style="color: #8cd0d3;">set</span> partials))
|
rlm@2
|
509 (<span style="color: #f0dfaf; font-weight: bold;">throw</span> (java.lang.IllegalArgumentException. <span style="color: #cc9393;">"Partials must be a collection of integers."</span>))))
|
rlm@2
|
510
|
rlm@2
|
511
|
rlm@2
|
512 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">DEFINITION : Differential Sequence</span>
|
rlm@2
|
513 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">A differential sequence is a sequence of differential terms, all with different partials.</span>
|
rlm@2
|
514 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">Internally, it is a map from the partials of each term to their coefficients.</span>
|
rlm@2
|
515
|
rlm@2
|
516 (<span style="color: #f0dfaf; font-weight: bold;">deftype</span> <span style="color: #f0dfaf;">DifferentialSeq</span>
|
rlm@2
|
517 [terms]
|
rlm@2
|
518 <span style="color: #708070;">;;</span><span style="color: #7f9f7f;">clojure.lang.IPersistentMap</span>
|
rlm@2
|
519 clojure.lang.Associative
|
rlm@2
|
520 (<span style="color: #8cd0d3;">assoc</span> [this key val]
|
rlm@2
|
521 (DifferentialSeq.
|
rlm@2
|
522 (<span style="color: #8cd0d3;">cons</span> (differential-term val key) terms)))
|
rlm@2
|
523 (<span style="color: #8cd0d3;">cons</span> [this x]
|
rlm@2
|
524 (DifferentialSeq. (<span style="color: #8cd0d3;">cons</span> x terms)))
|
rlm@2
|
525 (containsKey [this key]
|
rlm@2
|
526 (<span style="color: #8cd0d3;">not</span>(<span style="color: #8cd0d3;">nil?</span> (find-first #(<span style="color: #8cd0d3;">=</span> (.partials %) key) terms))))
|
rlm@2
|
527 (<span style="color: #8cd0d3;">count</span> [this] (<span style="color: #8cd0d3;">count</span> (.terms this)))
|
rlm@2
|
528 (<span style="color: #8cd0d3;">empty</span> [this] (DifferentialSeq. []))
|
rlm@2
|
529 (entryAt [this key]
|
rlm@2
|
530 ((<span style="color: #8cd0d3;">juxt</span> #(.partials %) #(.coefficient %))
|
rlm@2
|
531 (find-first #(<span style="color: #8cd0d3;">=</span> (.partials %) key) terms)))
|
rlm@2
|
532 (<span style="color: #8cd0d3;">seq</span> [this] (<span style="color: #8cd0d3;">seq</span> (.terms this))))
|
rlm@2
|
533
|
rlm@2
|
534
|
rlm@2
|
535 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">fmap</span> DifferentialSeq
|
rlm@2
|
536 [f dseq]
|
rlm@2
|
537 (DifferentialSeq. (fmap f (.terms dseq))))
|
rlm@2
|
538
|
rlm@2
|
539
|
rlm@2
|
540 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">BUILDING DIFFERENTIAL OBJECTS</span>
|
rlm@2
|
541
|
rlm@2
|
542 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">differential-seq</span>
|
rlm@2
|
543 <span style="color: #8fb28f;">"Define a differential sequence by specifying coefficient/partials</span>
|
rlm@2
|
544 <span style="color: #8fb28f;">pairs, which are used to create differential terms to populate the</span>
|
rlm@2
|
545 <span style="color: #8fb28f;">sequence."</span>
|
rlm@2
|
546 ([coefficient partials]
|
rlm@2
|
547 (DifferentialSeq. {(<span style="color: #8cd0d3;">set</span> partials) coefficient}))
|
rlm@2
|
548 ([coefficient partials & cps]
|
rlm@2
|
549 (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">odd?</span> (<span style="color: #8cd0d3;">count</span> cps))
|
rlm@2
|
550 (<span style="color: #f0dfaf; font-weight: bold;">throw</span> (Exception. <span style="color: #cc9393;">"differential-seq requires an even number of terms."</span>))
|
rlm@2
|
551 (DifferentialSeq.
|
rlm@2
|
552 (<span style="color: #8cd0d3;">reduce</span>
|
rlm@2
|
553 #(<span style="color: #8cd0d3;">assoc</span> %1 (<span style="color: #8cd0d3;">set</span> (<span style="color: #8cd0d3;">second</span> %2)) (<span style="color: #8cd0d3;">first</span> %2))
|
rlm@2
|
554 {(<span style="color: #8cd0d3;">set</span> partials) coefficient}
|
rlm@2
|
555 (<span style="color: #8cd0d3;">partition</span> 2 cps))))))
|
rlm@2
|
556
|
rlm@2
|
557
|
rlm@2
|
558
|
rlm@2
|
559 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">big-part</span>
|
rlm@2
|
560 <span style="color: #8fb28f;">"Returns the part of the differential sequence that is finite,</span>
|
rlm@2
|
561 <span style="color: #8fb28f;"> i.e. not infinitely small, as a differential sequence. If given a</span>
|
rlm@2
|
562 <span style="color: #8fb28f;"> non-differential object, returns that object."</span>
|
rlm@2
|
563 [dseq]
|
rlm@2
|
564 (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">not=</span> (<span style="color: #8cd0d3;">type</span> dseq) DifferentialSeq) dseq
|
rlm@2
|
565 (<span style="color: #f0dfaf; font-weight: bold;">let</span> [m (.terms dseq)
|
rlm@2
|
566 keys (<span style="color: #8cd0d3;">sort-by</span> count (<span style="color: #8cd0d3;">keys</span> m))
|
rlm@2
|
567 smallest-var (<span style="color: #8cd0d3;">last</span> (<span style="color: #8cd0d3;">last</span> keys))]
|
rlm@2
|
568 (DifferentialSeq.
|
rlm@2
|
569 (<span style="color: #8cd0d3;">reduce</span>
|
rlm@2
|
570 #(<span style="color: #8cd0d3;">assoc</span> %1 (<span style="color: #8cd0d3;">first</span> %2) (<span style="color: #8cd0d3;">second</span> %2))
|
rlm@2
|
571 {}
|
rlm@2
|
572 (<span style="color: #8cd0d3;">remove</span> #((<span style="color: #8cd0d3;">first</span> %) smallest-var) m))))))
|
rlm@2
|
573
|
rlm@2
|
574
|
rlm@2
|
575 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">small-part</span>
|
rlm@2
|
576 <span style="color: #8fb28f;">"Returns the part of the differential sequence that infinitely</span>
|
rlm@2
|
577 <span style="color: #8fb28f;"> small, as a differential sequence. If given a non-differential</span>
|
rlm@2
|
578 <span style="color: #8fb28f;"> object, returns that object."</span>
|
rlm@2
|
579 [dseq]
|
rlm@2
|
580 (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">not=</span> (<span style="color: #8cd0d3;">type</span> dseq) DifferentialSeq) 0
|
rlm@2
|
581 (<span style="color: #f0dfaf; font-weight: bold;">let</span> [m (.terms dseq)
|
rlm@2
|
582 keys (<span style="color: #8cd0d3;">sort-by</span> count (<span style="color: #8cd0d3;">keys</span> m))
|
rlm@2
|
583 smallest-var (<span style="color: #8cd0d3;">last</span> (<span style="color: #8cd0d3;">last</span> keys))]
|
rlm@2
|
584 (DifferentialSeq.
|
rlm@2
|
585 (<span style="color: #8cd0d3;">reduce</span>
|
rlm@2
|
586 #(<span style="color: #8cd0d3;">assoc</span> %1 (<span style="color: #8cd0d3;">first</span> %2) (<span style="color: #8cd0d3;">second</span> %2)) {}
|
rlm@2
|
587 (<span style="color: #8cd0d3;">filter</span> #((<span style="color: #8cd0d3;">first</span> %) smallest-var) m))))))
|
rlm@2
|
588
|
rlm@2
|
589
|
rlm@2
|
590
|
rlm@2
|
591 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">cartesian-product</span> [set1 set2]
|
rlm@2
|
592 (<span style="color: #8cd0d3;">reduce</span> concat
|
rlm@2
|
593 (<span style="color: #f0dfaf; font-weight: bold;">for</span> [x set1]
|
rlm@2
|
594 (<span style="color: #f0dfaf; font-weight: bold;">for</span> [y set2]
|
rlm@2
|
595 [x y]))))
|
rlm@2
|
596
|
rlm@2
|
597 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">differential-multiply</span>
|
rlm@2
|
598 <span style="color: #8fb28f;">"Multiply two differential sequences. The square of any differential</span>
|
rlm@2
|
599 <span style="color: #8fb28f;"> variable is zero since differential variables are infinitesimally</span>
|
rlm@2
|
600 <span style="color: #8fb28f;"> small."</span>
|
rlm@2
|
601 [dseq1 dseq2]
|
rlm@2
|
602 (DifferentialSeq.
|
rlm@2
|
603 (<span style="color: #8cd0d3;">reduce</span>
|
rlm@2
|
604 (<span style="color: #8cd0d3;">fn</span> [m [[vars1 coeff1] [vars2 coeff2]]]
|
rlm@2
|
605 (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">not</span> (<span style="color: #8cd0d3;">empty?</span> (clojure.set/<span style="color: #dfdfbf; font-weight: bold;">intersection</span> vars1 vars2)))
|
rlm@2
|
606 m
|
rlm@2
|
607 (<span style="color: #8cd0d3;">assoc</span> m (clojure.set/<span style="color: #dfdfbf; font-weight: bold;">union</span> vars1 vars2) (<span style="color: #8cd0d3;">*</span> coeff1 coeff2))))
|
rlm@2
|
608 {}
|
rlm@2
|
609 (cartesian-product (.terms dseq1) (.terms dseq2)))))
|
rlm@2
|
610
|
rlm@2
|
611
|
rlm@2
|
612
|
rlm@2
|
613 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">*</span> [DifferentialSeq DifferentialSeq]
|
rlm@2
|
614 [dseq1 dseq2]
|
rlm@2
|
615 (differential-multiply dseq1 dseq2))
|
rlm@2
|
616
|
rlm@2
|
617 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">+</span> [DifferentialSeq DifferentialSeq]
|
rlm@2
|
618 [dseq1 dseq2]
|
rlm@2
|
619 (DifferentialSeq.
|
rlm@2
|
620 (<span style="color: #8cd0d3;">merge-with</span> + (.terms dseq1) (.terms dseq2))))
|
rlm@2
|
621
|
rlm@2
|
622 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">*</span> [java.lang.Number DifferentialSeq]
|
rlm@2
|
623 [x dseq]
|
rlm@2
|
624 (fmap (<span style="color: #8cd0d3;">partial</span> * x) dseq))
|
rlm@2
|
625
|
rlm@2
|
626 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">*</span> [DifferentialSeq java.lang.Number]
|
rlm@2
|
627 [dseq x]
|
rlm@2
|
628 (fmap (<span style="color: #8cd0d3;">partial</span> * x) dseq))
|
rlm@2
|
629 <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">DERIVATIVES</span>
|
rlm@2
|
630
|
rlm@2
|
631
|
rlm@2
|
632
|
rlm@2
|
633 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">linear-approximator</span>
|
rlm@2
|
634 <span style="color: #8fb28f;">"Returns an operator that linearly approximates the given function."</span>
|
rlm@2
|
635 ([f df|dx]
|
rlm@2
|
636 (<span style="color: #8cd0d3;">fn</span> [x]
|
rlm@2
|
637 (<span style="color: #f0dfaf; font-weight: bold;">let</span> [big-part (big-part x)
|
rlm@2
|
638 small-part (small-part x)]
|
rlm@2
|
639 (<span style="color: #8cd0d3;">+</span> (fmap f big-part)
|
rlm@2
|
640 (<span style="color: #8cd0d3;">*</span> (fmap df|dx big-part) small-part)))))
|
rlm@2
|
641 ([f df|dx df|dy]
|
rlm@2
|
642 (<span style="color: #8cd0d3;">fn</span> [x y]
|
rlm@2
|
643 (<span style="color: #f0dfaf; font-weight: bold;">let</span> [X (big-part x)
|
rlm@2
|
644 Y (big-part y)
|
rlm@2
|
645 DX (small-part x)
|
rlm@2
|
646 DY (small-part y)]
|
rlm@2
|
647 (<span style="color: #8cd0d3;">+</span> (f X Y)
|
rlm@2
|
648 (<span style="color: #8cd0d3;">+</span> (<span style="color: #8cd0d3;">*</span> DX (df|dx X Y))
|
rlm@2
|
649 (<span style="color: #8cd0d3;">*</span> DY (df|dy X Y)))))))
|
rlm@2
|
650 )
|
rlm@2
|
651
|
rlm@2
|
652
|
rlm@2
|
653
|
rlm@2
|
654
|
rlm@2
|
655
|
rlm@2
|
656 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">D</span>[f]
|
rlm@2
|
657 (<span style="color: #8cd0d3;">fn</span>[x]
|
rlm@2
|
658 (f (differential-seq x [] 1 [0]))))
|
rlm@2
|
659
|
rlm@2
|
660 (<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">d</span>[f partials]
|
rlm@2
|
661 (<span style="color: #8cd0d3;">fn</span> [x]
|
rlm@2
|
662 (<span style="color: #8cd0d3;">get</span>
|
rlm@2
|
663 (.terms ((D f)x))
|
rlm@2
|
664 (<span style="color: #8cd0d3;">set</span> partials)
|
rlm@2
|
665 0
|
rlm@2
|
666 )))
|
rlm@2
|
667
|
rlm@2
|
668 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">exp</span> DifferentialSeq [x]
|
rlm@2
|
669 ((linear-approximator exp exp) x))
|
rlm@2
|
670
|
rlm@2
|
671 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">sin</span> DifferentialSeq
|
rlm@2
|
672 [x]
|
rlm@2
|
673 ((linear-approximator sin cos) x))
|
rlm@2
|
674
|
rlm@2
|
675 (<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">cos</span> DifferentialSeq
|
rlm@2
|
676 [x]
|
rlm@2
|
677 ((linear-approximator cos #(<span style="color: #8cd0d3;">-</span> (sin %))) x))
|
rlm@2
|
678
|
rlm@2
|
679
|
rlm@2
|
680
|
rlm@2
|
681 </pre>
|
rlm@2
|
682
|
rlm@2
|
683
|
rlm@2
|
684
|
rlm@2
|
685
|
rlm@2
|
686
|
rlm@2
|
687
|
rlm@2
|
688
|
rlm@2
|
689
|
rlm@2
|
690
|
rlm@2
|
691 </div>
|
rlm@2
|
692 </div>
|
rlm@2
|
693 </div>
|
rlm@2
|
694 <div id="postamble">
|
rlm@2
|
695 <p class="date">Date: 2011-08-11 04:10:36 EDT</p>
|
rlm@2
|
696 <p class="author">Author: Dylan Holmes</p>
|
rlm@2
|
697 <p class="creator">Org version 7.6 with Emacs version 23</p>
|
rlm@2
|
698 <a href="http://validator.w3.org/check?uri=referer">Validate XHTML 1.0</a>
|
rlm@2
|
699 </div>
|
rlm@2
|
700 </div>
|
rlm@2
|
701 </body>
|
rlm@2
|
702 </html>
|