-
Notifications
You must be signed in to change notification settings - Fork 292
/
core.clj
2987 lines (2362 loc) · 90.9 KB
/
core.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
;;; core.clj -- Core functions built on the CERN Colt Library
;; by David Edgar Liebke http://incanter.org
;; March 11, 2009
;; Copyright (c) David Edgar Liebke, 2009. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.htincanter.at the root of this
;; distribution. By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license. You must not
;; remove this notice, or any other, from this software.
;; CHANGE LOG
;; March 11, 2009: First version
(ns ^{:doc "This is the core numerics library for Incanter.
It provides functions for vector- and matrix-based
mathematical operations and the core data manipulation
functions for Incanter.
This library is built on core.matrix (https://github.com/mikera/core.matrix)
and Parallel Colt
(http://sites.google.com/site/piotrwendykier/software/parallelcolt)
an extension of the Colt numerics library
(http://acs.lbl.gov/~hoschek/colt/).
"
:author "David Edgar Liebke"}
incanter.core
(:refer-clojure :exclude [update])
(:use [incanter internal]
[incanter.infix :only (infix-to-prefix defop)]
[clojure.set :only (difference)]
[clojure.pprint :only (print-table)])
(:require [clojure.core.matrix :as m])
(:require [clojure.core.matrix.dataset :as ds])
(:require [clojure.core.matrix.linear :as l])
(:import (clojure.core.matrix.impl.dataset DataSet)
(cern.jet.math.tdouble DoubleArithmetic)
(cern.jet.stat.tdouble Gamma)
(javax.swing JTable JScrollPane JFrame)
(java.util Vector)))
(def ^{:dynamic true
:doc "This variable is bound to a dataset when the with-data macro is used.
functions like $ and $where can use $data as a default argument."}
$data nil)
(declare to-list to-vector vectorize dataset col-names to-matrix bind-rows)
(defn set-current-implementation [imp]
"Sets current matrix implementation"
(m/set-current-implementation imp))
(set-current-implementation :vectorz)
(defn matrix
"
Returns a matrix or vector, in a valid core.matrix format. You can use the slices function to
access the rows.
Equivalent to R's matrix function.
Examples:
(def A (matrix [[1 2 3] [4 5 6] [7 8 9]])) ; produces a 3x3 matrix
(def A2 (matrix [1 2 3 4 5 6 7 8 9] 3)) ; produces the same 3x3 matrix
(def B (matrix [1 2 3 4 5 6 7 8 9])) ; produces a vector with 9 elements
; since (plus row1 row2) adds the two rows element-by-element
(reduce plus A) ; produces the sums of the columns
; and since (sum row1) sums the elements of the row
(map sum A) ; produces the sums of the rows
"
([data]
(m/matrix data))
([data ncol]
(m/matrix (partition ncol (vectorize data))))
([init-val rows cols]
(m/compute-matrix [rows cols] (constantly init-val))))
(defn matrix?
"Tests if obj is core.matrix matrix"
([obj] (m/matrix? obj)))
(defn vec?
"Tests if obj is core.matrix vector"
([obj] (m/vec? obj)))
(defn ^:deprecated dataset?
"Determines if obj is of type clojure.core.matrix.impl.dataset.Dataset.
Deprecated. Please use clojure.core.matrix.dataset/dataset? instead."
([obj] (ds/dataset? obj)))
(defn dispatch
"Dispatch function for multimethods"
([obj]
(cond
(and (map? obj) (:charts obj)) ::multi-chart
(dataset? obj) ::dataset
(matrix? obj) ::matrix
(vec? obj) ::vector
(coll? obj) ::coll
(.contains (str (type obj)) "processing.core.PApplet") :sketch
:else (type obj))))
(defmulti nrow
"Returns the number of rows in the given matrix. Equivalent to R's nrow function."
dispatch)
(defmethod nrow ::dataset
[ds] (m/row-count ds))
(defmethod nrow ::matrix
[m] (m/row-count m))
(defmethod nrow ::vector
[v] (m/row-count v))
(defmethod nrow ::coll
[c] (count c))
(defmulti ncol
"Returns the number of columns in the given matrix. Equivalent to R's ncol function."
dispatch)
(defmethod ncol ::dataset
[ds] (m/column-count ds))
(defmethod ncol ::matrix
[m] (m/column-count m))
(defmethod ncol ::coll
[c] 1)
(defmethod ncol ::vector
[v] 1)
(defn ^:deprecated dim
"Returns a vector with the number of rows and columns of the given matrix.
Deprecated. Please use clojure.core.matrix/dimensionality instead.
"
([mat]
(m/shape mat)))
(defn ^:deprecated identity-matrix
"
Returns an n-by-n identity matrix.
Examples:
(identity-matrix 4)
Deprecated. Please use clojure.core.matrix/identity-matrix instead.
"
([^Integer n] (m/identity-matrix n)))
(defn ^:deprecated diag
"If given a matrix, diag returns a sequence of its diagonal elements.
If given a sequence, it returns a matrix with the sequence's elements
on its diagonal. Equivalent to R's diag function.
Examples:
(diag [1 2 3 4]) ; produces diagonal matrix
(def A (matrix [[1 2 3]
[4 5 6]
[7 8 9]]))
(diag A) ;; returns elements on main diagonal
Deprecated. Please use clojure.core.matrix/main-diagonal for getting elements on main diagonal
and clojure.core.matrix/diagonal-matrix for creating diagonal matrix instead.
"
[m]
(if (== 2 (m/dimensionality m))
(matrix (m/main-diagonal m))
(m/diagonal-matrix m)))
(defn ^:deprecated trans
"
Returns the transpose of the given matrix. Equivalent to R's t function
Examples:
(def A (matrix [[1 2 3]
[4 5 6]
[7 8 9]]))
(trans A)
Deprecated. Please use clojure.core.matrix/transpose instead.
"
([mat]
(m/transpose mat)))
(defn- except-for
"
Returns a lazy list of numbers ranging from 0 to n, except for the given exceptions.
Examples:
(except-for 10 3)
(except-for 10 [5 7])
"
([n exceptions]
(let [except (if (coll? exceptions) exceptions [exceptions])]
(for [i (range n) :when (reduce #(and %1 %2) (map #(not= i %) except))] i))))
(defmulti sel
"
Returns an element or subset of the given matrix, dataset, or list.
If the column or row is specified as an atomic object (index or name), then
the result will be returned as a list (only values from selected column or row).
Argument:
a matrix object, dataset, or list.
Options:
:rows (default true)
returns all rows by default, can pass a row index or sequence of row indices
:cols (default true)
returns all columns by default, can pass a column index or sequence of column indices
:except-rows (default nil) can pass a row index or sequence of row indices to exclude
:except-cols (default nil) can pass a column index or sequence of column indices to exclude
:filter-fn (default nil)
a function can be provided to filter the rows of the matrix
Examples:
(use 'incanter.datasets)
(def iris (to-matrix (get-dataset :iris)))
(sel iris 0 0) ; first element
(sel iris :rows 0 :cols 0) ; also first element
(sel iris :cols 0) ; first column of all rows
(sel iris :cols [0 2]) ; first and third column of all rows
(sel iris :rows (range 10) :cols (range 2)) ; first two columns of the first 10 rows
(sel iris :rows (range 10)) ; all columns of the first 10 rows
;; exclude rows or columns
(sel iris :except-rows (range 10)) ; all columns of all but the first 10 rows
(sel iris :except-cols 1) ; all columns except the second
;; return only the first 10 even rows
(sel iris :rows (range 10) :filter-fn #(even? (int (nth % 0))))
;; select rows where distance (third column) is greater than 50
(sel iris :filter #(> (nth % 2) 4))
;; examples with datasets
(use 'incanter.datasets)
(def us-arrests (get-dataset :us-arrests))
(sel us-arrests :cols \"State\")
(sel us-arrests :cols :State)
(sel us-arrests :cols [\"State\" \"Murder\"])
(sel us-arrests :cols [:State :Murder])
"
(fn [mat & options] [(dispatch mat) (keyword? (first options))]))
(defmethod sel [nil false] [])
(defmethod sel [nil true] [])
(defmethod sel [java.util.List false]
([^java.util.List lst rows cols]
(sel lst :rows rows :cols cols)))
(defmethod sel [java.util.List true]
([^java.util.List lst & {:keys [rows cols except-rows except-cols filter-fn all]}]
(let [rows (cond
rows rows
except-rows (except-for (nrow lst) except-rows)
:else true)
cols (cond
cols cols
except-cols (except-for (nrow (first lst)) except-cols)
all all
:else true)
lst (if (nil? filter-fn) lst (filter filter-fn lst))
all-rows? (or (true? rows) (= rows :all) all)
all-cols? (or (true? cols) (= cols :all) (= all :all))]
(cond
(and (number? rows) (number? cols))
(nth (nth lst rows) cols)
(and all-rows? (coll? cols))
(map (fn [r] (map #(nth r %) cols)) lst)
(and all-rows? (number? cols))
(map #(nth % cols) lst)
(and (coll? rows) (number? cols))
(map #(nth % cols)
(map #(nth lst %) rows))
(and (coll? rows) all-cols?)
(map #(nth lst %) rows)
(and (number? rows) all-cols?)
(nth lst rows)
(and (number? rows) (coll? cols))
(map #(nth (nth lst rows) %) cols)
(and (coll? rows) (coll? cols))
(map (fn [r] (map #(nth r %) cols))
(map #(nth lst %) rows))
(and all-rows? all-cols?)
lst))))
(defmethod sel [::matrix false]
([mat rows columns]
(matrix (m/select mat rows columns))))
(defmethod sel [::matrix true]
([mat & {:keys [rows cols except-rows except-cols filter-fn all]}]
(let [rows (cond
rows rows
except-rows (except-for (m/row-count mat) except-rows)
all all
:else :all)
cols (cond
cols cols
except-cols (except-for (m/column-count mat) except-cols)
all all
:else :all)
mat (if (nil? filter-fn) mat (apply bind-rows (filter filter-fn mat)))]
(matrix (m/select mat rows cols)))))
(prefer-method sel [::matrix true] [java.util.List true])
(prefer-method sel [::matrix false] [java.util.List false])
(defmethod sel :default
([mat & more]
(apply sel (matrix mat) more)))
(defn bind-rows
"
Returns the matrix resulting from concatenating the given matrices
and/or sequences by their rows. Equivalent to R's rbind.
Examples:
(def A (matrix [[1 2 3]
[4 5 6]
[7 8 9]]))
(def B (matrix [[10 11 12]
[13 14 15]]))
(bind-rows A B)
(bind-rows [1 2 3 4] [5 6 7 8])
"
([& args]
(->>
args
(map
#(let [dm (m/dimensionality %)]
(case dm
1 (m/row-matrix %)
2 %
(throw (RuntimeException.
(str "Can't bind rows to array of dimensionality " dm))))))
(apply m/join))))
(defn bind-columns
"
Returns the matrix resulting from concatenating the given matrices
and/or sequences by their columns. Equivalent to R's cbind.
Examples:
(def A (matrix [[1 2 3]
[4 5 6]
[7 8 9]]))
(def B (matrix [10 11 12]))
(bind-columns A B)
(bind-columns [1 2 3 4] [5 6 7 8])
"
[& args]
(->>
args
(map #(let [dm (m/dimensionality %)]
(case dm
1 (m/column-matrix %)
2 %
(throw (RuntimeException.
(str "Can't bind columns to array of dimensionality " dm))))))
(apply m/join-along 1)))
(defn inner-product [& args]
"Deprecated. Please use clojure.core.matrix/inner-product instead."
(apply m/inner-product args))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; MATH FUNCTIONS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn ^:deprecated plus
"
Performs element-by-element addition on multiple matrices, sequences
and/or numbers. Equivalent to R's + operator.
Examples:
(def A (matrix [[1 2 3]
[4 5 6]
[7 8 9]]))
(plus A A A)
(plus A 2)
(plus 2 A)
(plus [1 2 3] [1 2 3])
(plus [1 2 3] 2)
(plus 2 [1 2 3])
Deprecated. Please use clojure.core.matrix/add or
clojure.core.matrix.operators/+ instead.
"
[& args] (apply m/add args))
(defn ^:deprecated minus
"
Performs element-by-element subtraction on multiple matrices, sequences
and/or numbers. If only a single argument is provided, returns the negative
of the given matrix, sequence, or number. Equivalent to R's - operator.
Examples:
(def A (matrix [[1 2 3]
[4 5 6]
[7 8 9]]))
(minus A)
(minus A A A)
(minus A 2)
(minus 2 A)
(minus [1 2 3] [1 2 3])
(minus [1 2 3] 2)
(minus 2 [1 2 3])
(minus [1 2 3])
Deprecated. Please use clojure.core.matrix/sub or
clojure.core.matrix.operators/- instead.
"
[& args] (apply m/sub args))
(defn ^:deprecated mult
"
Performs element-by-element multiplication on multiple matrices, sequences
and/or numbers. Equivalent to R's * operator.
Examples:
(def A (matrix [[1 2 3]
[4 5 6]
[7 8 9]]))
(mult A A A)
(mult A 2)
(mult 2 A)
(mult [1 2 3] [1 2 3])
(mult [1 2 3] 2)
(mult 2 [1 2 3])
Deprecated. Please use clojure.core.matrix/emul or
clojure.core.matrix.operators/* instead.
"
[& args]
(apply m/emul args))
(defn ^:deprecated div
"
Performs element-by-element division on multiple matrices, sequences
and/or numbers. Equivalent to R's / operator.
Examples:
(def A (matrix [[1 2 3]
[4 5 6]
[7 8 9]]))
(div A A A)
(div A 2)
(div 2 A)
(div [1 2 3] [1 2 3])
(div [1 2 3] 2)
(div 2 [1 2 3])
(div [1 2 3]) ; returns [1 1/2 13]
Deprecated. Please use clojure.core.matrix/div or
clojure.core.matrix.operators// instead.
"
([& args] (apply m/div args)))
(defn safe-div
"DivideByZero safe alternative to clojures / function,
detects divide by zero and returns Infinity, -Infinity or NaN as appropriate.
"
([x] (safe-div 1 x))
([x y]
(m/emap
#(try (m/div %1 %2)
(catch ArithmeticException _
(cond (> %1 0) Double/POSITIVE_INFINITY
(zero? %1) Double/NaN
:else Double/NEGATIVE_INFINITY)))
x y))
([x y & more]
(reduce safe-div (safe-div x y) more)))
(defn- mapping-helper [func args]
(reduce (fn [A B]
(cond
(number? A) (func A B)
(dataset? A) (dataset (col-names A)
(mapping-helper func (list (m/rows A) B)))
(or (matrix? A)
(m/vec? A)) (m/emap #(func %1 B) A)
(and (coll? A) (coll? (first A)))
(map (fn [a] (map #(func %1 B) a)) A)
(coll? A) (map #(func %1 B) A)))
args))
(defn pow ;; TODO use jblas and fix meta
"
This is an element-by-element exponent function, raising the first argument
by the exponents in the remaining arguments. Equivalent to R's ^ operator.
"
[& args]
(mapping-helper #(Math/pow %1 %2) args))
(defn atan2 ;; TODO fix meta
"
Returns the atan2 of the elements in the given matrices, sequences or numbers.
Equivalent to R's atan2 function.
"
[& args]
(mapping-helper #(Math/atan2 %1 %2) args))
(defn sqrt
"
Returns the square-root of the elements in the given matrix, sequence or number.
Equivalent to R's sqrt function.
"
[A] (m/sqrt A))
(defn sq
"
Returns the square of the elements in the given matrix, sequence or number.
Equivalent to R's sq function.
"
([A] (mult A A)))
(defn log
"
Returns the natural log of the elements in the given matrix, sequence or number.
Equivalent to R's log function.
"
([A] (m/log A)))
(defn log2
"
Returns the log base 2 of the elements in the given matrix, sequence or number.
Equivalent to R's log2 function.
"
([A] (transform-with A #(/ (Math/log %) (Math/log 2)))))
(defn log10
"
Returns the log base 10 of the elements in the given matrix, sequence or number.
Equivalent to R's log10 function.
"
([A] (m/log10 A)))
(defn exp
"
Returns the exponential of the elements in the given matrix, sequence or number.
Equivalent to R's exp function."
([A] (m/exp A)))
(defn abs
"
Returns the absolute value of the elements in the given matrix, sequence or number.
Equivalent to R's abs function.
"
([A] (m/abs A)))
(defn sin
"
Returns the sine of the elements in the given matrix, sequence or number.
Equivalent to R's sin function.
"
([A] (m/sin A)))
(defn asin
"
Returns the arc sine of the elements in the given matrix, sequence or number.
Equivalent to R's asin function.
"
([A] (m/asin A)))
(defn cos
"
Returns the cosine of the elements in the given matrix, sequence or number.
Equivalent to R's cos function.
"
([A] (m/cos A)))
(defn acos
"
Returns the arc cosine of the elements in the given matrix, sequence or number.
Equivalent to R's acos function."
([A] (m/acos A)))
(defn tan
"
Returns the tangent of the elements in the given matrix, sequence or number.
Equivalent to R's tan function.
"
([A] (m/tan A)))
(defn atan
"
Returns the arc tangent of the elements in the given matrix, sequence or number.
Equivalent to R's atan function.
"
([A] (m/atan A)))
(defn factorial
"
Returns the factorial of k (k must be a positive integer). Equivalent to R's
factorial function.
Examples:
(factorial 6)
References:
http://incanter.org/docs/parallelcolt/api/cern/jet/math/tdouble/DoubleArithmetic.html
http://en.wikipedia.org/wiki/Factorial
"
([^Integer k] {:pre [(and (number? k) (not (neg? k)))]} (DoubleArithmetic/factorial k)))
(defn choose
"
Returns number of k-combinations (each of size k) from a set S with
n elements (size n), which is the binomial coefficient (also known
as the 'choose function') [wikipedia]
choose = n!/(k!(n - k)!)
Equivalent to R's choose function.
Examples:
(choose 25 6) ; => 177,100
References:
http://incanter.org/docs/parallelcolt/api/cern/jet/math/tdouble/DoubleArithmetic.html
http://en.wikipedia.org/wiki/Combination
"
([n k] (DoubleArithmetic/binomial (double n) (long k))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; MATRIX FUNCTIONS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti to-list
"Returns a list-of-vectors if the given matrix is two-dimensional
and a flat list if the matrix is one-dimensional."
dispatch)
(defmethod to-list ::matrix
([mat]
(cond
(or (m/row-matrix? mat)
(m/column-matrix? mat)
(= (m/dimensionality mat) 1)) (apply list (m/to-vector mat))
(m/scalar? mat) mat
:default (apply list (map #(apply list %)
(m/to-nested-vectors mat))))))
(defmethod to-list ::dataset
[data]
(->> (m/rows data) (map #(apply list %)) (apply list)))
(defmethod to-list ::vector
[data]
(apply list data))
(defmethod to-list nil [s] nil)
(defmethod to-list :default [s] s)
(defn ^:deprecated copy
"
Deprecated. Please use clojure.core.matrix/clone instead.
"
([mat]
(m/clone mat)))
(defn to-vect
"Converts an array into nested Clojure vectors.
Returns a vector-of-vectors if the given matrix is two-dimensional
and a flat vector if the matrix is one-dimensional. This is a bit
slower than the to-list function
"
[a]
(m/to-nested-vectors a))
(defn ^:deprecated mmult
"
Returns the matrix resulting from the matrix multiplication of the
the given arguments. Equivalent to R's %*% operator.
Examples:
(def A (matrix [[1 2 3]
[4 5 6]
[7 8 9]]))
(mmult A (trans A))
(mmult A (trans A) A)
References:
http://en.wikipedia.org/wiki/Matrix_multiplication
Deprecated. Please use clojure.core.matrix/mmul instead.
"
([& args]
(apply m/mmul args)))
(defn kronecker
"
Returns the Kronecker product of the given arguments.
Examples:
(def x (matrix (range 6) 2))
(def y (matrix (range 4) 2))
(kronecker 4 x)
(kronecker x 4)
(kronecker x y)
"
([& args]
(reduce (fn [A B]
(let [adims (long (m/dimensionality A))
bdims (long (m/dimensionality B))]
(cond
(and (== adims 0) (== bdims 0)) (* A B)
(and (== adims 1) (== bdims 1))
(-> (for [a B b B] (* a b))
(matrix))
(and (== adims 1) (== bdims 0)) (mult A B)
(and (== adims 2) (== bdims 2))
(apply bind-rows
(for [i (range (nrow A))]
(apply bind-columns
(for [j (range (ncol A))]
(mult (sel A i j) B)))))
(and (== adims 2) (== bdims 0)) (recur A (matrix [[B]]))
(and (== adims 2) (== bdims 1)) (recur A (m/column-matrix B)))))
args)))
(defn ^:deprecated solve
"
Returns a matrix solution if A is square, least squares solution otherwise.
Equivalent to R's solve function.
Examples:
(solve (matrix [[2 0 0] [0 2 0] [0 0 2]]))
References:
http://en.wikipedia.org/wiki/Matrix_inverse
Deprecated. Please use clojure.core.matrix/inverse for matrix inverse,
clojure.core.matrix.linear/solve for solving system of linear equations and
clojure.core.matrix.linear/least-squares for least-squares solution.
"
([A B]
(l/solve A B))
([A]
(l/solve A)))
(defn ^:deprecated det
"
Returns the determinant of the given matrix. Equivalent
to R's det function.
References:
http://en.wikipedia.org/wiki/LU_decomposition
Deprecated. Please use clojure.core.matrix/det instead.
"
([mat]
(m/det mat)))
(defn ^:deprecated trace
"
Returns the trace of the given matrix.
References:
http://en.wikipedia.org/wiki/Matrix_trace
Deprecated. Please use clojure.core.matrix/trace instead.
"
[mat]
(m/trace mat))
(defn vectorize
"
Returns the vectorization (i.e. vec) of the given matrix.
The vectorization of an m-by-n matrix A, denoted by vec(A)
is the m*n-by-1 column vector obtain by stacking the columns
of the matrix A on top of one another.
For instance:
(= (vectorize (matrix [[a b] [c d]])) (matrix [a c b d]))
Examples:
(def A (matrix [[1 2] [3 4]]))
(vectorize A)
References:
http://en.wikipedia.org/wiki/Vectorization_(mathematics)
"
([mat]
(m/to-vector mat)))
(defn half-vectorize
"
Returns the half-vectorization (i.e. vech) of the given matrix.
The half-vectorization, vech(A), of a symmetric nxn matrix A
is the n(n+1)/2 x 1 column vector obtained by vectorizing only
the upper triangular part of A.
For instance:
(= (half-vectorize (matrix [[a b] [b d]])) (matrix [a b d]))
Examples:
(def A (matrix [[1 2] [2 4]]))
(half-vectorize A)
References:
http://en.wikipedia.org/wiki/Vectorization_(mathematics)
"
([mat]
(for [j (range (nrow mat)) i (range j (nrow mat))] (sel mat i j))))
(defn ^:deprecated sum-of-squares
"
Returns the sum-of-squares of the given sequence.
Deprecated. Please use clojure.core.matrix/length-squared instead.
"
([x]
(if (or (m/row-matrix? x)
(m/column-matrix? x))
(m/length-squared (m/to-vector x))
(m/length-squared x))))
(defn ^:deprecated sum
"
Returns the sum of the given sequence.
Deprecated. Please use clojure.core.matrix/esum instead.
"
([x]
(m/esum x)))
(defn prod
"Returns the product of the given sequence."
([x]
(m/ereduce *' x)))
(defn cumulative-sum
"
Returns a sequence of cumulative sum for the given collection. For instance
The first value equals the first value of the argument, the second value is
the sum of the first two arguments, the third is the sum of the first three
arguments, etc.
Examples:
(use 'incanter.core)
(cumulative-sum (range 100))
"
([coll]
(loop [in-coll (rest coll)
cumu-sum [(first coll)]
cumu-val (first coll)]
(if (empty? in-coll)
cumu-sum
(let [cv (+ cumu-val (first in-coll))]
(recur (rest in-coll) (conj cumu-sum cv) cv))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; MATRIX DECOMPOSITION FUNCTIONS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn ^:deprecated decomp-cholesky
"
Returns the Cholesky decomposition of the given matrix. Equivalent to R's
chol function.
Returns:
a map containing two matrices with the keys [:L :L*] such that
Such that:
M = L.L*
Where
- M must be a hermitian, positive definite matrix
- L is a lower triangular matrix
- L* is the conjugate transpose of L
If :return parameter is specified in options map, it returns only specified keys.
Examples:
(use '(incanter core stats charts datasets))
;; load the iris dataset
(def iris (to-matrix (get-dataset :iris)))
;; take the Cholesky decomposition of the correlation matrix of the iris data.
(let [{:keys [L L*]} (decomp-cholesky (correlation iris))])
(let [{:keys [L*]} (decomp-cholesky (correlation iris {:return [:L*]}))])
References:
http://en.wikipedia.org/wiki/Cholesky_decomposition
Deprecated. Please use clojure.core.matrix.linear/cholesky instead.
"
([mat] (l/cholesky mat))
([mat options] (l/cholesky mat options)))
(defn ^:deprecated decomp-svd
"
Returns the Singular Value Decomposition (SVD) of the given matrix. Equivalent to
R's svd function.
If :return parameter is specified in options map, it returns only specified keys.
By default returns a map containing:
:S -- the diagonal matrix of singular values S (the diagonal in vector form)
:U -- the left singular vectors U
:V* -- the right singular vectors V
Examples:
(use 'incanter.core)
(def foo (matrix (range 9) 3))
(let [{:keys [U S V*]} (decomp-svd foo)] ....)
(let [{:keys [S]} (decomp-svd foo {:return [:S]})] ....)
References:
http://en.wikipedia.org/wiki/Singular_value_decomposition
Deprecated. Please use clojure.core.matrix.linear/svd instead.
"
([mat]
(l/svd mat))
([mat options]
(l/svd mat options)))
(defn ^:deprecated decomp-eigenvalue
"
Returns a map containing matrices for each of the the keys [:Q :rA :iA] such that:
M = Q.A.Q-1