! Copyright (C) 2012 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
-USING: accessors arrays combinators.short-circuit constructors
-fry grouping kernel math math.vectors sequences sequences.deep
-math.order parser assocs math.combinatorics ;
+USING: accessors arrays assocs combinators.short-circuit fry
+grouping kernel math math.functions math.order math.vectors
+parser prettyprint.custom sequences sequences.deep ;
IN: arrays.shaped
: flat? ( array -- ? ) [ sequence? ] any? not ; inline
ERROR: shape-mismatch shaped0 shaped1 ;
+DEFER: >shaped-array
+
: check-shape ( shaped-array shaped-array -- shaped-array shaped-array )
+ [ >shaped-array ] bi@
2dup [ shape>> ] bi@
sequence= [ shape-mismatch ] unless ;
M: sequence >col-array
[ flatten ] [ shape ] bi <col-array> ;
-: shaped+ ( a b -- c )
- check-shape
- [ [ underlying>> ] bi@ v+ ]
- [ drop shape>> clone ] 2bi shaped-array boa ;
+: shaped-unary-op ( shaped quot -- )
+ [ >shaped-array ] dip
+ [ underlying>> ] prepose
+ [ shape>> clone ] bi shaped-array boa ; inline
+
+: shaped-shaped-binary-op ( shaped0 shaped1 quot -- c )
+ [ check-shape ] dip
+ [ [ underlying>> ] bi@ ] prepose
+ [ drop shape>> clone ] 2bi shaped-array boa ; inline
+
+: shaped+ ( a b -- c ) [ v+ ] shaped-shaped-binary-op ;
+: shaped- ( a b -- c ) [ v- ] shaped-shaped-binary-op ;
+: shaped*. ( a b -- c ) [ v* ] shaped-shaped-binary-op ;
+
+: shaped*n ( a b -- c ) [ v*n ] curry shaped-unary-op ;
+: n*shaped ( a b -- c ) swap shaped*n ;
+
+: shaped-cos ( a -- b ) [ [ cos ] map ] shaped-unary-op ;
+: shaped-sin ( a -- b ) [ [ sin ] map ] shaped-unary-op ;
: shaped-array>array ( shaped-array -- array )
[ underlying>> ] [ shape>> ] bi
] if ;
: reshape ( shaped-array shape -- array )
- check-underlying-shape >>shape ;
+ check-underlying-shape
+ [ >shaped-array ] dip >>shape ;
: shaped-like ( shaped-array shape -- array )
[ underlying>> clone ] dip <shaped-array> ;
SYNTAX: sa{ \ } [ >shaped-array ] parse-literal ;
-USE: prettyprint.custom
! M: row-array pprint* shaped-array>array pprint* ;
! M: col-array pprint* shaped-array>array flip pprint* ;
M: shaped-array pprint-delims drop \ sa{ \ } ;