Back
double-array (clj)
(source)function
(double-array size-or-seq)
(double-array size init-val-or-seq)
Creates an array of doubles
Examples
mikera/core.matrix
(ns clojure.core.matrix.test-arrays
(:refer-clojure :exclude [vector?])
(:require [clojure.core.matrix.compliance-tester :as compliance]
[clojure.core.matrix :as m]
;[clojure.core.matrix.utils :refer :all]
[clojure.test :refer [deftest testing is]]
[clojure.core.matrix.macros :refer [array?]]))
(deftest zero-dimension-access
(testing "Arrays are assumed to be scalars if accessed with zero dimensions"
(is (m/equals [1] (m/mget (byte-array [1]))))
(is (m/equals [1] (m/mget (double-array [1]))))))
(deftest compliance-tests
(compliance/instance-test (int-array [1 2 3]))
(compliance/instance-test (float-array [1 2 3]))
(compliance/instance-test (long-array []))
(compliance/instance-test (char-array [\a \b \c]))
(compliance/instance-test (object-array [(double-array [1 2 3])]))
(compliance/instance-test (object-array [(short-array (map short [1 2 3]))]))
(compliance/instance-test (into-array (Class/forName "[D")
[(double-array [1 2])
(double-array [3 4])])))
mikera/core.matrix
(ns clojure.core.matrix.test-double-array
(:refer-clojure :exclude [==])
(:require [clojure.core.matrix.protocols :as mp]
[clojure.core.matrix.impl.pprint :as pprint]
[clojure.core.matrix.dataset :as ds]
[clojure.core.matrix.compliance-tester]
[clojure.core.matrix :refer :all]
[clojure.core.matrix.operators :refer [==]]
[clojure.core.matrix.macros :refer [error]]
[clojure.core.matrix.macros-clj :refer [error?]]
[clojure.test :refer :all]))
(deftest misc-regressions
(testing "shape sequnces"
(is (= [0] (shape (double-array []))))
(is (= [1 0] (shape [(double-array [])])))
(is (= [0] (shape (transpose (double-array [])))))))
(deftest test-create
(testing "making a double array"
(let [da (matrix :double-array [1 2])]
(is (equals [1.0 2.0] (seq da)))
(is (e= [1.0 2.0] (eseq da)))
(is (identical? (class (double-array [1])) (class da)))))
(testing "coercion from persistent vector"
(let [da (matrix :double-array [1 2])]
(is (e= [2.0 4.0] (seq (coerce da [2 4]))))
(is (identical? (class da) (class (coerce da [2 4])))))))
(deftest test-higher-dimensions
(let [m [[[1 2] [3 4]]]
dm (array :double-array m)]
(is (equals m dm)))
(is (= 1.0 (array :double-array 1))))
(deftest test-type
(is (= Double/TYPE (element-type (double-array [1 2])))))
(deftest row-setting
(let [a (double-array [1 2 3])]
(is (equals [1 10 3] (set-row a 1 10)))
(set-row! a 0 7)
(is (equals [7 2 3] (seq a)))))
(deftest column-setting
(let [a [(double-array [1 2]) (double-array [3 4])]]
(is (equals [[1.0 10.0] [3.0 10.0]] (set-column a 1 10)))))
(deftest test-slices
(testing "slices"
(let [m [(double-array [1 2]) (double-array [3 4])]]
(is (equals [1 2] (get-row m 0)))
(is (equals [2 4] (get-column m 1)))
(is (e= [1.0 2.0] (slices (get-row m 0))))
(is (e= [2.0 4.0] (slices (get-column m 1)))))))
(deftest test-scalar-slices
(testing "slices"
(let [da (double-array [1 2 3])
fs (first (slices da))]
(is (scalar? fs))
(is (== 0 (dimensionality fs)))
(is (not (array? fs)))))
(testing "slice views"
(let [da (double-array [1 2 3])
fs (first (slice-views da))]
(is (not (scalar? fs)))
(is (== 0 (dimensionality fs)))
(fill! fs 10)
(is (equals [10 2 3] da))
(is (array? fs))))
(testing "wrong dimension"
(let [da (double-array [1 2 3])]
(is (error? (slice da 1 1))))))
(deftest test-functional-ops
(testing "mapping"
(let [da (matrix :double-array [1 2])]
(is (= [2.0 3.0] (seq (emap inc da))))
(emap! inc da)
(is (= [2.0 3.0] (vec da)))))
(testing "nested double arrays"
(is (= [1.0 2.0 3.0 4.0] (eseq [(double-array [1 2]) (double-array [3 4])]))))
(testing "mapping indexed"
(let [da (matrix :double-array [4 5])
da2 (matrix :double-array [6 7])
da3 (matrix :double-array [8 9])]
(is (= [5.0 7.0] (seq (emap-indexed #(+ (reduce + %1) (inc %2)) da))))
(is (= [10.0 13.0] (seq (emap-indexed #(apply + (reduce + %1) %&) da da2))))
(is (= [18.0 22.0] (seq (emap-indexed #(apply + (reduce + %1) %&) da da2 da3)))))))
(deftest test-assign
(testing "assign from a persistent vector"
(let [da (double-array [1 2])]
(assign! da [2 3])
(is (= [2.0 3.0] (seq da)))))
(testing "assign from an array"
(let [da (double-array [1 2])]
(assign! da (double-array [2 4]))
(is (= [2.0 4.0] (seq da)))))
(testing "assign from a Number array"
(let [da (double-array [1 2])]
(mp/assign-array! da (into-array Number [2 5]))
(is (= [2.0 5.0] (seq da))))))
(deftest test-equals
(testing "equality with persistent vector"
(let [da (double-array [1 2])]
(is (= [1.0 2.0] (to-nested-vectors da)))
(is (equals [1.0 2.0] da))
(is (equals [1 2] da))
(is (equals da [1.0 2.0])))))
(deftest test-add-scaled
(let [da (double-array [1 2])]
(is (equals [11 22] (add-scaled da [1 2] 10)))
(is (equals [101 202] (add-scaled! da [1 2] 100)))
(is (equals [101 202] da))))
(deftest test-add-scaled-product
(let [da (double-array [1 2])]
(is (equals [2 6] (add-scaled-product da [1 2] [1 2] 1)))
(is (equals [101 202] (add-scaled-product! da [1 2] [10 10] 10)))
(is (equals [101 202] da))))
(deftest test-vector-scale
(testing "scale!"
(let [da (double-array [1.0 2.0])]
(is (equals [2.0 4.0] (scale! da 2))))))
(deftest test-vector-normalise
(testing "normalise!"
(let [da (double-array [4.0])]
(is (equals [1.0] (normalise! da))))))
(deftest test-mutable-add
(let [v (double-array [1 2 3])]
(add! v [10 10 10])
(sub! v [1 1 2])
(is (equals v [10 11 11]))))
(deftest test-mutable-map!
(let [v (double-array [1 2 3])]
(emap! clojure.core/+ v [10 10 10])
(is (equals v [11 12 13]))))
(deftest test-div
(is (equals [1 2] (div (double-array [2 4]) 2))))
(deftest test-div!
(let [da (double-array [2 4])]
(div! da 2)
(is (equals [1 2] da))))
(deftest test-mmul!
(let [da (double-array [2 4])]
(mmul! da 2 2)
(is (equals [8 16] da)))
(let [da (double-array [2 4])]
(mmul! da da da)
(is (equals [40 80] da))))
(deftest test-broadcast-coerce
(is (= [1.0 2.0] (mp/broadcast-coerce [0 0] (double-array [1 2])))))
(deftest test-to-double-arrays
(let [m [[1 2] [3 4]]
dm (clojure.core.matrix.impl.double-array/to-double-arrays m)]
(is (equals m dm)))
(is (= 1.0 (clojure.core.matrix.impl.double-array/to-double-arrays 1))))
(deftest test-mutable-multiply
(let [a (double-array [1 2])
b (double-array [2 3])]
(is (identical? a (mul! a b)))
(is (equals [2.0 6.0] (vec a)))
(is (equals [2.0 6.0] a))))
(deftest test-scale-add
(let [v (double-array [1 2 3])]
(scale-add! v 2.0 [10 20 30] 3 100)
(is (equals [132 164 196] (array [] v)))
(scale-add! v 0.0 [1 2 3] 2 [-1 -10 10])
(is (equals [1 -6 16] v))))
(deftest test-element-ops
(let [a (double-array [5 1 7 8 4])]
(is (== 1 (emin a)))
(is (== 8 (emax a)))
(is (== 25 (esum a)))))
(deftest test-doubles-outer-product
(let [a (double-array [1 3])
b (double-array [1 2])]
(is (equals [[1 2] [3 6]] (outer-product a b)))))
(deftest test-maths-ops
(testing "basic ops"
(let [da (double-array [1.2 2.3])]
(is (equals [1.0 2.0] (floor da))))))
(deftest test-add-emap
(testing "Unary"
(let [dest (double-array [1 10])
a (double-array [1.2 2.3])]
(is (equals [3.4 14.6] (add-emap! dest (partial * 2) a)))))
(testing "Binary"
(let [dest (double-array [1 10])
a (double-array [1.2 2.3])]
(is (equals [3.4 14.6] (add-emap! dest + a a))))))
(deftest test-set-emap
(testing "Unary"
(let [dest (double-array [1 10])
a (double-array [1.2 2.3])]
(is (equals [2.4 4.6] (set-emap! dest (partial * 2) a)))))
(testing "Binary"
(let [dest (double-array [1 10])
a (double-array [1.2 2.3])]
(is (equals [2.4 4.6] (set-emap! dest + a a))))))
(deftest instance-tests
(clojure.core.matrix.compliance-tester/instance-test (double-array []))
(clojure.core.matrix.compliance-tester/instance-test (double-array [1]))
(clojure.core.matrix.compliance-tester/instance-test (double-array [1 2]))
(clojure.core.matrix.compliance-tester/instance-test (double-array [-1 4 2 7 -3])))
(deftest test-select
(testing "select ops"
(let [da (double-array [1.2 3.4 5.6 7.8 9.1])]
(let [selected (select da :all)]
(is (= (class selected) (Class/forName "[D")))
(is (== selected da))
(is (not= selected da)))
(let [selected (select da 0)]
(is (scalar? selected))
(is (= 1.2 selected)))
(let [selected (select da [1 2])]
(is (= (class selected) (Class/forName "[D")))
(is (== selected [3.4 5.6]))))))
; TODO: complete 2d double array impl. element-map doesn't work as is when
; doing an outer-product because it's trying to cast an array as a double...
(comment deftest compliance-test
(clojure.core.matrix.compliance-tester/compliance-test (double-array [0.23])))
mikera/core.matrix
(ns clojure.core.matrix.test-persistent-vector-implementation
(:refer-clojure :exclude [vector?])
(:require [clojure.core.matrix.impl.wrappers :as wrap]
[clojure.core.matrix.compliance-tester]
clojure.core.matrix.impl.persistent-vector
[clojure.core.matrix.compliance-tester :as compliance]
[clojure.core.matrix :refer :all]
[clojure.core.matrix.macros-clj :refer [error?]]
[clojure.core.matrix.protocols :as mp]
[clojure.test :refer :all]))
(deftest test-regressions
(testing "vector 3D transpose"
(is (= [[[1]]] (transpose [[[1]]]))))
(testing "vector wrapper coerce"
(is (= 1.0 (coerce [] (wrap/wrap-scalar 1.0))))
(is (= [1.0 2.0] (coerce [] (slices (double-array [1 2]))))))
(testing "vector length"
(is (== 5 (length [3 4]))))
(testing "scalar broadcast"
(is (e= [11 12 13] (add [1 2 3] 10)))
(is (e= [11 12 13] (add 10 [1 2 3]))))
(testing "persistent vector shape"
(is (= [2] (seq (shape [1 2]))))
(is (= [0] (seq (shape [])))))
(testing "empty vector"
(is (e= [] (coerce [] [])))
(is (e= [] (assign [] 1.0)))
(is (empty? (eseq [])))
(is (nil? (coerce [] nil))))
(testing "broadcast on emap"
(is (equals [[6 7] [8 9]] (emap + [[1 2] [3 4]] 5)))
(is (equals [[6 7] [8 9]] (emap + 5 [[1 2] [3 4]]))))
(testing "Inner product incompatible shapes"
(is (error? (inner-product [1 2] [[3 4]])))))
(deftest test-double-coercion-302 ;; fix for #302
(testing "coercion to double array")
(is (equals [17.0] (to-double-array [(double-array [17])]))))
(deftest test-nested-implementation
(testing "nested double arrays"
(let [m [(double-array [1 2]) (double-array [3 4])]]
(is (not (mutable? m))) ;; persistent vector should not be mutable, even if components are
(is (== 2 (dimensionality m)))
(is (equals [3 7] (mmul m [1 1])))
(is (equals [2 4] (get-column m 1)))))
(testing "nested implementations"
(is (equals [(double-array [17])] (array :vectorz [(double-array [17])]) ))))
(deftest test-emap
(testing "basic"
(is (equals [2 3] (emap inc [1 2]))))
(testing "nested implementations"
(is (equals [[2 3]] (emap inc [(double-array [1 2])])))))
(deftest test-eseq
(testing "basic"
(is (= [2 3] (eseq [2 3]))))
(testing "nested implementations"
(is (= [1 2] (eseq [[1 2]])))
(is (= [1 2] (eseq [[1] [2]])))
(is (= [1.0 2.0] (eseq [(double-array [1 2])])))))
(deftest test-coerce
(testing "self-coerce"
(is (= [2] (coerce [] [2]))))
(testing "double arrays"
(is (= [1.0 2.0] (coerce [] (double-array [1 2])))))
(testing "nested sequences"
(is (= [[1 2] [3 4]] (coerce [] '((1 2) (3 4))))))
(testing "canonical translation to vectors"
(is (= [[17.0]] (coerce [] [(double-array [17])])))))
(deftest instance-tests
(testing "empty persistent vectors are supported"
(compliance/instance-test []))
(testing "matrices of symbols are supported"
(clojure.core.matrix.compliance-tester/instance-test ['a 'b]))
(testing "matrices of heterogeneous submatrices"
(clojure.core.matrix.compliance-tester/instance-test [[1 2.0] (double-array [3 4])])))
mikera/core.matrix
(ns clojure.core.matrix.test-api
(:refer-clojure :exclude [vector?])
; (:use [clojure.core.matrix])
(:require [clojure.core.matrix.protocols :as mp]
[clojure.core.matrix.linear :as li]
; [clojure.core.matrix :refer [:all]]
[clojure.core.matrix.operators :as op]
[clojure.core.matrix :as m
:refer [abs abs! acos acos! add add! add-inner-product! add-outer-product! add-product add-product! add-row add-scaled add-scaled! add-scaled-product add-scaled-product! array array? as-vector asin asin! assign assign! assign-array! atan atan! block-diagonal-matrix broadcast broadcast-coerce broadcast-like cbrt cbrt! ceil ceil! clamp clone cmp coerce column-count column-matrix column-matrix? columns compute-matrix conforming? conjoin conjoin-along cos cos! cosh cosh! cross cross! current-implementation current-implementation-object dense density det diagonal diagonal-matrix diagonal? dimension-count dimensionality distance div div! dot e* e= e== ecount eif element-type emap emap! emap-indexed emap-indexed! emax emin emul emul! ensure-mutable eq equals ereduce eseq esum exp exp! fill fill! floor floor! ge get-column get-row gt identity-matrix identity-matrix? immutable index index-seq index-seq-for-shape index? inner-product inverse join join-along label label-index labels le length length-squared lerp lerp! log log! log10 log10! logistic logistic! lower-triangular? lt magnitude magnitude-squared main-diagonal matrix matrix? maximum mget minimum mmul mset mset! mul mul! multiply-row mutable mutable? native native? ne negate negate! new-array new-matrix new-scalar-array new-sparse-array new-vector non-zero-count non-zero-indices normalise normalise! numerical? order orthogonal? outer-product pack permutation-matrix pow pow! rank relu relu! reshape rotate round round! row-count row-matrix row-matrix? rows same-shape? scalar scalar-array scalar? scale scale! scale-add scale-add! select select-indices select-view set-column set-column! set-current-implementation set-indices set-indices! set-inner-product! set-row set-row! set-selection set-selection! shape shift signum signum! sin sin! sinh sinh! slice slice-count slice-map slice-view slice-views slices softmax softmax! softplus softplus! sparse sparse-array sparse-matrix sparse? sqrt sqrt! square square? sub sub! submatrix subvector supports-dimensionality? supports-shape? swap-rows symmetric? tan tan! tanh tanh! to-degrees to-degrees! to-double-array to-nested-vectors to-object-array to-radians to-radians! to-vector trace transform transform! transpose transpose! upper-triangular? vec? zero-array zero-count zero-dimensional? zero-matrix zero-matrix? zero-vector validate-shape]]
[clojure.core.matrix.implementations :as imp]
[clojure.core.matrix.utils :refer [broadcast-shape]]
#?@(:clj [[clojure.core.matrix.macros :refer [error doseq-indexed]]
[clojure.core.matrix.macros-clj :refer [error?]]
[clojure.test :refer [deftest is testing run-tests]]
[clojure.core.matrix.demo.examples]]
:cljs [[cljs.test :refer-macros [deftest is testing run-tests]]
[thinktopic.aljabr.core :as aljabr]
;[clojure.core.matrix :refer-macros [with-implementation]]
[clojure.core.matrix.macros :refer-macros [error]]
[clojure.core.matrix.macros-cljs :refer-macros [error?]]]))
#?(:clj (:import [java.io StringWriter])))
(comment
(deftest test-implementations
(testing "vector implementation"
(is (clojure.core/vector? (imp/get-canonical-object :persistent-vector)))
(is (= :persistent-vector (imp/get-implementation-key []))))
(testing "non-existent implementation"
(is (nil? (imp/get-canonical-object :random-fictitious-implementation-key))))
(testing "with-implementation"
(is (= [1 2] (with-implementation [] (matrix [1 2]))))
#?(:clj
(is (= (class (double-array [1 2]))
(class (with-implementation :double-array (matrix [1 2])))))))))
(deftest test-square
(is (equals 81 (square 9)))
(is (equals [1 4] (square [1 2])))
(is (equals [[1 4]] (square [(double-array [1 2])]))))
(deftest test-new
(is (e= [0.0] (new-vector :ndarray 1)))
(is (e= [[0.0]] (new-matrix :ndarray 1 1)))
; This doesn't seem correct, as some implementations can zero out rather than
; have nil values when allocating. At least according to the documentation.
;(is (e= [nil] (new-array :ndarray [1])))
(is (equals [0 0 0] (new-vector :persistent-vector 3)))
(is (equals [0 0 0] (new-vector :ndarray-double 3)))
(is (= [0.0 0.0 0.0] (seq (new-vector :double-array 3))))
(is (e= [0.0 0.0 0.0] (new-vector :double-array 3))))
(comment
;;fails test
(deftest test-element-map
(is (equals 1 (emap inc (array 0))))
(is (equals [2] (emap inc (array [1]))))
(is (equals [[3]] (emap inc (array [[2]]))))
(is (equals [[[[5]]]] (emap inc (array [[[[4]]]]))))
(is (equals [10] (emap + [1] [2] [3] [4])))
(is (equals [10] (emap + [1] (broadcast 2 [1]) (double-array [3]) [4])))))
(deftest test-broadcast-coerce
(is (equals [2 2] (mp/broadcast-coerce [1 1] 2)))
(is (equals [2 2] (mp/broadcast-coerce [1 1] (double-array [2 2]))))
(is (equals [[7 7] [7 7]] (mp/broadcast-coerce [[1 2] [3 4]] 7)))
(is (equals [1 2 3] (mp/broadcast-coerce [2 3 4] [1 2 3])))
(is (error? (mp/broadcast-coerce [1 2 3] [1 2]))))
(deftest test-logistic
(is (== 0.0 (logistic -10000)))
(is (== 0.5 (logistic 0)))
(is (== 1.0 (logistic 10000)))
(is (equals [0 0.5 1] (logistic [-10000 0 10000])))
(let [da (double-array [-10000 0 10000])]
(logistic! da)
(is (equals [0 0.5 1] da)))
(is (error? (logistic! 0.7))))
(deftest test-softplus
(is (equals [0.0 (log 2) 1000.0] (softplus [-1000.0 0.0 1000.0])))
(let [da (double-array [-1000 0 1000])]
(softplus! da)
(is (equals [0.0 (log 2) 1000.0] da))))
(deftest test-relu
(is (equals [0.0 0.0 0.5 1000.0] (relu [-1000.0 0.0 0.5 1000.0])))
(let [da (double-array [-1000 0 1000])]
(relu! da)
(is (equals [0.0 0.0 1000.0] da))))
(deftest test-softmax
(is (equals [0.5 0.5] (softmax [10 10])))
(is (equals [0.0 1.0] (softmax [-100 100]) 0.000001))
(let [da (double-array [-10 -10])]
(softmax! da)
(is (equals [0.5 0.5] da))))
(comment
;;fails tests
(deftest test-mathsops
(testing "ops on scalars"
(is (== 1.0 (floor 1.2)))
(is (thrown? #? (:clj Throwable :cljs js/Error) (floor! 1.2))))
(testing "ops"
(is (= [1.0 2.0] (floor [1.2 2.7]))))
(testing "mutable maths ops"
(is (error? (signum! [1 2])))
(is (equals [1 0 1 -1] (signum! (double-array [1 0 2 -10])))))))
(deftest test-predicates
(testing "scalar predicates"
(is (not (array? 1)))
(is (scalar? 1))
(is (scalar? (m/mget [1 2 3] 1)))
(is (scalar? (first (slices [1 2 3])))))
(testing "clojure vector predicates"
(is (array? [1 2]))
(is (vec? [1 2]))
(is (array? [[1 2] [3 4]]))
(is (matrix? [[1 2] [3 4]]))
(is (not (vec? [[1 2] [3 4]])))
(is (not (matrix? [[[1 2] [2 3]] [[3 4] [5 6]]]))))
(testing "row and column predicates"
(is (not (column-matrix? [1])))
(is (column-matrix? [[1]]))
(is (not (row-matrix? [1])))
(is (row-matrix? [[1]]))
(is (not (column-matrix? [1 2])))
(is (column-matrix? [[1] [2]]))
(is (not (column-matrix? [[1 2 3]])))
(is (row-matrix? [[1 2 3]]))
(is (not (row-matrix? [1 2]))))
(testing "mutability"
(is (not (mutable? [1 2])))
(is (mutable? (double-array [1 2]))))
(testing "symmetry"
(is (symmetric? (matrix [[1 -3][-3 2]])))
(is (not (symmetric? (matrix [[1 -3][-10 2]]))))
(is (symmetric? (matrix [[1 -4 -5][-4 2 -6][-5 -6 3]])))
(is (not (symmetric? (matrix [[1 -4 -5][-4 2 -6][-5 -10 3]]))))
(is (not (symmetric? (matrix [[1 2 3 4]]))))
(is (not (symmetric? (matrix [[1][2][3][4]]))))
(is (symmetric? (matrix [1 2 3 4])))
(is (symmetric? 2))
(is (symmetric? nil))
(is (symmetric? (double-array [1 2 3 4])))
(is (symmetric? (array [1 2 3 4])))
(is (symmetric? (array [[1 -3][-3 2]])))
;(is (not (symmetric? (array [[1 -3][-10 2]]))))
))
mikera/core.matrix
(ns clojure.core.matrix.impl.test-wrappers
(:require [clojure.core.matrix.protocols :as mp]
[mikera.cljutils.error :refer [error?]]
[clojure.core.matrix.compliance-tester :as compliance]
[clojure.core.matrix :refer :all]
[clojure.core.matrix.impl.wrappers :refer :all]
[clojure.test :refer :all]))
(deftest test-mutability
(let [s (wrap-scalar 7)]
(is (== 10 (mget (mset! s 10))))
(is (== 10 (mget s))))
(let [s (wrap-scalar 7)]
(is (== 8 (mget (emap! inc s))))
(is (== 8 (mget s))))
(let [s (wrap-scalar 7)]
(scale! s 2)
(is (== 14 (mget s))))
(let [v (wrap-nd (double-array [1 2]))]
(is (equals [3 12] (add v [2 10])))
;(add! v [2 10]) TODO: fix mutation of NDWRapper
;(is (equals [3 12] v))
))