Meanwhile I have been slogging away at two's-complement negative,
absolute value, and multiply. With the two's-complement interpretation,
you know a binary number is negative when its leading bit is 1. Here is
my two's-complement stuff, with multiplication last:
hc =: {.@#:@(,: 2 * |) NB. Raul's improved #: (hash colon)
hcinv =: ([: -/ [: #. (,: [: +: 1 {. ]))"1 NB. Henry Rich
Table =: 2 2 2 2 $ 0 0,0 1,0 1,1 0,0 1,1 0,1 0,1 1 NB. sum of
three bits
stack =: ,.&.|: NB. stack x over y
hv =: (0 {:: <"1) :: ] NB. return head vector
op =: ] stack~ (2 {. [) , Table {~ [: < (2 {. [) , 2 { [: hv ]
ba =: 0 ,~ 0 ,.~ 0 ,.~ ,. NB. build argument, see tcpl
tcpl =: ([: }: [: {:"1 [: op/ ba)"1 NB. TWO'S-COMPLEMENT PLUS
Tbln =: 2 2 2 $ 0 0,1 1,1 1,0 1 NB. table for tcng, see below
ban =: 0 ,~ 0 ,.~ 0 ,.~ ] NB. build argument, see tcng
opn =: ] stack~ (0 { [) , Tbln {~ [: < (0 { [) ,~ 2 { [: hv ]
tcng =: (1 {"1 [: }: [: opn/ ban)"1 NB. TWO'S-COMPLEMENT NEGATIVE
tcav =: ]`tcng@.{."1 NB. TWO'S-COMPLEMENT ABSOLUTE VALUE
NB. base two multiply by a standard unit vector like 0 0 1 0
bme =: ([ ( (0 #~ 0 { ]) , [ , 0 #~ 1 { ]) [ (>:@] , [: <: #@[ - ])
1 i.~ ])"1
diag =: (* [: = [: i. #)"1 NB. diagonal matrix from diagonal
NB. BASE TWO MULTIPLY
bmul =: ([: tcpl/ [ bme [: ( (1 = +/)"1@] # ] ) [: diag ])"1
eor =: ((2 2$0 1 1 0) {~ [: < ,)"0 NB. exclusive or
NB. TWO'S-COMPLEMENT MULTIPLY
tcml =: (bmul&tcav`([: tcng bmul&tcav)@.(eor&{.))"1
hc 2 _3
0 1 0
1 0 1
0 1 0 tcml 1 0 1
1 1 1 0 1 0
hcinv 1 1 1 0 1 0
_6
On 12/13/2011 9:21 AM, Raul Miller wrote:
> After some thought, I am in favor of this version (I think first
> proposed by Henry):
>
> ((* *<&0) ,. #:) i:3
> _1 0 1
> _1 1 0
> _1 1 1
> 0 0 0
> 0 0 1
> 0 1 0
> 0 1 1
> #. ((* *<&0) ,. #:) i:3
> _3 _2 _1 0 1 2 3
>
> Note also:
> 2 | ((* *>&0) ,. #:) i:3
> 0 0 1
> 0 1 0
> 0 1 1
> 0 0 0
> 1 0 1
> 1 1 0
> 1 1 1
>
> That said this could be further "improved" by making the #: result
> follow the p. result pattern (least significant bit first):
>
> (|.@#: ,. * *<&0) i:3
> 1 1 _1
> 1 0 _1
> 0 1 _1
> 0 0 0
> 1 1 0
> 1 0 0
> 0 1 0
> (|.@#: , * *<&0)"0 i:3
> 1 0 _1
> 0 1 _1
> 1 _1 0
> 0 0 0
> 1 0 0
> 0 1 0
> 1 1 0
> #.@|. :(#. |.)"1 (|.@#: ,. * *<&0) i:3
> _1 _3 _2 0 3 1 2
> #.@|. :(#. |.)"1 (|.@#: , * *<&0)"0 i:3
> _3 _2 _1 0 1 2 3
> 2 p.~ (|.@#: , * *<&0)"0 i:3
> _3 _2 _1 0 1 2 3
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm