-
Notifications
You must be signed in to change notification settings - Fork 1
/
apache_matrix.clj
1210 lines (1082 loc) · 48.5 KB
/
apache_matrix.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns provisdom.math.apache-matrix
(:require
[clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as gen]
[clojure.spec.test.alpha :as st]
[orchestra.spec.test :as ost]
[provisdom.utility-belt.anomalies :as anomalies]
[provisdom.math.core :as m]
[provisdom.math.matrix :as mx]
[provisdom.math.vector :as vector]
[provisdom.math.tensor :as tensor]
[provisdom.math.arrays :as ar]
[provisdom.math.random :as random])
(:import
[org.apache.commons.math3.linear Array2DRowRealMatrix RealMatrix
QRDecomposition LUDecomposition CholeskyDecomposition
RectangularCholeskyDecomposition Array2DRowRealMatrix
EigenDecomposition SingularValueDecomposition
RRQRDecomposition DecompositionSolver MatrixUtils]))
(declare apache-matrix? apache-square-matrix? apache-matrix eigen-decomposition
rrqr-decomposition rows columns === transpose
positive-definite-apache-matrix-finite-by-squaring
positive-semidefinite-apache-matrix-finite-by-squaring diagonal mx* add
covariance-apache-matrix->correlation-apache-matrix
correlation-apache-matrix-by-squaring some-kv get-entry assoc-entry!
symmetric-apache-matrix-by-averaging! assoc-diagonal!)
(s/def ::rank ::m/int-non-)
;;;MATRIX TYPES
(defn apache-matrix?
"Returns true if an Apache Commons matrix."
[x]
(instance? Array2DRowRealMatrix x))
(s/fdef apache-matrix?
:args (s/cat :x any?)
:ret boolean?)
(s/def ::apache-matrix
(s/with-gen
apache-matrix?
#(gen/fmap apache-matrix (s/gen ::mx/matrix))))
(defn empty-apache-matrix?
"Returns true if an empty Apache Commons matrix."
[x]
(and (apache-matrix? x) (zero? (rows x))))
(s/fdef empty-apache-matrix?
:args (s/cat :x any?)
:ret boolean?)
(s/def ::empty-apache-matrix
(s/with-gen
empty-apache-matrix?
#(= (apache-matrix [[]]) %)))
(defn apache-matrix-finite?
"Returns true if an Apache Commons matrix without infinite numbers."
[x]
(and (apache-matrix? x)
(nil? (some-kv (fn [_ _ number]
(m/inf? number))
x))))
(s/fdef apache-matrix-finite?
:args (s/cat :x any?)
:ret boolean?)
(s/def ::apache-matrix-finite
(s/with-gen
apache-matrix-finite?
#(gen/fmap apache-matrix (s/gen ::mx/matrix-finite))))
(defn square-apache-matrix?
"Returns true if a square Apache Commons matrix (i.e., same number of rows and
columns)."
[x]
(and (apache-matrix? x) (.isSquare ^Array2DRowRealMatrix x)))
(s/fdef square-apache-matrix?
:args (s/cat :x any?)
:ret boolean?)
(s/def ::square-apache-matrix
(s/with-gen
square-apache-matrix?
#(gen/fmap apache-matrix (s/gen ::mx/square-matrix))))
(s/def ::square-apache-matrix-finite
(s/with-gen
(s/and square-apache-matrix? apache-matrix-finite?)
#(gen/fmap apache-matrix (s/gen ::mx/square-matrix-finite))))
(defn diagonal-apache-matrix?
"Returns true if a diagonal matrix (the entries outside the main diagonal are
all zero)."
[x]
(and (apache-matrix? x)
(nil? (some-kv (fn [i j e]
(not (or (= i j) (zero? e))))
x))))
(s/fdef diagonal-apache-matrix?
:args (s/cat :x any?)
:ret boolean?)
(s/def ::diagonal-apache-matrix
(s/with-gen
diagonal-apache-matrix?
#(gen/fmap apache-matrix (s/gen ::mx/diagonal-matrix))))
(defn upper-triangular-apache-matrix?
"Returns true if an upper triangular matrix (the entries below the main
diagonal are all zero)."
[x]
(and (square-apache-matrix? x)
(nil? (some-kv (fn [i j e]
(not (or (<= i j) (zero? e))))
x))))
(s/fdef upper-triangular-apache-matrix?
:args (s/cat :x any?)
:ret boolean?)
(s/def ::upper-triangular-apache-matrix
(s/with-gen
upper-triangular-apache-matrix?
#(gen/fmap apache-matrix (s/gen ::mx/upper-triangular-matrix))))
(defn lower-triangular-apache-matrix?
"Returns true if a lower triangular matrix (the entries above the main
diagonal are all zero)."
[x]
(and (square-apache-matrix? x)
(nil? (some-kv (fn [i j e]
(not (or (>= i j) (zero? e))))
x))))
(s/fdef lower-triangular-apache-matrix?
:args (s/cat :x any?)
:ret boolean?)
(s/def ::lower-triangular-apache-matrix
(s/with-gen
lower-triangular-apache-matrix?
#(gen/fmap apache-matrix (s/gen ::mx/lower-triangular-matrix))))
(defn symmetric-apache-matrix?
"Returns true is a symmetric Apache Commons matrix."
[x]
(and (apache-matrix? x) (MatrixUtils/isSymmetric x 0.0)))
(s/fdef symmetric-apache-matrix?
:args (s/cat :x any?)
:ret boolean?)
(s/def ::symmetric-apache-matrix
(s/with-gen
symmetric-apache-matrix?
#(gen/fmap apache-matrix (s/gen ::mx/symmetric-matrix))))
(defn positive-semidefinite-apache-matrix-finite?
"Returns true if a finite positive-semidefinite Apache matrix. Larger `accu`
creates more false positives and less false negatives."
[x accu]
(and (symmetric-apache-matrix? x)
(apache-matrix-finite? x)
(every? #(m/roughly-non-? % accu)
(try (::eigenvalues (eigen-decomposition x))
(catch Exception _ nil)))))
(s/fdef positive-semidefinite-apache-matrix-finite?
:args (s/cat :x any? :accu ::m/accu)
:ret boolean?)
(s/def ::positive-semidefinite-apache-matrix-finite
(s/with-gen
#(positive-semidefinite-apache-matrix-finite? % m/sgl-close)
#(gen/fmap (fn [m]
(positive-semidefinite-apache-matrix-finite-by-squaring
(apache-matrix m)))
(s/gen ::mx/square-matrix-finite))))
(defn positive-definite-apache-matrix-finite?
"Returns true if a finite positive definite Apache matrix. Larger `accu`
creates more false negatives and less false positives."
[x accu]
(and (symmetric-apache-matrix? x)
(apache-matrix-finite? x)
(every? #(> % accu)
(try (::eigenvalues (eigen-decomposition x))
(catch Exception _ nil)))))
(s/fdef positive-definite-apache-matrix-finite?
:args (s/cat :x any? :accu ::m/accu)
:ret boolean?)
(s/def ::positive-definite-apache-matrix-finite
(s/with-gen
#(positive-definite-apache-matrix-finite? % m/sgl-close)
#(gen/fmap (fn [m]
(positive-definite-apache-matrix-finite-by-squaring
(apache-matrix m)))
(s/gen ::mx/square-matrix-finite))))
(defn correlation-apache-matrix?
"Returns true if a finite positive definite Apache Commons matrix with a unit
diagonal. Larger `accu` creates more false negatives and less false
positives."
[x accu]
(and (positive-definite-apache-matrix-finite? x accu)
(every? m/one? (diagonal x))))
(s/fdef correlation-apache-matrix?
:args (s/cat :x any? :accu ::m/accu)
:ret boolean?)
(s/def ::correlation-apache-matrix
(s/with-gen
#(correlation-apache-matrix? % m/sgl-close)
#(gen/fmap (fn [m]
(correlation-apache-matrix-by-squaring (apache-matrix m)))
(s/gen ::mx/square-matrix-finite))))
;;;MATRIX CONSTRUCTORS
(defn- block-apache-matrix->apache-matrix
"Converts BlockRealMatrix (or other RealMatrix) to Array2DRowRealMatrix."
[block-apache-matrix]
(if (zero? (.getRowDimension ^RealMatrix block-apache-matrix))
(Array2DRowRealMatrix.)
(Array2DRowRealMatrix. ^"[[D" (.getData ^RealMatrix block-apache-matrix))))
(defn apache-matrix
"Returns a matrix using the Apache Commons matrix implementation."
[m]
(if (mx/empty-matrix? m)
(Array2DRowRealMatrix.)
(Array2DRowRealMatrix. ^"[[D" (ar/array2D :double m))))
(s/fdef apache-matrix
:args (s/cat :m ::mx/matrix)
:ret (s/nilable ::apache-matrix))
(defn apache-matrix->matrix
"Converts an Apache Commons matrix into a matrix."
[apache-m]
(if (zero? (rows apache-m))
[[]]
(mapv vec (.getData ^Array2DRowRealMatrix apache-m))))
(s/fdef apache-matrix->matrix
:args (s/cat :apache-m ::apache-matrix)
:ret ::mx/matrix)
(defn positive-semidefinite-apache-matrix-finite-by-squaring
"Returns a finite positive semidefinite Apache Commons matrix by first
squaring `square-apache-m-finite`."
[square-apache-m-finite]
(let [size (rows square-apache-m-finite)
new-m square-apache-m-finite]
(if (zero? size)
square-apache-m-finite
(loop [i 0]
(when (< i 10)
(let [lower-m (mx* new-m
(apache-matrix
(mx/diagonal-matrix
(repeat size (m/pow 10 (m/one- (m/pow 2 i)))))))
lower-m (mx* lower-m (transpose lower-m))
_ (symmetric-apache-matrix-by-averaging! lower-m)]
(if (positive-semidefinite-apache-matrix-finite? lower-m m/sgl-close)
lower-m
(recur (inc i)))))))))
(s/fdef positive-semidefinite-apache-matrix-finite-by-squaring
:args (s/cat :square-apache-m-finite ::square-apache-matrix-finite)
:ret (s/nilable ::positive-semidefinite-apache-matrix-finite))
(defn positive-definite-apache-matrix-finite-by-squaring
"Returns a finite positive definite Apache Commons matrix squaring it. Will
tweak original matrix if necessary to ensure positive definite."
[square-apache-m-finite]
(let [size (rows square-apache-m-finite)
new-m square-apache-m-finite]
(if (zero? size)
square-apache-m-finite
(loop [i 0]
(when (< i 10)
(let [lower-m (mx* new-m
(apache-matrix
(mx/diagonal-matrix
(repeat size (m/pow 10 (m/one- (m/pow 2 i)))))))
lower-m (if (zero? i)
lower-m
(add lower-m
(apache-matrix
(mx/diagonal-matrix
size (fn [_]
(* (if (odd? i) (- 1.0) 1.0)
(m/pow 10 (- i m/sgl-digits))))))))
lower-m (mx* lower-m (transpose lower-m))
_ (symmetric-apache-matrix-by-averaging! lower-m)]
(if (positive-definite-apache-matrix-finite? lower-m m/sgl-close)
lower-m
(recur (inc i)))))))))
(s/fdef positive-definite-apache-matrix-finite-by-squaring
:args (s/cat :square-apache-m-finite ::square-apache-matrix-finite)
:ret (s/nilable ::positive-definite-apache-matrix-finite))
(defn correlation-apache-matrix-by-squaring
"Returns a finite Correlation Apache Commons matrix by first squaring
`square-apache-m-finite`."
[square-apache-m-finite]
(if (zero? (rows square-apache-m-finite))
square-apache-m-finite
(let [new-m (covariance-apache-matrix->correlation-apache-matrix
(positive-definite-apache-matrix-finite-by-squaring square-apache-m-finite))
size (when new-m (rows new-m))]
(when new-m
(loop [i 0]
(when (< i 100)
(let [lower-m (mx* new-m (apache-matrix
(mx/diagonal-matrix
(repeat size (m/one- (* 0.01 i))))))
_ (symmetric-apache-matrix-by-averaging! lower-m)
_ (assoc-diagonal! lower-m (repeat size 1.0))]
(if (correlation-apache-matrix? lower-m m/sgl-close)
lower-m
(recur (inc i))))))))))
(s/fdef correlation-apache-matrix-by-squaring
:args (s/cat :square-apache-m-finite ::square-apache-matrix-finite)
:ret (s/nilable ::correlation-apache-matrix))
(defn rnd-positive-definite-apache-matrix-finite!
"Returns a finite positive definite Apache Commons matrix with a random
spectrum. The orthogonal matrices are generated by using 2 × `size` composed
Householder reflections.
Alternative #1: Sample from the Inverse-Wishart Distribution.
Alternative #2: Use [[positive-definite-apache-matrix-finite-by-squaring]]
with a random square matrix."
[size]
(if (zero? size)
(apache-matrix [[]])
(loop [i 0]
(if (< i 100)
(let [m (apache-matrix
(mx/rnd-spectral-matrix!
(vec (take size (random/rnd-lazy!)))))]
(if (positive-definite-apache-matrix-finite? m m/sgl-close)
m
(recur (inc i))))))))
(s/fdef rnd-positive-definite-apache-matrix-finite!
:args (s/cat :size ::vector/size)
:ret ::positive-definite-apache-matrix-finite)
(defn rnd-correlation-apache-matrix!
"Returns a correlation Apache Commons matrix from a covariance matrix
with a random spectrum. The orthogonal matrices are generated by using
2 * `size` composed Householder reflections.
Alternative #1: Sample Covariance from the Inverse-Wishart Distribution.
Alternative #2: Use [[correlation-apache-matrix-by-squaring]] with a
random square matrix."
[size]
(if (zero? size)
(apache-matrix [[]])
(covariance-apache-matrix->correlation-apache-matrix
(rnd-positive-definite-apache-matrix-finite! size))))
(s/fdef rnd-correlation-apache-matrix!
:args (s/cat :size ::vector/size)
:ret ::correlation-apache-matrix)
;;;MATRIX INFO
(defn rows
"Returns the number of rows."
[apache-m]
(.getRowDimension ^Array2DRowRealMatrix apache-m))
(s/fdef rows
:args (s/cat :apache-m ::apache-matrix)
:ret ::mx/rows)
(defn columns
"Returns the number of columns."
[apache-m]
(.getColumnDimension ^Array2DRowRealMatrix apache-m))
(s/fdef columns
:args (s/cat :apache-m ::apache-matrix)
:ret ::mx/columns)
(defn get-entry
"Returns the specified Apache Commons matrix element."
[apache-m row column]
(try (.getEntry ^Array2DRowRealMatrix apache-m
row
column)
(catch Exception _ m/nan)))
(s/fdef get-entry
:args (s/and (s/cat :apache-m ::apache-matrix
:row ::mx/row
:column ::mx/column)
(fn [{:keys [apache-m row column]}]
(and (< row (rows apache-m))
(< column (columns apache-m)))))
:ret ::m/number)
(defn get-row
"Gets a `row` of an Apache Commons matrix, as a vector."
[apache-m row]
(try (vec (.getRow ^Array2DRowRealMatrix apache-m
row))
(catch Exception _ nil)))
(s/fdef get-row
:args (s/and (s/cat :apache-m ::apache-matrix :row ::mx/row)
(fn [{:keys [apache-m row]}]
(< row (rows apache-m))))
:ret (s/nilable ::vector/vector))
(defn get-column
"Gets a `column` of an Apache Commons matrix, as a vector."
[apache-m column]
(try (vec (.getColumn ^Array2DRowRealMatrix apache-m
column))
(catch Exception _ nil)))
(s/fdef get-column
:args (s/and (s/cat :apache-m ::apache-matrix :column ::mx/column)
(fn [{:keys [apache-m column]}]
(< column (columns apache-m))))
:ret (s/nilable ::vector/vector))
(defn diagonal
"Returns the specified diagonal of an Apache Commons matrix as a vector. If
'k'>0, returns a diagonal above the main diagonal. If 'k'<0, returns a
diagonal below the main diagonal. Works on both square and rectangular
matrices."
([apache-m]
(if (zero? (rows apache-m))
[]
(reduce (fn [tot e]
(conj tot (get-entry apache-m e e)))
[]
(range (min (rows apache-m) (columns apache-m))))))
([apache-m k]
(if (zero? (rows apache-m))
[]
(let [r (if (neg? k) (- k) 0)
c (if (pos? k) k 0)
nc (- (columns apache-m) c)
nr (- (rows apache-m) r)
start (- (min r c))
end (min nc nr)]
(if (pos? end)
(vec (for [i (range start end)]
(get-entry apache-m (+ i r) (+ i c))))
[])))))
(s/fdef diagonal
:args (s/cat :apache-m ::apache-matrix :k (s/? ::m/int))
:ret ::vector/vector)
(defn trace
"Calculates the trace of a square Apache Commons matrix (sum of elements on
main diagonal)."
[square-apache-m]
(.getTrace ^Array2DRowRealMatrix square-apache-m))
(s/fdef trace
:args (s/cat :square-apache-m ::square-apache-matrix)
:ret ::m/number)
(defn get-slices-as-matrix
"Performs a slice on the Apache Commons matrix given by the options.
Options:
`::mx/row-indices` returns all rows by default, can pass a row index or
sequence of row indices
`::mx/column-indices` returns all columns by default, can pass a column
index or sequence of column indices
`::mx/exception-row-indices` can pass a row index or sequence of row indices
to exclude
`::mx/exception-column-indices` can pass a column index or sequence of
column indices to exclude.
Exceptions override inclusions. Can be used to permute matrix through index
sequence ordering."
[apache-m {:keys [::mx/row-indices
::mx/column-indices
::mx/exception-row-indices
::mx/exception-column-indices]}]
(let [calc-fn (fn [i except-i n]
(cond (and (not i) (not except-i)) true
(not except-i) (if (number? i)
(if (< i n) i [])
(remove #(>= % n) i))
(number? i) (if (number? except-i)
(if (= except-i i)
[]
(if (< i n) i []))
(if (contains? (set except-i) i)
[]
(if (< i n) i [])))
:else (let [indices (or i (range n))]
(if (number? except-i)
(remove (fn [index]
(or (= except-i index) (>= index n)))
indices)
(reduce
(fn [tot e]
(if (or (>= e n) (some #(= % e) except-i))
tot
(conj tot e)))
[]
indices)))))
n-rows (rows apache-m)
rs (calc-fn row-indices exception-row-indices n-rows)
cs (calc-fn column-indices exception-column-indices (columns apache-m))
new-m (cond
(or (and (coll? rs) (empty? rs)) (and (coll? cs) (empty? cs))) nil
(and (number? rs) (number? cs)) [[(get-entry apache-m rs cs)]]
(and (number? rs) (coll? cs)) (mx/row-matrix (let [row-vector (get-row apache-m rs)]
(map (fn [c]
(get row-vector c))
cs)))
(and (number? rs) (true? cs)) (mx/row-matrix (get-row apache-m rs))
(and (coll? rs) (number? cs)) (mx/column-matrix (let [column-vector (get-column apache-m cs)]
(map (fn [r]
(get column-vector r))
rs)))
(and (coll? rs) (coll? cs)) (mapv (fn [row-vector]
(reduce (fn [tot column]
(conj tot (get row-vector column)))
[]
cs))
(map (fn [r]
(get-row apache-m r))
rs))
(and (coll? rs) (true? cs)) (mapv (fn [r]
(get-row apache-m r))
rs)
(and (true? rs) (number? cs)) (mx/column-matrix (get-column apache-m cs))
(and (true? rs) (coll? cs)) (mapv (fn [row]
(reduce (fn [tot column]
(conj tot (get-entry apache-m row column)))
[]
cs))
(range n-rows))
(and (true? rs) (true? cs)) apache-m)]
(if new-m
(if (apache-matrix? new-m)
new-m
(apache-matrix new-m))
(apache-matrix [[]]))))
(s/fdef get-slices-as-matrix
:args (s/cat :apache-m ::apache-matrix
:opts (s/keys :opt [::mx/row-indices
::mx/column-indices
::mx/exception-row-indices
::mx/exception-column-indices]))
:ret ::apache-matrix)
(s/def ::top-left ::apache-matrix)
(s/def ::bottom-left ::apache-matrix)
(s/def ::top-right ::apache-matrix)
(s/def ::bottom-right ::apache-matrix)
(defn matrix-partition
"Returns a map containing the four sub-matrices labeled `::top-left`,
`::bottom-left`, `::top-right`, and `::bottom-right`.
`first-bottom-row` is the bottom of where the slice will occur.
`first-right-column` is the right edge of where the slice will occur."
[apache-m first-bottom-row first-right-column]
{::top-left (get-slices-as-matrix apache-m {::mx/row-indices (range first-bottom-row)
::mx/column-indices (range first-right-column)})
::bottom-left (get-slices-as-matrix apache-m {::mx/exception-row-indices (range first-bottom-row)
::mx/column-indices (range first-right-column)})
::top-right (get-slices-as-matrix apache-m {::mx/row-indices (range first-bottom-row)
::mx/exception-column-indices (range first-right-column)})
::bottom-right (get-slices-as-matrix apache-m {::mx/exception-row-indices (range first-bottom-row)
::mx/exception-column-indices (range first-right-column)})})
(s/fdef matrix-partition
:args (s/cat :apache-m ::apache-matrix
:first-bottom-row ::mx/row
:first-right-column ::mx/column)
:ret (s/keys :req [::top-left ::bottom-left ::top-right ::bottom-right]))
(defn some-kv
"Returns the first logical true value of (pred row column number) for any
number in Apache Commons matrix, else nil.
Options: `::mx/by-row?` (default: true)."
([pred apache-m] (some-kv pred apache-m {::mx/by-row? true}))
([pred apache-m {:keys [::mx/by-row?] :or {by-row? true}}]
(let [mt (if by-row?
apache-m
(transpose apache-m))
rows (rows mt)]
(loop [row 0]
(when (< row rows)
(or (vector/some-kv (fn [column number]
(pred row column number))
(get-row mt row))
(recur (inc row))))))))
(s/fdef some-kv
:args (s/cat :pred (s/fspec :args (s/cat :row ::mx/row
:column ::mx/column
:number ::m/number)
:ret boolean?)
:apache-m ::apache-matrix
:opts (s/? (s/keys :opt [::mx/by-row?])))
:ret (s/nilable ::m/number))
;;;MATRIX MANIPULATION
(defn transpose
"Transposes an Apache Commons matrix by swapping rows and columns, returning a
new Apache Commons matrix."
[apache-m]
(if (zero? (rows apache-m))
apache-m
(.transpose ^Array2DRowRealMatrix apache-m)))
(s/fdef transpose
:args (s/cat :apache-m ::apache-matrix)
:ret ::apache-matrix)
(defn assoc-entry!
"Sets an entry in an Apache Commons matrix."
[apache-m row column number]
(when (and (< row (rows apache-m)) (< column (columns apache-m)))
(.setEntry ^Array2DRowRealMatrix apache-m
^long row
^long column
^double (double number))))
(s/fdef assoc-entry!
:args (s/cat :apache-m ::apache-matrix
:row ::mx/row
:column ::mx/column
:number ::m/number)
:ret nil)
(defn assoc-diagonal!
"Sets a diagonal in an Apache Commons matrix using the specified numbers."
[apache-m numbers]
(let [v (vec numbers)
c (count numbers)]
(when (= c (count (diagonal apache-m)))
(doseq [rc (range c)]
(assoc-entry! apache-m rc rc (get v rc 0.0))))))
(s/fdef assoc-diagonal!
:args (s/cat :apache-m ::apache-matrix :numbers ::m/numbers)
:ret nil)
(defn symmetric-apache-matrix-by-averaging!
"Updates symmetric Apache Commons matrix where each element above or below the
diagonal is equal to the average of the corresponding numbers. This is useful
to help with rounding errors."
[square-apache-m]
(doseq [row (range (rows square-apache-m))]
(doseq [column (range (inc row) (columns square-apache-m))]
(let [number (* 0.5 (+ (get-entry square-apache-m row column)
(get-entry square-apache-m column row)))]
(assoc-entry! square-apache-m row column number)
(assoc-entry! square-apache-m column row number)))))
(s/fdef symmetric-apache-matrix-by-averaging!
:args (s/cat :square-apache-m ::square-apache-matrix)
:ret nil)
(defn concat-rows
"Appends rows from all the Apache Common matrices after the first to the
first. Each matrix's column count must be the same or will return nil."
([] (apache-matrix [[]]))
([apache-m] apache-m)
([apache-m & apache-ms]
(when-let [new-m (apply mx/concat-rows
(apache-matrix->matrix apache-m)
(map apache-matrix->matrix apache-ms))]
(apache-matrix new-m))))
(s/fdef concat-rows
:args (s/or :zero (s/cat)
:one+ (s/cat :apache-m ::apache-matrix
:apache-ms (s/* ::apache-matrix)))
:ret (s/nilable ::apache-matrix))
(defn concat-columns
"Appends columns from all the Apache Common matrices after the first to the
first. Each matrix's row count must be the same or will return nil."
([] (apache-matrix [[]]))
([apache-m] apache-m)
([apache-m & apache-ms]
(when-let [new-m (apply mx/concat-columns
(apache-matrix->matrix apache-m)
(map apache-matrix->matrix apache-ms))]
(apache-matrix new-m))))
(s/fdef concat-columns
:args (s/or :zero (s/cat)
:one+ (s/cat :apache-m ::apache-matrix
:apache-ms (s/* ::apache-matrix)))
:ret (s/nilable ::apache-matrix))
(defn correlation-apache-matrix->covariance-apache-matrix
"Returns Covariance Apache Commons matrix from a Correlation Apache Commons
matrix."
[correlation-apache-matrix variances]
(when (= (count variances) (rows correlation-apache-matrix))
(if (zero? (count variances))
correlation-apache-matrix
(let [sqrt-m (apache-matrix
(mx/diagonal-matrix
(mapv m/sqrt variances)))
cov (mx* sqrt-m correlation-apache-matrix sqrt-m)
_ (symmetric-apache-matrix-by-averaging! cov)]
(when (positive-definite-apache-matrix-finite? cov m/sgl-close)
cov)))))
(s/fdef correlation-apache-matrix->covariance-apache-matrix
:args (s/cat :correlation-apache-matrix ::correlation-apache-matrix
:variances ::vector/vector-finite+)
:ret (s/nilable ::positive-definite-apache-matrix-finite))
(defn covariance-apache-matrix->correlation-apache-matrix
"Returns Correlation Apache Commons matrix from a Covariance Apache Commons
matrix."
[covariance-apache-matrix]
(if (zero? (rows covariance-apache-matrix))
covariance-apache-matrix
(let [inv-sqrt (apache-matrix
(mx/diagonal-matrix (map #(m/pow % -0.5)
(diagonal covariance-apache-matrix))))
corr (mx* inv-sqrt covariance-apache-matrix inv-sqrt)
_ (symmetric-apache-matrix-by-averaging! corr)
_ (assoc-diagonal! corr (repeat (rows inv-sqrt) 1.0))]
(when (correlation-apache-matrix? corr m/sgl-close)
corr))))
(s/fdef covariance-apache-matrix->correlation-apache-matrix
:args (s/cat :covariance-apache-matrix ::positive-definite-apache-matrix-finite)
:ret (s/nilable ::correlation-apache-matrix))
;;;MATRIX MATH
(defn ===
"Apache Commons matrix equality that works with NaN."
([apache-m] true)
([apache-m1 apache-m2]
(tensor/=== (apache-matrix->matrix apache-m1) (apache-matrix->matrix apache-m2)))
([apache-m1 apache-m2 & apache-ms]
(apply tensor/=== (apache-matrix->matrix apache-m1)
(apache-matrix->matrix apache-m2)
(map apache-matrix->matrix apache-ms))))
(s/fdef ===
:args (s/or :one (s/cat :apache-m ::apache-matrix)
:two+ (s/cat :apache-m1 ::apache-matrix
:apache-m2 ::apache-matrix
:apache-ms (s/* ::apache-matrix)))
:ret boolean?)
(defn mx*
"Apache Commons matrix multiplication. Number of columns of the first matrix
must match the number of rows of the second matrix."
([apache-m] apache-m)
([apache-m1 apache-m2]
(when (= (columns apache-m1) (rows apache-m2))
(if (zero? (rows apache-m1))
apache-m1
(.multiply ^Array2DRowRealMatrix apache-m1
^Array2DRowRealMatrix apache-m2))))
([apache-m1 apache-m2 & apache-ms]
(when-let [apache-m3 (mx* apache-m1 apache-m2)]
(apply mx* apache-m3 apache-ms))))
(s/fdef mx*
:args (s/or :one (s/cat :apache-m ::apache-matrix)
:two+ (s/cat :apache-m1 ::apache-matrix
:apache-m2 ::apache-matrix
:apache-ms (s/* ::apache-matrix)))
:ret (s/nilable ::apache-matrix))
(defn add
"Apache Commons matrix addition."
([apache-m] apache-m)
([apache-m1 apache-m2]
(if (and (zero? (rows apache-m1)) (zero? (rows apache-m2)))
apache-m1
(try (.add ^Array2DRowRealMatrix apache-m1
^Array2DRowRealMatrix apache-m2)
(catch Exception _ nil))))
([apache-m1 apache-m2 & apache-ms]
(when-let [apache-m3 (add apache-m1 apache-m2)]
(apply add apache-m3 apache-ms))))
(s/fdef add
:args (s/or :one (s/cat :apache-m ::apache-matrix)
:two+ (s/cat :apache-m1 ::apache-matrix
:apache-m2 ::apache-matrix
:apache-ms (s/* ::apache-matrix)))
:ret (s/nilable ::apache-matrix))
(defn subtract
"Apache Commons matrix subtraction."
([apache-m] apache-m)
([apache-m1 apache-m2]
(if (and (zero? (rows apache-m1)) (zero? (rows apache-m2)))
apache-m1
(try (.subtract ^Array2DRowRealMatrix apache-m1
^Array2DRowRealMatrix apache-m2)
(catch Exception _ nil))))
([apache-m1 apache-m2 & apache-ms]
(when-let [apache-m3 (subtract apache-m1 apache-m2)]
(apply subtract apache-m3 apache-ms))))
(s/fdef subtract
:args (s/or :one (s/cat :apache-m ::apache-matrix)
:two+ (s/cat :apache-m1 ::apache-matrix
:apache-m2 ::apache-matrix
:apache-ms (s/* ::apache-matrix)))
:ret (s/nilable ::apache-matrix))
(defn scalar-add
"Apache Commons matrix addition to a scalar."
[apache-m number]
(if (zero? (rows apache-m))
apache-m
(.scalarAdd ^Array2DRowRealMatrix apache-m
(double number))))
(s/fdef scalar-add
:args (s/cat :apache-m ::apache-matrix
:number ::m/number)
:ret ::apache-matrix)
(defn scalar-multiply
"Apache Commons matrix multiplication to a scalar."
[apache-m number]
(if (zero? (rows apache-m))
apache-m
(.scalarMultiply ^Array2DRowRealMatrix apache-m
(double number))))
(s/fdef scalar-multiply
:args (s/cat :apache-m ::apache-matrix
:number ::m/number)
:ret ::apache-matrix)
;;;MATRIX DECOMPOSITION
(s/def ::inverse (s/nilable ::square-apache-matrix))
(defn inverse
"Returns the inverse of a square Apache Commons matrix. Uses QR Decomposition
by default but will use other methods depending on matrix structure."
[square-apache-m]
(if (zero? (rows square-apache-m))
square-apache-m
(try (block-apache-matrix->apache-matrix
(MatrixUtils/inverse ^RealMatrix square-apache-m))
(catch Exception _ nil))))
(s/fdef inverse
:args (s/cat :square-apache-m ::square-apache-matrix)
:ret ::inverse)
(s/def ::LU-permutation (s/nilable ::square-apache-matrix))
(s/def ::L (s/nilable ::lower-triangular-apache-matrix))
(s/def ::U (s/nilable ::upper-triangular-apache-matrix))
(s/def ::determinant ::m/number)
(defn lu-decomposition-with-determinant-and-inverse
"Returns a map containing
`::L` -- the lower triangular factor Apache Commons matrix
`::U` -- the upper triangular factor Apache Commons matrix
`::LU-permutation` -- the permutation Apache Commons matrix
`::determinant` -- the determinant
`::inverse` -- the inverse Apache Commons matrix."
[square-apache-m]
(if (zero? (rows square-apache-m))
{::L square-apache-m
::U square-apache-m
::LU-permutation square-apache-m
::determinant m/nan
::inverse square-apache-m}
(let [lud (LUDecomposition. ^Array2DRowRealMatrix square-apache-m)
s (.getSolver lud)
inverse (try (.getInverse s) (catch Exception _ nil))
det (.getDeterminant lud)]
{::L (.getL lud)
::U (.getU lud)
::LU-permutation (.getP lud)
::determinant det
::inverse inverse})))
(s/fdef lu-decomposition-with-determinant-and-inverse
:args (s/cat :square-apache-m ::square-apache-matrix)
:ret (s/keys :req [::L ::U ::LU-permutation ::determinant ::inverse]))
(defn lu-decomposition-with-determinant
"Returns a map containing:
`::L` -- the lower triangular factor Apache Commons matrix
`::U` -- the upper triangular factor Apache Commons matrix
`::LU-permutation` -- the permutation Apache Commons matrix
`::determinant` -- the determinant."
[square-apache-m]
(if (zero? (rows square-apache-m))
{::L square-apache-m
::U square-apache-m
::LU-permutation square-apache-m
::determinant m/nan}
(let [lud (LUDecomposition. ^Array2DRowRealMatrix square-apache-m)]
{::L (.getL lud)
::U (.getU lud)
::LU-permutation (.getP lud)
::determinant (.getDeterminant lud)})))
(s/fdef lu-decomposition-with-determinant
:args (s/cat :square-apache-m ::square-apache-matrix)
:ret (s/keys :req [::L ::U ::LU-permutation ::determinant]))
(s/def ::eigenvectorsT ::square-apache-matrix)
(s/def ::eigenvalues-matrix ::apache-matrix)
(s/def ::eigenvectors ::square-apache-matrix)
(s/def ::eigenvalues (s/nilable ::vector/vector))
(defn eigen-decomposition
"Computes the Eigendecomposition of a diagonalisable matrix.
Returns a map containing:
`::eigenvectorsT` -- square Apache Commons matrix with each column containing
the eigenvectors
`::eigenvalues-matrix` -- Apache Commons matrix whose diagonal elements are
the eigenvalues, if they exist
`::eigenvalues` -- vector of real parts of eigenvalues (nil if imaginary
parts exist)
`::eigenvectors` -- square Apache Commons matrix with each row containing the
eigenvectors
`square-apache-m-finite` = `eigenvectorsT` × `eigenvalues-matrix` ×
(inverse `eigenvectorsT`)."
[square-apache-m-finite]
(if (zero? (rows square-apache-m-finite))
{::eigenvectorsT square-apache-m-finite
::eigenvalues-matrix square-apache-m-finite
::eigenvalues []
::eigenvectors square-apache-m-finite}
(try (let [r (EigenDecomposition. ^Array2DRowRealMatrix square-apache-m-finite)
eigenvalues (when-not (.hasComplexEigenvalues r)
(vec (.getRealEigenvalues r)))]
{::eigenvectorsT (.getV r)
::eigenvalues-matrix (.getD r)
::eigenvalues eigenvalues
::eigenvectors (.getVT r)})
(catch Exception e
{::anomalies/message (.getMessage e)
::anomalies/fn (var eigen-decomposition)
::anomalies/category ::anomalies/third-party}))))
(s/fdef eigen-decomposition
:args (s/cat :square-apache-m-finite ::square-apache-matrix-finite)
:ret (s/or :anomaly ::anomalies/anomaly
:eigen (s/keys :req [::eigenvectorsT ::eigenvalues-matrix
::eigenvalues ::eigenvectors])))
(s/def ::cholesky-L ::lower-triangular-apache-matrix)
(s/def ::cholesky-LT ::upper-triangular-apache-matrix)
(defn cholesky-decomposition
"Computes the Cholesky decomposition of a positive definite Apache Commons
matrix. Returns a map of two Apache Commons matrices `::cholesky-L` and
`::choleskyLT`. This is the Cholesky square root of a matrix, 'L' and 'LT'
such that `positive-definite-apache-m` = L × LT. Note that
`positive-definite-apache-m` must be positive semidefinite for this to exist,
but [[cholesky-decomposition]] requires strict positivity."
[positive-definite-apache-m]
(if (zero? (rows positive-definite-apache-m))
{::cholesky-L positive-definite-apache-m ::cholesky-LT positive-definite-apache-m}
(try (let [r (CholeskyDecomposition. ^Array2DRowRealMatrix positive-definite-apache-m)]
{::cholesky-L (.getL r)
::cholesky-LT (.getLT r)})
(catch Exception e
{::anomalies/message (.getMessage e)
::anomalies/fn (var cholesky-decomposition)
::anomalies/category ::anomalies/third-party}))))
(s/fdef cholesky-decomposition
:args (s/cat :positive-definite-apache-m ::positive-definite-apache-matrix-finite)
:ret (s/or :anomaly ::anomalies/anomaly
:res (s/keys :req [::cholesky-L ::cholesky-LT])))
(s/def ::rectangular-root ::apache-matrix)
(defn rectangular-cholesky-decomposition
"Calculates the rectangular Cholesky decomposition of a positive semidefinite
Apache Commons matrix. The rectangular Cholesky decomposition of a real
`positive-semidefinite-apache-m` consists of a `rectangular-root` matrix with
the same number of rows such that `positive-semidefinite-apache-m` is almost
equal to `rectangular-root` × (transpose `rectangular-root`), depending on a