From 085436a15648e3383424ae1b4524e9b5c8eaa401 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 30 Jan 2020 13:39:27 -0800 Subject: [PATCH] tensors: cleanup using, and make a few float math operations faster. --- extra/tensors/tensors.factor | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/extra/tensors/tensors.factor b/extra/tensors/tensors.factor index dd44ca72f5..e44ac4ebff 100644 --- a/extra/tensors/tensors.factor +++ b/extra/tensors/tensors.factor @@ -1,11 +1,9 @@ ! Copyright (C) 2019 HMC Clinic. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors alien.c-types alien.data arrays -concurrency.combinators grouping kernel locals math.functions -math.ranges math.statistics math multi-methods quotations sequences -sequences.extras sequences.private specialized-arrays -tensors.tensor-slice typed ; +USING: accessors alien.data arrays grouping kernel locals math +math.functions math.ranges multi-methods sequences +sequences.extras sequences.private specialized-arrays typed ; QUALIFIED-WITH: alien.c-types c 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 multi-methods:GENERIC: t* ( x y -- tensor ) 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 ; ! Divide two tensors or a tensor and a scalar multi-methods:GENERIC: t/ ( x y -- tensor ) METHOD: t/ { tensor tensor } [ / ] t-bop ; -METHOD: t/ { tensor number } [ / ] curry t-uop ; -METHOD: t/ { number tensor } [ / ] with t-uop ; +METHOD: t/ { tensor number } >float [ / ] curry t-uop ; +METHOD: t/ { number tensor } [ >float ] dip [ / ] with t-uop ; ! Divide two tensors or a tensor and a scalar multi-methods:GENERIC: t% ( x y -- tensor ) METHOD: t% { tensor tensor } [ mod ] t-bop ; -METHOD: t% { tensor number } [ mod ] curry t-uop ; -METHOD: t% { number tensor } [ mod ] with t-uop ; +METHOD: t% { tensor number } >float [ mod ] curry t-uop ; +METHOD: t% { number tensor } [ >float ] dip [ mod ] with t-uop ; i @@ -212,7 +210,8 @@ TYPED:: matmul ( tensor1: tensor tensor2: tensor -- tensor3: tensor ) : ind-mults ( shape -- seq ) 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 ) [ /mod ] map reverse nip ; PRIVATE>