I guess different people see things differently.  I actually didn't
understand the intent of the imperative version, even though it takes
less lines I guess.  There's quite a few things called convolution.
My naive implementation was functional and pretty fast.  Turns out it
can be even faster with just a little work (still functional):

(defn point-convolve [n ^doubles fs ^doubles gs]
  (let [window-size (alength fs)
        padding (repeat window-size 0.0)
        padded-gs (concat padding gs padding)
        window-of-gs (double-array (subvec (vec padded-gs)
                                           (inc n)
                                           (inc (+ n window-size))))
        reverse-window-fn (fn [i] (aget window-of-gs
                                        (mod  (- -1 i) window-size)))
        reverse-window (map reverse-window-fn (range window-size))]
    (reduce + (map * fs reverse-window))))

(defn convolve [^doubles fs ^doubles gs]
  (map #(point-convolve % fs gs)
       (range (+ (alength fs) (alength gs)))))

(let [dxs (double-array (for [i (range 3000000)] (rand 100)))
      dys (double-array (for [i (range 3000000)] (rand 100)))]
  (time (dotimes [_ 10] (convolve dxs dys))))

"Elapsed time: 0.506432 msecs"

Have a good weekend,
Carson

On Jul 17, 6:13 pm, Frederick Polgardy <f...@polgardy.com> wrote:
> I think it really doesn't get any clearer than this in terms of intent. While 
> I was adept at calculus-level math 20 years ago, I've forgotten the little I 
> knew of matrices. This is the first algorithm that has communicated by visual 
> inspection (to me) exactly what a convolution is.
>
> -Fred
>
> --
> Science answers questions; philosophy questions answers.
>
> On Jul 17, 2010, at 3:43 PM, Isaac Hodes wrote:
>
> > double convolve(double *xs, double *is, double *ys){
> >  int i,j;
> >  for(i=0; i<len(xs); i++){
> >    for(j=0; j<len(is); j++){
> >      ys[i+j] = ys[i+j] + (xs[i]*is[j]);
> >    }
> >  }
> >  return ys;
> > }

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to