1.2.3-SNAPSHOT Arrow_down_16x16
  • (diag m)
If given a matrix, diag returns a sequence of its diagonal elements.
If given a sequence, it returns a matrix with the sequence's elements
on its diagonal. Equivalent to R's diag function.

Examples:
(diag [1 2 3 4])

(def A (matrix [[1 2 3]
[4 5 6]
[7 8 9]]))
(diag A)


0 Examples top

Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source incanter/core.clj:151 top

(defn diag
"   If given a matrix, diag returns a sequence of its diagonal elements.
    If given a sequence, it returns a matrix with the sequence's elements
    on its diagonal. Equivalent to R's diag function.

    Examples:
      (diag [1 2 3 4])

      (def A (matrix [[1 2 3]
                      [4 5 6]
                      [7 8 9]]))
      (diag A)


"
   ([m]
    (cond
     (matrix? m)
      (seq (.toArray (.diagonal DoubleFactory2D/dense m)))
     (coll? m)
      (Matrix. (.diagonal DoubleFactory2D/dense (.make DoubleFactory1D/dense (double-array m))))
     (number? m)
      m)))
Vars in incanter.core/diag: coll? cond defn double-array number? seq
Used in 0 other vars

Comments top

No comments for diag. Log in to add a comment.