From b23e52f532f803112fe00ace3ae6844ecc551e26 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Tue, 2 Oct 2012 17:53:24 -0700 Subject: [PATCH] math.matrics: Add some combinators and indexing words to math.matrices. --- basis/math/matrices/matrices.factor | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/basis/math/matrices/matrices.factor b/basis/math/matrices/matrices.factor index 0b8f0a13b6..3f594c87d2 100644 --- a/basis/math/matrices/matrices.factor +++ b/basis/math/matrices/matrices.factor @@ -168,3 +168,31 @@ IN: math.matrices : outer ( u v -- m ) [ n*v ] curry map ; + +: row ( n m -- col ) + nth ; inline + +: rows ( seq m -- cols ) + '[ _ row ] map ; inline + +: col ( n m -- col ) + swap '[ _ swap nth ] map ; inline + +: cols ( seq m -- cols ) + '[ _ col ] map ; inline + +: matrix-map ( m quot -- ) + '[ _ map ] map ; inline + +: column-map ( m quot -- seq ) + [ [ first length iota ] keep ] dip '[ _ col @ ] map ; inline + +: cartesian-indices ( n -- matrix ) + iota dup cartesian-product ; inline + +: cartesian-matrix-map ( m quot -- m' ) + [ [ first length cartesian-indices ] keep ] dip + '[ _ @ ] matrix-map ; inline + +: cartesian-matrix-column-map ( m quot -- m' ) + [ cols first2 ] prepose cartesian-matrix-map ; inline