Skip to content

Commit e6e1e61

Browse files
committed
work to lazify /\
1 parent 1818bee commit e6e1e61

File tree

6 files changed

+107
-62
lines changed

6 files changed

+107
-62
lines changed

aplesque/forms.lisp

+16-18
Original file line numberDiff line numberDiff line change
@@ -81,31 +81,29 @@
8181
(odiv-size (reduce #'* (if dims (loop :for d :in dims :for dx :from 0
8282
:when (> dx axis) :collect d :when (= dx axis)
8383
:collect (aref c-degrees (1- (length degrees))))
84-
;; (loop :for d :across c-degrees :for dx :from 0
85-
;; :collect (abs d))
86-
))))
84+
(loop :for d :across c-degrees :for dx :from 0
85+
:collect (abs d))))))
8786
;; (print (list :eee dims idiv-size odiv-size c-degrees))
8887
(lambda (i)
8988
;; in compress-mode: degrees must = length of axis,
9089
;; zeroes are omitted from output, negatives add zeroes
9190
;; otherwise: zeroes pass through, negatives add zeroes, degrees>0 must = length of axis
92-
;; (print (list :ll i))
91+
;; (print (list :ll i odiv-size section-size))
9392
;; (setq dims (list 3))
9493
;; (setq odiv-size 6)
95-
(if t ; dims
96-
(multiple-value-bind (oseg remainder) (floor i odiv-size)
97-
(multiple-value-bind (oseg-index element-index) (floor remainder section-size)
98-
;; dimension index
99-
(let ((dx (loop :for d :across c-degrees :for di :from 0
100-
:when (> d oseg-index) :return di)))
101-
;; (print (list :dd dx oseg oseg-index section-size odiv-size))
102-
(if (< 0 (aref degrees dx))
103-
(+ element-index (* oseg idiv-size)
104-
(* section-size (if (not positive-indices)
105-
dx (or (loop :for p :across positive-indices
106-
:for px :from 0 :when (= p dx)
107-
:return px)
108-
1)))))))))))))
94+
(multiple-value-bind (oseg remainder) (floor i (max 1 odiv-size))
95+
(multiple-value-bind (oseg-index element-index) (floor remainder section-size)
96+
;; dimension index
97+
(let ((dx (loop :for d :across c-degrees :for di :from 0
98+
:when (> d oseg-index) :return di)))
99+
;; (print (list :dd dx oseg oseg-index section-size odiv-size))
100+
(if (< 0 (aref degrees dx))
101+
(+ element-index (* oseg idiv-size)
102+
(* section-size (if (not positive-indices)
103+
dx (or (loop :for p :across positive-indices
104+
:for px :from 0 :when (= p dx)
105+
:return px)
106+
1))))))))))))
109107

