views:

142

answers:

3

As part of a python simulation I have I take a 2d array and take the gradient of this array. This is done in scipy/numpy by convolving the 2d array with a filter with the appropriate weights.

So my question is if I want to do this in clojure reasonably fast does it make sense to do this in pure clojure, or is it better to use a Java image processing library and call into it from clojure?

A: 

You can use Java image processing libraries from Clojure. Here is something to get started.

(defn getPixels [^BufferedImage image] (-> image .getRaster .getDataBuffer .getData))

I don't know if this will compile(some help I am) I just translated it from some Scala code of mine in my head.

http://clojure.org/java_interop

Thanks, but I'm really more interested in understanding the performance differences in using Java libraries vs writing pure Clojure.
Ranjit
+2  A: 

A well optimized loop in Clojure will have performance close to Java code.

I will advise using either the primitive vectors or directly arrays from clojure to get very good performance.

I have read a blogpost on the subject: http://www.bestinclass.dk/index.clj/2010/03/functional-fluid-dynamics-in-clojure.html

The good process is to start with code that is clear and works and tweek in for perfomance afterwards.

Often, performance in Clojure revolves around getting rid of reflection and using primitives. This is explained here: http://clojure.org/java_interop

Nicolas Oury
A: 

If you really want optimal performance, you're probably going to need to do your calculations in native code (which, as I understand it, is what NumPy does). We used JAI on a previous project, and it worked well, but we weren't using it for anything advanced. It does seem to have support for kernels and convolutions, so it might work for your purposes. However, I don't believe that it is as flexible as NumPy.

Daniel Yankowsky