Skip to content

Instantly share code, notes, and snippets.

@belisarius222
Created March 30, 2023 15:16
Show Gist options
  • Save belisarius222/7402ab3965ebe79eebac832583029ba9 to your computer and use it in GitHub Desktop.
Save belisarius222/7402ab3965ebe79eebac832583029ba9 to your computer and use it in GitHub Desktop.
ray: ndarray in hoon
|%
+$ ray :: $ray: n-dimensional array
$: meta :: descriptor
data=@ux :: data, row-major order
==
+$ meta :: $meta: metadata for a $ray
$: shape=(list @) :: list of dimension lengths
kind :: element descriptor
==
+$ kind :: $kind: metadata for $ray elements
$: =bloq :: logarithm of bitwidth
aura=@tas :: name of data type
==
+$ baum :: $baum: n-dimensional array as a nested list
$@ @ :: single item
(lest baum) :: nonempty list of children, in row-major order
--
|%
++ print
|= a=ray
~> %slog.1^(to-tank a)
~
++ slog |=(a=ray (^slog (to-tank a) ~))
++ to-tank :: TODO nest dimensions
|= a=ray
^- tank
:+ %rose [" " "[" "]"]
%+ turn (ravel a)
|= i=@
^- tank
(sell [%atom aura.a ~] i)
::
++ add
|= [a=ray b=ray]
^- ray
?> =(-.a -.b)
:- -.a
=/ ali (ravel a)
=/ bob (ravel b)
%+ rep bloq.a
=| res=(list @)
%- flop
|- ^+ res
?@ ali res
?@ bob res
=/ sum ((add-scalar [bloq aura]:a) i.ali i.bob)
$(ali t.ali, bob t.bob, res [sum res])
::
++ add-scalar
|= [=bloq aura=@tas]
^- $-([@ @] @)
?+ aura ~|(aura !!)
?(%u %ub %ux %ud %uv %uw) ~(sum fe bloq)
%r
?+ bloq !!
%7 ~(add rq %n)
%6 ~(add rd %n)
%5 ~(add rs %n)
%4 ~(add rh %n)
==
::
:: TODO signed integers -- add new 2's complement aura?
==
:: +new: construct a $ray from metadata and a $baum of items
:: +items: deconstruct a $ray into a $baum of items
::
++ new
|= [=meta items=baum]
^- ray
[meta ((wrap bloq.meta (len meta)) (rep bloq.meta (ravel-baum items)))]
::
++ new-auto :: derive shape from input items
|= [[=bloq aura=@tas] =baum]
^- ray
!! :: TODO
::
++ get-item :: extract item at index .dex
|= [=ray dex=(list @)]
^- @ux
(cut bloq.ray [(get-bloq-offset -.ray dex) 1] data.ray)
::
++ set-item :: set item at index .dex to .val
|= [=ray dex=(list @) val=@]
^+ ray
:- -.ray
(sew bloq.ray [(get-bloq-offset -.ray dex) 1 val] data.ray)
::
++ get-bloq-offset :: get bloq offset of n-dimensional index
|= [=meta dex=(list @)]
^- @
+((get-item-number shape.meta dex))
::
++ get-item-number :: convert n-dimensional index to scalar index
|= [shape=(list @) dex=(list @)]
^- @
=. dex (flop dex)
=/ sap (flop shape)
=/ cof 1
=/ ret 0
|- ^+ ret
?~ sap ret
?~ dex !!
?> (lth i.dex i.sap)
%= $
sap t.sap
dex t.dex
cof (mul cof i.sap)
ret (^add ret (mul i.dex cof))
==
::
++ get-item-index
|= [shape=(list @) num=@]
^- (list @)
=/ len (roll shape mul)
=- (roll - ^add)
%+ turn shape
|= wid=@
(mod (div len wid) num)
::
++ ravel
|= a=ray
^- (list @)
=/ len (len -.a)
%+ turn (gulf 0 (dec len))
|= i=@
(get-item a (get-item-index shape.a i))
::
++ ravel-baum
|= =baum
^- (list @ux)
?@ baum [baum]~
%- zing
(turn baum ..$)
:: +len: number of elements in a $ray
:: +dim: number of dimensions in a $ray
::
++ len |=(m=meta (roll shape.m mul))
++ dim |=(m=meta (lent shape.m))
:: +wrap: add tag to an atom to preserve leading zeros
:: +unwrap: strip tag that preserves leading zeros
::
++ wrap
|= [=bloq len=@]
|= n=@
^- @ux
(^add (lsh [bloq len] 1) n)
::
++ unwrap
|= [=bloq len=@]
|= data=@
^- @ux
(end [bloq len] data)
--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment