! 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
[ [ sq ] map-sum ] map-sum sqrt
] if ; inline foldable
-M: zero-matrix l1-norm drop 0 ; inline
-M: matrix l1-norm matrix-l1-norm ; inline
+! XXX: M: zero-matrix l1-norm drop 0 ; inline
+! XXX: M: matrix l1-norm matrix-l1-norm ; inline
-M: zero-matrix l2-norm drop 0 ; inline
-M: matrix l2-norm matrix-l2-norm ; inline
+! XXX: M: zero-matrix l2-norm drop 0 ; inline
+! XXX: 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
+! XXX: M: zero-matrix l-infinity-norm drop 0 ; inline
+! XXX: M: matrix l-infinity-norm matrix-l-infinity-norm ; inline
ALIAS: frobenius-norm matrix-l2-norm
ALIAS: hilbert-schmidt-norm matrix-l2-norm
: matrix-p-norm-entrywise ( m p -- n )
[ 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
+! XXX: M: zero-matrix p-norm-default 2drop 0 ; inline
+! XXX: M: matrix p-norm-default matrix-p-norm-entrywise ; inline
: matrix-p-norm ( m p -- n )
over zero-matrix? [ 2drop 0 ] [
} cond
] if ; inline foldable
-M: zero-matrix p-norm 2drop 0 ; inline
-M: matrix p-norm matrix-p-norm ; inline
+! XXX: M: zero-matrix p-norm 2drop 0 ; inline
+! XXX: M: matrix p-norm matrix-p-norm ; inline
: matrix-normalize ( m -- m' )
dup zero-matrix? [
USING: help.markup help.syntax kernel math math.functions
-math.matrices sequences ;
+sequences ;
IN: math.vectors
ARTICLE: "math-vectors-arithmetic" "Vector arithmetic"
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, usually written ∥・∥₁." }
-{ $notes "See " { $link matrix-l1-norm } " for matrix-specific documentation about this word." }
{ $examples
{ $example
"USING: math.vectors prettyprint ;"
HELP: l2-norm
{ $values { "k" sequence } { "x" "a non-negative real number" } }
-{ $contract "Implementation for the default " { $link norm } ", in 𝑙₂ (" { $snippet "L^2" } ") vector space, usually written ∥・∥₂." }
-{ $notes "See " { $link matrix-l2-norm } " for matrix-specific documentation about this word." } ;
+{ $contract "Implementation for the default " { $link norm } ", in 𝑙₂ (" { $snippet "L^2" } ") vector space, usually written ∥・∥₂." } ;
HELP: norm
{ $values { "k" sequence } { "x" "a non-negative real number" } }
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, usually written ∥・∥∞. 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 ;"
HELP: p-norm
{ $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, usually written ∥・∥ₚ." }
-{ $notes "See " { $link matrix-p-norm } " for matrix-specific documentation about this word." } ;
+{ $contract "Computes the norm (size) of " { $snippet "k" } " in 𝑙ₚ (" { $snippet "L^p" } ") vector space, usually written ∥・∥ₚ." } ;
{ norm-sq l1-norm l2-norm norm l-infinity-norm p-norm } related-words
{ $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." } ;
-{ normalize matrix-normalize } related-words
-
HELP: distance
{ $values { "u" { $sequence number } } { "v" { $sequence number } } { "x" "a non-negative real number" } }
{ $description "Outputs the Euclidean distance between two vectors." } ;
GENERIC: norm-sq ( v -- x )
M: object norm-sq [ absq ] [ + ] map-reduce ; inline
-GENERIC: l1-norm ( k -- x )
-M: object l1-norm [ abs ] map-sum ; inline
+: l1-norm ( k -- x ) [ abs ] map-sum ; inline
+
+: l2-norm ( k -- x ) norm-sq sqrt ; inline
-GENERIC: l2-norm ( k -- x )
-M: object l2-norm norm-sq sqrt ; inline
ALIAS: norm l2-norm
-GENERIC: l-infinity-norm ( k -- x )
-M: object l-infinity-norm supremum ; inline
+: l-infinity-norm ( k -- x ) supremum ; inline
-GENERIC#: p-norm-default 1 ( k p -- x )
-M: object p-norm-default
+: p-norm-default ( k p -- x )
[ [ [ abs ] dip ^ ] curry map-sum ] keep recip ^ ; inline
-GENERIC#: p-norm 1 ( k p -- x )
-M: object p-norm
+: p-norm ( k p -- x )
{
{ [ dup 1 = ] [ drop l1-norm ] }
{ [ dup 2 = ] [ drop l2-norm ] }