math.vectors: adding p-norm.
parent
ffeaf448a7
commit
95b82eecfa
|
@ -43,6 +43,7 @@ ARTICLE: "math-vectors-arithmetic" "Vector arithmetic"
|
||||||
norm
|
norm
|
||||||
norm-sq
|
norm-sq
|
||||||
normalize
|
normalize
|
||||||
|
p-norm
|
||||||
}
|
}
|
||||||
"Comparing entire vectors:"
|
"Comparing entire vectors:"
|
||||||
{ $subsections
|
{ $subsections
|
||||||
|
@ -456,6 +457,10 @@ HELP: norm
|
||||||
{ $values { "v" "a sequence of numbers" } { "x" "a non-negative real number" } }
|
{ $values { "v" "a sequence of numbers" } { "x" "a non-negative real number" } }
|
||||||
{ $description "Computes the length of a mathematical vector." } ;
|
{ $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
|
HELP: normalize
|
||||||
{ $values { "u" "a sequence of numbers, not all zero" } { "v" "a sequence of numbers" } }
|
{ $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." } ;
|
{ $description "Outputs a vector with the same direction as " { $snippet "u" } " but length 1." } ;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
IN: math.vectors.tests
|
IN: math.vectors.tests
|
||||||
USING: math.vectors tools.test kernel specialized-arrays compiler
|
USING: math.vectors tools.test kernel specialized-arrays compiler
|
||||||
kernel.private alien.c-types ;
|
kernel.private alien.c-types math.functions ;
|
||||||
SPECIALIZED-ARRAY: int
|
SPECIALIZED-ARRAY: int
|
||||||
|
|
||||||
[ { 1 2 3 } ] [ 1/2 { 2 4 6 } n*v ] unit-test
|
[ { 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
|
[ 5 ] [ { 1 2 } norm-sq ] unit-test
|
||||||
[ 13 ] [ { 2 3 } 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
|
{ t } [ { 1 2 3 } [ norm ] [ 2 p-norm ] bi = ] unit-test
|
||||||
[ { 2.5 1.0 } ] [ { 1.0 2.5 } { 2.5 1.0 } 1.0 vnlerp ] unit-test
|
{ t } [ { 1 2 3 } 3 p-norm 3.301927248894626 1e-10 ~ ] unit-test
|
||||||
[ { 1.75 1.75 } ] [ { 1.0 2.5 } { 2.5 1.0 } 0.5 vnlerp ] 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
|
[ { 1.75 2.125 } ] [ { 1.0 2.5 } { 2.5 1.0 } { 0.5 0.25 } vlerp ] unit-test
|
||||||
|
|
||||||
|
|
|
@ -218,6 +218,9 @@ M: object norm-sq [ absq ] [ + ] map-reduce ; inline
|
||||||
|
|
||||||
: norm ( v -- x ) norm-sq sqrt ; 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
|
: normalize ( u -- v ) dup norm v/n ; inline
|
||||||
|
|
||||||
GENERIC: distance ( u v -- x )
|
GENERIC: distance ( u v -- x )
|
||||||
|
|
Loading…
Reference in New Issue