math.vectors: implement v^, v^n, n^v.
parent
6077f2d6c6
commit
aed6825b4a
|
@ -9,6 +9,7 @@ ARTICLE: "math-vectors-arithmetic" "Vector arithmetic"
|
||||||
v+-
|
v+-
|
||||||
v*
|
v*
|
||||||
v/
|
v/
|
||||||
|
v^
|
||||||
}
|
}
|
||||||
"Vector unary operations:"
|
"Vector unary operations:"
|
||||||
{ $subsections
|
{ $subsections
|
||||||
|
@ -30,6 +31,8 @@ ARTICLE: "math-vectors-arithmetic" "Vector arithmetic"
|
||||||
n+v
|
n+v
|
||||||
v-n
|
v-n
|
||||||
n-v
|
n-v
|
||||||
|
v^n
|
||||||
|
n^v
|
||||||
}
|
}
|
||||||
"Saturated arithmetic (only on " { $link "specialized-arrays" } "):"
|
"Saturated arithmetic (only on " { $link "specialized-arrays" } "):"
|
||||||
{ $subsections
|
{ $subsections
|
||||||
|
@ -212,7 +215,7 @@ HELP: vtruncate
|
||||||
|
|
||||||
HELP: n+v
|
HELP: n+v
|
||||||
{ $values { "n" "a number" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
|
{ $values { "n" "a number" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
|
||||||
{ $description "Adds " { $snippet "n" } " to each element of " { $snippet "u" } "." } ;
|
{ $description "Adds " { $snippet "n" } " to each element of " { $snippet "v" } "." } ;
|
||||||
|
|
||||||
HELP: v+n
|
HELP: v+n
|
||||||
{ $values { "u" "a sequence of numbers" } { "n" "a number" } { "w" "a sequence of numbers" } }
|
{ $values { "u" "a sequence of numbers" } { "n" "a number" } { "w" "a sequence of numbers" } }
|
||||||
|
@ -220,7 +223,7 @@ HELP: v+n
|
||||||
|
|
||||||
HELP: n-v
|
HELP: n-v
|
||||||
{ $values { "n" "a number" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
|
{ $values { "n" "a number" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
|
||||||
{ $description "Subtracts each element of " { $snippet "u" } " from " { $snippet "n" } "." } ;
|
{ $description "Subtracts each element of " { $snippet "v" } " from " { $snippet "n" } "." } ;
|
||||||
|
|
||||||
HELP: v-n
|
HELP: v-n
|
||||||
{ $values { "u" "a sequence of numbers" } { "n" "a number" } { "w" "a sequence of numbers" } }
|
{ $values { "u" "a sequence of numbers" } { "n" "a number" } { "w" "a sequence of numbers" } }
|
||||||
|
@ -228,7 +231,7 @@ HELP: v-n
|
||||||
|
|
||||||
HELP: n*v
|
HELP: n*v
|
||||||
{ $values { "n" "a number" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
|
{ $values { "n" "a number" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
|
||||||
{ $description "Multiplies each element of " { $snippet "u" } " by " { $snippet "n" } "." } ;
|
{ $description "Multiplies each element of " { $snippet "v" } " by " { $snippet "n" } "." } ;
|
||||||
|
|
||||||
HELP: v*n
|
HELP: v*n
|
||||||
{ $values { "u" "a sequence of numbers" } { "n" "a number" } { "w" "a sequence of numbers" } }
|
{ $values { "u" "a sequence of numbers" } { "n" "a number" } { "w" "a sequence of numbers" } }
|
||||||
|
@ -236,7 +239,7 @@ HELP: v*n
|
||||||
|
|
||||||
HELP: n/v
|
HELP: n/v
|
||||||
{ $values { "n" "a number" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
|
{ $values { "n" "a number" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
|
||||||
{ $description "Divides " { $snippet "n" } " by each element of " { $snippet "u" } "." }
|
{ $description "Divides " { $snippet "n" } " by each element of " { $snippet "v" } "." }
|
||||||
{ $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
|
{ $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
|
||||||
|
|
||||||
HELP: v/n
|
HELP: v/n
|
||||||
|
@ -244,6 +247,14 @@ HELP: v/n
|
||||||
{ $description "Divides each element of " { $snippet "u" } " by " { $snippet "n" } "." }
|
{ $description "Divides each element of " { $snippet "u" } " by " { $snippet "n" } "." }
|
||||||
{ $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
|
{ $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
|
||||||
|
|
||||||
|
HELP: n^v
|
||||||
|
{ $values { "n" "a number" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
|
||||||
|
{ $description "Raises " { $snippet "n" } " to the power of each element of " { $snippet "v" } "." } ;
|
||||||
|
|
||||||
|
HELP: v^n
|
||||||
|
{ $values { "u" "a sequence of numbers" } { "n" "a number" } { "w" "a sequence of numbers" } }
|
||||||
|
{ $description "Raises each element of " { $snippet "u" } " to the power of " { $snippet "n" } "." } ;
|
||||||
|
|
||||||
HELP: v+
|
HELP: v+
|
||||||
{ $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
|
{ $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
|
||||||
{ $description "Adds " { $snippet "u" } " and " { $snippet "v" } " component-wise." } ;
|
{ $description "Adds " { $snippet "u" } " and " { $snippet "v" } " component-wise." } ;
|
||||||
|
@ -276,6 +287,10 @@ HELP: v/
|
||||||
{ $description "Divides " { $snippet "u" } " by " { $snippet "v" } " component-wise." }
|
{ $description "Divides " { $snippet "u" } " by " { $snippet "v" } " component-wise." }
|
||||||
{ $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
|
{ $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
|
||||||
|
|
||||||
|
HELP: v^
|
||||||
|
{ $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
|
||||||
|
{ $description "Raises " { $snippet "u" } " to the power of " { $snippet "v" } " component-wise." } ;
|
||||||
|
|
||||||
HELP: vmax
|
HELP: vmax
|
||||||
{ $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
|
{ $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
|
||||||
{ $description "Creates a sequence where each element is the maximum of the corresponding elements from " { $snippet "u" } " and " { $snippet "v" } "." }
|
{ $description "Creates a sequence where each element is the maximum of the corresponding elements from " { $snippet "u" } " and " { $snippet "v" } "." }
|
||||||
|
|
|
@ -8,6 +8,10 @@ SPECIALIZED-ARRAY: int
|
||||||
[ { 1 2 3 } ] [ { 2 4 6 } 2 v/n ] unit-test
|
[ { 1 2 3 } ] [ { 2 4 6 } 2 v/n ] unit-test
|
||||||
[ { 1/1 1/2 1/3 } ] [ 1 { 1 2 3 } n/v ] unit-test
|
[ { 1/1 1/2 1/3 } ] [ 1 { 1 2 3 } n/v ] unit-test
|
||||||
|
|
||||||
|
{ { 1 4 27 } } [ { 1 2 3 } { 1 2 3 } v^ ] unit-test
|
||||||
|
{ { 1 4 9 } } [ { 1 2 3 } 2 v^n ] unit-test
|
||||||
|
{ { 2 4 8 } } [ 2 { 1 2 3 } n^v ] unit-test
|
||||||
|
|
||||||
[ 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
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,15 @@ M: object v*hs+ [ * ] 2map (h+) ; inline
|
||||||
GENERIC: v/ ( u v -- w )
|
GENERIC: v/ ( u v -- w )
|
||||||
M: object v/ [ / ] 2map ; inline
|
M: object v/ [ / ] 2map ; inline
|
||||||
|
|
||||||
|
GENERIC: v^ ( u v -- w )
|
||||||
|
M: object v^ [ ^ ] 2map ; inline
|
||||||
|
|
||||||
|
GENERIC: v^n ( u n -- w )
|
||||||
|
M: object v^n [ ^ ] curry map ; inline
|
||||||
|
|
||||||
|
GENERIC: n^v ( n v -- w )
|
||||||
|
M: object n^v [ ^ ] with map ; inline
|
||||||
|
|
||||||
GENERIC: vavg ( u v -- w )
|
GENERIC: vavg ( u v -- w )
|
||||||
M: object vavg [ + 2 / ] 2map ; inline
|
M: object vavg [ + 2 / ] 2map ; inline
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue