Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added type hinting to speed up matrix op performance.

  • Loading branch information...
commit e87760140d9dd1a1a8f930c3aed3aa0bbab4c3d9 1 parent 3c441e2
@silvamo silvamo authored
View
6 modules/incanter-core/src/incanter/core.clj
@@ -432,7 +432,7 @@
"
([& args] (if (= (count args) 1)
- (combine-with 0 (first args) clojure.core/- minus)
+ (combine-with (Integer. 0) (first args) clojure.core/- minus)
(reduce (fn [A B] (combine-with A B clojure.core/- minus)) args))))
@@ -478,7 +478,7 @@
"
([& args] (if (= (count args) 1)
- (combine-with 1 (first args) clojure.core// div)
+ (combine-with (Integer. 1) (first args) clojure.core// div)
(reduce (fn [A B] (combine-with A B clojure.core// div)) args))))
@@ -722,7 +722,7 @@
"
([^Matrix A & B]
(if B
- (Matrix. (.solve (DenseDoubleAlgebra.) A (first B)))
+ (Matrix. (.solve (DenseDoubleAlgebra.) A ^Matrix (first B)))
(Matrix. (.inverse (DenseDoubleAlgebra.) A)))))
View
33 modules/incanter-core/src/incanter/internal.clj
@@ -28,18 +28,24 @@
(defn is-matrix
" Test if obj is 'derived' from ::matrix (e.g. class incanter.Matrix)."
- ([obj] (isa? (class obj) ::matrix)))
+ ([obj] (instance? Matrix obj)))
+;; ([obj] (isa? (class obj) ::matrix)))
+(def double_arr_type (Class/forName "[D"))
(defn make-matrix
([data]
- (cond
+ (cond
+ (instance? double_arr_type data)
+ (Matrix. ^"[D" data)
(coll? (first data))
(Matrix. ^"[[D" (into-array (map double-array data)))
(number? (first data))
(Matrix. (double-array data))))
([data ncol]
(cond
+ (instance? double_arr_type data)
+ (Matrix. ^"[D" data (int ncol))
(or (coll? data) (.isArray (class data)))
(Matrix. (double-array data) (int ncol))
(number? data)
@@ -55,15 +61,20 @@
(defmacro ^Matrix transform-with [A op fun]
- `(cond
- (is-matrix ~A)
- (.assign (.copy ~A) (. DoubleFunctions ~fun))
- (and (coll? ~A) (coll? (first ~A)))
- (.assign ^Matrix (make-matrix ~A) (. DoubleFunctions ~fun))
- (coll? ~A)
- (map ~op ~A)
- (number? ~A)
- (~op ~A)))
+ (let [mA (with-meta (gensym "A") {:tag "Matrix"})
+ df (with-meta (gensym "fun") {:tag "DoubleFunction"})]
+ `(let [~df (. DoubleFunctions ~fun)]
+ (cond
+ (is-matrix ~A)
+ (let [~mA ~A]
+ (.assign (hint "Matrix" (.copy ~mA)) ~df))
+ (and (coll? ~A) (coll? (first ~A)))
+ (let [~mA (make-matrix ~A)]
+ (.assign ~mA ~df))
+ (coll? ~A)
+ (map ~op ~A)
+ (number? ~A)
+ (~op ~A)))))
(defmacro combine-with [A B op fun]
Please sign in to comment.
Something went wrong with that request. Please try again.