forked from numcl/numcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.lisp
140 lines (117 loc) · 3.84 KB
/
example.lisp
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
(ql:quickload :numcl)
(in-package :numcl)
;; initialization
(zeros 5)
(zeros '(5 5))
(zeros 5 :type 'fixnum)
(ones 5)
(arange 10)
;; reshaping
(reshape (arange 10) '(2 5))
;; arange with negative steps
(arange -10 10 3)
(arange 10 -10 -3)
;; concatenation, stacking, unstacking
(concatenate (list (zeros 10) (ones 10)))
(concatenate (list (reshape (zeros 10) '(2 5))
(reshape (ones 10) '(2 5))))
(concatenate (list (reshape (zeros 10) '(2 5))
(reshape (ones 10) '(2 5)))
:axis 1)
(stack (list (reshape (zeros 10) '(2 5))
(reshape (ones 10) '(2 5))))
(stack (list (reshape (zeros 10) '(2 5))
(reshape (ones 10) '(2 5)))
:axis 1)
(stack (list (reshape (zeros 10) '(2 5))
(reshape (ones 10) '(2 5)))
:axis 2)
(unstack (reshape (ones 10) '(2 5)))
(unstack (reshape (ones 10) '(2 5)) :axis 1)
;; array slice access
(defparameter *a* (reshape (arange 100) '(4 5 5)))
*a*
(aref *a* 0)
(aref *a* '(0 1))
(aref *a* 0 '(0 2) 0)
(setf (aref *a* 2 '(0 2) 1) (full 2 100))
*a*
;; broadcasting
(numcl:+ (arange 5) (reshape (arange 3) '(3 1)))
(numcl:* (arange 5) (reshape (arange 3) '(3 1)))
(defparameter *a* (arange 10))
(defparameter *b* (reshape (asarray '(1 5 2)) '(t 1)))
*b*
(* *b* *a*)
(sin (* *b* *a*))
(exp (* *b* *a*))
(log (1+ (* *b* *a*)))
(+ 3 (log (1+ (* *b* *a*))))
(arange -5 5 2)
(log (arange -5 5 2))
(arange 10.0d0)
(+ 1 (arange 10.0d0))
(+ (arange 10.0d0) (arange 10.0))
(arange 3.3)
(arange 3.3 5.5 1.1)
(arange 3.3 5.5 1.1d0)
(arange 3.3d0 5.5d0 1.1d0)
(arange 3.3d0 11/2 1.1d0)
(arange 2 1 1)
(arange 1 2 5)
(arange 5 :type 'double-float)
(arange 2 5 :type 'double-float)
(arange 2 5 2 :type 'double-float)
(arange 5)
(type-of *)
(arange 5.0)
(type-of *)
(arange 5.0d0)
(type-of *)
(+ (arange 5) 3)
(type-of *)
(+ (arange 5) 3.0)
(type-of *)
(+ (arange 5.0) 3)
(type-of *)
(/ (arange 5) (arange 1 6))
(type-of *)
(= (arange 5) (reshape (arange 1 6) '(t 1))) ;; #2A((0 1 0 0 0) (0 0 1 0 0) (0 0 0 1 0) (0 0 0 0 1) (0 0 0 0 0))
(< (arange 5) (reshape (arange 1 6) '(t 1))) ;; #2A((1 0 0 0 0) (1 1 0 0 0) (1 1 1 0 0) (1 1 1 1 0) (1 1 1 1 1))
(logand (arange 5) (arange 1 6))
;; type inference on logand
(logand (uniform 3 7 3) (reshape (uniform 3 7 3) '(t 1)))
(logand (uniform 3 7 3) (reshape (uniform 3 7 3) '(t 1)))
(logand (uniform -3 7 3) (reshape (uniform 3 7 3) '(t 1)))
;; (logand minus minus) is guaranteed to be minus, therefore this is a signed-integer
(logand (uniform -3 7 3) (reshape (uniform -3 7 3) '(t 1)))
;; reduction
(sum (arange 5))
(sum (reshape (arange 125) '(5 5 5))) ; sum all elements
(sum (reshape (arange 125) '(5 5 5)) :axes '()) ; does nothing
(sum (reshape (arange 125) '(5 5 5)) :axes '(0))
(sum (reshape (arange 125) '(5 5 5)) :axes '(0 1))
(sum (reshape (arange 125) '(5 5 5)) :axes '(0 1 2)) ; sum all elements, again
(prod (reshape (1+ (arange 16)) '(4 4)))
(prod (reshape (1+ (arange 16)) '(4 4)) :axes '(0))
(amax (reshape (arange 16) '(4 4)))
(amax (reshape (arange 16) '(4 4)) :axes '(0))
(amax (reshape (arange 16) '(4 4)) :axes '(1))
(amin (reshape (arange 16) '(4 4)))
(amin (reshape (arange 16) '(4 4)) :axes '(0))
(amin (reshape (arange 16) '(4 4)) :axes '(1))
(let ((b (uniform 0 10 '(5 5))))
(print b)
(print (nonzero b))
(print (where b #'zerop))
(print (argwhere b #'zerop))
(print (apply #'mapcar (alexandria:curry #'aref b) (where b #'zerop)))
(print (apply #'mapcar (alexandria:curry #'aref b) (where b #'plusp)))
(print (apply #'mapcar (alexandria:curry #'aref b) (where b #'evenp)))
(print (take b (where b #'zerop)))
(print (take b (where b #'plusp)))
(print (take b (where b #'evenp)))
;; (print (mapcar (alexandria:curry #'aref b) (where b #'zerop)))
;; (print (mapcar (alexandria:curry #'aref b) (where b #'plusp)))
;; (print (mapcar (alexandria:curry #'aref b) (where b #'evenp)))
)