Created
December 3, 2011 02:48
-
-
Save newfoundresearch/1425837 to your computer and use it in GitHub Desktop.
A method for constructing incanter matrices in an easy fashion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; enables you to define an incanter matrix (2-dimensional only) by a rule based on i j index | |
(defmacro matrix-rule [m index-rule application-fn] | |
`(incanter/matrix | |
(into [] (for [i# (range 0 (incanter/nrow ~m))] | |
(into [] (for [j# (range 0 (incanter/ncol ~m))] | |
(if (~index-rule i# j#) | |
(~application-fn i# j#) | |
(incanter/sel ~m i# j#)))))))) | |
(defn diagonal? [i j] | |
(= i j)) | |
(defn upper? [i j] | |
(> j i)) | |
(defn lower? [i j] | |
(< j i)) | |
;; best if used in a threaded way | |
(-> (incanter.core/matrix 0 3 3) | |
(matrix-rule diagonal? (fn [i j] 5)) ;; make the diagonal 5 | |
(matrix-rule upper? (fn [i j] (+ i j))) ;; make the upper diagonal equal to i + j | |
(matrix-rule lower? (fn [i j] (- i j)))) ;; make the lower diagonal equal to i - j |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment