<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Conal Elliott &#187; linear map</title>
	<atom:link href="http://conal.net/blog/tag/linear-map/feed" rel="self" type="application/rss+xml" />
	<link>http://conal.net/blog</link>
	<description>Inspirations &#38; experiments, mainly about denotative/functional programming in Haskell</description>
	<lastBuildDate>Thu, 25 Jul 2019 18:15:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.17</generator>
	<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2F&amp;language=en_US&amp;category=text&amp;title=Conal+Elliott&amp;description=Inspirations+%26amp%3B+experiments%2C+mainly+about+denotative%2Ffunctional+programming+in+Haskell&amp;tags=blog" type="text/html" />
	<item>
		<title>Paper: Beautiful differentiation</title>
		<link>http://conal.net/blog/posts/paper-beautiful-differentiation</link>
		<comments>http://conal.net/blog/posts/paper-beautiful-differentiation#comments</comments>
		<pubDate>Tue, 24 Feb 2009 08:05:10 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[beautiful code]]></category>
		<category><![CDATA[calculus on manifolds]]></category>
		<category><![CDATA[derivative]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[linear map]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[paper]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=85</guid>
		<description><![CDATA[I have another paper draft for submission to ICFP 2009. This one is called Beautiful differentiation, The paper is a culmination of the several posts I&#8217;ve written on derivatives and automatic differentiation (AD). I&#8217;m happy with how the derivation keeps getting simpler. Now I&#8217;ve boiled extremely general higher-order AD down to a Functor and Applicative [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Paper: Beautiful differentiation

Tags: derivative, functor, applicative functor, beautiful code, calculus on manifolds, linear map, math, paper

URL: http://conal.net/blog/posts/paper-beautiful-differentiation/

-->

<!-- references -->

<!-- teaser -->

<p>I have another paper draft for submission to <a href="http://www.cs.nott.ac.uk/~gmh/icfp09.html" title="conference page">ICFP 2009</a>.
This one is called <em><a href="http://conal.net/papers/beautiful-differentiation" title="paper">Beautiful differentiation</a></em>, 
The paper is a culmination of the <a href="http://conal.net/blog/tag/derivative/">several posts</a> I&#8217;ve written on derivatives and automatic differentiation (AD).
I&#8217;m happy with how the derivation keeps getting simpler.
Now I&#8217;ve boiled extremely general higher-order AD down to a <code>Functor</code> and <code>Applicative</code> morphism.</p>

<p>I&#8217;d love to get some readings and feedback.
I&#8217;m a bit over the page the limit, so I&#8217;ll have to do some trimming before submitting.</p>

<p>The abstract:</p>

<blockquote>
  <p>Automatic differentiation (AD) is a precise, efficient, and convenient
  method for computing derivatives of functions. Its implementation can be
  quite simple even when extended to compute all of the higher-order
  derivatives as well. The higher-dimensional case has also been tackled,
  though with extra complexity. This paper develops an implementation of
  higher-dimensional, higher-order differentiation in the extremely
  general and elegant setting of <em>calculus on manifolds</em> and derives that
  implementation from a simple and precise specification.</p>
  
  <p>In order to motivate and discover the implementation, the paper poses
  the question &#8220;What does AD mean, independently of implementation?&#8221; An
  answer arises in the form of <em>naturality</em> of sampling a function and its
  derivative. Automatic differentiation flows out of this naturality
  condition, together with the chain rule. Graduating from first-order to
  higher-order AD corresponds to sampling all derivatives instead of just
  one. Next, the notion of a derivative is generalized via the notions of
  vector space and linear maps. The specification of AD adapts to this
  elegant and very general setting, which even <em>simplifies</em> the
  development.</p>
</blockquote>

<p>You can <a href="http://conal.net/papers/beautiful-differentiation" title="paper">get the paper and see current errata here</a>.</p>

<p>The submission deadline is March 2, so comments before then are most helpful to me.</p>

<p>Enjoy, and thanks!</p>

<!--
**Edits**:

* 2009-02-09: just fiddling around
-->
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=85&amp;md5=2f6565c8f001a5925c3e6dbd29158c37"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/paper-beautiful-differentiation/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fpaper-beautiful-differentiation&amp;language=en_GB&amp;category=text&amp;title=Paper%3A+Beautiful+differentiation&amp;description=I+have+another+paper+draft+for+submission+to+ICFP+2009.+This+one+is+called+Beautiful+differentiation%2C+The+paper+is+a+culmination+of+the+several+posts+I%26%238217%3Bve+written+on+derivatives+and...&amp;tags=applicative+functor%2Cbeautiful+code%2Ccalculus+on+manifolds%2Cderivative%2Cfunctor%2Clinear+map%2Cmath%2Cpaper%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Comparing formulations of higher-dimensional, higher-order derivatives</title>
		<link>http://conal.net/blog/posts/comparing-formulations-of-higher-dimensional-higher-order-derivatives</link>
		<comments>http://conal.net/blog/posts/comparing-formulations-of-higher-dimensional-higher-order-derivatives#comments</comments>
		<pubDate>Sun, 25 Jan 2009 01:40:07 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[derivative]]></category>
		<category><![CDATA[linear map]]></category>
		<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=77</guid>
		<description><![CDATA[I just reread Jason Foutz&#8217;s post Higher order multivariate automatic differentiation in Haskell, as I&#8217;m thinking about this topic again. I like his trick of using an IntMap to hold the partial derivatives and (recursively) the partials of those partials, etc. Some thoughts: I bet one can eliminate the constant (C) case in Jason&#8217;s representation, [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Comparing formulations of higher-dimensional, higher-order derivatives

Tags: derivative, linear map, math, comparison

URL: http://conal.net/blog/posts/comparing-formulations-of-higher-dimensional-higher-order-derivatives/

-->

<!-- references -->

<!-- -->

<p>I just reread Jason Foutz&#8217;s post <a href="http://metavar.blogspot.com/2008/02/higher-order-multivariate-automatic.html">Higher order multivariate automatic differentiation in Haskell</a>, as I&#8217;m thinking about this topic again.  I like his trick of using an <code>IntMap</code> to hold the partial derivatives and (recursively) the partials of those partials, etc.</p>

<p>Some thoughts:</p>

<ul>
<li><p>I bet one can eliminate the constant (<code>C</code>) case in Jason&#8217;s representation, and hence 3/4 of the cases to handle, without much loss in performance.  He already has a fairly efficient representation of constants, which is a <code>D</code> with an empty <code>IntMap</code>.</p></li>
<li><p>I imagine there&#8217;s also a nice generalization of the code for combining two finite maps used in his third multiply case.  The code&#8217;s meaning and correctness follows from a model for those maps as total functions with missing elements denoting a default value (zero in this case).</p></li>
<li><p>Jason&#8217;s data type reminds me of a sparse matrix representation, but cooler in how it&#8217;s infinitely nested.  Perhaps depth <em>n</em> (starting with zero) is a sparse <em>n-dimensional</em> matrix.</p></li>
<li><p>Finally, I suspect there&#8217;s a close connection between Jason&#8217;s <code>IntMap</code>-based implementation and my <code>LinearMap</code>-based implementation described in <em><a href="http://conal.net/blog/posts/higher-dimensional-higher-order-derivatives-functionally/" title="blog post">Higher-dimensional, higher-order derivatives, functionally</a></em> and in <em><a href="http://conal.net/blog/posts/simpler-more-efficient-functional-linear-maps/">Simpler, more efficient, functional linear maps</a></em>.  For the case of <em>R<sup>n</sup></em>, my formulation uses a trie with entries for <em>n</em> basis elements, while Jason&#8217;s uses an <code>IntMap</code> (which is also a trie) with <em>n</em> entries (counting any implicit zeros).</p></li>
</ul>

<p>I suspect Jason&#8217;s formulation is more efficient (since it optimizes the constant case), while mine is more statically typed and more flexible (since it handles more than <em>R<sup>n</sup></em>).</p>

<p>For optimizing constants, I think I&#8217;d prefer having a single constructor with a <code>Maybe</code> for the derivatives, to eliminate code duplication.</p>

<p>I am still trying to understand the paper <em><a href="http://www.bcl.hamilton.ie/~qobi/tower/papers/popl2007a.pdf" title="paper by Barak Pearlmutter and Jeffrey Siskind">Lazy Multivariate Higher-Order Forward-Mode AD</a></em>, with its management of various epsilons.</p>

<p>A final remark: I prefer the term &#8220;higher-dimensional&#8221; over the traditional &#8220;multivariate&#8221;.
I hear classic syntax/semantics confusion in the latter.</p>

<!--
**Edits**:

* 2008-02-09: just fiddling around
-->
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=77&amp;md5=b3c10818ce32ef5eab99aec62217d345"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/comparing-formulations-of-higher-dimensional-higher-order-derivatives/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fcomparing-formulations-of-higher-dimensional-higher-order-derivatives&amp;language=en_GB&amp;category=text&amp;title=Comparing+formulations+of+higher-dimensional%2C+higher-order+derivatives&amp;description=I+just+reread+Jason+Foutz%26%238217%3Bs+post+Higher+order+multivariate+automatic+differentiation+in+Haskell%2C+as+I%26%238217%3Bm+thinking+about+this+topic+again.+I+like+his+trick+of+using+an+IntMap+to+hold...&amp;tags=comparison%2Cderivative%2Clinear+map%2Cmath%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Simpler, more efficient, functional linear maps</title>
		<link>http://conal.net/blog/posts/simpler-more-efficient-functional-linear-maps</link>
		<comments>http://conal.net/blog/posts/simpler-more-efficient-functional-linear-maps#comments</comments>
		<pubDate>Mon, 20 Oct 2008 01:26:05 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[linear map]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[memoization]]></category>
		<category><![CDATA[trie]]></category>
		<category><![CDATA[type family]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=57</guid>
		<description><![CDATA[A previous post described a data type of functional linear maps. As Andy Gill pointed out, we had a heck of a time trying to get good performance. This note describes a new representation that is very simple and much more efficient. It&#8217;s terribly obvious in retrospect but took me a good while to stumble [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- teaser -->

<p
>A <a href="http://conal.net/blog/posts/functional-linear-maps/" title="Blog post: &quot;Functional linear maps&quot;"
  >previous post</a
  > described a data type of functional linear maps. As <a href="http://www.unsafeperformio.com/index.php" title="Andy Gill's home page"
  >Andy Gill</a
  > <a href="http://blog.unsafeperformio.com/?p=23" title="Blog post: &quot;Performance problems with functional representation of derivatives&quot;"
  >pointed out</a
  >, we had a heck of a time trying to get good performance. This note describes a new representation that is <em
  >very simple</em
  > and much more efficient. It&#8217;s terribly obvious in retrospect but took me a good while to stumble onto.</p
>

<p
>The Haskell module described here is part of the <a href="http://haskell.org/haskellwiki/vector-space" title="Library wiki page: &quot;vector-space&quot;"
  >vector-space library</a
  > (version 0.5 or later) and requires ghc version 6.10 or better (for associated types).</p
>

<p
><strong
  >Edits</strong
  >:</p
>

<ul
><li
  >2008-11-09: Changed remarks about versions. The vector-space version 0.5 depends on ghc 6.10.</li
  ><li
  >2008-10-21: Fixed the <a href="http://haskell.org/haskellwiki/vector-space" title="Library wiki page: &quot;vector-space&quot;"
    >vector-space library</a
    > link in the teaser.</li
  ></ul
>

<p><span id="more-57"></span></p>

<div id="linear-maps"
><h3
  >Linear maps</h3
  ><p
  >Semantically, a <em
    >linear map</em
    > is a function <code
    >f &#8759; a &#8594; b</code
    > such that, for all scalar values <code
    >s</code
    > and &quot;vectors&quot; <code
    >u, v &#8759; a</code
    >, the following properties hold:</p
  ><div class=math-inset>
<p
  ><span class="math"
    ><em
      >f</em
      > (<em
      >s</em
      > ⋅ <em
      >u</em
      >) = <em
      >s</em
      > ⋅ <em
      >f</em
      > <em
      >u</em
      ></span
    ></p
  ><p
  ><span class="math"
    ><em
      >f</em
      > (<em
      >u</em
      > + <em
      >v</em
      >) = <em
      >f</em
      > <em
      >u</em
      > + <em
      >f</em
      > <em
      >v</em
      ></span
    ></p
  ></div>
<p
  >By repeated application of these properties,</p
  ><div class=math-inset>
<p
  ><span class="math"
    ><em
      >f</em
      > (<em
      >s</em
      ><sub
      >1</sub
      > ⋅ <em
      >u</em
      ><sub
      >1</sub
      > + ⋯ + <em
      >s</em
      ><sub
      ><em
    >n</em
    ></sub
      > ⋅ <em
      >u</em
      ><sub
      ><em
    >n</em
    ></sub
      >) = <em
      >s</em
      ><sub
      >1</sub
      > ⋅ <em
      >f</em
      > <em
      >u</em
      ><sub
      >1</sub
      > + ⋯ + <em
      >s</em
      ><sub
      ><em
    >n</em
    ></sub
      > ⋅ <em
      >f</em
      > <em
      >u</em
      ><sub
      ><em
    >n</em
    ></sub
      ></span
    ></p
  ></div>
<p
  >Taking the <em
    >u<sub>i</sub></em
    > as basis vectors, this form implies that a linear function is determined by its behavior on any <a href="http://en.wikipedia.org/wiki/Basis_(linear_algebra)" title="Wikipedia article"
    >basis</a
    > of its domain type.</p
  ><p
  >Therefore, a linear function can be represented simply as a function from a basis, using the representation described in <a href="http://conal.net/blog/posts/vector-space-bases-via-type-families/" title="Blog post: &quot;Vector space bases via type families&quot;"
    ><em
      >Vector space bases via type families</em
      ></a
    >.</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >type</span
      > u <span class="fu"
      >:-*</span
      > v <span class="fu"
      >=</span
      > <span class="dt"
      >Basis</span
      > u &#8594; v<br
       /></code
    ></pre
  ><p
  >The semantic function converts from <code
    >(u :-* v)</code
    > to <code
    >(u &#8594; v)</code
    >. It decomposes a source vector into its coordinates, applies the basis function to basis representations, and linearly combines the results.</p
  ><pre class="sourceCode haskell"
  ><code
    >lapply <span class="dv"
      >&#8759;</span
      > ( <span class="dt"
      >VectorSpace</span
      > u, <span class="dt"
      >VectorSpace</span
      > v<br
       />         , <span class="dt"
      >Scalar</span
      > u <span class="fu"
      >~</span
      > <span class="dt"
      >Scalar</span
      > v, <span class="dt"
      >HasBasis</span
      > u ) &#8658;<br
       />         (u <span class="fu"
      >:-*</span
      > v) &#8594; (u &#8594; v)<br
       />lapply lm <span class="fu"
      >=</span
      > &#955; u &#8594; sumV [s <span class="fu"
      >*^</span
      > lm b <span class="fu"
      >|</span
      > (b,s) &#8592; decompose u]<br
       /></code
    ></pre
  ><p
  >or</p
  ><pre class="sourceCode haskell"
  ><code
    >lapply lm <span class="fu"
      >=</span
      > linearCombo &#8728; <span class="fu"
      >fmap</span
      > (first lm) &#8728; decompose<br
       /></code
    ></pre
  ><p
  >The reverse function is easier. Convert a function <code
    >f</code
    >, presumed linear, to a linear map representation:</p
  ><pre class="sourceCode haskell"
  ><code
    >linear <span class="dv"
      >&#8759;</span
      > (<span class="dt"
      >VectorSpace</span
      > u, <span class="dt"
      >VectorSpace</span
      > v, <span class="dt"
      >HasBasis</span
      > u) &#8658;<br
       />         (u &#8594; v) &#8594; (u <span class="fu"
      >:-*</span
      > v)<br
       /></code
    ></pre
  ><p
  >It suffices to apply <code
    >f</code
    > to basis values:</p
  ><pre class="sourceCode haskell"
  ><code
    >linear f <span class="fu"
      >=</span
      > f &#8728; basisValue<br
       /></code
    ></pre
  ></div
>

<div id="memoization"
><h3
  >Memoization</h3
  ><p
  >The idea of the linear map representation is to reconstruct an entire (linear) function out of just a few samples. In other words, we can make a very small sampling of function's domain, and re-use those values in order to compute the function's value at <em
    >all</em
    > domain values. As implemented above, however, this trick makes function application more expensive, not less. If <code
    >lm = linear f</code
    >, then each use of <code
    >lapply lm</code
    > can apply <code
    >f</code
    > to the value of every basis element, and then linearly combine results.</p
  ><p
  >A simple trick fixes this efficiency problem: <em
    >memoize</em
    > the linear map. We could do the memoization privately, e.g.,</p
  ><pre class="sourceCode haskell"
  ><code
    >linear f <span class="fu"
      >=</span
      > memo (f &#8728; basisValue)<br
       /></code
    ></pre
  ><p
  >If <code
    >lm = linear f</code
    >, then no matter how many times <code
    >lapply lm</code
    > is applied, the function <code
    >f</code
    > can only get applied as many times as the dimension of the domain of <code
    >f</code
    >.</p
  ><p
  >However, there are several other ways to make linear maps, and it would be easy to forget to memoize each combining form. So, instead of the function representation above, I ensure that the function be memoized by representing it as a <a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="Blog post: &quot;Elegant memoization with functional memo tries&quot;"
    >memo trie</a
    >.</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >type</span
      > u <span class="fu"
      >:-*</span
      > v <span class="fu"
      >=</span
      > <span class="dt"
      >Basis</span
      > u &#8603; v<br
       /></code
    ></pre
  ><p
  >The conversion functions <code
    >linear</code
    > and <code
    >lapply</code
    > need just a little tweaking. Split <code
    >memo</code
    > into its definition <code
    >untrie &#8728; trie</code
    >, and then move the second phase (<code
    >untrie</code
    >) into <code
    >lapply</code
    >. We'll also have to add <code
    >HasTrie</code
    > constraints:</p
  ><pre class="sourceCode haskell"
  ><code
    >linear <span class="dv"
      >&#8759;</span
      > ( <span class="dt"
      >VectorSpace</span
      > u, <span class="dt"
      >VectorSpace</span
      > v<br
       />         , <span class="dt"
      >HasBasis</span
      > u, <span class="dt"
      >HasTrie</span
      > (<span class="dt"
      >Basis</span
      > u) ) &#8658;<br
       />         (u &#8594; v) &#8594; (u <span class="fu"
      >:-*</span
      > v)<br
       />linear f <span class="fu"
      >=</span
      > trie (f &#8728; basisValue)<br
       /><br
       />lapply <span class="dv"
      >&#8759;</span
      > ( <span class="dt"
      >VectorSpace</span
      > u, <span class="dt"
      >VectorSpace</span
      > v, <span class="dt"
      >Scalar</span
      > u <span class="fu"
      >~</span
      > <span class="dt"
      >Scalar</span
      > v<br
       />         , <span class="dt"
      >HasBasis</span
      > u, <span class="dt"
      >HasTrie</span
      > (<span class="dt"
      >Basis</span
      > u) ) &#8658;<br
       />         (u <span class="fu"
      >:-*</span
      > v) &#8594; (u &#8594; v)<br
       />lapply lm <span class="fu"
      >=</span
      > linearCombo &#8728; <span class="fu"
      >fmap</span
      > (first (untrie lm)) &#8728; decompose<br
       /></code
    ></pre
  ><p
  >Now we can build up linear maps conveniently and efficiently by using the operations on memo tries shown in <a href="http://conal.net/blog/posts/composing-memo-tries/" title="blog post"
    ><em
      >Composing memo tries</em
      ></a
    >. For instance, suppose that <code
    >h</code
    > is a linear function of two arguments (linear in <em
    >both</em
    >, not it <em
    >each</em
    >) and <code
    >m</code
    > and <code
    >n</code
    > are two linear maps. Then <code
    >liftA2 h m n</code
    > is the linear function that applies <code
    >h</code
    > to the results of <code
    >m</code
    > and <code
    >n</code
    >.</p
  ><pre class="sourceCode haskell"
  ><code
    >lapply (liftA2 h m n) a <span class="fu"
      >=</span
      > h (lapply m a) (lapply n a)<br
       /></code
    ></pre
  ><p
  >Exploiting the applicative functor instance for functions, we get another formulation:</p
  ><pre class="sourceCode haskell"
  ><code
    >lapply (liftA2 h m n) <span class="fu"
      >=</span
      > liftA2 h (lapply m) (lapply n)<br
       /></code
    ></pre
  ><p
  >In other words, the meaning of a <code
    >liftA2</code
    > is the <code
    >liftA2</code
    > of the meanings, as discussed in <a href="http://conal.net/blog/posts/simplifying-semantics-with-type-class-morphisms" title="blog post"
    ><em
      >Simplifying semantics with type class morphisms</em
      ></a
    >.</p
  ></div
>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=57&amp;md5=a424d64bbbfc4572f7de631b69af7b6f"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/simpler-more-efficient-functional-linear-maps/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fsimpler-more-efficient-functional-linear-maps&amp;language=en_GB&amp;category=text&amp;title=Simpler%2C+more+efficient%2C+functional+linear+maps&amp;description=A+previous+post+described+a+data+type+of+functional+linear+maps.+As+Andy+Gill+pointed+out%2C+we+had+a+heck+of+a+time+trying+to+get+good+performance.+This+note...&amp;tags=linear+map%2Cmath%2Cmemoization%2Ctrie%2Ctype+family%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Functional linear maps</title>
		<link>http://conal.net/blog/posts/functional-linear-maps</link>
		<comments>http://conal.net/blog/posts/functional-linear-maps#comments</comments>
		<pubDate>Wed, 04 Jun 2008 05:49:20 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[derivative]]></category>
		<category><![CDATA[linear map]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[type family]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=51</guid>
		<description><![CDATA[Two earlier posts described a simple and general notion of derivative that unifies the many concrete notions taught in traditional calculus courses. All of those variations turn out to be concrete representations of the single abstract notion of a linear map. Correspondingly, the various forms of mulitplication in chain rules all turn out to be [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Functional linear maps

Tags: linear maps, math, derivatives, type families

URL: http://conal.net/blog/posts/functional-linear-maps/

-->

<!-- references -->

<!-- teaser -->

<p>Two <a href="http://conal.net/blog/posts/what-is-a-derivative-really/" title="Blog post: &quot;What is a derivative, really?&quot;">earlier</a> <a href="http://conal.net/blog/posts/higher-dimensional-higher-order-derivatives-functionally/" title="Blog post: &quot;Higher-dimensional, higher-order derivatives, functionally&quot;">posts</a> described a simple and general notion of <em>derivative</em> that unifies the many concrete notions taught in traditional calculus courses.
All of those variations turn out to be concrete representations of the single abstract notion of a <em>linear map</em>.
Correspondingly, the various forms of mulitplication in chain rules all turn out to be implementations of <em>composition</em> of linear maps.
For simplicity, I suggested a direct implementation of linear maps as functions.
Unfortunately, that direct representation thwarts efficiency, since functions, unlike data structures, do not cache by default.</p>

<p>This post presents a <em>data</em> representation of linear maps that makes crucial use of (a) linearity and (b) the recently added language feature <em>indexed type families</em> (&#8220;associated types&#8221;).</p>

<p>For a while now, I&#8217;ve wondered if a library for linear maps could replace and generalize matrix libraries.
After all, matrices represent of a restricted class of linear maps.
Unlike conventional matrix libraries, however, the linear map library described in this post captures matrix/linear-map dimensions via <em>static typing</em>.
The composition function defined below statically enforces the conformability property required of matrix multiplication (which implements linear map composition).
Likewise, conformance for addition of linear maps is also enforced simply and statically.
Moreover, with sufficiently sophisticated coaxing of the Haskell compiler, of the sort <a href="http://reddit.com/goto?id=6lx36" title="Blog post: &quot;Haskell as fast as C: working at a high altitude for low level performance&quot;">Don Stewart does</a>, perhaps a library like this one could also have terrific performance.  (It doesn&#8217;t yet.)</p>

<p>You can read and try out the code for this post in the module <a href="http://code.haskell.org/vector-space/doc/html/src/Data-LinearMap.html" title="Source module: Data.LinearMap">Data.LinearMap</a> in version 0.2.0 or later of the <a href="http://haskell.org/haskellwiki/vector-space" title="Library wiki page: &quot;vector-space&quot;">vector-space</a> package.
That module also contains an implementation of linear map composition, as well as <code>Functor</code>-like and <code>Applicative</code>-like operations.
<a href="http://www.unsafeperformio.com/index.php" title="Andy Gill's home page">Andy Gill</a> has been helping me get to the bottom of some some severe performance problems, apparently involving huge amounts of redundant dictionary creation.</p>

<p><strong>Edits</strong>:</p>

<ul>
<li>2008-06-04: Brief explanation of the associated data type declaration.</li>
</ul>

<!-- without a comment or something here, the last item above becomes a paragraph -->

<p><span id="more-51"></span></p>

<h3>Linear maps</h3>

<p>Semantically, a <em>linear map</em> is a function <code>f :: a -&gt; b</code> such that, for all scalar values <code>c</code> and &#8220;vectors&#8221; <code>u, v :: a</code>, the following properties hold:</p>

<pre><code>f (c *^ u)  == c *^ f u
f (u ^+^ v) == f u ^+^ f v
</code></pre>

<p>where <code>(*^)</code> and <code>(^+^)</code> are scalar multiplication and vector addition.
(See <code>VectorSpace</code> details in a <a href="http://conal.net/blog/posts/higher-dimensional-higher-order-derivatives-functionally/" title="Blog post: &quot;Higher-dimensional, higher-order derivatives, functionally&quot;">previous post</a>.)</p>

<p>Although the semantics of a linear map will be a function, the representation will be a data structure.</p>

<pre><code>data a :-* b = ...
</code></pre>

<p>The semantic function is</p>

<pre><code>lapply :: (LMapDom a s, VectorSpace b s) =&gt;
          (a :-* b) -&gt; (a -&gt; b)  -- result will be linear
</code></pre>

<p>The first constraint says that we know how to represent linear maps whose domain is the vector space <code>a</code>, which has the associated scalar field <code>s</code>.
The second constraint say that <code>b</code> must be a vector space over that same scalar field.</p>

<p>Conversely, there is also a function to turn any linear function into a linear map:</p>

<pre><code>linear :: LMapDom a s =&gt; (a -&gt; b) -&gt; (a :-* b)  -- argument must be linear
</code></pre>

<p>These two functions and the linear map data type are packaged up as the <code>LMapDom</code> type class:</p>

<pre><code>-- | Domain of a linear map.
class VectorSpace a s =&gt; LMapDom a s | a -&gt; s where
  -- | Linear map type
  data (:-*) a :: * -&gt; *
  -- | Linear map as function
  lapply :: VectorSpace b s =&gt; (a :-* b) -&gt; (a -&gt; b)
  -- | Function (assumed linear) as linear map.
  linear :: (a -&gt; b) -&gt; (a :-* b)
</code></pre>

<p>The <code>data</code> definition means that the data type <code>(a :-* b)</code> (of linear maps from <code>a</code> to <code>b</code>) has a variety of representations, each one associated with a type <code>a</code>.</p>

<p>These two conversion functions are required to be inverses:</p>

<pre><code>{-# RULES

"linear.lapply"   forall m. linear (lapply m) = m

"lapply.linear"   forall f. lapply (linear f) = f

 #-}
</code></pre>

<h3>Scalar domains</h3>

<p>Consider a linear function <code>f</code> over a scalar domain.
Then</p>

<pre><code>f s == f (s *^ 1)
    == s *^ f 1  -- by linearity
</code></pre>

<p>Therefore, <code>f</code> is fully determined by its value at <code>1</code>, and so an adequate representation of <code>f</code> is then simply the value <code>f 1</code>.</p>

<p>This observation leads to <code>LMapDom</code> instances like the following:</p>

<pre><code>instance LMapDom Double Double where
  data Double :-* o  = DoubleL o
  lapply (DoubleL o) = (*^ o)
  linear f           = DoubleL (f 1)
</code></pre>

<h3>Non-scalar domains</h3>

<p>Maps over non-scalar domains are a little trickier.
Consider a linear function <code>f</code> over a domain of pairs of scalar values.
Then</p>

<pre><code>f (a,b) == f (a *^ (1,0) ^+^ b *^ (0,1))
        == f (a *^ (1,0)) ^+^ f (b *^ (0,1))  -- linearity
        == a *^ f (1,0) ^+^ b *^ f (0,1)      -- linearity twice more
</code></pre>

<p>So <code>f</code> is determined by <code>f (1,0)</code> and <code>f (0,1)</code> and thus can be represented by those two values.</p>

<pre><code>instance LMapDom (Double,Double) Double where
  data (Double,Double) :-* o = PairD o o
  PairD ao bo `lapply` (a,b) = a *^ ao ^+^ b *^ bo
  linear f = PairD (f (0,1)) (f (1,0))
</code></pre>

<p>and similarly for triples, etc.</p>

<p>This definition works fine, but I want something compositional.
I&#8217;d like linear maps over pairs of pairs and so on.</p>

<h3>Composing domains</h3>

<p>We can still use part of our linearity property.  Using <code>zeroV</code> as the zero vector for arbitrary vector spaces,</p>

<pre><code>f (a,b) == f ((a,zeroV) ^+^ (zeroV,b))
        == f (a,zeroV) ^+^ f (zeroV,b)
</code></pre>

<p>We see that <code>f</code> is determined by its behavior when either argument is zero.</p>

<p>In other words, <code>f</code> can be reconstructed from two other functions over simpler domains:</p>

<pre><code>fa a = f (a,zeroV)
fb b = f (zeroV,b)
</code></pre>

<p>If <code>f :: (a,b) -&gt; o</code>, then <code>fa :: a -&gt; o</code> and <code>fb :: b -&gt; o</code>.</p>

<p>Exercise: show that <code>fa</code> and <code>fb</code> are linear if <code>f</code> is.
We can thus reduce the problem of representing the linear function <code>f</code> to the problems of representing <code>fa</code> and <code>fb</code>.
This insight is captured in the following <code>LMapDom</code> instance:</p>

<pre><code>instance (LMapDom a s, LMapDom b s) =&gt; LMapDom (a,b) s where
  data (a,b) :-* o = PairL (a :-* o) (b :-* o)
  PairL ao bo `lapply` (a,b) = ao `lapply` a ^+^ bo `lapply` b
  linear f = PairL (linear ( a -&gt; f (a,zeroV)))
                   (linear ( b -&gt; f (zeroV,b)))
</code></pre>

<p>Of course, there are similar instances for triples, etc, as well as for tuple variants with strict fields, such as OpenGL&#8217;s <code>Vector2</code> and <code>Vector3</code> types.</p>

<h3>What have we done?</h3>

<p>If you&#8217;ve studied linear algebra, you may be thinking now about the idea of a <em>basis</em> of a vector space.
A basis is a minimal set of vectors that can be combined linearly to cover the entire vector space.
Any linear map is determined by its behavior on any basis.
For scalars, the set <code>{1}</code> is a basis, while for pairs of scalars, <code>{(1,0), (0,1)}</code> is a basis.
It is not just coincidental that exactly these basis vectors showed up in the definitions of <code>linear</code> for <code>Double</code> and <code>(Double,Double)</code>.</p>

<p>In the general pairing instance of <code>LMapDom</code> above, bases are built up recursively.
Each recursive call to <code>linear</code> results in a data structure that holds the values of <code>fa</code> over a basis for <code>a</code> and the values of <code>fb</code> over a basis for <code>b</code>.
Each of those basis vectors corresponds to a basis vector for <code>(a,b)</code>, by <code>zeroV</code>-padding.</p>

<p>The <em>dimension</em> of a vector space is the number of elements in a basis (which is independent of the particular choice of basis).
For vector space types <code>a</code> and <code>b</code>,</p>

<pre><code>dimension (a,b) == dimension a + dimension b
</code></pre>

<p>which corresponds to the fact that our linear map representation (as built by <code>linear</code>) contains samples for each basis element of <code>a</code>, <em>plus</em> samples for each basis element of <code>b</code> (all <code>zeroV</code>-padded).</p>

<h3>Working with linear maps</h3>

<p>Besides the instances above for creating and applying linear maps, what else can we do?
For starters, let&#8217;s define the identity linear map.
Since the identity function is already linear, simply convert it to a linear map:</p>

<pre><code>idL :: LMapDom a s =&gt; a :-* a
idL = linear id
</code></pre>

<p>Another very useful tool is transforming a linear map by transforming a linear function.</p>

<pre><code>inL :: (LMapDom c s, VectorSpace b s', LMapDom a s') =&gt;
        ((a -&gt; b) -&gt; (c -&gt; d)) -&gt; ((a :-* b) -&gt; (c :-* d))
inL h = linear . h . lapply
</code></pre>

<p>where the higher-order function <code>h</code> is assumed to map linear functions to linear functions.</p>

<p>We don&#8217;t have to stop at <em>unary</em> transformations of linear functions.</p>

<pre><code>-- | Transform a linear maps by transforming linear functions.
inL2 :: ( LMapDom c s, VectorSpace b s', LMapDom a s'
        , LMapDom e s, VectorSpace d s ) =&gt;
        ((a  -&gt; b) -&gt; (c  -&gt; d) -&gt; (e  -&gt; f))
     -&gt; ((a :-* b) -&gt; (c :-* d) -&gt; (e :-* f))
inL2 h = inL . h . lapply
</code></pre>

<p>The type constraints are starting to get hairy.
Fortunately, they&#8217;re entirely inferred by the compiler.</p>

<p>Let&#8217;s do some inlining and simplification to see what goes on inside <code>inL2</code>:</p>

<pre><code>inL2 h m n
  == (inL . h . lapply) m n                -- inline inL2
  == inL (h (lapply m)) n                  -- inline (.)
  == (linear . (h (lapply m)) . lapply) n  -- inline inL
  == linear (h (lapply m) (lapply n))      -- inline (.)
</code></pre>

<p>Ternary transformations are defined similarly.
I&#8217;ll spare you the type constraints this time.</p>

<pre><code>inL3 :: ( ... ) =&gt;
        ((a  -&gt; b) -&gt; (c  -&gt; d) -&gt; (e  -&gt; f) -&gt; (p  -&gt; q))
     -&gt; ((a :-* b) -&gt; (c :-* d) -&gt; (e :-* f) -&gt; (p :-* q))
inL3 h = inL2 . h . lapply
</code></pre>

<p>Look what happens when these operations are composed.
As an example,</p>

<pre><code>inL h . inL g
  == (linear . h . lapply) . (linear . g . lapply)
  == linear . h . lapply . linear . g . lapply    -- associativity of (.)
  == linear . h . g . lapply                      -- rule "lapply.linear"
  == inL (h . g)
</code></pre>

<p>This transformation is not actually happening in the compiler yet.
The &#8220;lapply.linear&#8221; rule is not firing, and I don&#8217;t know why.
I&#8217;d appreciate suggestions.</p>

<p>There are a few more operations defined in <a href="http://code.haskell.org/vector-space/doc/html/src/Data-LinearMap.html" title="Source module: Data.LinearMap">Data.LinearMap</a>.
I&#8217;ll end with this simple, general definition of composition of linear maps:</p>

<pre><code>-- | Compose linear maps
(.*) :: (VectorSpace c s, LMapDom b s, LMapDom a s) =&gt;
        (b :-* c) -&gt; (a :-* b) -&gt; (a :-* c)
(.*) = inL2 (.)
</code></pre>

<h3>Derivative towers again</h3>

<p>A similar, but recursive, definition is used in the new definition of the the general chain rule for infinite derivative towers, updated since the post <em><a href="http://conal.net/blog/posts/higher-dimensional-higher-order-derivatives-functionally/" title="Blog post: &quot;Higher-dimensional, higher-order derivatives, functionally&quot;">Higher-dimensional, higher-order derivatives, functionally</a></em>.</p>

<pre><code>(@.) :: (LMapDom b s, LMapDom a s, VectorSpace c s) =&gt;
        (b :~&gt; c) -&gt; (a :~&gt; b) -&gt; (a :~&gt; c)
(h @. g) a0 = D c0 (inL2 (@.) c' b')
  where
    D b0 b' = g a0
    D c0 c' = h b0
</code></pre>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=51&amp;md5=9b9afbe99da6c23039f2be00a2cb2c53"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/functional-linear-maps/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Ffunctional-linear-maps&amp;language=en_GB&amp;category=text&amp;title=Functional+linear+maps&amp;description=Two+earlier+posts+described+a+simple+and+general+notion+of+derivative+that+unifies+the+many+concrete+notions+taught+in+traditional+calculus+courses.+All+of+those+variations+turn+out+to+be...&amp;tags=derivative%2Clinear+map%2Cmath%2Ctype+family%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Higher-dimensional, higher-order derivatives, functionally</title>
		<link>http://conal.net/blog/posts/higher-dimensional-higher-order-derivatives-functionally</link>
		<comments>http://conal.net/blog/posts/higher-dimensional-higher-order-derivatives-functionally#comments</comments>
		<pubDate>Wed, 21 May 2008 05:29:32 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[beautiful code]]></category>
		<category><![CDATA[derivative]]></category>
		<category><![CDATA[linear map]]></category>
		<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=49</guid>
		<description><![CDATA[The post Beautiful differentiation showed some lovely code that makes it easy to compute not just the values of user-written functions, but also all of its derivatives (infinitely many). This elegant technique is limited, however, to functions over a scalar (one-dimensional) domain. Next, we explored what it means to transcend that limitation, asking and answering [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Higher-dimensional, higher-order derivatives, functionally

Tags: derivatives, linear maps, math, beautiful code

Alternative titles:

Derivative towers across the 8th dimension
Higher-dimensional derivative towers
Higher-dimensional, higher-order derivatives, functionally

URL: http://conal.net/blog/posts/higher-dimensional-higher-order-derivatives-functionally/

-->

<!-- references -->

<!-- teaser -->

<p>The post <em><a href="http://conal.net/blog/posts/beautiful-differentiation/" title="Blog post: &quot;Beautiful differentiation&quot;">Beautiful differentiation</a></em> showed some lovely code that makes it easy to compute not just the values of user-written functions, but also <em>all</em> of its derivatives (infinitely many).
This elegant technique is limited, however, to functions over a <em>scalar</em> (one-dimensional) domain.
Next, we explored what it means to transcend that limitation, asking and answering the question <em><a href="http://conal.net/blog/posts/what-is-a-derivative-really/" title="Blog post: &quot;What is a derivative, really?&quot;">What is a derivative, really?</a></em>
The answer to that question is that derivative values are <em>linear maps</em> saying how small input changes result in output changes.
This answer allows us to unify several different notions of derivatives and their corresponding chain rules into a single simple and powerful form.</p>

<p>This third post combines the ideas from the two previous posts, to easily compute infinitely many derivatives of functions over arbitrary-dimensional domains.</p>

<p>The code shown here is part of a <a href="http://haskell.org/haskellwiki/vector-space" title="Library wiki page: &quot;vector-space&quot;">new Haskell library</a>, which you can download and play with or peruse on the web.</p>

<!--
**Edits**:

* 2008-02-09: just fiddling around
-->

<!-- without a comment or something here, the last item above becomes a paragraph -->

<p><span id="more-49"></span></p>

<h3>The general setting: vector spaces</h3>

<p>Linear maps (transformations) lie at the heart of the generalized idea of derivative <a href="http://conal.net/blog/posts/what-is-a-derivative-really/" title="Blog post: &quot;What is a derivative, really?&quot;">described earlier</a>.
Talking about linearity requires a few simple operations, which are encapsulated in the the abstract interface known from math as a <em>vector space</em>.</p>

<p>A vector space <code>v</code> has an associated type <code>s</code> of scalar values (a field) and a set of operations.
In Haskell,</p>

<pre><code>class VectorSpace v s | v -&gt; s where
  zeroV   :: v              -- the zero vector
  (*^)    :: s -&gt; v -&gt; v    -- scale a vector
  (^+^)   :: v -&gt; v -&gt; v    -- add vectors
  negateV :: v -&gt; v         -- additive inverse
</code></pre>

<p>In many cases, we&#8217;ll want to add inner (dot) products as well, to form an <em>inner product space</em>:</p>

<pre><code>class VectorSpace v s =&gt; InnerSpace v s | v -&gt; s where
  (&lt;.&gt;) :: v -&gt; v -&gt; s
</code></pre>

<p>Several other useful operations can be defined in terms of these five methods.
For instance, vector subtraction and linear interpolation for vector spaces, and magnitude and normalization (rescaling to unit length) for inner product spaces.
The <a href="http://haskell.org/haskellwiki/vector-space" title="Library wiki page: &quot;vector-space&quot;">vector-space</a> library defines instances for <code>Float</code>, <code>Double</code>, and <code>Complex</code>, as well as pairs, triples, and quadruples of vectors, and functions with vector ranges.
(By &#8220;vector&#8221; here, I mean any instance of <code>VectorSpace</code>, recursively).</p>

<p>It&#8217;s pretty easy to define new instances of your own.
For instance, here is the library&#8217;s definition of functions as vector spaces, using the same techniques <a href="http://conal.net/blog/posts/beautiful-differentiation/" title="Blog post: &quot;Beautiful differentiation&quot;">as before</a>:</p>

<pre><code>instance VectorSpace v s =&gt; VectorSpace (a-&gt;v) s where
  zeroV   = pure   zeroV
  (*^) s  = fmap   (s *^)
  (^+^)   = liftA2 (^+^)
  negateV = fmap   negateV
</code></pre>

<p>Linear transformations could perhaps be defined as an abstract data type, with primitives and a composition operator.
I don&#8217;t know how to provide enough primitives for all possibly types of interest.
I also played with linear maps as a <a href="http://www.haskell.org/haskellwiki/GHC/Type_families">type family</a>, indexed on the domain or range type, but it didn&#8217;t quite work out for me.
For now, I&#8217;ll simply represent a linear map as a function, define a type synonym as reminder of intention:</p>

<pre><code>type a :-* b = a -&gt; b       -- linear map
</code></pre>

<p>This definition makes some things quite convenient.
Function composition, <code>(.)</code>, implements linear map composition.
The function <code>VectorSpace</code> instance (above) gives the customary meaning for linear maps as vector spaces.
Like, <code>(-&gt;)</code>, this new <code>(:-*)</code> operator is <em>right</em>-associative, so <code>a :-* b :-* c</code> means <code>a :-* (b :-* c)</code>.</p>

<h4>Derivative towers</h4>

<p>A derivative tower contains a value and <em>all</em> derivatives of a function at a point.
<a href="http://conal.net/blog/posts/what-is-a-derivative-really/" title="Blog post: &quot;What is a derivative, really?&quot;">Previously</a>, I&#8217;d suggested the following type for derivative towers.</p>

<pre><code>data a :&gt; b = D b (a :&gt; (a :-* b))   -- old definition
</code></pre>

<p>The values in one of these towers have types <code>b</code>, <code>a :-&gt; b</code>, <code>a :-&gt; a :-&gt; b</code>, &#8230;.
So, for instance, a second derivative value is a linear map from <code>a</code> to linear maps from <code>a</code> to b.
(Uncurrying a second derivative yields a <em>bilinear</em> map.)</p>

<p>Since making this suggestion, I&#8217;ve gotten simpler code using the following variation, which I&#8217;ll use instead:</p>

<pre><code>data a :&gt; b = D b (a :-* (a :&gt; b))
</code></pre>

<p>Now a tower value is a regular value, plus a linear map that yields a tower for the derivative.</p>

<p>We can also write this second version more simply, without the linearity reminder:</p>

<pre><code>data a :&gt; b = D b (a :~&gt; b)
</code></pre>

<p>where <code>a :~&gt; b</code> is the type of infinitely differentiable functions, represented as a function that produces a derivative tower:</p>

<pre><code>type a :~&gt; b = a -&gt; (a :&gt; b)
</code></pre>

<h4>Basics</h4>

<p>As in <em><a href="http://conal.net/blog/posts/beautiful-differentiation/" title="Blog post: &quot;Beautiful differentiation&quot;">Beautiful differentiation</a></em>, constant functions have all derivatives equal to zero:</p>

<pre><code>dConst :: VectorSpace b s =&gt; b -&gt; a:&gt;b
dConst b = b `D` const dZero

dZero :: VectorSpace b s =&gt; a:&gt;b
dZero = dConst zeroV
</code></pre>

<p>Note the use of the standard Haskell function <code>const</code>, which makes constant functions (always returning the same value).
Also, the use of the zero vector required me to use a <code>VectorSpace</code> constraint in the type signature.
(I could have used <code>0</code> and <code>Num</code> instead, but <code>Num</code> requires more methods and so is less general than <code>VectorSpace</code>.)</p>

<p>The differentiable identity function plays a very important role.
Its towers are sometimes called &#8220;the derivation variable&#8221; or similar, but it&#8217;s a not really a variable.
The definition is quite terse:</p>

<pre><code>dId :: VectorSpace u s =&gt; u :~&gt; u
dId u = D u ( du -&gt; dConst du)
</code></pre>

<p>What&#8217;s going on here?
The differentiable identity function, <code>dId</code>, takes an argument <code>u</code> and yields a tower.
The regular value (the <em>0<sup>th</sup></em> derivative) is simply the argument <code>u</code>, as one would expect from an identity function.
The derivative (a linear map) turns a tiny input offset, <code>du</code>, to a resulting output offset, which is also <code>du</code> (also as expected from an identity function).
The higher derivatives are all zero, so our first derivative tower is <code>dConst du</code>.</p>

<h4>Linear functions</h4>

<p>Returning, for a few moments, to thinking of derivatives as numbers, let&#8217;s consider about the function <code>f =  x -&gt; m * x + b</code> for some values <code>m</code> and <code>b</code>.
We&#8217;d usually say that the derivative of <code>f</code> is equal to <code>m</code> everywhere, and indeed <code>f</code> can be interpreted as a line with (constant) slope <code>m</code> and y-intercept <code>b</code>.
In the language of linear algebra, the function <code>f</code> is <em>affine</em> in general, and is (more specifically) <em>linear</em> only when <code>b == 0</code>.</p>

<p>In the generalized view of derivatives as linear maps, we say instead that the derivative is <code>x -&gt; m * x</code>.
The derivative everywhere is almost the same as <code>f</code> itself.
If we take <code>b == 0</code> (so that <code>f</code> is linear and not just affine), then the derivative of <code>f</code> is exactly <code>f</code>, everywhere!
Consequently, its higher derivatives are all zero.</p>

<p>In the generalized view of derivatives as linear maps, this relationship always holds.
The derivative of a linear function <code>f</code> is <code>f</code> everywhere.
We can encapsulate this general property as a utility function:</p>

<pre><code>linearD :: VectorSpace v s =&gt; (u :-* v) -&gt; (u :~&gt; v)
linearD f u = D (f u) ( du -&gt; dConst (f du))
</code></pre>

<p>The <code>dConst</code> here sets up all of the higher derivatives to be zero.
This definition can also be written more succinctly:</p>

<pre><code>linearD f u = D (f u) (dConst . f)
</code></pre>

<p>You may have noticed a similarity between this discussion of linear functions and the identity function above.
This similarity is more than coincidental, because the identity function is linear.
With this insight, we can write a more compact definition for <code>dId</code>, replacing the one above:</p>

<pre><code>dId = linearD id
</code></pre>

<p>As other examples of linear functions, here are differentiable versions of the functions <code>fst</code> and <code>snd</code>, which extract element from a pair.</p>

<pre><code>fstD :: VectorSpace a s =&gt; (a,b) :~&gt; a
fstD = linearD fst

sndD :: VectorSpace b s =&gt; (a,b) :~&gt; b
sndD = linearD snd
</code></pre>

<h4>Numeric operations</h4>

<p>Numeric operations can be specified much as they were <a href="http://conal.net/blog/posts/beautiful-differentiation/" title="Blog post: &quot;Beautiful differentiation&quot;">previously</a>.
First, those definition again (with variable names changed),</p>

<pre><code>instance Num b =&gt; Num (Dif b) where
  fromInteger               = dConst . fromInteger
  D u0 u' + D v0 v'         = D (u0 + v0) (u' + v')
  D u0 u' - D v0 v'         = D (u0 - v0) (u' - v')
  u@(D u0 u') * v@(D v0 v') = D (u0 * v0) (u' * v + u * v')
</code></pre>

<p>Now the new definition:</p>

<pre><code>instance (Num b, VectorSpace b b) =&gt; Num (a:&gt;b) where
  fromInteger               = dConst . fromInteger
  D u0 u' + D v0 v'         = D (u0 + v0) (u' + v')
  D u0 u' - D v0 v'         = D (u0 - v0) (u' - v')
  u@(D u0 u') * v@(D v0 v') =
    D (u0 * v0) ( da -&gt; (u * v' da) + (u' da * v))
</code></pre>

<p>The main change shows up in multiplication.
It is no longer meaningful to write something like <code>u' * v</code>, because <code>u' :: b :-* (a :&gt; b)</code>, while <code>v :: a :&gt; b</code>.
Instead, <code>v'</code> gets <em>applied to</em> the small change in input before multiplying by <code>u</code>.
Likewise, <code>u'</code> gets <em>applied to</em> the small change in input before multiplying by <code>v</code>.</p>

<p>The same sort of change has happened silently in the sum and difference cases, but are hidden by the numeric overloadings provided for functions.
Written more explicitly:</p>

<pre><code>  D u0 u' + D v0 v' = D (u0 + v0) ( da -&gt; u' da + v' da)
</code></pre>

<p>By the way, a bit of magic can also hide the &#8220;<code>da -&gt; ...</code>&#8221; in the definition of multiplication:</p>

<pre><code>  u@(D u0 u') * v@(D v0 v') = D (u0 * v0) ((u *) . v' + (* v) . u')
</code></pre>

<p>The derivative part can be deciphered as follows: transform (the input change) by <code>v'</code> and then pre-multiply by <code>u</code>; transform (the input change) by <code>u'</code> and then post-multiply by <code>v</code>; and add the result.
If this sort of wizardry isn&#8217;t your game, forget about it and use the more explicit form.</p>

<h4>Composition &#8212; the chain rule</h4>

<p>Here&#8217;s the chain rule we used <a href="http://conal.net/blog/posts/beautiful-differentiation/" title="Blog post: &quot;Beautiful differentiation&quot;">earlier</a>.</p>

<pre><code>(&gt;-&lt;) :: (Num a) =&gt; (a -&gt; a) -&gt; (Dif a -&gt; Dif a) -&gt; (Dif a -&gt; Dif a)
f &gt;-&lt; f' =  u@(D u0 u') -&gt; D (f u0) (f' u * u')
</code></pre>

<p>The new one differs just slightly:</p>

<pre><code>(&gt;-&lt;) :: VectorSpace u s =&gt;
         (u -&gt; u) -&gt; ((a :&gt; u) -&gt; (a :&gt; s)) -&gt; (a :&gt; u) -&gt; (a :&gt; u)
f &gt;-&lt; f' =  u@(D u0 u') -&gt; D (f u0) ( da -&gt; f' u *^ u' da)
</code></pre>

<p>Or we can hide the <code>da</code>, as with multiplication:</p>

<pre><code>f &gt;-&lt; f' =  u@(D u0 u') -&gt; D (f u0) ((f' u *^) . u')
</code></pre>

<p>With this change, all of the method definitions in <em><a href="http://conal.net/blog/posts/beautiful-differentiation/" title="Blog post: &quot;Beautiful differentiation&quot;">Beautiful differentiation</a></em> work as before, with only the 
For instance,</p>

<pre><code>instance (Fractional b, VectorSpace b b) =&gt; Fractional (a:&gt;b) where
  fromRational = dConst . fromRational
  recip        = recip &gt;-&lt; recip sqr
</code></pre>

<p>See <a href="http://haskell.org/haskellwiki/vector-space" title="Library wiki page: &quot;vector-space&quot;">the library</a> for details.</p>

<h4>The chain rule pure and simple</h4>

<p>The <code>(&gt;-&lt;)</code> operator above is specialized form of the chain rule that is convenient for automatic differentiation.
In its simplest and most general form, the chain rule says</p>

<pre><code>deriv (f . g) x = deriv f (g x) . deriv g x
</code></pre>

<p>The composition on the right hand side is on linear maps (derivatives).
You may be used to seeing the chain rule in one or more of its specialized forms, using some form of product (scalar/scalar, scalar/vector, vector/vector dot, matrix/vector) instead of composition.
Those forms all mean the same as this general case, but are defined on various <em>representations</em> of linear maps, instead of linear maps themselves.</p>

<p>The chain rule above constructs only the first derivatives.
Instead, we&#8217;ll construct all of the derivatives by using all of the derivatives of <code>f</code> and <code>g</code>.</p>

<pre><code>(@.) :: (b :~&gt; c) -&gt; (a :~&gt; b) -&gt; (a :~&gt; c)
(f @. g) a0 = D c0 (c' @. b')
  wfere
    D b0 b' = g a0
    D c0 c' = f b0
</code></pre>

<h4>Coming attractions</h4>

<p>In this post, we&#8217;ve combined <a href="http://conal.net/blog/posts/beautiful-differentiation/" title="Blog post: &quot;Beautiful differentiation&quot;">derivative towers</a> with <a href="http://conal.net/blog/posts/what-is-a-derivative-really/" title="Blog post: &quot;What is a derivative, really?&quot;">generalized derivatives (based on linear maps)</a>, for constructing infinitely many derivatives of functions over multi-dimensional (or scalar) domains.
The inner workings are subtler than the previous code, but almost as simple to express and just as easy to use.</p>

<p>If you&#8217;re interested in learning more about generalized derivatives, I recommend the book <a href="http://books.google.com/books?hl=en&amp;id=g_EXJtkz7PYC" title="Book: &quot;Calculus on Manifolds&quot;, by Michael Spivak">Calculus on Manifolds</a>.</p>

<p>Future posts will include:</p>

<ul>
<li>A look at an efficiency issue and consider some solutions.</li>
<li>Elegant executable specifications of smooth surfaces, using derivatives for the surface normals used in shading.</li>
</ul>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=49&amp;md5=076ecf56639f40c0b1075f23a066ba1f"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/higher-dimensional-higher-order-derivatives-functionally/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fhigher-dimensional-higher-order-derivatives-functionally&amp;language=en_GB&amp;category=text&amp;title=Higher-dimensional%2C+higher-order+derivatives%2C+functionally&amp;description=The+post+Beautiful+differentiation+showed+some+lovely+code+that+makes+it+easy+to+compute+not+just+the+values+of+user-written+functions%2C+but+also+all+of+its+derivatives+%28infinitely+many%29.+This...&amp;tags=beautiful+code%2Cderivative%2Clinear+map%2Cmath%2Cblog" type="text/html" />
	</item>
		<item>
		<title>What is a derivative, really?</title>
		<link>http://conal.net/blog/posts/what-is-a-derivative-really</link>
		<comments>http://conal.net/blog/posts/what-is-a-derivative-really#comments</comments>
		<pubDate>Mon, 19 May 2008 05:01:08 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[derivative]]></category>
		<category><![CDATA[linear map]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=48</guid>
		<description><![CDATA[The post Beautiful differentiation showed how easily and beautifully one can construct an infinite tower of derivative values in Haskell programs, while computing plain old values. The trick (from Jerzy Karczmarczuk) was to overload numeric operators to operate on the following (co)recursive type: data Dif b = D b (Dif b) This representation, however, works [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: What is a derivative, really?

Tags: derivatives, linear maps, math

URL: http://conal.net/blog/posts/what-is-a-derivative-really/

-->

<!-- references -->

<!-- teaser -->

<p>The post <em><a href="http://conal.net/blog/posts/beautiful-differentiation/" title="Blog post: &quot;Beautiful differentiation&quot;">Beautiful differentiation</a></em> showed how easily and beautifully one can construct an infinite tower of derivative values in Haskell programs, while computing plain old values.
The trick (from Jerzy Karczmarczuk) was to overload numeric operators to operate on the following (co)recursive type:</p>

<pre><code>data Dif b = D b (Dif b)
</code></pre>

<p>This representation, however, works only when differentiating functions from a <em>scalar</em> (one-dimensional) domain, i.e., functions of type <code>a -&gt; b</code> for a scalar type <code>a</code>.
The reason for this limitation is that only in those cases can the type of derivative values be identified with the type of regular values.</p>

<p>Consider a function <code>f :: (R,R) -&gt; R</code>, where <code>R</code> is, say, <code>Double</code>.
The <em>value</em> of <code>f</code> at a domain value <code>(x,y)</code> has type <code>R</code>, but the derivative of <code>f</code> consists of <em>two</em> partial derivatives.
Moreover, the second derivative consists of <em>four</em> partial second-order derivatives (or three, depending how you count).
A function <code>f :: (R,R) -&gt; (R,R,R)</code> also has two partial derivatives at each point <code>(x,y)</code>, each of which is a triple.
That pair of triples is commonly written as a two-by-three matrix.</p>

<p>Each of these situations has its own derivative shape <em>and</em> its own chain rule (for the derivative of function compositions), using plain-old multiplication, scalar-times-vector, vector-dot-vector, matrix-times-vector, or matrix-times-matrix.
Second derivatives are more complex and varied.</p>

<p>How many forms of derivatives and chain rules are enough?
Are we doomed to work with a plethora of increasingly complex types of derivatives, as well as the diverse chain rules needed to accommodate all compatible pairs of derivatives?
Fortunately, not.
<em>There is a single, simple, unifying generalization</em>.
By reconsidering what we mean by a derivative value, we can see that these various forms are all representations of a single notion, <em>and</em> all the chain rules mean the same thing on the meanings of the representations.</p>

<p>This blog post is about that unifying view of derivatives.</p>

<p><strong>Edits</strong>:</p>

<ul>
<li>2008-05-20: There are several comments about this post <a href="http://reddit.com/info/6jw8w/comments/">on reddit</a>.</li>
<li>2008-05-20: Renamed derivative operator from <code>D</code> to <code>deriv</code> to avoid confusion with the data constructor for derivative towers.</li>
<li>2008-05-20: Renamed linear map type from <code>(:-&gt;)</code> to <code>(:-*)</code> to make it visually closer to a standard notation.</li>
</ul>

<!-- without a comment or something here, the last item above becomes a paragraph -->

<p><span id="more-48"></span></p>

<h3>What&#8217;s a derivative?</h3>

<p>To get an intuitive sense of what&#8217;s going on with derivatives in general, let&#8217;s look at some examples.
If you already know about <a href="http://books.google.com/books?hl=en&amp;id=g_EXJtkz7PYC" title="Book: &quot;Calculus on Manifolds&quot;, by Michael Spivak">calculus on manifolds</a>, you might want to <a href="#NewDif">skip ahead</a></p>

<h4>One dimension</h4>

<p>Start with a simple function on real numbers:</p>

<pre><code>f1 :: R -&gt; R
f1 x = x^2 + 3*x + 1
</code></pre>

<p>Writing the derivative of a function <code>f</code> as <code>deriv f</code>, let&#8217;s now consider the question: what is <code>deriv f1</code>?
We might say that</p>

<pre><code>deriv f1 x = 2*x+3
</code></pre>

<p>so e.g., <code>deriv f1 5 = 13</code>.
In other words, <code>f1</code> is changing 13 times as fast as its argument, when its argument is passing 5.</p>

<p>Rephrased yet again, if <code>dx</code> is a very tiny number, then <code>f1(5+dx) - f1 5</code> is very nearly <code>13 * dx</code>.
If <code>f1</code> maps seconds to meters, then <code>deriv f1 5</code> is 13 meters per second.
So already, we can see that the range of <code>f</code> (meters) and the range of <code>deriv f</code> (meters/second) disagree.</p>

<h4>Two dimensions in and one dimension out</h4>

<p>As a second example, consider a two-dimensional domain:</p>

<pre><code>f2 :: (R,R) -&gt; R
f2 (x,y) = 2*x*y + 3*x + 5*y + 7
</code></pre>

<p>Again, let&#8217;s consider some units, to get a guess of what kind of thing <code>deriv f2 (x,y)</code> really is.
Suppose that <code>f2</code> measures altitude of terrain above a plane, as a function of the position in the plane.
(So <code>f2</code> is a &#8220;height field&#8221;.)
You can guess that <code>deriv f (x,y)</code> is going to have something to do with how fast the altitude is changing, i.e. the slope, at <code>(x,y)</code>.
But there isn&#8217;t a single slope.
Instead, there&#8217;s a slope for <em>every</em> possible compass direction (a hiker&#8217;s degrees of freedom).</p>

<p>Now consider the conventional math answer to what is <code>deriv f2 (x,y)</code>.
Since <code>f2</code> has a two-dimensional domain, it has two partial derivatives, and its derivative is commonly written as a pair of the two partials:</p>

<pre><code>deriv f2 (x,y) = (2*y+3, 2*x+5)
</code></pre>

<p>In our example, these two pieces of information correspond to two of the possible slopes.
The first is the slope if heading directly east, and the second if directly north (increasing <code>x</code> and increasing <code>y</code>, respectively).</p>

<p>What good does it do our hiker to be told just two of the infinitude of possible slopes at a point?
The answer is perhaps magical: for well-behaved terrains, these two pieces of information are enough to calculate <em>all</em> (infinitely many) slopes, with just a bit of math.
Every direction can be described as partly east and partly north (perhaps negatively for westish and southish directions).
Given a direction angle <code>ang</code> (where east is zero and north is 90 degrees), the east and north components are <code>cos ang</code> and <code>sin ang</code>, respectively.
When heading in the direction <code>ang</code>, the slope will be a weighted sum of the north-going slope and the east-going slope, where the weights are the north and south components (<code>cos ang</code> and <code>sin ang</code>).</p>

<p>Instead of angles, our hiker may prefer thinking directly about the north and east components of a tiny step from the position <code>(x,y)</code>.
If the step is small enough and lands <code>dx</code> feet to the east and <code>dy</code> feet to the north, then the change in altitude, <code>f2(x+dx,y+dy) - f2(x,y)</code> is very nearly equal to <code>(2*y+3)*dx + (2*x+5)*dy</code>.
If we use <code>(&lt;.&gt;)</code> to mean dot (inner) product, then this change in altitude is <code>deriv f2 (x,y) &lt;.&gt; (dx,dy)</code>.</p>

<p>From this second example, we can see that the derivative value is not a range value, but also not a rate-of-change of range values.
It&#8217;s a pair of such rates with the know-how to use those rates to determine output changes.</p>

<h4>Two dimensions in and three dimensions out</h4>

<p>Next, imagine moving around on a surface in space, say a torus, and suppose that the surface has grid marks to define a two-dimensional parameter space.
As our hiker travels around in the 2D parameter space, his position in 3D space changes accordingly, more flexibly than just an altitude.
This situation corresponds to a function from 2D to 3D:</p>

<pre><code>f3 :: (R,R) -&gt; (R,R,R)
</code></pre>

<p>At any position <code>(s,t)</code> in the parameter space, and for every choice of direction through parameter space, each of the the coordinates of the position in 3D space has a rate of change.
Again, if the function is mathematically well-behaved (differentiable), then all of these rates of change can be summarized in two partial derivatives.
This time, however, each partial derivative has components in <em>X</em>, <em>Y</em>, and <em>Z</em>, so it takes six numbers to describe the 3D velocities for all possible directions in parameter space.
These numbers are usually written as a 3-by-2 matrix <code>m</code> (the <em>Jacobian</em> of <code>f3</code>).
Given a small parameter step <code>(dx,dy)</code>, the resulting change in 3D position is equal to the product of the derivative matrix and the difference vector, i.e., <code>m `timesVec` (dx,dy)</code>.</p>

<h3>A common perspective</h3>

<p>The examples above use different representations for derivatives: scalar numbers, a vector (pair of numbers), and a matrix.
Common to <em>all</em> of these representations is the ability to turn a small step in the function&#8217;s domain into a resulting step in the range.</p>

<ul>
<li>In <code>f1</code>, the (scalar) derivative <code>c</code> really means <code>(c *)</code>, meaning multiply by <code>c</code>.</li>
<li>In <code>f2</code>, the (vector) derivative <code>v</code> means <code>(v &lt;.&gt;)</code>.</li>
<li>In <code>f3</code>, the (matrix) derivative <code>m</code> means <code>(m `timesVec`)</code>.</li>
</ul>

<p>So, the common meaning of these derivative representations is a function, and not just any function, but a <em>linear</em> function&#8211;often called a &#8220;linear map&#8221; or &#8220;linear transformation&#8221;.
For a function <code>lf</code> to be <em>linear</em> in this context means that</p>

<ul>
<li><code>lf (u+v) == lf u + lf v</code>, and</li>
<li><code>lf (c*v) == c * lf v</code>, for scalar values <code>c</code>.</li>
</ul>

<p>Now what about the different chain rules, saying to combine derivative values via various kinds of products (scalar/scalar, scalar/vector, vector/vector dot, matrix/vector)?
Each of these products implements the same abstract notion, which is <em>composition</em> of linear maps.</p>

<h3 id="NewDif">What about <code>Dif</code>?</h3>

<p>Now let&#8217;s return to the derivative towers we used before:</p>

<pre><code>data Dif b = D b (Dif b)
</code></pre>

<p>As I mentioned above, this representation only works when derivative values can be represented just like range values.
That punning of derivative values with range values works when the domain type is one dimensional.
For functions over higher-dimensional domains, we&#8217;ll have to use a different representation.</p>

<p>Assume a type of linear functions from <code>a</code> to <code>b</code>:</p>

<pre><code>type a :-* b = . . .
</code></pre>

<p>(In Haskell, type constructors beginning with a colon are used infix.)
Since the derivative type depends on domain as well as range, our derivative tower will have two type parameters instead of one.
To make definitions prettier, I&#8217;ll change derivative towers to an infix operator as well.</p>

<pre><code>data a :&gt; b = D b (a :&gt; (a :-* b))
</code></pre>

<p>An infinitely differentiable function is then one that produces a derivative tower:</p>

<pre><code>type a :~&gt; b = a -&gt; (a:&gt;b)
</code></pre>

<h3>What&#8217;s next?</h3>

<p>Perhaps now you&#8217;re wondering:</p>

<ul>
<li>Are these lovely ideas workable in practice?</li>
<li>What happens to the code from <em><a href="http://conal.net/blog/posts/beautiful-differentiation/" title="Blog post: &quot;Beautiful differentiation&quot;">Beautiful differentiation</a></em>?</li>
<li>What use are derivatives, anyway?</li>
</ul>

<p>These questions and more will be answered in upcoming installments.</p>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=48&amp;md5=1ab0baee13e05d2dc55d999eedde56b9"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/what-is-a-derivative-really/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fwhat-is-a-derivative-really&amp;language=en_GB&amp;category=text&amp;title=What+is+a+derivative%2C+really%3F&amp;description=The+post+Beautiful+differentiation+showed+how+easily+and+beautifully+one+can+construct+an+infinite+tower+of+derivative+values+in+Haskell+programs%2C+while+computing+plain+old+values.+The+trick+%28from+Jerzy...&amp;tags=derivative%2Clinear+map%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
