<div dir="ltr">hmatrix and ad don&#39;t (currently) mix.<div><br></div><div style>The problem is that hmatrix uses a packed structure that can&#39;t hold any of the AD mode variants we have as an Element. =(</div><div style>
<br></div><div style>I&#39;ve been working with Alex Lang to explore in ad 4.0 ways that we can support monomorphic AD modes and still present largely the same API. We&#39;ve seen a number of performance gains off of this refactoring already, but it doesn&#39;t go far enough to address what you need.<br>
</div><div style><br></div><div style>A goal a bit farther out is to support AD on vector/matrix operations, but it is a much bigger refactoring than the one currently in the pipeline. =/</div><div style><br></div><div style>
To support automatic differentiation on vector-based operations in a form that works with standard BLAS-like storage like the packed matrix rep used in hmatrix we need to convert from a &#39;matrix of AD variables&#39; to an &#39;AD mode over of matrices&#39;. This is similar to the difference between a matrix of complex numbers and a real matrix plus an imaginary matrix.</div>
<div style><br></div><div style>This is a long term goal, but not one you&#39;re likely to see support for out of &#39;ad&#39; in the short term.</div><div style><br></div><div style><div>I can&#39;t build AD on hmatrix itself due in part to licensing restrictions and differing underlying storage requirements, so there are a lot of little issues in making that latter vision a reality.</div>
</div><div style><br></div><div style>-Edward</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Apr 9, 2013 at 10:46 AM, Dominic Steinitz <span dir="ltr">&lt;<a href="mailto:dominic@steinitz.org" target="_blank">dominic@steinitz.org</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Cafe,<br>
<br>
Suppose I want to find the grad of a function then it&#39;s easy I just<br>
use <a href="http://hackage.haskell.org/package/ad-3.4" target="_blank">http://hackage.haskell.org/package/ad-3.4</a>:<br>
<br>
import Numeric.AD<br>
import Data.Foldable (Foldable)<br>
import Data.Traversable (Traversable)<br>
<br>
data MyMatrix a = MyMatrix (a, a)<br>
  deriving (Show, Functor, Foldable, Traversable)<br>
<br>
f :: Floating a =&gt; MyMatrix a -&gt; a<br>
f (MyMatrix (x, y)) = exp $ negate $ (x^2 + y^2) / 2.0<br>
<br>
main :: IO ()<br>
main = do<br>
  putStrLn $ show $ f $ MyMatrix (0.0, 0.0)<br>
  putStrLn $ show $ grad f $ MyMatrix (0.0, 0.0)<br>
<br>
But now suppose I am doing some matrix calculations<br>
<a href="http://hackage.haskell.org/package/hmatrix-0.14.1.0" target="_blank">http://hackage.haskell.org/package/hmatrix-0.14.1.0</a> and I want to find<br>
the grad of a function of a matrix:<br>
<br>
import Numeric.AD<br>
import Numeric.LinearAlgebra<br>
import Data.Foldable (Foldable)<br>
import Data.Traversable (Traversable)<br>
<br>
g :: (Element a, Floating a) =&gt; Matrix a -&gt; a<br>
g m = exp $ negate $ (x^2 + y^2) / 2.0<br>
  where r = (toLists m)!!0<br>
        x = r!!0<br>
        y = r!!1<br>
<br>
main :: IO ()<br>
main = do<br>
  putStrLn $ show $ g $ (1 &gt;&lt; 2) ([0.0, 0.0] :: [Double])<br>
  putStrLn $ show $ grad g $ (1 &gt;&lt; 2) ([0.0, 0.0] :: [Double])<br>
<br>
Then I am in trouble:<br>
<br>
/Users/dom/Dropbox/Private/Whales/MyAD.hs:24:21:<br>
    No instance for (Traversable Matrix) arising from a use of `grad&#39;<br>
    Possible fix: add an instance declaration for (Traversable Matrix)<br>
    In the expression: grad g<br>
    In the second argument of `($)&#39;, namely<br>
      `grad g $ (1 &gt;&lt; 2) ([0.0, 0.0] :: [Double])&#39;<br>
    In the second argument of `($)&#39;, namely<br>
      `show $ grad g $ (1 &gt;&lt; 2) ([0.0, 0.0] :: [Double])&#39;<br>
<br>
/Users/dom/Dropbox/Private/Whales/MyAD.hs:24:26:<br>
    Could not deduce (Element<br>
                        (ad-3.4:<a href="http://Numeric.AD.Internal.Types.AD" target="_blank">Numeric.AD.Internal.Types.AD</a> s Double))<br>
      arising from a use of `g&#39;<br>
    from the context (Numeric.AD.Internal.Classes.Mode s)<br>
      bound by a type expected by the context:<br>
                 Numeric.AD.Internal.Classes.Mode s =&gt;<br>
                 Matrix (ad-3.4:<a href="http://Numeric.AD.Internal.Types.AD" target="_blank">Numeric.AD.Internal.Types.AD</a> s Double)<br>
                 -&gt; ad-3.4:<a href="http://Numeric.AD.Internal.Types.AD" target="_blank">Numeric.AD.Internal.Types.AD</a> s Double<br>
      at /Users/dom/Dropbox/Private/Whales/MyAD.hs:24:21-26<br>
    Possible fix:<br>
      add an instance declaration for<br>
      (Element (ad-3.4:<a href="http://Numeric.AD.Internal.Types.AD" target="_blank">Numeric.AD.Internal.Types.AD</a> s Double))<br>
    In the first argument of `grad&#39;, namely `g&#39;<br>
    In the expression: grad g<br>
    In the second argument of `($)&#39;, namely<br>
      `grad g $ (1 &gt;&lt; 2) ([0.0, 0.0] :: [Double])&#39;<br>
<br>
What are my options here? Clearly I can convert my matrix into a list<br>
(which is traversable), find the grad and convert it back into a<br>
matrix but given I am doing numerical calculations and speed is an<br>
important factor, this seems undesirable.<br>
<br>
I think I would have the same problem with:<br>
<br>
<a href="http://hackage.haskell.org/package/repa" target="_blank">http://hackage.haskell.org/package/repa</a><br>
<a href="http://hackage.haskell.org/package/yarr-1.3.1" target="_blank">http://hackage.haskell.org/package/yarr-1.3.1</a><br>
<br>
although I haven&#39;¯t checked.<br>
<br>
Thanks, Dominic.<br>
<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br></div>