diff --git a/basis/math/vectors/vectors-docs.factor b/basis/math/vectors/vectors-docs.factor index 920fbd81e6..05477952f6 100644 --- a/basis/math/vectors/vectors-docs.factor +++ b/basis/math/vectors/vectors-docs.factor @@ -43,6 +43,7 @@ ARTICLE: "math-vectors-arithmetic" "Vector arithmetic" norm norm-sq normalize + p-norm } "Comparing entire vectors:" { $subsections @@ -456,6 +457,10 @@ HELP: norm { $values { "v" "a sequence of numbers" } { "x" "a non-negative real number" } } { $description "Computes the length of a mathematical vector." } ; +HELP: p-norm +{ $values { "v" "a sequence of numbers" } { "p" "a positive real number" } { "x" "a non-negative real number" } } +{ $description "Computes the length of a mathematical vector in " { $snippet "L^p" } " space." } ; + HELP: normalize { $values { "u" "a sequence of numbers, not all zero" } { "v" "a sequence of numbers" } } { $description "Outputs a vector with the same direction as " { $snippet "u" } " but length 1." } ; diff --git a/basis/math/vectors/vectors-tests.factor b/basis/math/vectors/vectors-tests.factor index 3f6a34cea3..826bbfb68c 100644 --- a/basis/math/vectors/vectors-tests.factor +++ b/basis/math/vectors/vectors-tests.factor @@ -1,6 +1,6 @@ IN: math.vectors.tests USING: math.vectors tools.test kernel specialized-arrays compiler -kernel.private alien.c-types ; +kernel.private alien.c-types math.functions ; SPECIALIZED-ARRAY: int [ { 1 2 3 } ] [ 1/2 { 2 4 6 } n*v ] unit-test @@ -11,9 +11,12 @@ SPECIALIZED-ARRAY: int [ 5 ] [ { 1 2 } norm-sq ] unit-test [ 13 ] [ { 2 3 } norm-sq ] unit-test -[ { 1.0 2.5 } ] [ { 1.0 2.5 } { 2.5 1.0 } 0.0 vnlerp ] unit-test -[ { 2.5 1.0 } ] [ { 1.0 2.5 } { 2.5 1.0 } 1.0 vnlerp ] unit-test -[ { 1.75 1.75 } ] [ { 1.0 2.5 } { 2.5 1.0 } 0.5 vnlerp ] unit-test +{ t } [ { 1 2 3 } [ norm ] [ 2 p-norm ] bi = ] unit-test +{ t } [ { 1 2 3 } 3 p-norm 3.301927248894626 1e-10 ~ ] unit-test + +[ { 1.0 2.5 } ] [ { 1.0 2.5 } { 2.5 1.0 } 0.0 vnlerp ] unit-test +[ { 2.5 1.0 } ] [ { 1.0 2.5 } { 2.5 1.0 } 1.0 vnlerp ] unit-test +[ { 1.75 1.75 } ] [ { 1.0 2.5 } { 2.5 1.0 } 0.5 vnlerp ] unit-test [ { 1.75 2.125 } ] [ { 1.0 2.5 } { 2.5 1.0 } { 0.5 0.25 } vlerp ] unit-test diff --git a/basis/math/vectors/vectors.factor b/basis/math/vectors/vectors.factor index 3f933c10b5..93db261545 100644 --- a/basis/math/vectors/vectors.factor +++ b/basis/math/vectors/vectors.factor @@ -218,6 +218,9 @@ M: object norm-sq [ absq ] [ + ] map-reduce ; inline : norm ( v -- x ) norm-sq sqrt ; inline +: p-norm ( v p -- x ) + [ [ [ abs ] dip ^ ] curry map-sum ] keep recip ^ ; inline + : normalize ( u -- v ) dup norm v/n ; inline GENERIC: distance ( u v -- x )