Skip to content

Instantly share code, notes, and snippets.

@fogus
Created February 22, 2010 19:35
Show Gist options
  • Save fogus/311397 to your computer and use it in GitHub Desktop.
Save fogus/311397 to your computer and use it in GitHub Desktop.
(defprotocol MatrexOps
(like [_ r c])
(viewSelection [_ r c]))
;; Here's where the problem lies. There is no level of abstraction higher than an abstract class (i.e. no interfaces)
;; but I still want to use the Parallel Colt 2D matrix impl. The CernOps protocol would define a delegation layer
;; intended to be used for PColt libs that expect an AbstractMatrix.
;;
(defprotocol CernOps
(cardinality [_])
;; ... a bunch more
(setQuick [_ r c v]))
(deftype Matrex [matrex] ;; name it differently so as not to clash
MatrexOps
(like [r c] nil)
(viewSelection [r c] nil)
CernOps
(cardinality [] 42)
;; ... a bunch more
(setQuick [r c v] (.setQuick matrex r c v))
clojure.lang.ISeq
;; ...
clojure.lang.Counted
(count [] (.rows matrex))
)
;; Creates a closed instance of a concrete impl of AbstractMatrix
(defn matrex
[r c]
(let [m (cern.colt.matrix.tdouble.impl.DenseColDoubleMatrix2D. r c)] ;; Million dollar question: should this be locked in a cell?
(Matrex m))) ;; ... my instincts say yes.
(def m (matrex 3 3))
(setQuick m 0 0 Math/PI)
m
; #:Matrex{:matrex #<DenseColDoubleMatrix2D 3 x 3 matrix
; 3.141593 0 0
; 0 0 0
; 0 0 0>}
(cardinality m) ;=> 42
(count m) ;=> 3
(.setQuick m 0 1 9)
m
; #:Matrex{:matrex #<DenseColDoubleMatrix2D 3 x 3 matrix
; 3.141593 9 0
; 0 0 0
; 0 0 0>}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment