math.vectors: Implement infinity p-norm and special-case l1-norm and l2-norm.
parent
40992bd3b6
commit
6c9a6122de
|
@ -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 )
|
||||
|
|
Loading…
Reference in New Issue