This file contains hidden or 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
// 8.5 us for 10k doubles: 2.3 GFlops | |
public double dotProduct(double [] ws, double[] xs) { | |
double result = 0.0; | |
for (int i=0; i < ws.length; ++i) { | |
result += ws[i] * xs[i]; | |
} | |
return result; | |
} |
This file contains hidden or 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
;; 8.5 us for 10k doubles: 2.3 GFlops | |
(require '[hiphip.double :as dbl]) | |
(defn dot-product [ws xs] | |
(dbl/asum [x xs w ws] (* x w))) |
This file contains hidden or 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
(dbl/aset xs i (* 2.0 (dbl/aget xs (inc i))))) |
This file contains hidden or 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
(defn add-in-place! | |
"Modify xs, incrementing each element by the corresponding element of ys" | |
[xs ys] | |
(dbl/afill! [x xs y ys] (+ x y))) | |
(defn pointwise-product | |
"Produce a new double array with the product of corresponding elements of xs and ys" | |
[xs ys] | |
(dbl/amap [x xs y ys] (* x y))) |
This file contains hidden or 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
(defn pointwise-product | |
"Produce a new double array with the product of corresponding elements of xs and ys" | |
[xs ys] | |
(dbl/amap [x xs y ys] (* x y))) |
This file contains hidden or 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
(defn standard-deviation [xs] | |
(let [mean (dbl/amean xs) | |
mean-sq (/ (dbl/asum [x xs] (* x x)) | |
(dbl/alength xs))] | |
(Math/sqrt (- mean-sq (* mean mean))))) | |
(= 1 (dbl/amax-index (double-array [1.0 3.0 2.0]))) | |
(defn max-normalize [xs] | |
(let [m (dbl/amax xs)] |
This file contains hidden or 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
(defn top-k | |
"Return the top k elements of xs according to score-fn" | |
[k score-fn xs] | |
(let [n (count xs) | |
scores (dbl/amake [i n] (score-fn (nth xs i))) | |
^ints idxs (dbl/amax-indices scores k)] | |
(seq (hiphip.array/amake Object [i k] (nth xs (aget idxs (- n i 1))))))) |
This file contains hidden or 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
(defn fill-string-pointwise-product | |
[^{:tag "[Ljava.lang.String;"} os ^doubles xs ^longs ys] | |
(array/afill! String [_ os x xs y ys] (str (* x y)))) |
This file contains hidden or 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
;; Initialize xs to (range (alength xs)). | |
(afill! [[i x] xs] i) |
This file contains hidden or 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
;; Clear Leiningen's default jvm-opts. | |
:jvm-opts ^:replace [] |