Matrix arithmetic is one of those cases where I feel indexing is
clearer than filter/map functions.
However, if you're representing matrices as vectors as vectors, this
will likely come with a little bit of extra overhead.
That's not a problem usually because if you plan on doing any sort of
heavy numerical calculations on matrices, the performance you get on
vectors-of-vectors is very poor. So if speed is a concern, you should
represent your matrices as Java arrays in which case the overhead of
indexing goes away.
Here's some example code:
(def matrix [[1 2 3]
[4 5 6]
[7 8 9]])
(def size 3)
(def positions
(for [c (range size)
r (range (inc c) size)]
[r c]))
(reduce (fn [matrix [r c]]
(assoc-in matrix [r c]
(get-in matrix [c r])))
matrix
positions)
Hope that helps
-Patrick
PS: And if you do use Java arrays in the future, you can make the code
much more elegant by replacing the "for" with a "doseq" and eliminate
the need for a "reduce".
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en