]> gitweb.factorcode.org Git - factor.git/commitdiff
vnorm rename
authorCat Stevens <catb0t@protonmail.ch>
Sun, 1 Mar 2020 20:30:00 +0000 (15:30 -0500)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 22 Dec 2021 20:08:54 +0000 (12:08 -0800)
16 files changed:
basis/math/matrices/matrices-docs.factor
basis/math/matrices/matrices-tests.factor
basis/math/matrices/matrices.factor
basis/math/quaternions/quaternions-tests.factor
basis/math/vectors/simd/simd-docs.factor
basis/math/vectors/simd/simd-tests.factor
basis/math/vectors/simd/simd.factor
basis/math/vectors/vectors-docs.factor
basis/math/vectors/vectors-tests.factor
basis/math/vectors/vectors.factor
basis/sequences/cords/cords.factor
extra/gml/coremath/coremath.factor
extra/gml/viewer/viewer.factor
extra/jamshred/oint/oint.factor
extra/jamshred/tunnel/tunnel.factor
extra/math/similarity/similarity.factor

index 7ad09c968e82451711fb8d26c8219b49c75bbcf3..4ba8822240b8e6811c929c37751c429056edcbfa 100644 (file)
@@ -60,7 +60,7 @@ ARTICLE: "math.matrices" "Matrix operations"
 $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
@@ -80,7 +80,6 @@ $nl
     zero-matrix?
     zero-square-matrix?
     null-matrix?
-
 }
 
 "There are many ways to create 2-dimensional matrices:"
@@ -108,7 +107,7 @@ $nl
 }
 
 "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~ }
@@ -138,13 +137,17 @@ $nl
     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
@@ -957,56 +960,72 @@ HELP: mmax
     }
 } ;
 
-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 ."
@@ -1015,51 +1034,57 @@ $nl "This is the matrix norm for any " { $snippet "p, q โ‰ฅ 1" } ". It is still
 } ;
 
 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
index d55ce7361661a7d8379d2dd44400c7f5e02f44a1..6a015b8b4d087ca3994f1de22fd01ead774930ed 100644 (file)
@@ -1,6 +1,7 @@
 ! 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
@@ -318,24 +319,24 @@ 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
@@ -349,13 +350,13 @@ PRIVATE>
 ] 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
 
index 8825b7e8ef7e2d97d320490b76c93ec38cedfb9a..3d5c7439f990311d8bfcc57371569b90f5da2b63 100644 (file)
@@ -2,10 +2,18 @@
 ! 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
@@ -260,47 +268,61 @@ DEFER: matrix-set-nths
 : 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 )
index cb082aecac083076221bbb8d51b5ddaa5c578ab4..c1149290da08f510deaea40e919378ceace46ce6 100644 (file)
@@ -8,11 +8,11 @@ CONSTANT: qi { 0 1 0 0 }
 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
index 5f7d3189fa6f11e918acff90fe16ba1219c34b93..f4b684a38827e2f0e23112f6a56d1274503d2670 100644 (file)
@@ -23,7 +23,7 @@ $nl
 $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
index 9f4a17e64e5ee178b453961709d6c96e138bf65d..683ff3c52a4f087fe730240955ecbd6c1e6c0d03 100644 (file)
@@ -15,7 +15,7 @@ IN: math.vectors.simd.tests
 ! 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
 
@@ -64,7 +64,7 @@ CONSTANT: vector-words
         { 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+ } }
index 0d67ad2ea56b2533263e9af239b185bcfb778951..fde5f7cdc4accca83d96412d0a274c8d6e8b86a7 100644 (file)
@@ -94,7 +94,7 @@ DEFER: simd-construct-op
 
 : (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
 
@@ -254,7 +254,7 @@ M: simd-128 v-n over simd-with v- ; 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 ;
index 5b18ee8b1a8e1e33336931c87eed6bee8cb1e2e4..74313bca3aaddfc2e94669bba702060220688ceb 100644 (file)
@@ -1,5 +1,5 @@
 USING: help.markup help.syntax kernel math math.functions
-sequences ;
+math.matrices sequences ;
 IN: math.vectors
 
 ARTICLE: "math-vectors-arithmetic" "Vector arithmetic"
@@ -20,6 +20,7 @@ ARTICLE: "math-vectors-arithmetic" "Vector arithmetic"
     vfloor
     vceiling
     vtruncate
+    normalize
 }
 "Vector/scalar and scalar/vector binary operations:"
 { $subsections
@@ -44,9 +45,10 @@ ARTICLE: "math-vectors-arithmetic" "Vector arithmetic"
 "Inner product and norm:"
 { $subsections
     vdot
-    norm
     norm-sq
-    normalize
+    l1-norm
+    l2-norm
+    l-infinity-norm
     p-norm
 }
 "Comparing entire vectors:"
@@ -481,17 +483,50 @@ HELP: norm-sq
 { $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" } }
index 48de485977d188e6a3e57b3365ee457b957f879a..0faeee341b3711bfa3bcbcb8eb9b4b6ed548c981 100644 (file)
@@ -15,7 +15,7 @@ SPECIALIZED-ARRAY: int
 { 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
index 8f499be5e43134d9089f89b08e3f529b94bb81ca..191838fefb5808c84d0d927c700daec254a50e99 100644 (file)
@@ -225,22 +225,29 @@ M: object hdot [ 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
+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
index 0576efdb86a1dd63b3168ae5a041d1d9c8726217..c974834c249338d170533ae0ecec31d6169cd769 100644 (file)
@@ -119,4 +119,4 @@ M: cord v*n '[ _ v*n ] cord-map ; 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
index c7a6de23150880519a74a2920c1c8d409c7d0823..048b279c82655bdbf368b730f3df9c7e403f22fb 100644 (file)
@@ -115,8 +115,8 @@ GML: abs ( x -- y )
     {
         { [ 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
index efa1d1a650d6371bab3f9b623f1cc6a720c989da..07ef315adc2b10a4bb25d1e816117153ab200289 100644 (file)
@@ -252,7 +252,7 @@ CONSTANT: edge-hitbox-radius 0.05
 
     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 ;
 
index 9b92c7b27451480ca28643138883c9e3bcb393f8..63bc29d9f560a0a5707513e8ed31b1f57c3a4fbb 100644 (file)
@@ -78,11 +78,11 @@ PRIVATE>
     [ 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- ;
index 94e4d7b8b0a08858749f6be722fde2706c5487d3..869d2c514d32cccd8934ba248a8706d34ee0bb1f 100644 (file)
@@ -91,7 +91,7 @@ CONSTANT: default-segment-radius 1
     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 ;
@@ -107,7 +107,7 @@ CONSTANT: distant 1000
     ] if ;
 
 :: collision-coefficient ( v w r -- c )
-    v norm 0 = [
+    v l2-norm 0 = [
         distant
     ] [
         v dup vdot :> a
index 34ff047d2af81fe778dfd94f386405952e60fc8c..398e7fed8acb35fbcb33a249d063010ca409a7ac 100644 (file)
@@ -7,13 +7,13 @@ sequences sequences.extras ;
 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