-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathplotter-arrays.lisp
136 lines (102 loc) · 3.64 KB
/
plotter-arrays.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
(in-package :plotter)
;; ------------------------------------------
;; generalized operators to accommodate <carrays> and others
;;
;; -------------------------------------------------------------------
(defmethod coerce-to-vector ((lst list))
(coerce lst 'vector))
(defmethod coerce-to-vector ((v vector))
v)
(defmethod coerce-to-vector ((a array))
(make-array (array-total-size a)
:element-type (array-element-type a)
:displaced-to a))
(defmethod coerce-to-vector ((cv c-arrays:<carray>))
(coerce-to-vector (c-arrays:convert-to-lisp-object cv)))
;;---------
(defmethod length-of (arg)
(length arg))
(defmethod length-of ((arg vector))
;; Careful here! We are using extensible vectors in some places. The
;; ARRAY-TOTAL-SIZE may not be the same as the actual occupancy of
;; the vectors.
(length arg))
(defmethod length-of ((arg array))
(array-total-size arg))
(defmethod length-of ((arg ca:<carray>))
(ca:carray-total-size arg))
;;---------
(defmethod vmax-of (arg)
(vmax arg))
(defmethod vmax-of ((arg vector))
(loop for x across arg maximize x))
(defmethod vmax-of ((arg array))
(loop for ix from 0 below (array-total-size arg)
maximize (row-major-aref arg ix)))
(defmethod vmax-of ((arg ca:<carray>))
(loop for ix from 0 below (ca:carray-total-size arg)
maximize (ca:row-major-caref arg ix)))
;;---------
(defmethod vmin-of ((arg sequence))
(vmin arg))
(defmethod vmin-of ((arg vector))
(loop for x across arg minimize x))
(defmethod vmin-of ((arg array))
(loop for ix from 0 below (array-total-size arg)
minimize (row-major-aref arg ix)))
(defmethod vmin-of ((arg ca:<carray>))
(loop for ix from 0 below (ca:carray-total-size arg)
minimize (ca:row-major-caref arg ix)))
;;---------
(defun vextrema-of (arg)
(vextrema (coerce-to-vector arg)))
;;---------
(defmethod array-total-size-of (arg)
(array-total-size arg))
(defmethod array-total-size-of ((arg vector))
(length arg))
(defmethod array-total-size-of ((arg ca:<carray>))
(ca:carray-total-size arg))
;;---------
(defmethod array-dimension-of (arg n)
(array-dimension arg n))
(defmethod array-dimension-of ((arg ca:<carray>) n)
(ca:carray-dimension arg n))
;;---------
(defmethod aref-of (arg &rest indices)
(apply #'aref arg indices))
(defmethod aref-of ((arg ca:<carray>) &rest indices)
(apply #'ca:caref arg indices))
;;---------
(defmethod row-major-aref-of (arg ix)
(row-major-aref arg ix))
(defmethod row-major-aref-of ((arg ca:<carray>) ix)
(ca:row-major-caref arg ix))
;;---------
(defmethod subseq-of (arg start &optional end)
(subseq arg start end))
(defmethod subseq-of ((arg vector) start &optional end)
(subseq arg start end))
(defmethod subseq-of ((arg array) start &optional end)
(let* ((limit (array-total-size arg))
(nel (- (or end limit) start))
(ans (make-array nel :element-type (array-element-type arg))))
(loop for ix from start below (or end limit)
for jx from 0
do
(setf (aref ans jx) (row-major-aref arg ix)))
ans))
(defmethod subseq-of ((arg ca:<carray>) start &optional end)
(let* ((limit (ca:carray-total-size arg))
(nel (- (or end limit) start))
(ans (make-array nel
:element-type
(cond ((ca:is-float-array arg) 'single-float)
((ca:is-double-array arg) 'double-float)
(t 'bignum))
)))
(loop for ix from start below (or end limit)
for jx from 0
do
(setf (aref ans jx) (ca:caref arg ix)))
ans))