tensors: cleanup using, and make a few float math operations faster.
parent
c196f9f022
commit
085436a156
|
@ -1,11 +1,9 @@
|
||||||
! Copyright (C) 2019 HMC Clinic.
|
! Copyright (C) 2019 HMC Clinic.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
|
|
||||||
USING: accessors alien.c-types alien.data arrays
|
USING: accessors alien.data arrays grouping kernel locals math
|
||||||
concurrency.combinators grouping kernel locals math.functions
|
math.functions math.ranges multi-methods sequences
|
||||||
math.ranges math.statistics math multi-methods quotations sequences
|
sequences.extras sequences.private specialized-arrays typed ;
|
||||||
sequences.extras sequences.private specialized-arrays
|
|
||||||
tensors.tensor-slice typed ;
|
|
||||||
|
|
||||||
QUALIFIED-WITH: alien.c-types c
|
QUALIFIED-WITH: alien.c-types c
|
||||||
SPECIALIZED-ARRAY: c:float
|
SPECIALIZED-ARRAY: c:float
|
||||||
|
@ -113,20 +111,20 @@ METHOD: t- { number tensor } [ >float ] dip [ - ] with t-uop ;
|
||||||
! Multiply a tensor with either another tensor or a scalar
|
! Multiply a tensor with either another tensor or a scalar
|
||||||
multi-methods:GENERIC: t* ( x y -- tensor )
|
multi-methods:GENERIC: t* ( x y -- tensor )
|
||||||
METHOD: t* { tensor tensor } [ * ] t-bop ;
|
METHOD: t* { tensor tensor } [ * ] t-bop ;
|
||||||
METHOD: t* { tensor number } [ * ] curry t-uop ;
|
METHOD: t* { tensor number } >float [ * ] curry t-uop ;
|
||||||
METHOD: t* { number tensor } [ >float ] dip [ * ] with t-uop ;
|
METHOD: t* { number tensor } [ >float ] dip [ * ] with t-uop ;
|
||||||
|
|
||||||
! Divide two tensors or a tensor and a scalar
|
! Divide two tensors or a tensor and a scalar
|
||||||
multi-methods:GENERIC: t/ ( x y -- tensor )
|
multi-methods:GENERIC: t/ ( x y -- tensor )
|
||||||
METHOD: t/ { tensor tensor } [ / ] t-bop ;
|
METHOD: t/ { tensor tensor } [ / ] t-bop ;
|
||||||
METHOD: t/ { tensor number } [ / ] curry t-uop ;
|
METHOD: t/ { tensor number } >float [ / ] curry t-uop ;
|
||||||
METHOD: t/ { number tensor } [ / ] with t-uop ;
|
METHOD: t/ { number tensor } [ >float ] dip [ / ] with t-uop ;
|
||||||
|
|
||||||
! Divide two tensors or a tensor and a scalar
|
! Divide two tensors or a tensor and a scalar
|
||||||
multi-methods:GENERIC: t% ( x y -- tensor )
|
multi-methods:GENERIC: t% ( x y -- tensor )
|
||||||
METHOD: t% { tensor tensor } [ mod ] t-bop ;
|
METHOD: t% { tensor tensor } [ mod ] t-bop ;
|
||||||
METHOD: t% { tensor number } [ mod ] curry t-uop ;
|
METHOD: t% { tensor number } >float [ mod ] curry t-uop ;
|
||||||
METHOD: t% { number tensor } [ mod ] with t-uop ;
|
METHOD: t% { number tensor } [ >float ] dip [ mod ] with t-uop ;
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
|
@ -152,9 +150,9 @@ METHOD: t% { number tensor } [ mod ] with t-uop ;
|
||||||
! Perform matrix multiplication muliplying an
|
! Perform matrix multiplication muliplying an
|
||||||
! mxn matrix with a nxp matrix
|
! mxn matrix with a nxp matrix
|
||||||
TYPED:: 2d-matmul ( vec1: float-array start1: fixnum
|
TYPED:: 2d-matmul ( vec1: float-array start1: fixnum
|
||||||
vec2: float-array start2: fixnum
|
vec2: float-array start2: fixnum
|
||||||
res: float-array start3: fixnum
|
res: float-array start3: fixnum
|
||||||
m: fixnum n: fixnum p: fixnum -- )
|
m: fixnum n: fixnum p: fixnum -- )
|
||||||
! For each element in the range, we want to compute the dot product of the
|
! For each element in the range, we want to compute the dot product of the
|
||||||
! corresponding row and column
|
! corresponding row and column
|
||||||
m [ :> i
|
m [ :> i
|
||||||
|
@ -212,7 +210,8 @@ TYPED:: matmul ( tensor1: tensor tensor2: tensor -- tensor3: tensor )
|
||||||
: ind-mults ( shape -- seq )
|
: ind-mults ( shape -- seq )
|
||||||
<reversed> 1 swap [ swap [ * ] keep ] map nip ;
|
<reversed> 1 swap [ swap [ * ] keep ] map nip ;
|
||||||
|
|
||||||
! helper for transpose: given shape, flat index, & mults for the shape, gives nd index
|
! helper for transpose: given shape, flat index, & mults for
|
||||||
|
! the shape, gives nd index
|
||||||
: transpose-index ( i shape -- seq )
|
: transpose-index ( i shape -- seq )
|
||||||
<reversed> [ /mod ] map reverse nip ;
|
<reversed> [ /mod ] map reverse nip ;
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
Loading…
Reference in New Issue