$nl
"In this vocabulary's documentation, " { $snippet "m" } " and " { $snippet "matrix" } " are the conventional names used for a given matrix object. " { $snippet "m" } " may also refer to a number."
$nl
-"The " { $vocab-link "math.matrices.extras" } "vocabulary implements extensions to this one."
+"The " { $vocab-link "math.matrices.extras" } " vocabulary implements extensions to this one."
$nl
"Matrices are classified their mathematical properties, and by predicate words:"
$nl
zero-matrix?
zero-square-matrix?
null-matrix?
-
}
"There are many ways to create 2-dimensional matrices:"
}
"By-element mathematical operations on a matrix:"
-{ $subsections mneg m+n m-n m*n m/n n+m n-m n*m n/m }
+{ $subsections matrix-normalize mneg m+n m-n m*n m/n n+m n-m n*m n/m }
"By-element mathematical operations of two matricess:"
{ $subsections m+ m- m* m/ m~ }
anti-diagonal
}
-"The following matrix norms are provided in the ๐โ vector space; these words are equivalent to โฅ๏ฝฅโฅโ for " { $snippet "p = 1, 2, โ, n" } ", respectively:"
+"The following matrix norms are provided in the ๐โ and " { $snippet "L^p,q" } " vector spaces; these words are equivalent to โฅ๏ฝฅโฅโ and โฅ๏ฝฅโฅ^p,q for " { $snippet "p = 1, 2, โ, โ" } ", and " { $snippet "p, q โ โ" } ", respectively:"
{ $subsections
- m-1norm
- m-infinity-norm
- frobenius-norm
- matrix-p-norm-entrywise
+ matrix-l1-norm
+ matrix-l2-norm
+ matrix-l-infinity-norm
matrix-p-norm
+ matrix-p-q-norm
+}
+"For readability, user code should prefer the available generic versions of the above, from " { $vocab-link "math.vectors" } ", which are optimized the same:"
+{ $subsections
+ l1-norm l2-norm l-infinity-norm p-norm
} ;
! PREDICATE CLASSES
}
} ;
-HELP: m-1norm
+{ l2-norm frobenius-norm hilbert-schmidt-norm } related-words
+
+HELP: matrix-l1-norm
{ $values { "m" matrix } { "n" number } }
-{ $description "Find the size of a matrix in ๐โ (" { $snippet "L^โ" } ") vector space, usually written โฅ๏ฝฅโฅโ."
+{ $description "Find the norm (size) of a matrix in ๐โ (" { $snippet "L^โ" } ") vector space, usually written โฅ๏ฝฅโฅโ."
$nl "This is the matrix norm when " { $snippet "p=1" } ", and is the overall maximum of the sums of the columns." }
-{ $notelist { $equiv-word-note "transpose" m-infinity-norm } }
+{ $notelist
+ { "User code should call the generic " { $link l1-norm } " instead." }
+ { $equiv-word-note "matrix-specific" l1-norm }
+ { $equiv-word-note { $snippet "p = 1" } matrix-p-norm }
+ { $equiv-word-note "transpose" matrix-l-infinity-norm }
+ $2d-only-note
+}
{ $examples
{ $example
"USING: math.matrices prettyprint ;"
- "{ { 2 -2 1 } { 1 3 -1 } { 2 -4 2 } } m-1norm ."
+ "{ { 2 -2 1 } { 1 3 -1 } { 2 -4 2 } } matrix-l1-norm ."
"9"
}
} ;
-HELP: m-infinity-norm
+HELP: matrix-l2-norm
{ $values { "m" matrix } { "n" number } }
-{ $description "Find the size of a matrix, in ๐โ (" { $snippet "L^โ" } ") vector space, usually written โฅ๏ฝฅโฅโ."
-$nl "This is the matrix norm when " { $snippet "p=โ" } ", and is the overall maximum of the sums of the rows." }
-{ $notelist { $equiv-word-note "transpose" m-1norm } }
+{ $description "Find the norm (size) of a matrix in ๐โ (" { $snippet "L^2" } ") vector space, usually written โฅ๏ฝฅโฅโโ."
+$nl "This is the matrix norm when " { $snippet "p=2" } ", and is the square root of the sums of the squares of all the elements of the matrix." }
+{ $notelist
+ { "This norm is sometimes called the Hilbert-Schmidt norm." }
+ { "User code should call the generic " { $link p-norm } " instead." }
+ { $equiv-word-note "matrix-specific" l2-norm }
+ { $equiv-word-note { $snippet "p = 2" } matrix-p-norm }
+ { $equiv-word-note "transpose" l1-norm }
+ $2d-only-note
+}
{ $examples
{ $example
"USING: math.matrices prettyprint ;"
- "{ { 2 -2 1 } { 1 3 -1 } { 2 -4 2 } } m-infinity-norm ."
- "8"
+ "{ { 1 1 } { 1 1 } } matrix-l2-norm ."
+ "2.0"
}
} ;
-HELP: frobenius-norm
+HELP: matrix-l-infinity-norm
{ $values { "m" matrix } { "n" number } }
-{ $description "Find the size of a matrix in ๐โ (" { $snippet "L^2" } ") vector space, usually written โฅ๏ฝฅโฅโโ."
-$nl "This is the matrix norm when " { $snippet "p=2" } ", and is the square root of the sums of the squares of all the elements of the matrix." }
+{ $description "Find the norm (size) of a matrix, in ๐โ (" { $snippet "L^โ" } ") vector space, usually written โฅ๏ฝฅโฅโ."
+$nl "This is the matrix norm when " { $snippet "p=โ" } ", and is the overall maximum of the sums of the rows." }
{ $notelist
- { "This norm is sometimes called the Hilbert-Schmidt norm." }
- { "Because " $snippet { "p=2" } ", this word could be named " { $snippet "m-2norm" } "." }
+ { "User code should call the generic " { $link l1-norm } " instead." }
+ { $equiv-word-note "matrix-specific" l-infinity-norm }
+ { $equiv-word-note { $snippet "p = โ" } matrix-p-norm }
+ { $equiv-word-note "transpose" matrix-l1-norm }
+ $2d-only-note
}
{ $examples
{ $example
"USING: math.matrices prettyprint ;"
- "{ { 1 1 } { 1 1 } } frobenius-norm ."
- "2.0"
+ "{ { 2 -2 1 } { 1 3 -1 } { 2 -4 2 } } matrix-l-infinity-norm ."
+ "8"
}
} ;
-{ m-1norm m-infinity-norm frobenius-norm } related-words
-
HELP: matrix-p-q-norm
-{ $values { "m" matrix } { "p" "positive real number" } { "q" "positive real number" } { "n" "non-negative real number" } }
-{ $description "Find the size of a matrix in " { $snippet "L^p,q" } " vector space."
-$nl "This is the matrix norm for any " { $snippet "p, q รข\89ยฅ 1" } ". It is still an entry-wise norm, like " { $link matrix-p-norm-entrywise } "." }
+{ $values { "m" matrix } { "p" "a positive real number" } { "q" "a positive real number" } { "n" "a non-negative real number" } }
+{ $description "Find the norm (size) of a matrix in " { $snippet "L^p,q" } " vector space."
+$nl "This is the matrix norm for any " { $snippet "p, q รข\88\88 รข\84\9d" } ". It is still an entry-wise norm, like " { $link matrix-p-norm-entrywise } ", and is not an induced or Schatten norm." }
{ $examples
- "Equivalent to " { $link frobenius-norm } " for " { $snippet "p = q = 2 " } ":"
+ "Equivalent to " { $link l2-norm } " for " { $snippet "p = q = 2 " } ":"
{ $example
"USING: math.matrices prettyprint ;"
"{ { 1 1 } { 1 1 } } 2 2 matrix-p-q-norm ."
} ;
HELP: matrix-p-norm-entrywise
-{ $values { "m" matrix } { "p" "positive real number" } { "n" "non-negative real number" } }
+{ $values { "m" matrix } { "p" "a positive real number" } { "n" "a non-negative real number" } }
{ $description "Find the entry-wise norm of a matrix, in ๐โ (" { $snippet "L^p" } ") vector space." }
-{ $notes "This word is distinct from a Schatten p-norm, as well as any of " { $links m-1norm frobenius-norm m-infinity-norm } "." }
+{ $notes "This word is not an induced or Schatten norm, and it is distinct from all of " { $links matrix-l1-norm matrix-l2-norm matrix-l-infinity-norm } "." }
{ $examples
- { $example
- "USING: math.matrices prettyprint ;"
- "4 4 1 <matrix> 2 matrix-p-norm-entrywise ."
- "4.0"
- }
+ { $example
+ "USING: math.matrices prettyprint ;"
+ "4 4 1 <matrix> 2 matrix-p-norm-entrywise ."
+ "4.0"
+ }
} ;
HELP: matrix-p-norm
-{ $values { "m" matrix } { "p" "positive real number" } { "n" "non-negative real number" } }
-{ $description "Find the size of a matrix in ๐โ (" { $snippet "L^p" } ") vector space, usually written โฅ๏ฝฅโฅโ. For " { $snippet "p โ 1, 2, โ" } ", this is an \"entry-wise\" norm." }
+{ $values { "m" matrix } { "p" "a positive real number" } { "n" "a non-negative real number" } }
+{ $description "Find the norm (size) of a matrix in ๐โ (" { $snippet "L^p" } ") vector space, usually written โฅ๏ฝฅโฅโ. For " { $snippet "p โ 1, 2, โ" } ", this is an \"entry-wise\" norm." }
+{ $notelist
+ { "User code should call the generic " { $link p-norm } " instead." }
+ { $equiv-word-note "matrix-specific" p-norm }
+ { $equiv-word-note { $snippet "p = q" } matrix-p-q-norm }
+ $2d-only-note
+}
{ $examples
- "Calls " { $link m-1norm } ":"
- { $example
- "USING: math.matrices prettyprint ;"
- "4 4 1 <matrix> 1 matrix-p-norm ."
- "4"
- }
- "Falls back to " { $link matrix-p-norm-entrywise } ":"
- { $example
- "USING: math.functions math.matrices prettyprint ;"
- "2 2 3 <matrix> 1.5 matrix-p-norm 7.559 10e-4 ~ ."
- "t"
- }
+ "Calls " { $link l1-norm } ":"
+ { $example
+ "USING: math.matrices prettyprint ;"
+ "4 4 1 <matrix> 1 matrix-p-norm ."
+ "4"
+ }
+ "Falls back to " { $link matrix-p-norm-entrywise } ":"
+ { $example
+ "USING: math.functions math.matrices prettyprint ;"
+ "2 2 3 <matrix> 1.5 matrix-p-norm 7.559 10e-4 ~ ."
+ "t"
+ }
} ;
{ matrix-p-norm matrix-p-norm-entrywise } related-words
{ matrix-p-norm matrix-p-q-norm } related-words
-HELP: normalize-matrix
-{ $values { "m" matrix } { "m'" matrix } }
-{ $description "Normalize a matrix. Each element from the input matrix is computed as a fraction of the maximum element. The maximum element becomes " { $snippet "1/1" } "." }
+HELP: matrix-normalize
+{ $values { "m" "a matrix with at least 1 non-zero number" } { "m'" matrix } }
+{ $description "Normalize a matrix containing at least 1 non-zero element. Each element from the input matrix is computed as a fraction of the maximum element. The maximum element becomes " { $snippet "1/1" } "." }
{ $notelist
- $2d-only-note
- { $matrix-scalar-note max abs / }
+ $2d-only-note
+ { $matrix-scalar-note max abs / }
}
{ $examples
- { $example
- "USING: math.matrices prettyprint ;"
- "{ { 5 9 } { 15 17 } } normalize-matrix ."
- "{ { 5/17 9/17 } { 15/17 1 } }"
- }
+ { $example
+ "USING: math.matrices prettyprint ;"
+ "{ { 5 9 } { 15 17 } } matrix-normalize ."
+ "{ { 5/17 9/17 } { 15/17 1 } }"
+ }
} ;
HELP: main-diagonal
! Copyright (C) 2005, 2010, 2018, 2020 Slava Pestov, Joe Groff, and Cat Stevens.
USING: arrays assocs combinators.short-circuit grouping kernel
-math math.statistics sequences sequences.deep tools.test ;
+math math.statistics math.vectors sequences sequences.deep
+tools.test ;
IN: math.matrices
<PRIVATE
] unit-test
{ 9 }
-[ { { 2 -2 1 } { 1 3 -1 } { 2 -4 2 } } m-1norm ] unit-test
+[ { { 2 -2 1 } { 1 3 -1 } { 2 -4 2 } } matrix-l1-norm ] unit-test
{ 8 }
-[ { { 2 -2 1 } { 1 3 -1 } { 2 -4 2 } } m-infinity-norm ] unit-test
+[ { { 2 -2 1 } { 1 3 -1 } { 2 -4 2 } } matrix-l-infinity-norm ] unit-test
{ 2.0 }
-[ { { 1 1 } { 1 1 } } frobenius-norm ] unit-test
+[ { { 1 1 } { 1 1 } } matrix-l2-norm ] unit-test
{ 10e-8 }
[
5.4772255
- { { 1 2 } { 3 4 } } frobenius-norm
+ { { 1 2 } { 3 4 } } matrix-l2-norm
] unit-test~
{ 10e-6 }
[
36.94590
- { { 1 2 } { 4 8 } { 16 32 } } frobenius-norm
+ { { 1 2 } { 4 8 } { 16 32 } } matrix-l2-norm
] unit-test~
! equivalent to frobenius for p = q = 2
] unit-test~
{ { { -1 0 } { 0 0 } } }
-[ { { -2 0 } { 0 0 } } normalize-matrix ] unit-test
+[ { { -2 0 } { 0 0 } } matrix-normalize ] unit-test
{ { { -1 0 } { 0 1/2 } } }
-[ { { -2 0 } { 0 1 } } normalize-matrix ] unit-test
+[ { { -2 0 } { 0 1 } } matrix-normalize ] unit-test
{ t }
-[ 3 3 <zero-matrix> dup normalize-matrix = ] unit-test
+[ 3 3 <zero-matrix> dup matrix-normalize = ] unit-test
! diagonals
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays classes.singleton columns combinators
combinators.short-circuit combinators.smart formatting fry
+<<<<<<< HEAD
grouping kernel locals math math.bits math.functions math.order
math.private math.ranges math.statistics math.vectors
math.vectors.private sequences sequences.deep sequences.private
slots.private summary ;
+=======
+grouping kernel kernel.private locals math math.bits
+math.functions math.order math.private math.ranges
+math.statistics math.vectors math.vectors.private sequences
+sequences.deep sequences.extras sequences.private slots.private
+summary ;
+>>>>>>> 0ac3067a26 (vnorm rename)
IN: math.matrices
! defined here because of issue #1943
: mmin ( m -- n ) [ 1/0. ] dip [ [ min ] each ] each ;
: mmax ( m -- n ) [ -1/0. ] dip [ [ max ] each ] each ;
-: m-infinity-norm ( m -- n )
- dup zero-matrix? [ drop 0. ] [
+: matrix-l-infinity-norm ( m -- n )
+ dup zero-matrix? [ drop 0 ] [
[ [ abs ] map-sum ] map supremum
- ] if ;
+ ] if ; inline foldable
-: m-1norm ( m -- n )
- dup zero-matrix? [ drop 0. ] [
- flip m-infinity-norm
- ] if ;
+: matrix-l1-norm ( m -- n )
+ dup zero-matrix? [ drop 0 ] [
+ flip matrix-l-infinity-norm
+ ] if ; inline foldable
-: frobenius-norm ( m -- n )
- dup zero-matrix? [ drop 0. ] [
+: matrix-l2-norm ( m -- n )
+ dup zero-matrix? [ drop 0 ] [
[ [ sq ] map-sum ] map-sum sqrt
- ] if ;
+ ] if ; inline foldable
+
+M: zero-matrix l1-norm drop 0 ; inline
+M: matrix l1-norm matrix-l1-norm ; inline
+
+M: zero-matrix l2-norm drop 0 ; inline
+M: matrix l2-norm matrix-l2-norm ; inline
+
+M: zero-matrix l-infinity-norm drop 0 ; inline
+M: matrix l-infinity-norm matrix-l-infinity-norm ; inline
-ALIAS: hilbert-schmidt-norm frobenius-norm
+ALIAS: frobenius-norm matrix-l2-norm
+ALIAS: hilbert-schmidt-norm matrix-l2-norm
:: matrix-p-q-norm ( m p q -- n )
- m dup zero-matrix? [ drop 0. ] [
+ m dup zero-matrix? [ drop 0 ] [
[ [ sq ] map-sum q p / ^ ] map-sum q recip ^
- ] if ;
+ ] if ; inline foldable
: matrix-p-norm-entrywise ( m p -- n )
- dup zero-matrix? [ 2drop 0. ] [
- [ flatten1 V{ } like ] dip p-norm-default
- ] if ;
+ [ flatten1 V{ } like ] dip p-norm-default ; inline
+
+M: zero-matrix p-norm-default 2drop 0 ; inline
+M: matrix p-norm-default matrix-p-norm-entrywise ; inline
: matrix-p-norm ( m p -- n )
- dup zero-matrix? [ 2drop 0. ] [
+ over zero-matrix? [ 2drop 0 ] [
{
- { [ dup 1 = ] [ drop m-1norm ] }
- { [ dup 2 = ] [ drop frobenius-norm ] }
- { [ dup fp-infinity? ] [ drop m-infinity-norm ] }
+ { [ dup 1 number= ] [ drop matrix-l1-norm ] }
+ { [ dup 2 number= ] [ drop matrix-l2-norm ] }
+ { [ dup fp-infinity? ] [ drop matrix-l-infinity-norm ] }
[ matrix-p-norm-entrywise ]
} cond
- ] if ;
+ ] if ; inline foldable
+
+M: zero-matrix p-norm 2drop 0 ; inline
+M: matrix p-norm matrix-p-norm ; inline
-: normalize-matrix ( m -- m' )
- dup zero-matrix? [ ] [
+: matrix-normalize ( m -- m' )
+ dup zero-matrix? [
dup mabs mmax m/n
- ] if ;
+ ] unless ; inline foldable
! well-defined for square matrices; but works on nonsquare too
: main-diagonal ( matrix -- seq )
CONSTANT: qj { 0 0 1 0 }
CONSTANT: qk { 0 0 0 1 }
-{ 1.0 } [ qi norm ] unit-test
-{ 1.0 } [ qj norm ] unit-test
-{ 1.0 } [ qk norm ] unit-test
-{ 1.0 } [ q1 norm ] unit-test
-{ 0.0 } [ q0 norm ] unit-test
+{ 1.0 } [ qi l2-norm ] unit-test
+{ 1.0 } [ qj l2-norm ] unit-test
+{ 1.0 } [ qk l2-norm ] unit-test
+{ 1.0 } [ q1 l2-norm ] unit-test
+{ 0.0 } [ q0 l2-norm ] unit-test
{ t } [ qi qj q* qk = ] unit-test
{ t } [ qj qk q* qi = ] unit-test
{ t } [ qk qi q* qj = ] unit-test
$nl
"SSE2 introduces double-precision SIMD (" { $snippet "double-2" } ") and integer SIMD (all types). Integer SIMD is missing a few features; in particular, the " { $link vmin } " and " { $link vmax } " operations only work on " { $snippet "uchar-16" } " and " { $snippet "short-8" } "."
$nl
-"SSE3 introduces horizontal adds (summing all components of a single vector register), which are useful for computing dot products. Where available, SSE3 operations are used to speed up " { $link sum } ", " { $link vdot } ", " { $link norm-sq } ", " { $link norm } ", and " { $link distance } "."
+"SSE3 introduces horizontal adds (summing all components of a single vector register), which are useful for computing dot products. Where available, SSE3 operations are used to speed up " { $link sum } ", " { $link vdot } ", " { $link norm-sq } ", " { $link l2-norm } ", and " { $link distance } "."
$nl
"SSSE3 introduces " { $link vabs } " for " { $snippet "char-16" } ", " { $snippet "short-8" } " and " { $snippet "int-4" } "."
$nl
! Test type propagation
{ V{ float } } [ [ { float-4 } declare norm-sq ] final-classes ] unit-test
-{ V{ float } } [ [ { float-4 } declare norm ] final-classes ] unit-test
+{ V{ float } } [ [ { float-4 } declare l2-norm ] final-classes ] unit-test
{ V{ float-4 } } [ [ { float-4 } declare normalize ] final-classes ] unit-test
{ n+v { +scalar+ +vector+ -> +vector+ } }
{ n-v { +scalar+ +vector+ -> +vector+ } }
{ n/v { +scalar+ +vector+ -> +vector+ } }
- { norm { +vector+ -> +nonnegative+ } }
+ { l2-norm { +vector+ -> +nonnegative+ } }
{ norm-sq { +vector+ -> +nonnegative+ } }
{ normalize { +vector+ -> +vector+ } }
{ v* { +vector+ +vector+ -> +vector+ } }
: (vvx->v-op) ( a b obj rep quot: ( (a) (b) obj rep -- (c) ) -- c )
[ [ simd-unbox ] [ underlying>> ] bi* ] 3dip 2curry 2curry make-underlying ; inline
-
+
: vv->v-op ( a b rep quot: ( (a) (b) rep -- (c) ) fallback-quot -- c )
[ '[ _ (vv->v-op) ] ] [ '[ drop @ ] ] bi* if-both-vectors-match ; inline
M: simd-128 v*n over simd-with v* ; inline
M: simd-128 v/n over simd-with v/ ; inline
M: simd-128 norm-sq dup vdot assert-positive ; inline
-M: simd-128 distance v- norm ; inline
+M: simd-128 distance v- l2-norm ; inline
M: simd-128 >pprint-sequence ;
M: simd-128 pprint* pprint-object ;
USING: help.markup help.syntax kernel math math.functions
-sequences ;
+math.matrices sequences ;
IN: math.vectors
ARTICLE: "math-vectors-arithmetic" "Vector arithmetic"
vfloor
vceiling
vtruncate
+ normalize
}
"Vector/scalar and scalar/vector binary operations:"
{ $subsections
"Inner product and norm:"
{ $subsections
vdot
- norm
norm-sq
- normalize
+ l1-norm
+ l2-norm
+ l-infinity-norm
p-norm
}
"Comparing entire vectors:"
{ $values { "v" { $sequence number } } { "x" "a non-negative real number" } }
{ $description "Computes the squared length of a mathematical vector." } ;
-HELP: norm
-{ $values { "v" { $sequence number } } { "x" "a non-negative real number" } }
-{ $description "Computes the length of a mathematical vector." } ;
+HELP: l1-norm
+{ $values { "k" sequence } { "x" "a non-negative real number" } }
+{ $contract "Computes the norm (size) of " { $snippet "k" } " in ๐โ (" { $snippet "L^1" } ") vector space." }
+{ $notes "See " { $link matrix-l1-norm } " for matrix-specific documentation about this word." }
+{ $examples
+ { $example
+ "USING: math.vectors prettyprint ;"
+ "{ 1 2 3 4 } l1-norm ."
+ "10"
+ }
+} ;
+
+HELP: l2-norm
+{ $values { "k" sequence } { "x" "a non-negative real number" } }
+{ $contract "Computes the norm (size) of " { $snippet "k" } " in ๐โ (" { $snippet "L^2" } ") vector space." }
+{ $notes "See " { $link matrix-l2-norm } " for matrix-specific documentation about this word." }
+{ $examples
+ { $example
+ "USING: math.vectors math.functions prettyprint ;"
+ "{ 1 2 3 4 } l2-norm 5.4772255 10e-8 ~ ."
+ "t"
+ }
+} ;
+
+HELP: l-infinity-norm
+{ $values { "k" sequence } { "x" "a non-negative real number" } }
+{ $contract "Computes the norm (size) of " { $snippet "k" } " in ๐โ (" { $snippet "L^โ" } ") vector space. For a mathematical vector, this is simply its " { $link supremum } "." }
+{ $notes "See " { $link matrix-l-infinity-norm } " for matrix-specific documentation about this word." }
+{ $examples
+ { $example
+ "USING: math.vectors prettyprint ;"
+ "{ 1 2 3 4 } l-infinity-norm ."
+ "4"
+ }
+} ;
HELP: p-norm
-{ $values { "v" { $sequence number } } { "p" "a positive real number" } { "x" "a non-negative real number" } }
-{ $description "Computes the length of a mathematical vector in " { $snippet "L^p" } " space." } ;
+{ $values { "k" { $sequence number } } { "p" "a positive real number" } { "x" "a non-negative real number" } }
+{ $contract "Computes the norm (size) of " { $snippet "k" } " in ๐โ (" { $snippet "L^p" } ") vector space." }
+{ $notes "See " { $link matrix-p-norm } " for matrix-specific documentation about this word." } ;
HELP: normalize
-{ $values { "v" "a sequence of numbers, not all zero" } { "w" { $sequence number } } }
-{ $description "Outputs a vector with the same direction as " { $snippet "v" } " but length 1." } ;
+{ $values { "v" { $sequence "at least 1 non-zero number" } } { "w" { $sequence number } } }
+{ $description "Outputs a vector with the same direction as " { $snippet "v" } ", but length 1." } ;
HELP: distance
{ $values { "u" { $sequence number } } { "v" { $sequence number } } { "x" "a non-negative real number" } }
{ 5 } [ { 1 2 } norm-sq ] unit-test
{ 13 } [ { 2 3 } norm-sq ] unit-test
-{ t } [ { 1 2 3 } [ norm ] [ 2 p-norm ] bi = ] unit-test
+{ t } [ { 1 2 3 } [ l2-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
GENERIC: norm-sq ( v -- x )
M: object norm-sq [ absq ] [ + ] map-reduce ; inline
-: l1-norm ( v -- x ) [ abs ] map-sum ; inline
+GENERIC: l1-norm ( k -- x )
+M: object l1-norm [ abs ] map-sum ; inline
-: norm ( v -- x ) norm-sq sqrt ; inline
+GENERIC: l2-norm ( k -- x )
+M: object l2-norm norm-sq sqrt ; inline
-: p-norm-default ( v p -- x )
+GENERIC: l-infinity-norm ( k -- x )
+M: object l-infinity-norm supremum ; inline
+
+GENERIC#: p-norm-default 1 ( k p -- x )
+M: object p-norm-default
[ [ [ abs ] dip ^ ] curry map-sum ] keep recip ^ ; inline
-: p-norm ( v p -- x )
+GENERIC#: p-norm 1 ( k p -- x )
+M: object p-norm
{
{ [ dup 1 = ] [ drop l1-norm ] }
- { [ dup 2 = ] [ drop norm ] }
+ { [ dup 2 = ] [ drop l2-norm ] }
{ [ dup fp-infinity? ] [ drop supremum ] }
[ p-norm-default ]
} cond ;
-: normalize ( v -- w ) dup norm v/n ; inline
+: normalize ( v -- w ) dup l2-norm v/n ; inline
GENERIC: distance ( u v -- x )
M: object distance [ - absq ] [ + ] 2map-reduce sqrt ; inline
M: cord v/n '[ _ v/n ] cord-map ; inline
M: cord norm-sq [ norm-sq ] cord-both + ; inline
-M: cord distance v- norm ; inline
+M: cord distance v- l2-norm ; inline
{
{ [ dup integer? ] [ abs ] }
{ [ dup float? ] [ abs ] }
- { [ dup vec2d? ] [ norm ] }
- { [ dup vec3d? ] [ norm ] }
+ { [ dup vec2d? ] [ l2-norm ] }
+ { [ dup vec3d? ] [ l2-norm ] }
} cond ;
: must-be-positive ( x -- x ) dup 0 < [ "Domain error" throw ] when ; inline
ray-t 0.0 >= edge-t 0.0 0.5 between? and [
source direction ray-t v*n v+
- edge-source edge-direction edge-t v*n v+ v- norm
+ edge-source edge-direction edge-t v*n v+ v- l2-norm
edge-hitbox-radius <
] [ f ] if ;
[ location>> ] bi@ swap v- ;
: distance ( oint oint -- distance )
- distance-vector norm ;
+ distance-vector l2-norm ;
: scalar-projection ( v1 v2 -- n )
! the scalar projection of v1 onto v2
- [ vdot ] [ norm ] bi / ;
+ [ vdot ] [ l2-norm ] bi / ;
: proj-perp ( u v -- w )
dupd proj v- ;
over location>> swap v- swap forward>> proj-perp ;
: distance-from-centre ( seg loc -- distance )
- vector-to-centre norm ;
+ vector-to-centre l2-norm ;
: wall-normal ( seg oint -- n )
location>> vector-to-centre normalize ;
] if ;
:: collision-coefficient ( v w r -- c )
- v norm 0 = [
+ v l2-norm 0 = [
distant
] [
v dup vdot :> a
IN: math.similarity
: euclidian-similarity ( a b -- n )
- v- norm 1 + recip ;
+ v- l2-norm 1 + recip ;
: pearson-similarity ( a b -- n )
over length 3 < [ 2drop 1.0 ] [ population-corr 0.5 * 0.5 + ] if ;
: cosine-similarity ( a b -- n )
- [ vdot ] [ [ norm ] bi@ * ] 2bi / ;
+ [ vdot ] [ [ l2-norm ] bi@ * ] 2bi / ;
<PRIVATE