math.vectors: Implement infinity p-norm and special-case l1-norm and l2-norm.

db4
Doug Coleman 2012-08-30 16:47:45 -07:00
parent 40992bd3b6
commit 6c9a6122de
1 changed files with 11 additions and 1 deletions

View File

@ -225,11 +225,21 @@ M: object h. [ conjugate * ] [ + ] 2map-reduce ; inline
GENERIC: norm-sq ( v -- x )
M: object norm-sq [ absq ] [ + ] map-reduce ; inline
: l1-norm ( v -- x ) [ abs ] map-sum ; inline
: norm ( v -- x ) norm-sq sqrt ; inline
: p-norm ( v p -- x )
: p-norm-default ( v p -- x )
[ [ [ abs ] dip ^ ] curry map-sum ] keep recip ^ ; inline
: p-norm ( v p -- x )
{
{ [ dup 1 = ] [ drop l1-norm ] }
{ [ dup 2 = ] [ drop norm ] }
{ [ dup fp-infinity? ] [ drop supremum ] }
[ p-norm-default ]
} cond ;
: normalize ( u -- v ) dup norm v/n ; inline
GENERIC: distance ( u v -- x )