Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first pass at sparse matrix support. only to and from default sparse …

…matrices. still need to do colt 2d matrices, benchmark, and make sure matrix operations +, -, *, and decomps are working with sparse matrices.
  • Loading branch information...
commit aa8efc784d7c7cca98983c95b0638e55c6800836 1 parent 94dfe2b
Bradford authored
View
2  README.md
@@ -1,8 +1,6 @@
# Infer
## Inference and Machine Learning in Clojure
-*WARNING*: this is an early project and the API will have breaking changes.
-
Infer is a library for machine learning and statistical inference, designed to be used in real production systems.
Infer is written in [Clojure](http://clojure.org/) and leverages many JVM packages.
View
3  project.clj
@@ -4,7 +4,8 @@
[org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]
[clojure-csv/clojure-csv "1.1.0"]
[org.apache.commons/commons-math "2.0"]
- [ujmp-complete "0.2.4"]]
+ [ujmp-complete "0.2.4"]
+ [colt/colt "1.2.0"]]
:dev-dependencies [[org.clojars.mmcgrana/lein-javac "0.1.0"]
[swank-clojure "1.2.0"]
[lein-clojars "0.5.0"]])
View
50 src/infer/matrix.clj
@@ -5,7 +5,11 @@
MatrixFactory
Ops])
(:import [org.ujmp.core.matrix Matrix2D])
- (:import [org.ujmp.core.doublematrix DenseDoubleMatrix2D DoubleMatrix2D])
+ (:import [org.ujmp.colt
+ ColtSparseDoubleMatrix2D])
+ (:import [org.ujmp.core.doublematrix
+ DoubleMatrix DenseDoubleMatrix2D
+ DoubleMatrix2D SparseDoubleMatrix2D])
(:import org.ujmp.core.calculation.Calculation$Ret)
(:import org.ujmp.core.doublematrix.calculation.general.decomposition.Chol))
@@ -36,6 +40,50 @@
:else
(MatrixFactory/importFromArray #^"[[D" (doubles-2d xs))))
+(defn sparse-matrix [xs]
+ (let [n-rows (count xs)
+ cols (reduce (fn [acc row]
+ (union acc (into #{} (keys row))))
+ #{}
+ xs)
+ m (MatrixFactory/sparse (long-array [n-rows (+ (apply max cols) 1)]))
+ row-indices (range 0 (count xs))]
+ (dorun
+ (map (fn [row r]
+ (dorun (map (fn [[c v]]
+ (.setDouble m v (long-array [r c])))
+ row)))
+ xs
+ row-indices))
+ m))
+
+(defn from-sparse-matrix [m]
+ (map (fn [coord]
+ (conj (into [] (map int coord)) (.getDouble m coord)))
+ (.availableCoordinates m)))
+
+(defn from-sparse-2d-matrix [m]
+ (let [map-row (fn [[r row]] (into {} (map (fn [[_ b c]] [b c]) row)))]
+ (map map-row
+ (group-by first (from-sparse-matrix m)))))
+
+(defn sparse-colt-matrix [xs]
+ (let [n-rows (count xs)
+ cols (reduce (fn [acc row]
+ (union acc (into #{} (keys row))))
+ #{}
+ xs)
+ m (ColtSparseDoubleMatrix2D. (long-array [n-rows (+ (apply max cols) 1)]))
+ row-indices (range 0 (count xs))]
+ (dorun
+ (map (fn [row r]
+ (dorun (map (fn [[c v]]
+ (.setDouble m v (long-array [r c])))
+ row)))
+ xs
+ row-indices))
+ m))
+
(defn column-matrix [ys]
(matrix (map vector ys)))
View
6 test/infer/matrix_test.clj
@@ -36,6 +36,12 @@
b (from-matrix A)]
(is (= a b))))
+(deftest to-and-from-sparse-matrix
+ (let [a [{0 1, 5 2, 9 3} {4 4,9 5, 16 6}]
+ A (sparse-matrix a)
+ b (from-sparse-2d-matrix A)]
+ (is (= a b))))
+
(deftest to-and-from-column-matrix
(let [a [1 2 3]
A (column-matrix
Please sign in to comment.
Something went wrong with that request. Please try again.