110108
(defun indexer-turn (axis idims &optional degrees)
111109
"Return indices of an array rotated as with the [⌽ rotate] or [⊖ rotate first] functions."

libraries/dfns/array/array.apl

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ display ← { ⎕IO←0 ⍝ Boxed display of array.
154154
deco{type open ,axes } type and axes vector
155155
axes{(-2⍴⍴)1+×} array axis types
156156
open{(1)} exposure of null axes
157-
trim{(~1 1=' ')/} removal of extra blank cols
157+
trim{(~1 1=' ')/} removal of extra blank cols
158158
char{:'' ('¯',⎕D)'#~'} simple scalar type
159159
type{{(1=)'+'},char¨} simple array type
160160
line{(49=⎕DT 1)' -'} underline for atom

spec.lisp

+28-20
Original file line numberDiff line numberDiff line change
@@ -991,14 +991,13 @@
991991
(is "2 1 1 2 3 3 2⍉3 2 3 4 2 4 3⍴⍳1728"
992992
#3A(((1 16) (602 617) (1203 1218)) ((385 400) (986 1001) (1587 1602))))))
993993
(/ (has :title "Replicate")
994-
(dyadic (expand-array nil t index-origin axes)
995-
;; (funcall (lambda (n io &optional axes)
996-
;; (lambda (omega alpha)
997-
;; (make-instance 'vader-expand :base omega :argument alpha :index-origin io
998-
;; :inverse t :axis (or (first axes) :last))))
999-
;; nil index-origin axes)
1000-
)
1001-
(meta (primary :axes axes :implicit-args (index-origin))
994+
(dyadic ;; (expand-array nil t index-origin axes)
995+
(funcall (lambda (n io &optional axes)
996+
(lambda (omega alpha)
997+
(make-instance 'vader-expand :base omega :argument alpha :index-origin io
998+
:inverse t :axis (or (first axes) :last))))
999+
nil index-origin axes))
1000+
(meta (primary :axes axes :implicit-args (index-origin) :virtual-support t)
10021001
(dyadic :on-axis :last
10031002
:inverse (λωαχ (if (is-unitary omega)
10041003
;; TODO: this inverse functionality is probably not complete
@@ -1040,8 +1039,13 @@
10401039
(is "⍴2 3/[2]0 2 0⍴0" #(0 5 0))
10411040
(is "⍴0/2 3 4⍴⍳9" #(2 3 0))))
10421041
(⌿ (has :title "Replicate First")
1043-
(dyadic (expand-array t t index-origin axes))
1044-
(meta (primary :axes axes :implicit-args (index-origin))
1042+
(dyadic ;; (expand-array t t index-origin axes)
1043+
(funcall (lambda (n io &optional axes)
1044+
(lambda (omega alpha)
1045+
(make-instance 'vader-expand :base omega :argument alpha :index-origin io
1046+
:inverse t :axis (or (first axes) io))))
1047+
nil index-origin axes))
1048+
(meta (primary :axes axes :implicit-args (index-origin) :virtual-support t)
10451049
(dyadic :on-axis :first
10461050
:inverse (λωαχ (if (is-unitary omega)
10471051
;; TODO: this inverse functionality is probably not complete
@@ -1059,14 +1063,13 @@
10591063
(1 0 0 3 3 3 0 0 0 0 5 5 5 5 5)
10601064
(1 0 0 3 3 3 0 0 0 0 5 5 5 5 5)))))
10611065
(\\ (has :title "Expand")
1062-
(dyadic (expand-array nil nil index-origin axes)
1063-
;; (funcall (lambda (n io &optional axes)
1064-
;; (lambda (omega alpha)
1065-
;; (make-instance 'vader-expand :base omega :argument alpha :index-origin io
1066-
;; :axis (or (first axes) :last))))
1067-
;; nil index-origin axes)
1068-
)
1069-
(meta (primary :axes axes :implicit-args (index-origin))
1066+
(dyadic ;; (expand-array nil nil index-origin axes)
1067+
(funcall (lambda (n io &optional axes)
1068+
(lambda (omega alpha)
1069+
(make-instance 'vader-expand :base omega :argument alpha :index-origin io
1070+
:axis (or (first axes) :last))))
1071+
nil index-origin axes))
1072+
(meta (primary :axes axes :implicit-args (index-origin) :virtual-support t)
10701073
(dyadic :on-axis :last))
10711074
(tests (is "4\\2" #(2 2 2 2))
10721075
(is "3\\7" #(7 7 7))
@@ -1092,8 +1095,13 @@
10921095
(is "⍴0 0 0\\0 0⍴0" #(0 3))
10931096
(is "0 0 0 0\\3 0⍴0" #2A((0 0 0 0) (0 0 0 0) (0 0 0 0)))))
10941097
(⍀ (has :title "Expand First")
1095-
(dyadic (expand-array t nil index-origin axes))
1096-
(meta (primary :axes axes :implicit-args (index-origin))
1098+
(dyadic ;; (expand-array t nil index-origin axes)
1099+
(funcall (lambda (n io &optional axes)
1100+
(lambda (omega alpha)
1101+
(make-instance 'vader-expand :base omega :argument alpha :index-origin io
1102+
:axis (or (first axes) io))))
1103+
nil index-origin axes))
1104+
(meta (primary :axes axes :implicit-args (index-origin) :virtual-support t)
10971105
(dyadic :on-axis :first))
10981106
(tests (is "2⍀5" #(5 5))
10991107
(is "2⍀1" #*11)

varray/#varray.asd#

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
x;;;; varray.asd
2+
3+
(asdf:defsystem #:varray
4+
:description "Describe varray here"
5+
:author "Your Name <[email protected]>"
6+
:license "Specify license here"
7+
:version "0.0.1"
8+
:serial t
9+
:depends-on ("aplesque")
10+
:components ((:file "package")
11+
(:file "varray")))

varray/.#varray.asd

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

varray/varray.lisp

+50-23
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
"The prototype representation of an item is returned by the (apl-array-prototype) function."
3535
(if (and (arrayp item)
3636
(array-displacement item)
37+
(vectorp (array-displacement item))
3738
(listp (aref (array-displacement item) 0))
3839
(member :empty-array-prototype (aref (array-displacement item) 0)))
40+
;; if an empty array prototype has been stored, retrieve it
3941
(getf (aref (array-displacement item) 0) :empty-array-prototype)
4042
(apl-array-prototype item)))
4143

@@ -89,13 +91,18 @@
8991
(if (= 0 (array-rank array))
9092
array (if (= 0 (array-total-size array))
9193
(prototype-of array)
92-
(lambda (index) (row-major-aref array index)))))
94+
(lambda (index)
95+
(row-major-aref array index)))))
9396

9497
(defmethod render ((item t))
9598
item)
9699

97100
(defmethod render ((varray varray))
101+
;; (print :abc)
102+
;; (print (list :ss (shape-of varray)
103+
;; (prototype-of varray)))
98104
(let ((output-shape (shape-of varray))
105+
(prototype (prototype-of varray))
99106
(indexer (indexer-of varray)))
100107
(if output-shape
101108
(if (zerop (reduce #'* output-shape))
@@ -108,10 +115,13 @@
108115
(make-array (shape-of varray) :element-type (assign-element-type
109116
this-prototype)))))
110117
output))
111-
(let ((output (make-array (shape-of varray) :element-type (etype-of varray))))
118+
(let ((output (make-array (shape-of varray) :element-type (etype-of varray)
119+
)))
120+
;; (print (list :out output (etype-of varray)))
112121
(dotimes (i (array-total-size output))
113122
(let ((indexed (funcall indexer i)))
114-
(if indexed (setf (row-major-aref output i) indexed))))
123+
(if indexed (setf (row-major-aref output i) indexed)
124+
(setf (row-major-aref output i) prototype))))
115125
output))
116126
(funcall indexer 1))))
117127

@@ -137,7 +147,8 @@
137147
;; the default shape of a derived array is the same as its base array
138148
(defmethod prototype-of ((varray varray-derived))
139149
(if (varrayp (vader-base varray))
140-
(apl-array-prototype (funcall (indexer-of (vader-base varray)) 0))
150+
;; (apl-array-prototype (funcall (indexer-of (vader-base varray)) 0))
151+
(prototype-of (vader-base varray))
141152
(prototype-of (vader-base varray))))
142153

143154
(defmethod shape-of ((varray varray-derived))
@@ -240,6 +251,7 @@
240251
nil (:documentation "A reshaped array as from the [⍴ reshape] function."))
241252

242253
(defmethod prototype-of ((varray vader-reshape))
254+
(shape-of (vader-base varray)) ;; must get shape so that base array can be rendered
243255
(let ((indexer (indexer-of (vader-base varray))))
244256
;; TODO: remove-disclose when [⍴ shape] is virtually implemented
245257
(aplesque::make-empty-array (disclose (if (not (functionp indexer))
@@ -251,7 +263,7 @@
251263
(get-or-assign-shape
252264
varray (let ((arg (setf (vads-argument varray)
253265
(render (vads-argument varray)))))
254-
(if (vectorp arg)
266+
(if (typep arg 'sequence)
255267
(coerce arg 'list)
256268
(list arg)))))
257269

@@ -361,12 +373,16 @@
361373
(defclass vader-expand (varray-derived vad-on-axis vad-with-io vad-with-argument vad-invertable)
362374
nil (:documentation "An expanded (as from [\ expand]) or compressed (as from [/ compress]) array."))
363375

364-
(defmethod prototype-of ((varray vader-expand))
365-
(let ((indexer (indexer-of (vader-base varray))))
366-
;; TODO: remove-disclose when [⍴ shape] is virtually implemented
367-
(aplesque::make-empty-array (disclose (if (not (functionp indexer))
368-
indexer ;; ←← remove
369-
(funcall (indexer-of (vader-base varray)) 0))))))
376+
;; (defmethod prototype-of ((varray vader-expand))
377+
;; (call-next-method)
378+
;; ;; (prototype-of (vader-base varray))
379+
;; ;; (let ((indexer (indexer-of (vader-base varray))))
380+
;; ;; (print (list :vb (vader-base varray)))
381+
;; ;; ;; TODO: remove-disclose when [⍴ shape] is virtually implemented
382+
;; ;; (aplesque::make-empty-array (disclose (if (not (functionp indexer))
383+
;; ;; indexer ;; ←← remove
384+
;; ;; (funcall (indexer-of (vader-base varray)) 0)))))
385+
;; )
370386

371387
(defmethod shape-of ((varray vader-expand))
372388
"The shape of an expanded or compressed array."
@@ -377,18 +393,25 @@
377393
(degrees (setf (vads-argument varray)
378394
(funcall (lambda (i)
379395
(if (arrayp i)
380-
i (vector i)))
396+
(if (< 0 (array-rank i))
397+
i (vector (aref i)))
398+
(vector i)))
381399
(render (vads-argument varray)))))
382400
(base-shape (copy-list (shape-of (vader-base varray))))
383401
(base-rank (length base-shape))
384402
(is-inverse (vads-inverse varray))
385403
(axis (setf (vads-axis varray)
386-
(max 0 (- (if (eq :last (vads-axis varray))
387-
base-rank (vads-axis varray))
388-
(vads-io varray))))))
404+
(max 0 (if (eq :last (vads-axis varray))
405+
(1- base-rank)
406+
(- (vads-axis varray)
407+
(vads-io varray)))))))
389408

390409
;; (print (list :br degrees-count is-inverse base-shape base-rank axis (vads-axis varray)
391410
;; (vader-base varray)))
411+
412+
;; (print (list :va (vads-argument varray)))
413+
414+
;; (print (list :deg degrees))
392415

393416
(cond ((and base-shape (zerop (reduce #'* base-shape)))
394417
;; (print :ee)
@@ -434,7 +457,6 @@
434457
(append (butlast base-shape) (list 0)))
435458
((and (not base-shape)
436459
(not (arrayp degrees)))
437-
;; (print :gg)
438460
(setf (vads-argument varray) (list (abs degrees))))
439461
((and is-inverse base-shape degrees-count (< 1 degrees-count)
440462
(nth axis base-shape)
@@ -474,11 +496,18 @@
474496
:collect (if (/= index axis) dim (* 1 ex-dim)))))))))
475497

476498
(defmethod indexer-of ((varray vader-expand))
477-
(let ((base-indexer (indexer-of (vader-base varray)))
478-
(indexer (indexer-expand (coerce (vads-argument varray) 'vector)
479-
(shape-of (vader-base varray))
480-
(vads-axis varray)
481-
(vads-inverse varray))))
499+
;; (Print (list :eeo (vads-argument varray)
500+
;; (vader-base varray)
501+
;; (shape-of (vader-base varray))
502+
;; (vads-axis varray)
503+
;; (vads-inverse varray)))
504+
;; (print (vads-argument varray))
505+
(let* ((arg-vector (coerce (vads-argument varray) 'vector))
506+
(base-indexer (indexer-of (vader-base varray)))
507+
(indexer (if (< 0 (length arg-vector))
508+
(indexer-expand arg-vector (shape-of (vader-base varray))
509+
(vads-axis varray)
510+
(vads-inverse varray)))))
482511
;; (PRINT (LIST :ss (shape-of varray)))
483512
(lambda (index)
484513
;; (print (list :iin base-indexer (funcall indexer index) indexer))
@@ -488,8 +517,6 @@
488517
(let ((indexed (funcall indexer index)))
489518
(if indexed (funcall base-indexer indexed)))))))
490519

491-
;; (defclass vader-meta-scalar-pass (varray-derived) nil)
492-
493520
(defclass vader-turn (varray-derived vad-on-axis vad-with-io vad-with-argument)
494521
nil (:documentation "A rotated array as from the [⌽ rotate] function."))
495522

0 commit comments

Comments
 (0)