(defn symmetric-matrix
"
Returns a symmetric matrix from the given data, which represents the lower triangular elements
ordered by row. This is not the inverse of half-vectorize which returns a vector of the upper-triangular
values, unless the :lower option is set to false.
Options:
:lower (default true) -- lower-triangular. Set :lower to false to reverse the half-vectorize function.
Examples:
(use 'incanter.core)
(symmetric-matrix [1
2 3
4 5 6
7 8 9 10])
(half-vectorize
(symmetric-matrix [1
2 3
4 5 6
7 8 9 10] :lower false))
"
([data & options]
(let [opts (when options (apply assoc {} options))
lower? (if (false? (:lower opts)) false true)
n (count data)
p (int (second (solve-quadratic 1/2 1/2 (- 0 n))))
mat (incanter.Matrix. p p 0)
indices (if lower?
(for [i (range p) j (range p) :when (<= j i)] [i j])
(for [i (range p) j (range p) :when (<= i j)] [j i]))]
(doseq [idx (range n)]
(let [[i j] (nth indices idx)]
(.set mat i j (nth data idx))
(.set mat j i (nth data idx))))
mat)))
Used in 0 other vars
Comments top
No comments for symmetric-matrix. Log in to add a comment.