Created
January 21, 2013 20:00
-
-
Save siscia/4588806 to your computer and use it in GitHub Desktop.
Basic implementation of parallel colt's DenseDoubleMatrix2D
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
(ns parallel-colt-matrix.impl | |
(:use [core.matrix.protocols]) | |
(:require [core.matrix.implementations :as imp]) | |
(:import [cern.colt.matrix.tdouble.impl DenseDoubleMatrix2D]) | |
(:import [cern.colt.matrix.tdouble.algo DenseDoubleAlgebra])) | |
(extend-type DenseDoubleMatrix2D | |
PImplementation | |
(implementation-key [m] | |
"Returns a keyword representing this implementation. | |
Each implementation should have one unique key." | |
:parallel-colt) | |
(construct-matrix [m data] | |
"Returns a new matrix containing the given data. Data should be in the form of either nested sequences or a valid existing matrix" | |
(DenseDoubleMatrix2D. (into-array (map #(into-array Double/TYPE (map double %)) data)))) | |
(new-vector [m length] | |
"Returns a new vector (1D column matrix) of the given length." | |
(throw (Exception. "Only 2D matrix, use new-matrix"))) | |
(new-matrix [m rows columns] | |
"Returns a new matrix (regular 2D matrix) with the given number of rows and columns." | |
(DenseDoubleMatrix2D. rows columns)) | |
(new-matrix-nd [m shape] | |
"Returns a new general matrix of the given shape. | |
Shape must be a sequence of dimension sizes." | |
(throw (Exception. "Only 2D matrix, use new-matrix"))) | |
PDimensionInfo | |
(dimensionality [m] | |
"Returns the number of dimensions of a matrix" | |
2) | |
(get-shape [m] | |
"Returns the shape of the matrix, as an array or sequence of dimension sizes" | |
[(.columns m) (.rows m)]) | |
(is-scalar? [m] | |
"Tests whether an object is a scalar value" | |
false) | |
(is-vector? [m] | |
"Tests whether an object is a vector (1D matrix)" | |
false) | |
(dimension-count [m dimension-number] | |
"Returns the size of a specific dimension | |
I assumed 0 for colunms 1 for rows" | |
(assert (< dimension-number 3)) | |
(assert (>= dimension-number 0)) | |
(case dimension-number | |
0 (.columns m) | |
1 (.rows m))) | |
PIndexedAccess | |
(get-1d [m row] | |
(-> (.viewRow m row) (.toArray) (vec))) | |
(get-2d [m row column] | |
(.get m row column)) | |
(get-nd [m indexes] | |
(throw (Exception. "It is only a 2D Array"))) | |
PCoercion | |
(coerce-param [m param] | |
"Attempts to coerce param into a matrix format supported by the implementation of matrix m. | |
May return nil if unable to do so, in which case a default implementation can be used." | |
nil) | |
PIndexedSetting | |
(set-1d [m row v] | |
(throw (Exception. "It is a 2D matrix, specify another dimension, use set-2d(!)"))) | |
(set-2d [m row column v] | |
(.set m row column v)) ;;We could use .setQuick but then we need | |
;;to add a pre-condition to check if the index is in the bound, and | |
;;I guess it will be slower than simply use .set | |
(set-nd [m indexes v] | |
(throw (Exception. "It is a 2D matrix, no other dimension, use set-2d(!)"))) | |
PMatrixEquality | |
(matrix-equals [a b] | |
(.equals a b)) | |
PMatrixAdd | |
(matrix-add [m a] | |
(assert (= (get-shape m) (get-shape a))) | |
(let [new-m (new-matrix m (dimension-count m 0) (dimension-count m 1))] | |
(doall | |
(for [row (range (dimension-count m 1)) | |
col (range (dimension-count m 0))] | |
(.setQuick new-m row col (+ (get-2d m row col) (get-2d a row col))))) | |
new-m)) | |
(matrix-sub [m a] | |
(assert (= (get-shape m) (get-shape a))) | |
(let [new-m (new-matrix m (dimension-count m 0) (dimension-count m 1))] | |
(doall | |
(for [row (range (dimension-count m 1)) | |
col (range (dimension-count m 0))] | |
(.setQuick new-m row col (- (get-2d m row col) (get-2d a row col))))) | |
new-m)) | |
PMatrixMultiply | |
(matrix-multiply [m a] | |
(-> (DenseDoubleAlgebra.) | |
(.mult m a))) | |
(element-multiply [m a])) | |
(imp/register-implementation (DenseDoubleMatrix2D. 2 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment