diff sicm/bk/utils.html @ 2:b4de894a1e2e

initial import
author Robert McIntyre <rlm@mit.edu>
date Fri, 28 Oct 2011 00:03:05 -0700
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sicm/bk/utils.html	Fri Oct 28 00:03:05 2011 -0700
     1.3 @@ -0,0 +1,1482 @@
     1.4 +<?xml version="1.0" encoding="utf-8"?>
     1.5 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     1.6 +               "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     1.7 +<html xmlns="http://www.w3.org/1999/xhtml"
     1.8 +lang="en" xml:lang="en">
     1.9 +<head>
    1.10 +<title>Building a Classical Mechanics Library in Clojure</title>
    1.11 +<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    1.12 +<meta name="generator" content="Org-mode"/>
    1.13 +<meta name="generated" content="2011-08-09 18:41:37 EDT"/>
    1.14 +<meta name="author" content="Robert McIntyre & Dylan Holmes"/>
    1.15 +<meta name="description" content=""/>
    1.16 +<meta name="keywords" content=""/>
    1.17 +<style type="text/css">
    1.18 + <!--/*--><![CDATA[/*><!--*/
    1.19 +  html { font-family: Times, serif; font-size: 12pt; }
    1.20 +  .title  { text-align: center; }
    1.21 +  .todo   { color: red; }
    1.22 +  .done   { color: green; }
    1.23 +  .tag    { background-color: #add8e6; font-weight:normal }
    1.24 +  .target { }
    1.25 +  .timestamp { color: #bebebe; }
    1.26 +  .timestamp-kwd { color: #5f9ea0; }
    1.27 +  .right  {margin-left:auto; margin-right:0px;  text-align:right;}
    1.28 +  .left   {margin-left:0px;  margin-right:auto; text-align:left;}
    1.29 +  .center {margin-left:auto; margin-right:auto; text-align:center;}
    1.30 +  p.verse { margin-left: 3% }
    1.31 +  pre {
    1.32 +	border: 1pt solid #AEBDCC;
    1.33 +	background-color: #F3F5F7;
    1.34 +	padding: 5pt;
    1.35 +	font-family: courier, monospace;
    1.36 +        font-size: 90%;
    1.37 +        overflow:auto;
    1.38 +  }
    1.39 +  table { border-collapse: collapse; }
    1.40 +  td, th { vertical-align: top;  }
    1.41 +  th.right  { text-align:center;  }
    1.42 +  th.left   { text-align:center;   }
    1.43 +  th.center { text-align:center; }
    1.44 +  td.right  { text-align:right;  }
    1.45 +  td.left   { text-align:left;   }
    1.46 +  td.center { text-align:center; }
    1.47 +  dt { font-weight: bold; }
    1.48 +  div.figure { padding: 0.5em; }
    1.49 +  div.figure p { text-align: center; }
    1.50 +  textarea { overflow-x: auto; }
    1.51 +  .linenr { font-size:smaller }
    1.52 +  .code-highlighted {background-color:#ffff00;}
    1.53 +  .org-info-js_info-navigation { border-style:none; }
    1.54 +  #org-info-js_console-label { font-size:10px; font-weight:bold;
    1.55 +                               white-space:nowrap; }
    1.56 +  .org-info-js_search-highlight {background-color:#ffff00; color:#000000;
    1.57 +                                 font-weight:bold; }
    1.58 +  /*]]>*/-->
    1.59 +</style>
    1.60 +<link rel="stylesheet" type="text/css" href="../css/aurellem.css" />
    1.61 +<script type="text/javascript">
    1.62 +<!--/*--><![CDATA[/*><!--*/
    1.63 + function CodeHighlightOn(elem, id)
    1.64 + {
    1.65 +   var target = document.getElementById(id);
    1.66 +   if(null != target) {
    1.67 +     elem.cacheClassElem = elem.className;
    1.68 +     elem.cacheClassTarget = target.className;
    1.69 +     target.className = "code-highlighted";
    1.70 +     elem.className   = "code-highlighted";
    1.71 +   }
    1.72 + }
    1.73 + function CodeHighlightOff(elem, id)
    1.74 + {
    1.75 +   var target = document.getElementById(id);
    1.76 +   if(elem.cacheClassElem)
    1.77 +     elem.className = elem.cacheClassElem;
    1.78 +   if(elem.cacheClassTarget)
    1.79 +     target.className = elem.cacheClassTarget;
    1.80 + }
    1.81 +/*]]>*///-->
    1.82 +</script>
    1.83 +<script type="text/javascript" src="../MathJax/MathJax.js">
    1.84 +<!--/*--><![CDATA[/*><!--*/
    1.85 +    MathJax.Hub.Config({
    1.86 +        // Only one of the two following lines, depending on user settings
    1.87 +        // First allows browser-native MathML display, second forces HTML/CSS
    1.88 +            config: ["MMLorHTML.js"], jax: ["input/TeX"],
    1.89 +        //  jax: ["input/TeX", "output/HTML-CSS"],
    1.90 +        extensions: ["tex2jax.js","TeX/AMSmath.js","TeX/AMSsymbols.js",
    1.91 +                     "TeX/noUndefined.js"],
    1.92 +        tex2jax: {
    1.93 +            inlineMath: [ ["\\(","\\)"] ],
    1.94 +            displayMath: [ ['$$','$$'], ["\\[","\\]"], ["\\begin{displaymath}","\\end{displaymath}"] ],
    1.95 +            skipTags: ["script","noscript","style","textarea","pre","code"],
    1.96 +            ignoreClass: "tex2jax_ignore",
    1.97 +            processEscapes: false,
    1.98 +            processEnvironments: true,
    1.99 +            preview: "TeX"
   1.100 +        },
   1.101 +        showProcessingMessages: true,
   1.102 +        displayAlign: "left",
   1.103 +        displayIndent: "2em",
   1.104 +
   1.105 +        "HTML-CSS": {
   1.106 +             scale: 100,
   1.107 +             availableFonts: ["STIX","TeX"],
   1.108 +             preferredFont: "TeX",
   1.109 +             webFont: "TeX",
   1.110 +             imageFont: "TeX",
   1.111 +             showMathMenu: true,
   1.112 +        },
   1.113 +        MMLorHTML: {
   1.114 +             prefer: {
   1.115 +                 MSIE:    "MML",
   1.116 +                 Firefox: "MML",
   1.117 +                 Opera:   "HTML",
   1.118 +                 other:   "HTML"
   1.119 +             }
   1.120 +        }
   1.121 +    });
   1.122 +/*]]>*///-->
   1.123 +</script>
   1.124 +</head>
   1.125 +<body>
   1.126 +
   1.127 +<div id="content">
   1.128 +
   1.129 +
   1.130 +
   1.131 +<div class="header">
   1.132 +  <div class="float-right">	
   1.133 +    <!-- 
   1.134 +    <form>
   1.135 +      <input type="text"/><input type="submit" value="search the blog &raquo;"/> 
   1.136 +    </form>
   1.137 +    -->
   1.138 +  </div>
   1.139 +
   1.140 +  <h1>aurellem <em>&#x2609;</em></h1>
   1.141 +  <ul class="nav">
   1.142 +    <li><a href="/">read the blog &raquo;</a></li>
   1.143 +    <!-- li><a href="#">learn about us &raquo;</a></li-->
   1.144 +  </ul>
   1.145 +</div>
   1.146 +
   1.147 +<h1 class="title">Building a Classical Mechanics Library in Clojure</h1>
   1.148 +
   1.149 +
   1.150 +
   1.151 +
   1.152 +
   1.153 +
   1.154 +
   1.155 +<div id="table-of-contents">
   1.156 +<h2>Table of Contents</h2>
   1.157 +<div id="text-table-of-contents">
   1.158 +<ul>
   1.159 +<li><a href="#sec-1">1 Generic Arithmetic </a></li>
   1.160 +<li><a href="#sec-2">2 Useful Data Types </a>
   1.161 +<ul>
   1.162 +<li><a href="#sec-2-1">2.1 Complex Numbers </a></li>
   1.163 +<li><a href="#sec-2-2">2.2 Tuples and Tensors </a>
   1.164 +<ul>
   1.165 +<li><a href="#sec-2-2-1">2.2.1 Contraction </a></li>
   1.166 +<li><a href="#sec-2-2-2">2.2.2 Matrices </a></li>
   1.167 +</ul>
   1.168 +</li>
   1.169 +<li><a href="#sec-2-3">2.3 Power Series </a></li>
   1.170 +</ul>
   1.171 +</li>
   1.172 +<li><a href="#sec-3">3 Basic Utilities </a>
   1.173 +<ul>
   1.174 +<li><a href="#sec-3-1">3.1 Sequence manipulation </a></li>
   1.175 +<li><a href="#sec-3-2">3.2 Ranges, Airity and Function Composition </a></li>
   1.176 +</ul>
   1.177 +</li>
   1.178 +<li><a href="#sec-4">4 Numerical Methods </a></li>
   1.179 +<li><a href="#sec-5">5 Differentiation </a></li>
   1.180 +<li><a href="#sec-6">6 Symbolic Manipulation </a></li>
   1.181 +</ul>
   1.182 +</div>
   1.183 +</div>
   1.184 +
   1.185 +<div id="outline-container-1" class="outline-2">
   1.186 +<h2 id="sec-1"><span class="section-number-2">1</span> Generic Arithmetic </h2>
   1.187 +<div class="outline-text-2" id="text-1">
   1.188 +
   1.189 +
   1.190 +
   1.191 +
   1.192 +
   1.193 +<pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">ns</span> sicm.utils)
   1.194 +(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
   1.195 +
   1.196 +
   1.197 +
   1.198 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">all-equal?</span> [coll]
   1.199 +  (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">empty?</span> (<span style="color: #8cd0d3;">rest</span> coll)) true
   1.200 +      (<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))
   1.201 +           (<span style="color: #f0dfaf; font-weight: bold;">recur</span> (<span style="color: #8cd0d3;">rest</span> coll)))))
   1.202 +
   1.203 +
   1.204 +(<span style="color: #f0dfaf; font-weight: bold;">defprotocol</span> <span style="color: #f0dfaf;">Arithmetic</span>
   1.205 +  (zero [this])
   1.206 +  (one [this])
   1.207 +  (negate [this])
   1.208 +  (invert [this])
   1.209 +  (conjugate [this]))
   1.210 +
   1.211 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">zero?</span> [x]
   1.212 +  (<span style="color: #8cd0d3;">=</span> x (zero x)))
   1.213 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">one?</span> [x]
   1.214 +  (<span style="color: #8cd0d3;">=</span> x (one x)))
   1.215 +
   1.216 +
   1.217 +
   1.218 +(<span style="color: #8cd0d3;">extend-protocol</span> Arithmetic
   1.219 +  java.lang.Number
   1.220 +  (zero [x] 0)
   1.221 +  (one [x] 1)
   1.222 +  (negate [x] (<span style="color: #8cd0d3;">-</span> x))
   1.223 +  (invert [x] (<span style="color: #8cd0d3;">/</span> x))
   1.224 +  )
   1.225 +
   1.226 +(<span style="color: #8cd0d3;">extend-protocol</span> Arithmetic
   1.227 +  clojure.lang.IFn
   1.228 +  (one [f] identity)
   1.229 +  (negate [f] (<span style="color: #8cd0d3;">comp</span> negate f))
   1.230 +  (invert [f] (<span style="color: #8cd0d3;">comp</span> invert f)))
   1.231 +
   1.232 +    
   1.233 +(<span style="color: #8cd0d3;">extend-protocol</span> Arithmetic
   1.234 +  clojure.lang.Seqable
   1.235 +  (zero [this] (<span style="color: #8cd0d3;">map</span> zero this))
   1.236 +  (one [this] (<span style="color: #8cd0d3;">map</span> one this))
   1.237 +  (invert [this] (<span style="color: #8cd0d3;">map</span> invert this))
   1.238 +  (negate [this] (<span style="color: #8cd0d3;">map</span> negate this)))
   1.239 +
   1.240 +
   1.241 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">ordered-like</span>
   1.242 +  <span style="color: #8fb28f;">"Create a comparator using the sorted collection as an</span>
   1.243 +<span style="color: #8fb28f;">  example. Elements not in the sorted collection are sorted to the</span>
   1.244 +<span style="color: #8fb28f;">  end."</span>
   1.245 +  [sorted-coll]
   1.246 +  (<span style="color: #f0dfaf; font-weight: bold;">let</span> [
   1.247 +        sorted-coll? (<span style="color: #8cd0d3;">set</span> sorted-coll) 
   1.248 +        ascending-pair?
   1.249 +        (<span style="color: #8cd0d3;">set</span>(<span style="color: #8cd0d3;">reduce</span> concat
   1.250 +                    (map-indexed
   1.251 +                     (<span style="color: #8cd0d3;">fn</span> [n x]
   1.252 +                       (<span style="color: #8cd0d3;">map</span> #(<span style="color: #8cd0d3;">vector</span> x %) (<span style="color: #8cd0d3;">nthnext</span> sorted-coll n)))
   1.253 +                     sorted-coll)))]
   1.254 +    (<span style="color: #8cd0d3;">fn</span> [x y]
   1.255 +      (<span style="color: #f0dfaf; font-weight: bold;">cond</span>
   1.256 +       (<span style="color: #8cd0d3;">=</span> x y) 0
   1.257 +       (ascending-pair? [x y]) -1
   1.258 +       (ascending-pair? [y x]) 1
   1.259 +       (sorted-coll? x) -1
   1.260 +       (sorted-coll? y) 1))))
   1.261 +  
   1.262 +
   1.263 +
   1.264 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">type-precedence</span>
   1.265 +  (ordered-like [incanter.Matrix]))
   1.266 +
   1.267 +(<span style="color: #f0dfaf; font-weight: bold;">defmulti</span> <span style="color: #f0dfaf;">add</span>
   1.268 +    (<span style="color: #8cd0d3;">fn</span> [x y]
   1.269 +      (<span style="color: #8cd0d3;">sort</span> type-precedence [(<span style="color: #8cd0d3;">type</span> x)(<span style="color: #8cd0d3;">type</span> y)]))) 
   1.270 +
   1.271 +(<span style="color: #f0dfaf; font-weight: bold;">defmulti</span> <span style="color: #f0dfaf;">multiply</span>
   1.272 +  (<span style="color: #8cd0d3;">fn</span> [x y]
   1.273 +    (<span style="color: #8cd0d3;">sort</span> type-precedence [(<span style="color: #8cd0d3;">type</span> x) (<span style="color: #8cd0d3;">type</span> y)])))
   1.274 +
   1.275 +(<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">add</span> [java.lang.Number java.lang.Number] [x y] (<span style="color: #8cd0d3;">+</span> x y))
   1.276 +(<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">multiply</span> [java.lang.Number java.lang.Number] [x y] (<span style="color: #8cd0d3;">*</span> x y))
   1.277 +
   1.278 +(<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">multiply</span> [incanter.Matrix java.lang.Integer] [x y]
   1.279 +           (<span style="color: #f0dfaf; font-weight: bold;">let</span> [args (<span style="color: #8cd0d3;">sort</span> #(type-precedence (<span style="color: #8cd0d3;">type</span> %1)(<span style="color: #8cd0d3;">type</span> %2)) [x y])
   1.280 +                 matrix (<span style="color: #8cd0d3;">first</span> args)
   1.281 +                 scalar (<span style="color: #8cd0d3;">second</span> args)]
   1.282 +             (incanter.core/matrix (<span style="color: #8cd0d3;">map</span> (<span style="color: #8cd0d3;">partial</span> map (<span style="color: #8cd0d3;">partial</span> multiply scalar)) matrix))))
   1.283 +           
   1.284 +</pre>
   1.285 +
   1.286 +
   1.287 +
   1.288 +
   1.289 +
   1.290 +</div>
   1.291 +
   1.292 +</div>
   1.293 +
   1.294 +<div id="outline-container-2" class="outline-2">
   1.295 +<h2 id="sec-2"><span class="section-number-2">2</span> Useful Data Types </h2>
   1.296 +<div class="outline-text-2" id="text-2">
   1.297 +
   1.298 +
   1.299 +
   1.300 +</div>
   1.301 +
   1.302 +<div id="outline-container-2-1" class="outline-3">
   1.303 +<h3 id="sec-2-1"><span class="section-number-3">2.1</span> Complex Numbers </h3>
   1.304 +<div class="outline-text-3" id="text-2-1">
   1.305 +
   1.306 +
   1.307 +
   1.308 +
   1.309 +<pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
   1.310 +
   1.311 +(<span style="color: #f0dfaf; font-weight: bold;">defprotocol</span> <span style="color: #f0dfaf;">Complex</span>
   1.312 +  (real-part [z])
   1.313 +  (imaginary-part [z])
   1.314 +  (magnitude-squared [z])
   1.315 +  (angle [z])
   1.316 +  (conjugate [z])
   1.317 +  (norm [z]))
   1.318 +
   1.319 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">complex-rectangular</span>
   1.320 +  <span style="color: #8fb28f;">"Define a complex number with the given real and imaginary</span>
   1.321 +<span style="color: #8fb28f;">  components."</span>
   1.322 +  [re im]
   1.323 +  (<span style="color: #8cd0d3;">reify</span> Complex
   1.324 +         (real-part [z] re)
   1.325 +         (imaginary-part [z] im)
   1.326 +         (magnitude-squared [z] (<span style="color: #8cd0d3;">+</span> (<span style="color: #8cd0d3;">*</span> re re) (<span style="color: #8cd0d3;">*</span> im im)))
   1.327 +         (angle [z] (java.lang.Math/atan2 im re))
   1.328 +         (conjugate [z] (complex-rectangular re (<span style="color: #8cd0d3;">-</span> im)))
   1.329 +
   1.330 +         Arithmetic
   1.331 +         (zero [z] (complex-rectangular 0 0))
   1.332 +         (one [z] (complex-rectangular 1 0))
   1.333 +         (negate [z] (complex-rectangular (<span style="color: #8cd0d3;">-</span> re) (<span style="color: #8cd0d3;">-</span> im)))
   1.334 +         (invert [z] (complex-rectangular
   1.335 +                      (<span style="color: #8cd0d3;">/</span> re (magnitude-squared z))
   1.336 +                      (<span style="color: #8cd0d3;">/</span> (<span style="color: #8cd0d3;">-</span> im) (magnitude-squared z))))
   1.337 +
   1.338 +         Object
   1.339 +         (toString [_]
   1.340 +                   (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #f0dfaf; font-weight: bold;">and</span> (zero? re) (zero? im)) (<span style="color: #8cd0d3;">str</span> 0)
   1.341 +                       (<span style="color: #8cd0d3;">str</span>
   1.342 +                        (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">not</span>(zero? re))
   1.343 +                          re)
   1.344 +                        (<span style="color: #f0dfaf; font-weight: bold;">if</span> ((<span style="color: #8cd0d3;">comp</span> not zero?) im)
   1.345 +                          (<span style="color: #8cd0d3;">str</span>
   1.346 +                           (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">neg?</span> im) <span style="color: #cc9393;">"-"</span> <span style="color: #cc9393;">"+"</span>)
   1.347 +                           (<span style="color: #f0dfaf; font-weight: bold;">if</span> ((<span style="color: #8cd0d3;">comp</span> not one?) (java.lang.Math/abs im))
   1.348 +                           (java.lang.Math/abs im))
   1.349 +                           <span style="color: #cc9393;">"i"</span>)))))))
   1.350 +
   1.351 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">complex-polar</span>
   1.352 +  <span style="color: #8fb28f;">"Define a complex number with the given magnitude and angle."</span>
   1.353 +  [mag ang]
   1.354 +  (<span style="color: #8cd0d3;">reify</span> Complex
   1.355 +         (magnitude-squared [z] (<span style="color: #8cd0d3;">*</span> mag mag))
   1.356 +         (angle [z] angle)
   1.357 +         (real-part [z] (<span style="color: #8cd0d3;">*</span> mag (java.lang.Math/cos ang)))
   1.358 +         (imaginary-part [z] (<span style="color: #8cd0d3;">*</span> mag (java.lang.Math/sin ang)))
   1.359 +         (conjugate [z] (complex-polar mag (<span style="color: #8cd0d3;">-</span> ang)))
   1.360 +
   1.361 +         Arithmetic
   1.362 +         (zero [z] (complex-polar 0 0))
   1.363 +         (one [z] (complex-polar 1 0))
   1.364 +         (negate [z] (complex-polar (<span style="color: #8cd0d3;">-</span> mag) ang))
   1.365 +         (invert [z] (complex-polar (<span style="color: #8cd0d3;">/</span> mag) (<span style="color: #8cd0d3;">-</span> ang)))
   1.366 +
   1.367 +         Object
   1.368 +         (toString [_] (<span style="color: #8cd0d3;">str</span> mag <span style="color: #cc9393;">" * e^(i"</span> ang<span style="color: #cc9393;">")"</span>))
   1.369 +         ))
   1.370 +
   1.371 +
   1.372 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">Numbers are complex quantities</span>
   1.373 +
   1.374 +(<span style="color: #8cd0d3;">extend-protocol</span> Complex
   1.375 +  java.lang.Number
   1.376 +  (real-part [x] x)
   1.377 +  (imaginary-part [x] 0)
   1.378 +  (magnitude [x] x)
   1.379 +  (angle [x] 0)
   1.380 +  (conjugate [x] x))
   1.381 +
   1.382 +
   1.383 +</pre>
   1.384 +
   1.385 +
   1.386 +
   1.387 +
   1.388 +
   1.389 +
   1.390 +</div>
   1.391 +
   1.392 +</div>
   1.393 +
   1.394 +<div id="outline-container-2-2" class="outline-3">
   1.395 +<h3 id="sec-2-2"><span class="section-number-3">2.2</span> Tuples and Tensors </h3>
   1.396 +<div class="outline-text-3" id="text-2-2">
   1.397 +
   1.398 +
   1.399 +<p>
   1.400 +A tuple is a vector which is spinable&mdash;it can be either <i>spin up</i> or <i>spin down</i>. (Covariant, contravariant; dual vectors)
   1.401 +</p>
   1.402 +
   1.403 +
   1.404 +<pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
   1.405 +
   1.406 +(<span style="color: #f0dfaf; font-weight: bold;">defprotocol</span> <span style="color: #f0dfaf;">Spinning</span>
   1.407 +  (up? [this])
   1.408 +  (down? [this]))
   1.409 +
   1.410 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">spin</span>
   1.411 +  <span style="color: #8fb28f;">"Returns the spin of the Spinning s, either :up or :down"</span>
   1.412 +  [<span style="color: #dfdfbf; font-weight: bold;">#^Spinning</span> s]
   1.413 +  (<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>))
   1.414 +
   1.415 +
   1.416 +(<span style="color: #f0dfaf; font-weight: bold;">deftype</span> <span style="color: #f0dfaf;">Tuple</span>
   1.417 +  [spin coll]
   1.418 +  clojure.lang.Seqable
   1.419 +  (<span style="color: #8cd0d3;">seq</span> [this] (<span style="color: #8cd0d3;">seq</span> (.coll this)))
   1.420 +  clojure.lang.Counted
   1.421 +  (<span style="color: #8cd0d3;">count</span> [this] (<span style="color: #8cd0d3;">count</span> (.coll this))))
   1.422 +
   1.423 +(<span style="color: #8cd0d3;">extend-type</span> Tuple
   1.424 +  Spinning
   1.425 +  (up? [this] (<span style="color: #8cd0d3;">=</span> <span style="color: #8cd0d3;">::up</span> (.spin this)))
   1.426 +  (down? [this] (<span style="color: #8cd0d3;">=</span> <span style="color: #8cd0d3;">::down</span> (.spin this))))
   1.427 +
   1.428 +(<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">print-method</span> Tuple
   1.429 +  [o w]
   1.430 +  (<span style="color: #8cd0d3;">print-simple</span> (<span style="color: #8cd0d3;">str</span> (<span style="color: #f0dfaf; font-weight: bold;">if</span> (up? o) 'u 'd) (.coll o))  w))
   1.431 +
   1.432 +
   1.433 +
   1.434 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">up</span>
   1.435 +  <span style="color: #8fb28f;">"Create a new up-tuple containing the contents of coll."</span>
   1.436 +  [coll]
   1.437 +  (Tuple. <span style="color: #8cd0d3;">::up</span> coll))       
   1.438 +
   1.439 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">down</span>
   1.440 +  <span style="color: #8fb28f;">"Create a new down-tuple containing the contents of coll."</span>
   1.441 +  [coll]
   1.442 +  (Tuple. <span style="color: #8cd0d3;">::down</span> coll))
   1.443 +
   1.444 +
   1.445 +</pre>
   1.446 +
   1.447 +
   1.448 +
   1.449 +
   1.450 +
   1.451 +</div>
   1.452 +
   1.453 +<div id="outline-container-2-2-1" class="outline-4">
   1.454 +<h4 id="sec-2-2-1"><span class="section-number-4">2.2.1</span> Contraction </h4>
   1.455 +<div class="outline-text-4" id="text-2-2-1">
   1.456 +
   1.457 +<p>Contraction is a binary operation that you can apply to compatible
   1.458 + tuples. Tuples are compatible for contraction if they have the same
   1.459 + length and opposite spins, and if the corresponding items in each
   1.460 + tuple are both numbers or both compatible tuples.
   1.461 +</p>
   1.462 +
   1.463 +
   1.464 +
   1.465 +<pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
   1.466 +
   1.467 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">numbers?</span>
   1.468 +  <span style="color: #8fb28f;">"Returns true if all arguments are numbers, else false."</span>
   1.469 +  [&amp; xs]
   1.470 +  (<span style="color: #8cd0d3;">every?</span> number? xs))
   1.471 +
   1.472 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">contractible?</span>
   1.473 +  <span style="color: #8fb28f;">"Returns true if the tuples a and b are compatible for contraction,</span>
   1.474 +<span style="color: #8fb28f;">  else false. Tuples are compatible if they have the same number of</span>
   1.475 +<span style="color: #8fb28f;">  components, they have opposite spins, and their elements are</span>
   1.476 +<span style="color: #8fb28f;">  pairwise-compatible."</span>
   1.477 +  [a b]
   1.478 +  (<span style="color: #f0dfaf; font-weight: bold;">and</span>
   1.479 +   (<span style="color: #8cd0d3;">isa?</span> (<span style="color: #8cd0d3;">type</span> a) Tuple)
   1.480 +   (<span style="color: #8cd0d3;">isa?</span> (<span style="color: #8cd0d3;">type</span> b) Tuple)
   1.481 +   (<span style="color: #8cd0d3;">=</span> (<span style="color: #8cd0d3;">count</span> a) (<span style="color: #8cd0d3;">count</span> b))
   1.482 +   (<span style="color: #8cd0d3;">not=</span> (spin a) (spin b))
   1.483 +   
   1.484 +   (<span style="color: #8cd0d3;">not-any?</span> false?
   1.485 +             (<span style="color: #8cd0d3;">map</span> #(<span style="color: #f0dfaf; font-weight: bold;">or</span>
   1.486 +                    (numbers? %1 %2)
   1.487 +                    (contractible? %1 %2))
   1.488 +                  a b))))
   1.489 +
   1.490 +
   1.491 +
   1.492 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">contract</span>
   1.493 +  <span style="color: #8fb28f;">"Contracts two tuples, returning the sum of the</span>
   1.494 +<span style="color: #8fb28f;">  products of the corresponding items. Contraction is recursive on</span>
   1.495 +<span style="color: #8fb28f;">  nested tuples."</span>
   1.496 +  [a b]
   1.497 +  (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">not</span> (contractible? a b))
   1.498 +    (<span style="color: #f0dfaf; font-weight: bold;">throw</span>
   1.499 +     (Exception. <span style="color: #cc9393;">"Not compatible for contraction."</span>))
   1.500 +    (<span style="color: #8cd0d3;">reduce</span> +
   1.501 +            (<span style="color: #8cd0d3;">map</span>
   1.502 +             (<span style="color: #8cd0d3;">fn</span> [x y]
   1.503 +               (<span style="color: #f0dfaf; font-weight: bold;">if</span> (numbers? x y)
   1.504 +                 (<span style="color: #8cd0d3;">*</span> x y)
   1.505 +                 (contract x y)))
   1.506 +             a b))))
   1.507 +
   1.508 +</pre>
   1.509 +
   1.510 +
   1.511 +
   1.512 +
   1.513 +</div>
   1.514 +
   1.515 +</div>
   1.516 +
   1.517 +<div id="outline-container-2-2-2" class="outline-4">
   1.518 +<h4 id="sec-2-2-2"><span class="section-number-4">2.2.2</span> Matrices </h4>
   1.519 +<div class="outline-text-4" id="text-2-2-2">
   1.520 +
   1.521 +
   1.522 +
   1.523 +
   1.524 +<pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
   1.525 +(<span style="color: #8cd0d3;">require</span> 'incanter.core) <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">use incanter's fast matrices</span>
   1.526 +
   1.527 +(<span style="color: #f0dfaf; font-weight: bold;">defprotocol</span> <span style="color: #f0dfaf;">Matrix</span>
   1.528 +  (rows [matrix])
   1.529 +  (cols [matrix])
   1.530 +  (diagonal [matrix])
   1.531 +  (trace [matrix])
   1.532 +  (determinant [matrix])
   1.533 +  (transpose [matrix])
   1.534 +  (conjugate [matrix])
   1.535 +)
   1.536 +
   1.537 +(<span style="color: #8cd0d3;">extend-protocol</span> Matrix
   1.538 +  incanter.Matrix
   1.539 +  (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))))
   1.540 +  (cols [rs] (<span style="color: #8cd0d3;">map</span> up (<span style="color: #8cd0d3;">apply</span> map vector rs)))
   1.541 +  (diagonal [matrix] (incanter.core/diag matrix) )
   1.542 +  (determinant [matrix] (incanter.core/det matrix))
   1.543 +  (trace [matrix] (incanter.core/trace matrix))
   1.544 +  (transpose [matrix] (incanter.core/trans matrix))
   1.545 +  )
   1.546 +
   1.547 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">count-rows</span> [matrix]
   1.548 +  ((<span style="color: #8cd0d3;">comp</span> count rows) matrix))
   1.549 +
   1.550 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">count-cols</span> [matrix]
   1.551 +  ((<span style="color: #8cd0d3;">comp</span> count cols) matrix))
   1.552 +
   1.553 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">square?</span> [matrix]
   1.554 +  (<span style="color: #8cd0d3;">=</span> (count-rows matrix) (count-cols matrix)))
   1.555 +
   1.556 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">identity-matrix</span>
   1.557 +  <span style="color: #8fb28f;">"Define a square matrix of size n-by-n with 1s along the diagonal and</span>
   1.558 +<span style="color: #8fb28f;">  0s everywhere else."</span>
   1.559 +  [n]
   1.560 +  (incanter.core/identity-matrix n))
   1.561 +
   1.562 +
   1.563 +
   1.564 +
   1.565 +
   1.566 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">matrix-by-rows</span>
   1.567 +  <span style="color: #8fb28f;">"Define a matrix by giving its rows."</span>
   1.568 +  [&amp; rows]
   1.569 +  (<span style="color: #f0dfaf; font-weight: bold;">if</span>
   1.570 +   (<span style="color: #8cd0d3;">not</span> (all-equal? (<span style="color: #8cd0d3;">map</span> count rows)))
   1.571 +   (<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>))
   1.572 +   (incanter.core/matrix (<span style="color: #8cd0d3;">vec</span> rows))))
   1.573 +
   1.574 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">matrix-by-cols</span>
   1.575 +  <span style="color: #8fb28f;">"Define a matrix by giving its columns"</span>
   1.576 +  [&amp; cols]
   1.577 +  (<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)))
   1.578 +   (<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>))
   1.579 +   (incanter.core/matrix (<span style="color: #8cd0d3;">vec</span> (<span style="color: #8cd0d3;">apply</span> map vector cols)))))
   1.580 +
   1.581 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">identity-matrix</span>
   1.582 +  <span style="color: #8fb28f;">"Define a square matrix of size n-by-n with 1s along the diagonal and</span>
   1.583 +<span style="color: #8fb28f;">  0s everywhere else."</span>
   1.584 +  [n]
   1.585 +  (incanter.core/identity-matrix n))
   1.586 +
   1.587 +
   1.588 +
   1.589 +(<span style="color: #8cd0d3;">extend-protocol</span> Arithmetic
   1.590 +  incanter.Matrix
   1.591 +  (one [matrix]
   1.592 +       (<span style="color: #f0dfaf; font-weight: bold;">if</span> (square? matrix)
   1.593 +         (identity-matrix (count-rows matrix))
   1.594 +         (<span style="color: #f0dfaf; font-weight: bold;">throw</span> (Exception. <span style="color: #cc9393;">"Non-square matrices have no multiplicative unit."</span>))))
   1.595 +  (zero [matrix]
   1.596 +        (<span style="color: #8cd0d3;">apply</span> matrix-by-rows (<span style="color: #8cd0d3;">map</span> zero (rows matrix))))
   1.597 +  (negate [matrix]
   1.598 +          (<span style="color: #8cd0d3;">apply</span> matrix-by-rows (<span style="color: #8cd0d3;">map</span> negate (rows matrix))))
   1.599 +  (invert [matrix]
   1.600 +          (incanter.core/solve matrix)))
   1.601 +
   1.602 +
   1.603 +
   1.604 +(<span style="color: #f0dfaf; font-weight: bold;">defmulti</span> <span style="color: #f0dfaf;">coerce-to-matrix</span>
   1.605 + <span style="color: #8fb28f;">"Converts x into a matrix, if possible."</span>
   1.606 + type)
   1.607 +
   1.608 +(<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">coerce-to-matrix</span> incanter.Matrix [x] x)
   1.609 +(<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">coerce-to-matrix</span> Tuple [x]
   1.610 +           (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">apply</span> numbers? (<span style="color: #8cd0d3;">seq</span> x))
   1.611 +             (<span style="color: #f0dfaf; font-weight: bold;">if</span> (up? x)
   1.612 +             (matrix-by-cols (<span style="color: #8cd0d3;">seq</span> x))
   1.613 +             (matrix-by-rows (<span style="color: #8cd0d3;">seq</span> x)))
   1.614 +             (<span style="color: #f0dfaf; font-weight: bold;">throw</span> (Exception. <span style="color: #cc9393;">"Non-numerical tuple cannot be converted into a matrix."</span>)))) 
   1.615 +             
   1.616 +  
   1.617 +
   1.618 +
   1.619 +
   1.620 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">(defn matrix-by-cols</span>
   1.621 +<span style="color: #708070;">;;   </span><span style="color: #7f9f7f;">"Define a matrix by giving its columns."</span>
   1.622 +<span style="color: #708070;">;;   </span><span style="color: #7f9f7f;">[&amp; cols]</span>
   1.623 +<span style="color: #708070;">;;   </span><span style="color: #7f9f7f;">(cond</span>
   1.624 +<span style="color: #708070;">;;    </span><span style="color: #7f9f7f;">(not (all-equal? (map count cols)))</span>
   1.625 +<span style="color: #708070;">;;    </span><span style="color: #7f9f7f;">(throw (Exception. "All columns in a matrix must have the same number of elements."))</span>
   1.626 +<span style="color: #708070;">;;    </span><span style="color: #7f9f7f;">:else</span>
   1.627 +<span style="color: #708070;">;;    </span><span style="color: #7f9f7f;">(reify Matrix</span>
   1.628 +<span style="color: #708070;">;;        </span><span style="color: #7f9f7f;">(cols [this] (map up cols))</span>
   1.629 +<span style="color: #708070;">;;        </span><span style="color: #7f9f7f;">(rows [this] (map down (apply map vector cols)))</span>
   1.630 +<span style="color: #708070;">;;        </span><span style="color: #7f9f7f;">(diagonal [this] (map-indexed (fn [i col] (nth col i) cols)))</span>
   1.631 +<span style="color: #708070;">;;        </span><span style="color: #7f9f7f;">(trace [this]</span>
   1.632 +<span style="color: #708070;">;;               </span><span style="color: #7f9f7f;">(if (not= (count-cols this) (count-rows this))</span>
   1.633 +<span style="color: #708070;">;;                 </span><span style="color: #7f9f7f;">(throw (Exception.</span>
   1.634 +<span style="color: #708070;">;;                         </span><span style="color: #7f9f7f;">"Cannot take the trace of a non-square matrix."))</span>
   1.635 +<span style="color: #708070;">;;                 </span><span style="color: #7f9f7f;">(reduce + (diagonal this))))</span>
   1.636 +          
   1.637 +<span style="color: #708070;">;;        </span><span style="color: #7f9f7f;">(determinant [this]</span>
   1.638 +<span style="color: #708070;">;;                     </span><span style="color: #7f9f7f;">(if (not= (count-cols this) (count-rows this))</span>
   1.639 +<span style="color: #708070;">;;                       </span><span style="color: #7f9f7f;">(throw (Exception.</span>
   1.640 +<span style="color: #708070;">;;                               </span><span style="color: #7f9f7f;">"Cannot take the determinant of a non-square matrix."))</span>
   1.641 +<span style="color: #708070;">;;                       </span><span style="color: #7f9f7f;">(reduce * (map-indexed (fn [i col] (nth col i)) cols))))</span>
   1.642 +<span style="color: #708070;">;;        </span><span style="color: #7f9f7f;">)))</span>
   1.643 +
   1.644 +(<span style="color: #8cd0d3;">extend-protocol</span> Matrix  Tuple
   1.645 +                 (rows [this] (<span style="color: #f0dfaf; font-weight: bold;">if</span> (down? this)
   1.646 +                                (<span style="color: #8cd0d3;">list</span> this)
   1.647 +                                (<span style="color: #8cd0d3;">map</span> (<span style="color: #8cd0d3;">comp</span> up vector) this)))
   1.648 +
   1.649 +                 (cols [this] (<span style="color: #f0dfaf; font-weight: bold;">if</span> (up? this)
   1.650 +                                (<span style="color: #8cd0d3;">list</span> this)
   1.651 +                                (<span style="color: #8cd0d3;">map</span> (<span style="color: #8cd0d3;">comp</span> down vector) this))
   1.652 +                 ))
   1.653 +  
   1.654 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">matrix-multiply</span>
   1.655 +  <span style="color: #8fb28f;">"Returns the matrix resulting from the matrix multiplication of the given arguments."</span>
   1.656 +  ([A] (coerce-to-matrix A))
   1.657 +  ([A B] (incanter.core/mmult (coerce-to-matrix A) (coerce-to-matrix B)))
   1.658 +  ([M1 M2 &amp; Ms] (<span style="color: #8cd0d3;">reduce</span> matrix-multiply (matrix-multiply M1 M2) Ms)))
   1.659 +
   1.660 +</pre>
   1.661 +
   1.662 +
   1.663 +
   1.664 +
   1.665 +
   1.666 +</div>
   1.667 +</div>
   1.668 +
   1.669 +</div>
   1.670 +
   1.671 +<div id="outline-container-2-3" class="outline-3">
   1.672 +<h3 id="sec-2-3"><span class="section-number-3">2.3</span> Power Series </h3>
   1.673 +<div class="outline-text-3" id="text-2-3">
   1.674 +
   1.675 +
   1.676 +
   1.677 +
   1.678 +<pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
   1.679 +(<span style="color: #8cd0d3;">use</span> 'clojure.contrib.def)
   1.680 +
   1.681 +
   1.682 +
   1.683 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">series-fn</span>
   1.684 +  <span style="color: #8fb28f;">"The function corresponding to the given power series."</span>
   1.685 +  [series]
   1.686 +  (<span style="color: #8cd0d3;">fn</span> [x]
   1.687 +    (<span style="color: #8cd0d3;">reduce</span> +
   1.688 +            (map-indexed  (<span style="color: #8cd0d3;">fn</span>[n x] (<span style="color: #8cd0d3;">*</span> (<span style="color: #8cd0d3;">float</span> (<span style="color: #8cd0d3;">nth</span> series n)) (<span style="color: #8cd0d3;">float</span>(java.lang.Math/pow (<span style="color: #8cd0d3;">float</span> x) n)) ))
   1.689 +                          (<span style="color: #8cd0d3;">range</span> 20)))))
   1.690 +
   1.691 +(<span style="color: #f0dfaf; font-weight: bold;">deftype</span> <span style="color: #f0dfaf;">PowerSeries</span>
   1.692 +  [coll]
   1.693 +  clojure.lang.Seqable
   1.694 +  (<span style="color: #8cd0d3;">seq</span> [this] (<span style="color: #8cd0d3;">seq</span> (.coll this)))
   1.695 +  
   1.696 +  clojure.lang.Indexed
   1.697 +  (<span style="color: #8cd0d3;">nth</span> [this n] (<span style="color: #8cd0d3;">nth</span> (.coll this) n 0))
   1.698 +  (<span style="color: #8cd0d3;">nth</span> [this n not-found] (<span style="color: #8cd0d3;">nth</span> (.coll this) n not-found))
   1.699 +
   1.700 +  <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">clojure.lang.IFn</span>
   1.701 +  <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">(call [this] (throw(Exception.)))</span>
   1.702 +  <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">(invoke [this &amp; args] args</span>
   1.703 +  <span style="color: #708070;">;;      </span><span style="color: #7f9f7f;">(let [f </span>
   1.704 +  <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">)</span>
   1.705 +  <span style="color: #708070;">;; </span><span style="color: #7f9f7f;">(run [this] (throw(Exception.)))</span>
   1.706 +  )
   1.707 +
   1.708 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">power-series</span>
   1.709 +  <span style="color: #8fb28f;">"Returns a power series with the items of the coll as its</span>
   1.710 +<span style="color: #8fb28f;">  coefficients. Trailing zeros are added to the end of coll."</span>
   1.711 +  [coeffs]
   1.712 +  (PowerSeries. coeffs))
   1.713 +
   1.714 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">power-series-indexed</span>
   1.715 +  <span style="color: #8fb28f;">"Returns a power series consisting of the result of mapping f to the non-negative integers."</span>
   1.716 +  [f]
   1.717 +  (PowerSeries. (<span style="color: #8cd0d3;">map</span> f (<span style="color: #8cd0d3;">range</span>))))
   1.718 +
   1.719 +
   1.720 +(<span style="color: #f0dfaf; font-weight: bold;">defn-memo</span> <span style="color: #f0dfaf;">nth-partial-sum</span>
   1.721 +  ([series n]
   1.722 +     (<span style="color: #f0dfaf; font-weight: bold;">if</span> (zero? n) (<span style="color: #8cd0d3;">first</span> series)
   1.723 +         (<span style="color: #8cd0d3;">+</span> (<span style="color: #8cd0d3;">nth</span> series n)
   1.724 +            (nth-partial-sum series (<span style="color: #8cd0d3;">dec</span> n))))))
   1.725 +
   1.726 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">partial-sums</span> [series]
   1.727 +  (<span style="color: #8cd0d3;">lazy-seq</span> (<span style="color: #8cd0d3;">map</span> nth-partial-sum (<span style="color: #8cd0d3;">range</span>))))
   1.728 +
   1.729 +
   1.730 +
   1.731 +
   1.732 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">cos-series</span>
   1.733 +     (power-series-indexed
   1.734 +      (<span style="color: #8cd0d3;">fn</span>[n]
   1.735 +        (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">odd?</span> n) 0
   1.736 +            (<span style="color: #8cd0d3;">/</span>
   1.737 +             (<span style="color: #8cd0d3;">reduce</span> *
   1.738 +                     (<span style="color: #8cd0d3;">reduce</span> * (<span style="color: #8cd0d3;">repeat</span> (<span style="color: #8cd0d3;">/</span> n 2) -1))
   1.739 +                     (<span style="color: #8cd0d3;">range</span> 1 (<span style="color: #8cd0d3;">inc</span> n)))
   1.740 +             )))))
   1.741 +
   1.742 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">sin-series</span>
   1.743 +     (power-series-indexed
   1.744 +      (<span style="color: #8cd0d3;">fn</span>[n]
   1.745 +        (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">even?</span> n) 0
   1.746 +            (<span style="color: #8cd0d3;">/</span>
   1.747 +             (<span style="color: #8cd0d3;">reduce</span> *
   1.748 +                     (<span style="color: #8cd0d3;">reduce</span> * (<span style="color: #8cd0d3;">repeat</span> (<span style="color: #8cd0d3;">/</span> (<span style="color: #8cd0d3;">dec</span> n) 2) -1))
   1.749 +                     (<span style="color: #8cd0d3;">range</span> 1 (<span style="color: #8cd0d3;">inc</span> n)))
   1.750 +             )))))
   1.751 +
   1.752 +</pre>
   1.753 +
   1.754 +
   1.755 +
   1.756 +
   1.757 +
   1.758 +</div>
   1.759 +</div>
   1.760 +
   1.761 +</div>
   1.762 +
   1.763 +<div id="outline-container-3" class="outline-2">
   1.764 +<h2 id="sec-3"><span class="section-number-2">3</span> Basic Utilities </h2>
   1.765 +<div class="outline-text-2" id="text-3">
   1.766 +
   1.767 +
   1.768 +
   1.769 +</div>
   1.770 +
   1.771 +<div id="outline-container-3-1" class="outline-3">
   1.772 +<h3 id="sec-3-1"><span class="section-number-3">3.1</span> Sequence manipulation </h3>
   1.773 +<div class="outline-text-3" id="text-3-1">
   1.774 +
   1.775 +
   1.776 +
   1.777 +
   1.778 +
   1.779 +<pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">ns</span> sicm.utils)
   1.780 +
   1.781 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">do-up</span>
   1.782 +  <span style="color: #8fb28f;">"Apply f to each number from low to high, presumably for</span>
   1.783 +<span style="color: #8fb28f;">  side-effects."</span>
   1.784 +  [f low high]
   1.785 +  (<span style="color: #f0dfaf; font-weight: bold;">doseq</span> [i (<span style="color: #8cd0d3;">range</span> low high)] (f i)))
   1.786 +   
   1.787 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">do-down</span>
   1.788 +  <span style="color: #8fb28f;">"Apply f to each number from high to low, presumably for</span>
   1.789 +<span style="color: #8fb28f;">   side-effects."</span>
   1.790 +  [f high low]
   1.791 +  (<span style="color: #f0dfaf; font-weight: bold;">doseq</span> [i (<span style="color: #8cd0d3;">range</span> high low -1)] (f i)))
   1.792 +
   1.793 +
   1.794 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">all-equal?</span> [coll]
   1.795 +  (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">empty?</span> (<span style="color: #8cd0d3;">rest</span> coll)) true
   1.796 +      (<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))
   1.797 +           (<span style="color: #f0dfaf; font-weight: bold;">recur</span> (<span style="color: #8cd0d3;">rest</span> coll))))))
   1.798 +
   1.799 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">multiplier</span>
   1.800 +  <span style="color: #8fb28f;">"Returns a function that 'multiplies' the members of a collection,</span>
   1.801 +<span style="color: #8fb28f;">returning unit if called on an empty collection."</span>
   1.802 +  [multiply unit]
   1.803 +  (<span style="color: #8cd0d3;">fn</span> [coll] ((<span style="color: #8cd0d3;">partial</span> reduce multiply unit) coll)))
   1.804 +
   1.805 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">divider</span>
   1.806 +  <span style="color: #8fb28f;">"Returns a function that 'divides' the first element of a collection</span>
   1.807 +<span style="color: #8fb28f;">by the 'product' of the rest of the collection."</span>
   1.808 +  [divide multiply invert unit]
   1.809 +  (<span style="color: #8cd0d3;">fn</span> [coll]
   1.810 +    (<span style="color: #8cd0d3;">apply</span>
   1.811 +     (<span style="color: #8cd0d3;">fn</span>
   1.812 +       ([] unit)
   1.813 +       ([x] (invert x))
   1.814 +       ([x y] (divide x y))
   1.815 +       ([x y &amp; zs] (divide x (<span style="color: #8cd0d3;">reduce</span> multiply y zs))))
   1.816 +     coll)))
   1.817 +
   1.818 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">left-circular-shift</span>
   1.819 +  <span style="color: #8fb28f;">"Remove the first element of coll, adding it to the end of coll."</span>
   1.820 +  [coll]
   1.821 +  (<span style="color: #8cd0d3;">concat</span> (<span style="color: #8cd0d3;">rest</span> coll) (<span style="color: #8cd0d3;">take</span> 1 coll)))
   1.822 +
   1.823 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">right-circular-shift</span>
   1.824 +  <span style="color: #8fb28f;">"Remove the last element of coll, adding it to the front of coll."</span>
   1.825 +  [coll]
   1.826 +  (<span style="color: #8cd0d3;">cons</span> (<span style="color: #8cd0d3;">last</span> coll) (<span style="color: #8cd0d3;">butlast</span> coll)))
   1.827 +</pre>
   1.828 +
   1.829 +
   1.830 +
   1.831 +
   1.832 +
   1.833 +
   1.834 +
   1.835 +</div>
   1.836 +
   1.837 +</div>
   1.838 +
   1.839 +<div id="outline-container-3-2" class="outline-3">
   1.840 +<h3 id="sec-3-2"><span class="section-number-3">3.2</span> Ranges, Airity and Function Composition </h3>
   1.841 +<div class="outline-text-3" id="text-3-2">
   1.842 +
   1.843 +
   1.844 +
   1.845 +
   1.846 +<pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
   1.847 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">infinity</span> Double/POSITIVE_INFINITY)
   1.848 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">infinite?</span> [x] (Double/isInfinite x))
   1.849 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">finite?</span> (<span style="color: #8cd0d3;">comp</span> not infinite?)) 
   1.850 +
   1.851 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">arity-min</span>
   1.852 +  <span style="color: #8fb28f;">"Returns the smallest number of arguments f can take."</span>
   1.853 +  [f]
   1.854 +  (<span style="color: #8cd0d3;">apply</span>
   1.855 +   min
   1.856 +   (<span style="color: #8cd0d3;">map</span> (<span style="color: #8cd0d3;">comp</span> alength #(.getParameterTypes %))
   1.857 +        (<span style="color: #8cd0d3;">filter</span> (<span style="color: #8cd0d3;">comp</span> (<span style="color: #8cd0d3;">partial</span> = <span style="color: #cc9393;">"invoke"</span>) #(.getName %))
   1.858 +                (.getDeclaredMethods (<span style="color: #8cd0d3;">class</span> f))))))
   1.859 +  
   1.860 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">arity-max</span>
   1.861 +  <span style="color: #8fb28f;">"Returns the largest number of arguments f can take, possibly</span>
   1.862 +<span style="color: #8fb28f;">  Infinity."</span>
   1.863 +  [f]
   1.864 +  (<span style="color: #f0dfaf; font-weight: bold;">let</span> [methods (.getDeclaredMethods (<span style="color: #8cd0d3;">class</span> f))]
   1.865 +    (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">not-any?</span> (<span style="color: #8cd0d3;">partial</span> = <span style="color: #cc9393;">"doInvoke"</span>) (<span style="color: #8cd0d3;">map</span> #(.getName %) methods))
   1.866 +      (<span style="color: #8cd0d3;">apply</span> max
   1.867 +             (<span style="color: #8cd0d3;">map</span> (<span style="color: #8cd0d3;">comp</span> alength #(.getParameterTypes %))
   1.868 +                  (<span style="color: #8cd0d3;">filter</span> (<span style="color: #8cd0d3;">comp</span> (<span style="color: #8cd0d3;">partial</span> = <span style="color: #cc9393;">"invoke"</span>) #(.getName %))  methods)))
   1.869 +      infinity)))
   1.870 +
   1.871 +
   1.872 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">^</span>{<span style="color: #8cd0d3;">:arglists</span> '([f])
   1.873 +       <span style="color: #8cd0d3;">:doc</span> <span style="color: #cc9393;">"Returns a two-element list containing the minimum and</span>
   1.874 +<span style="color: #cc9393;">     maximum number of args that f can take."</span>}
   1.875 +     arity-interval
   1.876 +     (<span style="color: #8cd0d3;">juxt</span> arity-min arity-max))
   1.877 +
   1.878 +
   1.879 +
   1.880 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">--- intervals</span>
   1.881 +
   1.882 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">intersect</span>
   1.883 +  <span style="color: #8fb28f;">"Returns the interval of overlap between interval-1 and interval-2"</span>
   1.884 +  [interval-1 interval-2]
   1.885 +  (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #f0dfaf; font-weight: bold;">or</span> (<span style="color: #8cd0d3;">empty?</span> interval-1) (<span style="color: #8cd0d3;">empty?</span> interval-2)) []
   1.886 +      (<span style="color: #f0dfaf; font-weight: bold;">let</span> [left (<span style="color: #8cd0d3;">max</span> (<span style="color: #8cd0d3;">first</span> interval-1) (<span style="color: #8cd0d3;">first</span> interval-2))
   1.887 +            right (<span style="color: #8cd0d3;">min</span> (<span style="color: #8cd0d3;">second</span> interval-1) (<span style="color: #8cd0d3;">second</span> interval-2))]
   1.888 +        (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">&gt;</span> left right) []
   1.889 +            [left right]))))
   1.890 +
   1.891 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">same-endpoints?</span>
   1.892 +  <span style="color: #8fb28f;">"Returns true if the left endpoint is the same as the right</span>
   1.893 +<span style="color: #8fb28f;">  endpoint."</span>
   1.894 +  [interval]
   1.895 +  (<span style="color: #8cd0d3;">=</span> (<span style="color: #8cd0d3;">first</span> interval) (<span style="color: #8cd0d3;">second</span> interval)))
   1.896 +
   1.897 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">naturals?</span>
   1.898 +  <span style="color: #8fb28f;">"Returns true if the left endpoint is 0 and the right endpoint is</span>
   1.899 +<span style="color: #8fb28f;">infinite."</span>
   1.900 +  [interval]
   1.901 +  (<span style="color: #f0dfaf; font-weight: bold;">and</span> (zero? (<span style="color: #8cd0d3;">first</span> interval))
   1.902 +       (infinite? (<span style="color: #8cd0d3;">second</span> interval))))
   1.903 + 
   1.904 +
   1.905 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">fan-in</span>
   1.906 +  <span style="color: #8fb28f;">"Returns a function that pipes its input to each of the gs, then</span>
   1.907 +<span style="color: #8fb28f;">  applies f to the list of results. Consequently, f must be able to</span>
   1.908 +<span style="color: #8fb28f;">  take a number of arguments equal to the number of gs."</span>
   1.909 +  [f &amp; gs]
   1.910 +  (<span style="color: #8cd0d3;">fn</span> [&amp; args]
   1.911 +    (<span style="color: #8cd0d3;">apply</span> f (<span style="color: #8cd0d3;">apply</span> (<span style="color: #8cd0d3;">apply</span> juxt gs) args))))
   1.912 +
   1.913 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">fan-in</span>
   1.914 +  <span style="color: #8fb28f;">"Returns a function that pipes its input to each of the gs, then applies f to the list of results. The resulting function takes any number of arguments, but will fail if given arguments that are incompatible with any of the gs."</span>
   1.915 +  [f &amp; gs]
   1.916 +  (<span style="color: #8cd0d3;">comp</span> (<span style="color: #8cd0d3;">partial</span> apply f) (<span style="color: #8cd0d3;">apply</span> juxt gs)))
   1.917 +
   1.918 +
   1.919 +
   1.920 +(<span style="color: #f0dfaf; font-weight: bold;">defmacro</span> <span style="color: #f0dfaf;">airty-blah-sad</span> [f n more?]
   1.921 +  (<span style="color: #f0dfaf; font-weight: bold;">let</span> [syms (<span style="color: #8cd0d3;">vec</span> (<span style="color: #8cd0d3;">map</span> (<span style="color: #8cd0d3;">comp</span> gensym (<span style="color: #8cd0d3;">partial</span> str <span style="color: #cc9393;">"x"</span>)) (<span style="color: #8cd0d3;">range</span> n)))
   1.922 +         optional (<span style="color: #8cd0d3;">gensym</span> <span style="color: #cc9393;">"xs"</span>)]
   1.923 +    (<span style="color: #f0dfaf; font-weight: bold;">if</span> more?
   1.924 +      `(<span style="color: #8cd0d3;">fn</span> <span style="color: #f0dfaf;">~</span>(<span style="color: #8cd0d3;">conj</span> syms '&amp; optional)
   1.925 +         (<span style="color: #8cd0d3;">apply</span> ~f ~@syms ~optional))
   1.926 +      `(<span style="color: #8cd0d3;">fn</span> <span style="color: #f0dfaf;">~syms</span> (~f ~@syms)))))
   1.927 +
   1.928 +(<span style="color: #f0dfaf; font-weight: bold;">defmacro</span> <span style="color: #f0dfaf;">airt-whaa*</span> [f n more?]
   1.929 +  `(airty-blah-sad ~f ~n ~more?))
   1.930 +
   1.931 +
   1.932 +
   1.933 +
   1.934 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">fan-in*</span>
   1.935 +  <span style="color: #8fb28f;">"Returns a function that pipes its input to each of the gs, then</span>
   1.936 +<span style="color: #8fb28f;">  applies f to the list of results. Unlike fan-in, fan-in* strictly</span>
   1.937 +<span style="color: #8fb28f;">  enforces arity: it will fail if the gs do not have compatible</span>
   1.938 +<span style="color: #8fb28f;">  arities."</span>
   1.939 +  [f &amp; gs]
   1.940 +  (<span style="color: #f0dfaf; font-weight: bold;">let</span> [arity-in (<span style="color: #8cd0d3;">reduce</span> intersect (<span style="color: #8cd0d3;">map</span> arity-interval gs))
   1.941 +        left (<span style="color: #8cd0d3;">first</span> arity-in)
   1.942 +        right (<span style="color: #8cd0d3;">second</span> arity-in)
   1.943 +        composite (fan-in f gs)
   1.944 +        ]
   1.945 +    (<span style="color: #f0dfaf; font-weight: bold;">cond</span>
   1.946 +     (<span style="color: #8cd0d3;">empty?</span> arity-in)
   1.947 +     (<span style="color: #f0dfaf; font-weight: bold;">throw</span> (Exception. <span style="color: #cc9393;">"Cannot compose functions with incompatible arities."</span>))
   1.948 +
   1.949 +     (<span style="color: #8cd0d3;">not</span>
   1.950 +      (<span style="color: #f0dfaf; font-weight: bold;">or</span> (<span style="color: #8cd0d3;">=</span> left right)
   1.951 +          (<span style="color: #f0dfaf; font-weight: bold;">and</span> (finite? left)
   1.952 +               (<span style="color: #8cd0d3;">=</span> right infinity))))
   1.953 +         
   1.954 +     (<span style="color: #f0dfaf; font-weight: bold;">throw</span> (Exception.
   1.955 +             <span style="color: #cc9393;">"Compose can only handle arities of the form [n n] or [n infinity]"</span>))
   1.956 +     <span style="color: #8cd0d3;">:else</span>
   1.957 +     (airty-blah-sad composite left (<span style="color: #8cd0d3;">=</span> right infinity)))))
   1.958 +    
   1.959 +
   1.960 +
   1.961 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">compose-n</span> <span style="color: #8fb28f;">"Compose any number of functions together."</span>
   1.962 +  ([] identity)
   1.963 +  ([f] f)
   1.964 +  ([f &amp; fs]
   1.965 +  (<span style="color: #f0dfaf; font-weight: bold;">let</span> [fns (<span style="color: #8cd0d3;">cons</span> f fs)]
   1.966 +    (compose-bin (<span style="color: #8cd0d3;">reduce</span> fan-in (<span style="color: #8cd0d3;">butlast</span> fs)) (<span style="color: #8cd0d3;">last</span> fs))))
   1.967 +)
   1.968 +
   1.969 +
   1.970 +
   1.971 +
   1.972 +           
   1.973 +
   1.974 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">iterated</span>
   1.975 +  ([f n id] (<span style="color: #8cd0d3;">reduce</span> comp id (<span style="color: #8cd0d3;">repeat</span> n f)))
   1.976 +  ([f n] (<span style="color: #8cd0d3;">reduce</span> comp identity (<span style="color: #8cd0d3;">repeat</span> n f))))
   1.977 +
   1.978 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">iterate-until-stable</span>
   1.979 +  <span style="color: #8fb28f;">"Repeatedly applies f to x, returning the first result that is close</span>
   1.980 +<span style="color: #8fb28f;">enough to its predecessor."</span>
   1.981 +  [f close-enough? x]
   1.982 +  (<span style="color: #8cd0d3;">second</span> (swank.util/find-first
   1.983 +           (<span style="color: #8cd0d3;">partial</span> apply close-enough?)
   1.984 +           (<span style="color: #8cd0d3;">partition</span> 2 1 (<span style="color: #8cd0d3;">iterate</span> f x)))))
   1.985 +
   1.986 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">lexical&lt;</span> [x y]
   1.987 +  (<span style="color: #8cd0d3;">neg?</span> (<span style="color: #8cd0d3;">compare</span> (<span style="color: #8cd0d3;">str</span> x) (<span style="color: #8cd0d3;">str</span> y))))
   1.988 +
   1.989 +
   1.990 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">do-up</span>
   1.991 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">do-down</span>
   1.992 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">make-pairwise-test</span> comparator)
   1.993 +<span style="color: #708070;">;;</span><span style="color: #7f9f7f;">all-equal?</span>
   1.994 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">accumulation</span> multiplier)
   1.995 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">inverse-accumulation</span> divider)
   1.996 +<span style="color: #708070;">;;</span><span style="color: #7f9f7f;">left-circular-shift</span>
   1.997 +<span style="color: #708070;">;;</span><span style="color: #7f9f7f;">right-circular-shift</span>
   1.998 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">exactly-n?</span> same-endpoints?)
   1.999 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">any-number?</span> naturals?)
  1.1000 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">TODO compose</span>
  1.1001 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">TODO compose-n</span>
  1.1002 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">identity</span>
  1.1003 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">compose-2</span> fan-in)
  1.1004 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">compose-bin</span> fan-in*)
  1.1005 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">any?</span> (<span style="color: #8cd0d3;">constantly</span> true))
  1.1006 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">none?</span> (<span style="color: #8cd0d3;">constantly</span> false))
  1.1007 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">constant</span> constantly)
  1.1008 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">joint-arity</span> intersect)
  1.1009 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">a-reduce</span> reduce)
  1.1010 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">filter</span>
  1.1011 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">make-map</span> (<span style="color: #8cd0d3;">partial</span> partial map) )
  1.1012 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">bracket</span> juxt)
  1.1013 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">TODO apply-to-all</span>
  1.1014 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">TODO nary-combine</span>
  1.1015 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">TODO binary-combine</span>
  1.1016 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">TODO unary-combine</span>
  1.1017 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">iterated</span>
  1.1018 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">iterate-until-stable</span>
  1.1019 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">make-function-of-vector</span> (<span style="color: #8cd0d3;">partial</span> partial map))
  1.1020 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">make-function-of-arguments</span> (<span style="color: #8cd0d3;">fn</span> [f] (<span style="color: #8cd0d3;">fn</span> [&amp; args] (f args))))
  1.1021 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">alphaless</span> lexical&lt;)
  1.1022 +
  1.1023 +</pre>
  1.1024 +
  1.1025 +
  1.1026 +
  1.1027 +
  1.1028 +
  1.1029 +
  1.1030 +
  1.1031 +
  1.1032 +
  1.1033 +
  1.1034 +
  1.1035 +
  1.1036 +</div>
  1.1037 +</div>
  1.1038 +
  1.1039 +</div>
  1.1040 +
  1.1041 +<div id="outline-container-4" class="outline-2">
  1.1042 +<h2 id="sec-4"><span class="section-number-2">4</span> Numerical Methods </h2>
  1.1043 +<div class="outline-text-2" id="text-4">
  1.1044 +
  1.1045 +
  1.1046 +
  1.1047 +
  1.1048 +<pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
  1.1049 +(<span style="color: #f0dfaf; font-weight: bold;">import</span> java.lang.Math)
  1.1050 +(<span style="color: #8cd0d3;">use</span> 'clojure.contrib.def)
  1.1051 +
  1.1052 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">---- USEFUL CONSTANTS</span>
  1.1053 +
  1.1054 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">machine-epsilon</span>
  1.1055 +  <span style="color: #8fb28f;">"Smallest value representable on your machine, as determined by</span>
  1.1056 +<span style="color: #8fb28f;">successively dividing a number in half until consecutive results are</span>
  1.1057 +<span style="color: #8fb28f;">indistinguishable."</span>
  1.1058 +  []
  1.1059 +  (<span style="color: #8cd0d3;">ffirst</span>
  1.1060 +   (<span style="color: #8cd0d3;">drop-while</span>
  1.1061 +    (<span style="color: #8cd0d3;">comp</span> not zero? second)
  1.1062 +    (<span style="color: #8cd0d3;">partition</span> 2 1
  1.1063 +               (<span style="color: #8cd0d3;">iterate</span> (<span style="color: #8cd0d3;">partial</span> * 0.5) 1)))))
  1.1064 +
  1.1065 +
  1.1066 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">pi</span> (Math/PI))
  1.1067 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">two-pi</span> (<span style="color: #8cd0d3;">*</span> 2 pi))
  1.1068 +
  1.1069 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">eulers-gamma</span> 0.5772156649015328606065)
  1.1070 +
  1.1071 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">phi</span> (<span style="color: #8cd0d3;">/</span> (<span style="color: #8cd0d3;">inc</span> (Math/sqrt 5)) 2))
  1.1072 +
  1.1073 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">ln2</span> (Math/log 2))
  1.1074 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">ln10</span> (Math/log 10))
  1.1075 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">exp10</span> #(Math/pow 10 %))
  1.1076 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">exp2</span> #(Math/pow 2 %))
  1.1077 +
  1.1078 +
  1.1079 +<span style="color: #708070;">;;</span>
  1.1080 +
  1.1081 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">---- ANGLES AND TRIGONOMETRY</span>
  1.1082 +
  1.1083 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">angle-restrictor</span>
  1.1084 +  <span style="color: #8fb28f;">"Returns a function that ensures that angles lie in the specified interval of length two-pi."</span>
  1.1085 +  [max-angle]
  1.1086 +  (<span style="color: #f0dfaf; font-weight: bold;">let</span> [min-angle (<span style="color: #8cd0d3;">-</span> max-angle two-pi)]
  1.1087 +    (<span style="color: #8cd0d3;">fn</span> [x]
  1.1088 +      (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #f0dfaf; font-weight: bold;">and</span>
  1.1089 +           (<span style="color: #8cd0d3;">&lt;=</span> min-angle x)
  1.1090 +           (<span style="color: #8cd0d3;">&lt;</span> x max-angle))
  1.1091 +        x
  1.1092 +        (<span style="color: #f0dfaf; font-weight: bold;">let</span> [corrected-x (<span style="color: #8cd0d3;">-</span> x (<span style="color: #8cd0d3;">*</span> two-pi (Math/floor (<span style="color: #8cd0d3;">/</span> x two-pi))))]
  1.1093 +          (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">&lt;</span> corrected-x max-angle)
  1.1094 +            corrected-x
  1.1095 +            (<span style="color: #8cd0d3;">-</span> corrected-x two-pi)))))))
  1.1096 +
  1.1097 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">angle-restrict-pi</span>
  1.1098 +  <span style="color: #8fb28f;">"Coerces angles to lie in the interval from -pi to pi."</span>
  1.1099 +  [angle]
  1.1100 +  ((angle-restrictor pi) angle))
  1.1101 +
  1.1102 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">angle-restrict-two-pi</span>
  1.1103 +  <span style="color: #8fb28f;">"Coerces angles to lie in the interval from zero to two-pi"</span>
  1.1104 +  [angle]
  1.1105 +  ((angle-restrictor two-pi) angle))
  1.1106 +
  1.1107 +
  1.1108 +
  1.1109 +
  1.1110 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">invert</span> [x] (<span style="color: #8cd0d3;">/</span> x))
  1.1111 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">negate</span> [x] (<span style="color: #8cd0d3;">-</span> x))
  1.1112 +
  1.1113 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">exp</span> [x] (Math/exp x))
  1.1114 +
  1.1115 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">sin</span> [x] (Math/sin x))
  1.1116 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">cos</span> [x] (Math/cos x))
  1.1117 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">tan</span> [x] (Math/tan x))
  1.1118 +
  1.1119 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">sec</span> (<span style="color: #8cd0d3;">comp</span> invert cos))
  1.1120 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">csc</span> (<span style="color: #8cd0d3;">comp</span> invert sin))
  1.1121 +
  1.1122 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">sinh</span> [x] (Math/sinh x))
  1.1123 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">cosh</span> [x] (Math/cosh x))
  1.1124 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">tanh</span> [x] (Math/tanh x))
  1.1125 +
  1.1126 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">sech</span> (<span style="color: #8cd0d3;">comp</span> invert cosh))
  1.1127 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">csch</span> (<span style="color: #8cd0d3;">comp</span> invert sinh))
  1.1128 +
  1.1129 +
  1.1130 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">------------</span>
  1.1131 +
  1.1132 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">factorial</span>
  1.1133 +  <span style="color: #8fb28f;">"Computes the factorial of the nonnegative integer n."</span>
  1.1134 +  [n]
  1.1135 +  (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">neg?</span> n)
  1.1136 +    (<span style="color: #f0dfaf; font-weight: bold;">throw</span> (Exception. <span style="color: #cc9393;">"Cannot compute the factorial of a negative number."</span>))
  1.1137 +    (<span style="color: #8cd0d3;">reduce</span> * 1 (<span style="color: #8cd0d3;">range</span> 1 (<span style="color: #8cd0d3;">inc</span> n)))))
  1.1138 +
  1.1139 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">exact-quotient</span> [n d] (<span style="color: #8cd0d3;">/</span> n d))
  1.1140 +
  1.1141 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">binomial-coefficient</span>
  1.1142 +  <span style="color: #8fb28f;">"Computes the number of different ways to choose m elements from n."</span>
  1.1143 +  [n m]
  1.1144 +  (<span style="color: #8cd0d3;">assert</span> (<span style="color: #8cd0d3;">&lt;=</span> 0 m n))
  1.1145 +  (<span style="color: #f0dfaf; font-weight: bold;">let</span> [difference (<span style="color: #8cd0d3;">-</span> n m)]
  1.1146 +    (exact-quotient
  1.1147 +     (<span style="color: #8cd0d3;">reduce</span> * (<span style="color: #8cd0d3;">range</span> n (<span style="color: #8cd0d3;">max</span> difference m) -1 ))
  1.1148 +     (factorial (<span style="color: #8cd0d3;">min</span> difference m)))))
  1.1149 +
  1.1150 +(<span style="color: #f0dfaf; font-weight: bold;">defn-memo</span> <span style="color: #f0dfaf;">stirling-1</span>
  1.1151 +  <span style="color: #cc9393;">"Stirling numbers of the first kind: the number of permutations of n</span>
  1.1152 +<span style="color: #cc9393;">  elements with exactly m permutation cycles. "</span>
  1.1153 +  [n k]
  1.1154 +  <span style="color: #708070;">;</span><span style="color: #7f9f7f;">(assert (&lt;= 1 k n))</span>
  1.1155 +  (<span style="color: #f0dfaf; font-weight: bold;">if</span> (zero? n)
  1.1156 +    (<span style="color: #f0dfaf; font-weight: bold;">if</span> (zero? k) 1 0)
  1.1157 +    (<span style="color: #8cd0d3;">+</span> (stirling-1 (<span style="color: #8cd0d3;">dec</span> n) (<span style="color: #8cd0d3;">dec</span> k))
  1.1158 +       (<span style="color: #8cd0d3;">*</span> (<span style="color: #8cd0d3;">dec</span> n) (stirling-1 (<span style="color: #8cd0d3;">dec</span> n) k)))))
  1.1159 +
  1.1160 +(<span style="color: #f0dfaf; font-weight: bold;">defn-memo</span> <span style="color: #f0dfaf;">stirling-2</span> <span style="color: #708070;">;;</span><span style="color: #7f9f7f;">count-partitions</span>
  1.1161 +  <span style="color: #cc9393;">"Stirling numbers of the second kind: the number of ways to partition a set of n elements into k subsets."</span>
  1.1162 +  [n k]
  1.1163 +  (<span style="color: #f0dfaf; font-weight: bold;">cond</span>
  1.1164 +   (<span style="color: #8cd0d3;">=</span> k 1) 1
  1.1165 +   (<span style="color: #8cd0d3;">=</span> k n) 1
  1.1166 +   <span style="color: #8cd0d3;">:else</span> (<span style="color: #8cd0d3;">+</span> (stirling-2 (<span style="color: #8cd0d3;">dec</span> n) (<span style="color: #8cd0d3;">dec</span> k))
  1.1167 +            (<span style="color: #8cd0d3;">*</span> k (stirling-2 (<span style="color: #8cd0d3;">dec</span> n) k)))))
  1.1168 +
  1.1169 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">harmonic-number</span> [n]
  1.1170 +  (<span style="color: #8cd0d3;">/</span> (stirling-1 (<span style="color: #8cd0d3;">inc</span> n) 2)
  1.1171 +     (factorial n)))
  1.1172 +
  1.1173 +
  1.1174 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">sum</span>
  1.1175 +  [f low high]
  1.1176 +  (<span style="color: #8cd0d3;">reduce</span> + (<span style="color: #8cd0d3;">map</span> f (<span style="color: #8cd0d3;">range</span> low (<span style="color: #8cd0d3;">inc</span> high)))))
  1.1177 +
  1.1178 +</pre>
  1.1179 +
  1.1180 +
  1.1181 +
  1.1182 +
  1.1183 +
  1.1184 +
  1.1185 +
  1.1186 +
  1.1187 +
  1.1188 +
  1.1189 +
  1.1190 +
  1.1191 +
  1.1192 +
  1.1193 +
  1.1194 +</div>
  1.1195 +
  1.1196 +</div>
  1.1197 +
  1.1198 +<div id="outline-container-5" class="outline-2">
  1.1199 +<h2 id="sec-5"><span class="section-number-2">5</span> Differentiation </h2>
  1.1200 +<div class="outline-text-2" id="text-5">
  1.1201 +
  1.1202 +
  1.1203 +<p>
  1.1204 +We compute derivatives by passing special <b>differential objects</b> \([x,
  1.1205 +dx]\) through functions. Roughly speaking, applying a function \(f\) to a
  1.1206 +differential object \([x, dx]\) should produce a new differential
  1.1207 +object \([f(x),\,Df(x)\cdot dx]\).
  1.1208 +</p>
  1.1209 +
  1.1210 +
  1.1211 +\([x,\,dx]\xrightarrow{\quad f \quad}[f(x),\,Df(x)\cdot dx]\)
  1.1212 +<p>
  1.1213 +Notice that you can obtain the derivative of \(f\) from this
  1.1214 +differential object, as it is the coefficient of the \(dx\) term. Also,
  1.1215 +as you apply successive functions using this rule, you get the
  1.1216 +chain-rule answer you expect:
  1.1217 +</p>
  1.1218 +
  1.1219 +
  1.1220 +\([f(x),\,Df(x)\cdot dx]\xrightarrow{\quad g\quad} [gf(x),\,
  1.1221 +Dgf(x)\cdot Df(x) \cdot dx ]\)
  1.1222 +
  1.1223 +<p>
  1.1224 +In order to generalize to multiple variables and multiple derivatives,
  1.1225 +we use a <b>power series of differentials</b>, a sortred infinite sequence which
  1.1226 +contains all terms like \(dx\cdot dy\), \(dx^2\cdot dy\), etc.
  1.1227 +</p>
  1.1228 +
  1.1229 +
  1.1230 +
  1.1231 +
  1.1232 +<pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
  1.1233 +
  1.1234 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">differential-seq</span>
  1.1235 +  <span style="color: #8fb28f;">"Constructs a sequence of differential terms from a numerical</span>
  1.1236 +<span style="color: #8fb28f;">coefficient and a list of keys for variables. If no coefficient is supplied, uses 1."</span>
  1.1237 +  ([variables] (differential-seq* 1 variables))
  1.1238 +  ([coefficient variables &amp; cvs]
  1.1239 +     (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">number?</span> coefficient)
  1.1240 +       (<span style="color: #8cd0d3;">conj</span> (<span style="color: #8cd0d3;">assoc</span> {} (<span style="color: #8cd0d3;">apply</span> sorted-set variables) coefficient)
  1.1241 +             (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">empty?</span> cvs)
  1.1242 +               nil
  1.1243 +               (<span style="color: #8cd0d3;">apply</span> differential-seq* cvs)))
  1.1244 +       (<span style="color: #8cd0d3;">apply</span> differential-seq* 1 coefficient 1 variables cvs)  
  1.1245 +       )))
  1.1246 +
  1.1247 +
  1.1248 +
  1.1249 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">differential-add</span>
  1.1250 +  <span style="color: #8fb28f;">"Add two differential sequences by combining like terms."</span>
  1.1251 +  [dseq1 dseq2]
  1.1252 +  (<span style="color: #8cd0d3;">merge-with</span> + dseq1 dseq2))
  1.1253 +
  1.1254 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">differential-multiply</span>
  1.1255 +  <span style="color: #8fb28f;">"Multiply two differential sequences. The square of any differential variable is zero since differential variables are infinitesimally small."</span>
  1.1256 +  [dseq1 dseq2]
  1.1257 +  (<span style="color: #8cd0d3;">reduce</span>
  1.1258 +   (<span style="color: #8cd0d3;">fn</span> [m [[vars1 coeff1] [vars2 coeff2]]]
  1.1259 +     (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">empty?</span> (clojure.set/<span style="color: #dfdfbf; font-weight: bold;">intersection</span> vars1 vars2))
  1.1260 +       (<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))
  1.1261 +       m))
  1.1262 +   {}
  1.1263 +  (clojure.contrib.combinatorics/cartesian-product
  1.1264 +   dseq1
  1.1265 +   dseq2)))
  1.1266 +
  1.1267 +
  1.1268 +
  1.1269 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">big-part</span>
  1.1270 +  <span style="color: #8fb28f;">"Returns the 'finite' part of the differential sequence."</span>
  1.1271 +  [dseq]
  1.1272 +  (<span style="color: #f0dfaf; font-weight: bold;">let</span>
  1.1273 +      [keys (<span style="color: #8cd0d3;">sort-by</span> count (<span style="color: #8cd0d3;">keys</span> dseq))
  1.1274 +       pivot-key (<span style="color: #8cd0d3;">first</span> (<span style="color: #8cd0d3;">last</span> keys))]
  1.1275 +
  1.1276 +    (<span style="color: #8cd0d3;">apply</span> hash-map
  1.1277 +    (<span style="color: #8cd0d3;">reduce</span> concat
  1.1278 +            (<span style="color: #8cd0d3;">filter</span> (<span style="color: #8cd0d3;">comp</span> pivot-key first) dseq)
  1.1279 +            ))))
  1.1280 +
  1.1281 +
  1.1282 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">small-part</span>
  1.1283 +  <span style="color: #8fb28f;">"Returns the 'infinitesimal' part of the differential sequence."</span>
  1.1284 +  [dseq]
  1.1285 +    (<span style="color: #f0dfaf; font-weight: bold;">let</span>
  1.1286 +      [keys (<span style="color: #8cd0d3;">sort-by</span> count (<span style="color: #8cd0d3;">keys</span> dseq))
  1.1287 +       pivot-key (<span style="color: #8cd0d3;">first</span> (<span style="color: #8cd0d3;">last</span> keys))]
  1.1288 +
  1.1289 +    (<span style="color: #8cd0d3;">apply</span> hash-map
  1.1290 +    (<span style="color: #8cd0d3;">reduce</span> concat
  1.1291 +            (<span style="color: #8cd0d3;">remove</span> (<span style="color: #8cd0d3;">comp</span> pivot-key first) dseq)
  1.1292 +            ))))
  1.1293 +
  1.1294 +
  1.1295 +
  1.1296 +
  1.1297 +
  1.1298 +
  1.1299 +
  1.1300 +
  1.1301 +
  1.1302 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">;; A differential term consists of a numerical coefficient and a</span>
  1.1303 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">;; sorted  </span>
  1.1304 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">(defrecord DifferentialTerm [coefficient variables])</span>
  1.1305 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">(defmethod print-method DifferentialTerm</span>
  1.1306 +<span style="color: #708070;">;;    </span><span style="color: #7f9f7f;">[o w]</span>
  1.1307 +<span style="color: #708070;">;;     </span><span style="color: #7f9f7f;">(print-simple</span>
  1.1308 +<span style="color: #708070;">;;      </span><span style="color: #7f9f7f;">(apply str (.coefficient o)(map (comp (partial str "d") name) (.variables o)))</span>
  1.1309 +<span style="color: #708070;">;;      </span><span style="color: #7f9f7f;">w))</span>
  1.1310 +
  1.1311 +
  1.1312 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">(defn differential-seq</span>
  1.1313 +<span style="color: #708070;">;;   </span><span style="color: #7f9f7f;">"Constructs a sequence of differential terms from a numerical</span>
  1.1314 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">coefficient and a list of keywords for variables. If no coefficient is</span>
  1.1315 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">supplied, uses 1."</span>
  1.1316 +<span style="color: #708070;">;;   </span><span style="color: #7f9f7f;">([variables] (differential-seq 1 variables))</span>
  1.1317 +<span style="color: #708070;">;;   </span><span style="color: #7f9f7f;">([coefficient variables]</span>
  1.1318 +<span style="color: #708070;">;;      </span><span style="color: #7f9f7f;">(list</span>
  1.1319 +<span style="color: #708070;">;;       </span><span style="color: #7f9f7f;">(DifferentialTerm. coefficient (apply sorted-set variables))))</span>
  1.1320 +<span style="color: #708070;">;;   </span><span style="color: #7f9f7f;">([coefficient variables &amp; cvs]</span>
  1.1321 +<span style="color: #708070;">;;      </span><span style="color: #7f9f7f;">(sort-by</span>
  1.1322 +<span style="color: #708070;">;;       </span><span style="color: #7f9f7f;">#(vec(.variables %))</span>
  1.1323 +<span style="color: #708070;">;;       </span><span style="color: #7f9f7f;">(concat (differential-seq coefficient variables) (apply differential-seq cvs)))))</span>
  1.1324 +
  1.1325 +</pre>
  1.1326 +
  1.1327 +
  1.1328 +
  1.1329 +
  1.1330 +
  1.1331 +
  1.1332 +
  1.1333 +
  1.1334 +
  1.1335 +
  1.1336 +
  1.1337 +
  1.1338 +
  1.1339 +
  1.1340 +
  1.1341 +
  1.1342 +</div>
  1.1343 +
  1.1344 +</div>
  1.1345 +
  1.1346 +<div id="outline-container-6" class="outline-2">
  1.1347 +<h2 id="sec-6"><span class="section-number-2">6</span> Symbolic Manipulation </h2>
  1.1348 +<div class="outline-text-2" id="text-6">
  1.1349 +
  1.1350 +
  1.1351 +
  1.1352 +
  1.1353 +
  1.1354 +<pre class="src src-clojure">(<span style="color: #f0dfaf; font-weight: bold;">in-ns</span> 'sicm.utils)
  1.1355 +
  1.1356 +(<span style="color: #f0dfaf; font-weight: bold;">deftype</span> <span style="color: #f0dfaf;">Symbolic</span> [type expression]
  1.1357 +  Object
  1.1358 +  (equals [this that]
  1.1359 +          (<span style="color: #f0dfaf; font-weight: bold;">cond</span>
  1.1360 +           (<span style="color: #8cd0d3;">=</span> (.expression this) (.expression that)) true
  1.1361 +           <span style="color: #8cd0d3;">:else</span>
  1.1362 +           (Symbolic.
  1.1363 +            java.lang.Boolean
  1.1364 +            (<span style="color: #8cd0d3;">list</span> '= (.expression this) (.expression that)))
  1.1365 +          )))
  1.1366 +
  1.1367 +
  1.1368 +
  1.1369 +
  1.1370 +(<span style="color: #f0dfaf; font-weight: bold;">deftype</span> <span style="color: #f0dfaf;">AbstractSet</span> [glyph membership-test])
  1.1371 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">member?</span> [abstract-set x]
  1.1372 +  ((.membership-test abstract-set) x))
  1.1373 +
  1.1374 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">------------ Some important AbstractSets</span>
  1.1375 +
  1.1376 +
  1.1377 +(<span style="color: #f0dfaf; font-weight: bold;">def</span> <span style="color: #f0dfaf;">Real</span>
  1.1378 +     (AbstractSet.
  1.1379 +      'R
  1.1380 +      (<span style="color: #8cd0d3;">fn</span>[x](<span style="color: #8cd0d3;">number?</span> x))))
  1.1381 +
  1.1382 +
  1.1383 +<span style="color: #708070;">;; </span><span style="color: #7f9f7f;">------------ Create new AbstractSets from existing ones</span>
  1.1384 +
  1.1385 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">abstract-product</span>
  1.1386 +  <span style="color: #8fb28f;">"Gives the cartesian product of abstract sets."</span>
  1.1387 +  ([sets]
  1.1388 +       (<span style="color: #f0dfaf; font-weight: bold;">if</span> (<span style="color: #8cd0d3;">=</span> (<span style="color: #8cd0d3;">count</span> sets) 1) (<span style="color: #8cd0d3;">first</span> sets)
  1.1389 +           (AbstractSet.
  1.1390 +            (<span style="color: #8cd0d3;">symbol</span>
  1.1391 +             (<span style="color: #8cd0d3;">apply</span> str
  1.1392 +                    (<span style="color: #8cd0d3;">interpose</span> 'x (<span style="color: #8cd0d3;">map</span> #(.glyph %) sets))))
  1.1393 +           (<span style="color: #8cd0d3;">fn</span> [x]
  1.1394 +             (<span style="color: #f0dfaf; font-weight: bold;">and</span>
  1.1395 +              (<span style="color: #8cd0d3;">coll?</span> x)
  1.1396 +              (<span style="color: #8cd0d3;">=</span> (<span style="color: #8cd0d3;">count</span> sets) (<span style="color: #8cd0d3;">count</span> x))
  1.1397 +              (<span style="color: #8cd0d3;">reduce</span> #(<span style="color: #f0dfaf; font-weight: bold;">and</span> %1 %2)
  1.1398 +                      true
  1.1399 +                      (<span style="color: #8cd0d3;">map</span> #(member? %1 %2) sets x)))))))
  1.1400 +  ([abstract-set n]
  1.1401 +     (abstract-product (<span style="color: #8cd0d3;">repeat</span> n abstract-set))))
  1.1402 +
  1.1403 +
  1.1404 +              
  1.1405 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">abstract-up</span>
  1.1406 +  <span style="color: #8fb28f;">"Returns the abstract set of all up-tuples whose items belong to the</span>
  1.1407 +<span style="color: #8fb28f;">  corresponding abstract sets in coll."</span>
  1.1408 +  ([coll]
  1.1409 +     (AbstractSet.
  1.1410 +      (<span style="color: #8cd0d3;">symbol</span> (<span style="color: #8cd0d3;">str</span> <span style="color: #cc9393;">"u["</span>
  1.1411 +                   (<span style="color: #8cd0d3;">apply</span> str
  1.1412 +                          (<span style="color: #8cd0d3;">interpose</span> <span style="color: #cc9393;">" "</span>
  1.1413 +                                     (<span style="color: #8cd0d3;">map</span> #(.glyph %) coll)))
  1.1414 +                   <span style="color: #cc9393;">"]"</span>))
  1.1415 +      (<span style="color: #8cd0d3;">fn</span> [x]
  1.1416 +        (<span style="color: #f0dfaf; font-weight: bold;">and</span>
  1.1417 +         (<span style="color: #8cd0d3;">satisfies?</span> Spinning x)
  1.1418 +         (up? x)
  1.1419 +         (<span style="color: #8cd0d3;">=</span> (<span style="color: #8cd0d3;">count</span> coll) (<span style="color: #8cd0d3;">count</span> x))
  1.1420 +         (<span style="color: #8cd0d3;">reduce</span>
  1.1421 +          #(<span style="color: #f0dfaf; font-weight: bold;">and</span> %1 %2)
  1.1422 +          true
  1.1423 +          (<span style="color: #8cd0d3;">map</span> #(member? %1 %2) coll x))))))
  1.1424 +  ([abstract-set n]
  1.1425 +     (abstract-up (<span style="color: #8cd0d3;">repeat</span> n abstract-set))))
  1.1426 +
  1.1427 +
  1.1428 +(<span style="color: #f0dfaf; font-weight: bold;">defn</span> <span style="color: #f0dfaf;">abstract-down</span>
  1.1429 +  <span style="color: #8fb28f;">"Returns the abstract set of all down-tuples whose items belong to the</span>
  1.1430 +<span style="color: #8fb28f;">  corresponding abstract sets in coll."</span>
  1.1431 +  ([coll]
  1.1432 +     (AbstractSet.
  1.1433 +      (<span style="color: #8cd0d3;">symbol</span> (<span style="color: #8cd0d3;">str</span> <span style="color: #cc9393;">"d["</span>
  1.1434 +                   (<span style="color: #8cd0d3;">apply</span> str
  1.1435 +                          (<span style="color: #8cd0d3;">interpose</span> <span style="color: #cc9393;">" "</span>
  1.1436 +                                     (<span style="color: #8cd0d3;">map</span> #(.glyph %) coll)))
  1.1437 +                   <span style="color: #cc9393;">"]"</span>))
  1.1438 +      (<span style="color: #8cd0d3;">fn</span> [x]
  1.1439 +        (<span style="color: #f0dfaf; font-weight: bold;">and</span>
  1.1440 +         (<span style="color: #8cd0d3;">satisfies?</span> Spinning x)
  1.1441 +         (down? x)
  1.1442 +         (<span style="color: #8cd0d3;">=</span> (<span style="color: #8cd0d3;">count</span> coll) (<span style="color: #8cd0d3;">count</span> x))
  1.1443 +         (<span style="color: #8cd0d3;">reduce</span>
  1.1444 +          #(<span style="color: #f0dfaf; font-weight: bold;">and</span> %1 %2)
  1.1445 +          true
  1.1446 +          (<span style="color: #8cd0d3;">map</span> #(member? %1 %2) coll x))))))
  1.1447 +  ([abstract-set n]
  1.1448 +     (abstract-down (<span style="color: #8cd0d3;">repeat</span> n abstract-set))))
  1.1449 +
  1.1450 +              
  1.1451 +
  1.1452 +
  1.1453 +
  1.1454 +                                        <span style="color: #708070;">;</span><span style="color: #7f9f7f;">-------ABSTRACT FUNCTIONS</span>
  1.1455 +(<span style="color: #f0dfaf; font-weight: bold;">defrecord</span> <span style="color: #f0dfaf;">AbstractFn</span>
  1.1456 +  [<span style="color: #dfdfbf; font-weight: bold;">#^AbstractSet</span> domain <span style="color: #dfdfbf; font-weight: bold;">#^AbstractSet</span> codomain])
  1.1457 +
  1.1458 +
  1.1459 +(<span style="color: #f0dfaf; font-weight: bold;">defmethod</span> <span style="color: #f0dfaf;">print-method</span> AbstractFn
  1.1460 +  [o w]
  1.1461 +  (<span style="color: #8cd0d3;">print-simple</span>
  1.1462 +   (<span style="color: #8cd0d3;">str</span>
  1.1463 +    <span style="color: #cc9393;">"f:"</span>
  1.1464 +   (.glyph (<span style="color: #8cd0d3;">:domain</span> o))
  1.1465 +   <span style="color: #cc9393;">"--&gt;"</span>
  1.1466 +   (.glyph (<span style="color: #8cd0d3;">:codomain</span> o))) w))
  1.1467 +</pre>
  1.1468 +
  1.1469 +
  1.1470 +
  1.1471 +
  1.1472 +
  1.1473 +
  1.1474 +
  1.1475 +</div>
  1.1476 +</div>
  1.1477 +<div id="postamble">
  1.1478 +<p class="date">Date: 2011-08-09 18:41:37 EDT</p>
  1.1479 +<p class="author">Author: Robert McIntyre & Dylan Holmes</p>
  1.1480 +<p class="creator">Org version 7.6 with Emacs version 23</p>
  1.1481 +<a href="http://validator.w3.org/check?uri=referer">Validate XHTML 1.0</a>
  1.1482 +</div>
  1.1483 +</div>
  1.1484 +</body>
  1.1485 +</html>