Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For multi-dimensional arrays, the current best option in Haskell, in my view, is <strong><em><a href="http://hackage.haskell.org/package/repa" rel="noreferrer">repa</a></em></strong>.</p> <blockquote> <p>Repa provides high performance, regular, multi-dimensional, shape polymorphic parallel arrays. All numeric data is stored unboxed. Functions written with the Repa combinators are automatically parallel provided you supply +RTS -Nwhatever on the command line when running the program.</p> </blockquote> <p>Recently, it has been used for some image processing problems:</p> <ul> <li><a href="http://disciple-devel.blogspot.com/2011/03/real-time-edge-detection-in-haskell.html" rel="noreferrer">Real time edge detection</a></li> <li><a href="http://www.cse.unsw.edu.au/~benl/papers/stencil/stencil-icfp2011-sub.pdf" rel="noreferrer">Efficient Parallel Stencil Convolution in Haskell</a></li> </ul> <p>I've started writing <strong><a href="http://haskell.org/haskellwiki/Numeric_Haskell:_A_Repa_Tutorial" rel="noreferrer">a tutorial on the use of repa</a></strong>, which is a good place to start if you already know Haskell arrays, or the vector library. The key stepping stone is the use of shape types instead of simple index types, to address multidimensional indices (and even stencils).</p> <p>The <a href="http://hackage.haskell.org/package/repa-io" rel="noreferrer">repa-io</a> package includes support for reading and writing .bmp image files, though support for more formats is needed.</p> <p>Addressing your specific questions, here is a graphic, with discussion:</p> <hr> <p><img src="https://i.stack.imgur.com/dDAXD.png" alt="All three of UArray, Vector, and Repa support unboxing. Vector and Repa have a rich, flexible API, but UArray does not. UArray and Repa have multi-dimensional indexing, but Vector does not. They all have support for bit-packing, although Vector and Repa have some caveats in that regard. Vector and Repa interoperate with C data and code, but UArray does not. Only Repa supports stencils."></p> <hr> <p><em>On what basis should I choose between Vector.Unboxed and UArray?</em></p> <p>They have approximately the same underlying representation, however, the primary difference is the breadth of the API for working with vectors: they have almost all the operations you'd normally associate with lists (with a fusion-driven optimization framework), while <code>UArray</code> have almost no API.</p> <p><em>For color images I will wish to store triples of 16-bit integers or triples of single-precision floating-point numbers.</em></p> <p><code>UArray</code> has better support for multi-dimensional data, as it can use arbitrary data types for indexing. While this is possible in <code>Vector</code> (by writing an instance of <code>UA</code> for your element type), it isn't the primary goal of <code>Vector</code> -- instead, this is where <code>Repa</code> steps in, making it very easy to use custom data types stored in an efficient manner, thanks to the <em>shape</em> indexing.</p> <p>In <code>Repa</code>, your triple of shorts would have the type:</p> <pre><code>Array DIM3 Word16 </code></pre> <p>That is, a 3D array of Word16s.</p> <p><em>For bitonal images I will need to store only 1 bit per pixel.</em></p> <p>UArrays pack Bools as bits, Vector uses the instance for Bool which does do bit packing, instead using a representation based on <code>Word8</code>. Howver, it is easy to write a bit-packing implementation for vectors -- <a href="http://hpaste.org/46709/bit_packing_bools" rel="noreferrer">here is one</a>, from the (obsolete) uvector library. Under the hood, <code>Repa</code> uses <code>Vectors</code>, so I think it inherits that libraries representation choices.</p> <p><em>Is there a predefined datatype that can help me here by packing multiple pixels into a word</em> </p> <p>You can use the existing instances for any of the libraries, for different word types, but you may need to write a few helpers using Data.Bits to roll and unroll packed data. </p> <p><em>Finally, my arrays are two-dimensional</em></p> <p>UArray and Repa support efficient multi-dimensional arrays. Repa also has a rich interface for doing so. Vector on its own does not.</p> <hr> <p><em>Notable mentions:</em></p> <ul> <li><a href="http://hackage.haskell.org/package/hmatrix" rel="noreferrer">hmatrix</a>, a custom array type with extensive bindings to linear algebra packages. Should be bound to use the <code>vector</code> or <code>repa</code> types.</li> <li><a href="http://hackage.haskell.org/package/ix-shapable" rel="noreferrer">ix-shapeable</a>, getting more flexible indexing from regular arrays</li> <li><a href="http://hackage.haskell.org/package/chalkboard" rel="noreferrer">chalkboard</a>, Andy Gill's library for manipulating 2D images</li> <li><a href="http://hackage.haskell.org/package/Codec-Image-DevIL" rel="noreferrer">codec-image-devil</a>, read and write various image formats to UArray</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload