Skip to content

Commit

Permalink
play with core.matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlawrenceaspden committed Apr 10, 2014
1 parent 42e2811 commit 45dc65e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
60 changes: 60 additions & 0 deletions matrix.clj
@@ -0,0 +1,60 @@
(use 'clojure.core.matrix)

(def a (matrix [[2 0] [0 2]]))
(def b (matrix [[2 0] [0 2]]))

(mul a b) ;-> [[4 0] [0 4]]

(mmul a b) ;-> [[4 0] [0 4]]

(pm (matrix [[0 1][-1 0]]))

(mmul (matrix [1 2 3]) (matrix [1 2 3]))

(transpose a) ;-> [[2 0] [0 2]]

(inverse a) ; #<NDArrayDouble [[0.5 0.0] [0.0 0.5]]

(shape a) ;-> [2 2]

(dimensionality a)

(def M [[1 2 3]
[4 5 6]
[7 8 9]])

(det M) ;-> 6.661338147750939E-16

(pm (inverse M))

(slice M 1)

(apply + (slices M)) ; error

(use 'clojure.core.matrix.operators)

(apply + (slices M)) ;-> [12 15 18]

(+ 1 M) ;-> [[2 3 4] [5 6 7] [8 9 10]]
(+ [1 2 3] M) ;-> [[2 4 6] [5 7 9] [8 10 12]]
(+ (transpose [1 2 3]) M) ;-> [[2 4 6] [5 7 9] [8 10 12]]

(map inc M) ; error
(emap inc M) ;-> [[2 3 4] [5 6 7] [8 9 10]]
(ereduce * M) ;-> 362880

(def P (permutation-matrix [3 1 0 2]))

(mmul P [1 2 3 4]) ; -> #<NDArray [4.0 2.0 1.0 3.0]

(mmul (mmul P P) [1 2 3 4])

(mmul P P [1 2 3 4])

(mmul P P P [1 2 3 4]) ; #<NDArray [1.0 2.0 3.0 4.0]

(eigen-decomposition P)

(cholesky-decomposition P)

(current-implementation)
3 changes: 2 additions & 1 deletion project.clj
@@ -1,6 +1,7 @@
(defproject hobby-code "0.0.1"
:dependencies [
[org.clojure/core.logic "0.8.1"]
; [org.clojure/core.logic "0.8.1"]
[net.mikera/core.matrix "0.22.0"]
; [org.clojars.achim/multiset "0.1.0-SNAPSHOT"]
]

Expand Down

0 comments on commit 45dc65e

Please sign in to comment.