mapping over slices

33 views
Skip to first unread message

Ludwik Grodzki

unread,
May 11, 2014, 4:42:38 PM5/11/14
to numerica...@googlegroups.com
Hello.

What is the correct / fastest way to do a mapping operations over the slices of a matrix. The mapping I do happens to returns the same shape and so can be recombined into a matrix (but in general that doesn't have to be so). eg: to compute a matrix with each slice normalised to length 1 I do:

(let [imp :vectorz
      mtrx (m/matrix imp [[1 2 3] [4 5 6]])]
 (m/matrix imp
   (map (fn [row] (m/div row (m/length row))) (m/slices mtrx))))

Does the use of clojure's map and then the coercion back to a matrix make sense here? Is there something specific to core.matrix that would make such an operation faster or that would be more idiomatic?

Thanks.

Ludwik.

Mike Anderson

unread,
May 11, 2014, 6:18:36 PM5/11/14
to numerica...@googlegroups.com
Efficiency of different operations in core.matrix is dependent on the implementation.

You approach should normally be pretty efficient however - things are generally designed so that operating over slices is fast. It will certainly be very fast in Vectorz, where slices are just lightweight offsets into the same underlying data array.

Note that there is a `normalise` operation - so you can simplify the inner expression to:

     (map m/normalise (m/slices mtrx))

Mapping over slices and reassembling these to make a new array actually seems to be a sufficiently common operation that I'm wondering whether we should have a built-in function to do this. 

Maybe a "map-slices" function? Or "slicemap"?

Ludwik Grodzki

unread,
May 12, 2014, 5:06:44 AM5/12/14
to numerica...@googlegroups.com
Mike

Thanks for the input.

I agree, something like map-slices would make sense. 

some thoughts:
Presumably the output would also be a vector that would be reconstituted as part of map-slices (vs a seq of mapped over slices), or would that be too many concerns in one fn? Is there much cost to that coercion that specific implementation could get around? Also, should something like map-slices take advantage of parallelism under the hood and would that ever be implementation specific (I'm thinking, should there be a protocol for it?).

Mike Anderson

unread,
May 12, 2014, 5:47:14 AM5/12/14
to numerica...@googlegroups.com
I think it is tricky for map-slices to have an optimised protocol implementation - since it can take an arbitrary function it is hard to optimise the computational approach much further.

But as a convenience function it can certainly make sense. 

I think the output should be the joined results of all the slices (i.e. each slice of the result = f(each slice of the input)). So you can do :

(map-slices normalise some-big-matrix)
=> a big matrix back with all rows normalised

(map-slices length some-big-matrix)
=> a vector with the euclidean lengths of each row

Make sense?

Ludwik Grodzki

unread,
May 12, 2014, 6:01:02 AM5/12/14
to numerica...@googlegroups.com
Yes it does.

Thanks.
Reply all
Reply to author
Forward
0 new messages