1 USING: help.markup help.syntax math math.functions sequences ;
4 ARTICLE: "math-vectors" "Vector arithmetic"
5 "Any Factor sequence can be used to represent a mathematical vector, however for best performance, the sequences defined by the " { $vocab-link "specialized-arrays" } " and " { $vocab-link "math.vectors.simd" } " vocabularies should be used."
7 "Acting on vectors by a scalar:"
17 "Vector unary operations:"
21 { $subsection vfloor }
22 { $subsection vceiling }
23 { $subsection vtruncate }
24 "Vector/vector binary operations:"
30 "Saturated arithmetic (only on " { $link "specialized-arrays" } "):"
34 "Componentwise vector operations:"
40 { $subsection vunordered? }
44 { $subsection vbitand }
45 { $subsection vbitandn }
46 { $subsection vbitor }
47 { $subsection vbitxor }
48 { $subsection vlshift }
49 { $subsection vrshift }
50 "Componentwise logical operations:"
57 { $subsection vshuffle }
58 "Inner product and norm:"
61 { $subsection norm-sq }
62 { $subsection normalize }
63 "Comparing entire vectors:"
64 { $subsection distance }
67 { $subsection vsupremum }
68 { $subsection vinfimum }
69 { $subsection trilerp }
70 { $subsection bilerp }
72 { $subsection vnlerp }
73 { $subsection vbilerp } ;
78 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
79 { $description "Negates each element of " { $snippet "u" } "." } ;
82 { $values { "u" "a sequence of numbers" } { "v" "a sequence of non-negative real numbers" } }
83 { $description "Takes the absolute value of each element of " { $snippet "u" } "." } ;
86 { $values { "u" "a sequence of non-negative real numbers" } { "v" "a sequence of non-negative real numbers" } }
87 { $description "Takes the square root of each element of " { $snippet "u" } "." }
88 { $warning "For performance reasons, this does not work with negative inputs, unlike " { $link sqrt } "." } ;
91 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } }
92 { $description "Takes the " { $link floor } " of each element of " { $snippet "u" } "." } ;
95 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } }
96 { $description "Takes the " { $link ceiling } " of each element of " { $snippet "u" } "." } ;
99 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } }
100 { $description "Truncates each element of " { $snippet "u" } "." } ;
103 { $values { "n" "a number" } { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
104 { $description "Adds " { $snippet "n" } " to each element of " { $snippet "u" } "." } ;
107 { $values { "u" "a sequence of numbers" } { "n" "a number" } { "v" "a sequence of numbers" } }
108 { $description "Adds " { $snippet "n" } " to each element of " { $snippet "u" } "." } ;
111 { $values { "n" "a number" } { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
112 { $description "Subtracts each element of " { $snippet "u" } " from " { $snippet "n" } "." } ;
115 { $values { "u" "a sequence of numbers" } { "n" "a number" } { "v" "a sequence of numbers" } }
116 { $description "Subtracts " { $snippet "n" } " from each element of " { $snippet "u" } "." } ;
119 { $values { "n" "a number" } { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
120 { $description "Multiplies each element of " { $snippet "u" } " by " { $snippet "n" } "." } ;
123 { $values { "u" "a sequence of numbers" } { "n" "a number" } { "v" "a sequence of numbers" } }
124 { $description "Multiplies each element of " { $snippet "u" } " by " { $snippet "n" } "." } ;
127 { $values { "n" "a number" } { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
128 { $description "Divides " { $snippet "n" } " by each element of " { $snippet "u" } "." }
129 { $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
132 { $values { "u" "a sequence of numbers" } { "n" "a number" } { "v" "a sequence of numbers" } }
133 { $description "Divides each element of " { $snippet "u" } " by " { $snippet "n" } "." }
134 { $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
137 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
138 { $description "Adds " { $snippet "u" } " and " { $snippet "v" } " component-wise." } ;
141 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
142 { $description "Subtracts " { $snippet "v" } " from " { $snippet "u" } " component-wise." } ;
145 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
146 { $description "Adds and subtracts alternate elements of " { $snippet "v" } " and " { $snippet "u" } " component-wise." }
149 "USING: math.vectors prettyprint ;"
150 "{ 1 2 3 } { 2 3 2 } v+- ."
156 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
157 { $description "Subtracts " { $snippet "v" } " from " { $snippet "u" } " component-wise; any components which become negative are set to zero." } ;
160 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
161 { $description "Multiplies " { $snippet "u" } " and " { $snippet "v" } " component-wise." } ;
164 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
165 { $description "Divides " { $snippet "u" } " by " { $snippet "v" } " component-wise." }
166 { $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
169 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
170 { $description "Creates a sequence where each element is the maximum of the corresponding elements from " { $snippet "u" } " and " { $snippet "v" } "." }
171 { $examples { $example "USING: math.vectors prettyprint ;" "{ 1 2 5 } { -7 6 3 } vmax ." "{ 1 6 5 }" } } ;
174 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
175 { $description "Creates a sequence where each element is the minimum of the corresponding elements from " { $snippet "u" } " and " { $snippet "v" } "." }
176 { $examples { $example "USING: math.vectors prettyprint ;" "{ 1 2 5 } { -7 6 3 } vmin ." "{ -7 2 3 }" } } ;
179 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "x" "a real number" } }
180 { $description "Computes the dot product of two vectors." } ;
183 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
184 { $description "Adds " { $snippet "u" } " and " { $snippet "v" } " component-wise with saturation." }
188 "USING: alien.c-types math.vectors prettyprint specialized-arrays ;"
189 "SPECIALIZED-ARRAY: uchar"
190 "uchar-array{ 100 200 150 } uchar-array{ 70 70 70 } vs+ ."
191 "uchar-array{ 170 255 220 }"
193 "Without saturation:"
195 "USING: alien.c-types math.vectors prettyprint specialized-arrays ;"
196 "SPECIALIZED-ARRAY: uchar"
197 "uchar-array{ 100 200 150 } uchar-array{ 70 70 70 } v+ ."
198 "uchar-array{ 170 14 220 }"
203 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
204 { $description "Subtracts " { $snippet "v" } " from " { $snippet "u" } " component-wise with saturation." } ;
207 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
208 { $description "Multiplies " { $snippet "u" } " and " { $snippet "v" } " component-wise with saturation." } ;
211 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
212 { $description "Takes the bitwise and of " { $snippet "u" } " and " { $snippet "v" } " component-wise." }
213 { $notes "Unlike " { $link bitand } ", this word may be used on a specialized array of floats or doubles, in which case the bitwise representation of the floating point numbers is operated upon." } ;
216 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
217 { $description "Takes the bitwise and-not of " { $snippet "u" } " and " { $snippet "v" } " component-wise, where " { $snippet "x and-not y" } " is defined as " { $snippet "not(x) and y" } "." }
218 { $notes "This word may be used on a specialized array of floats or doubles, in which case the bitwise representation of the floating point numbers is operated upon." } ;
221 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
222 { $description "Takes the bitwise or of " { $snippet "u" } " and " { $snippet "v" } " component-wise." }
223 { $notes "Unlike " { $link bitor } ", this word may be used on a specialized array of floats or doubles, in which case the bitwise representation of the floating point numbers is operated upon." } ;
226 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
227 { $description "Takes the bitwise exclusive or of " { $snippet "u" } " and " { $snippet "v" } " component-wise." }
228 { $notes "Unlike " { $link bitxor } ", this word may be used on a specialized array of floats or doubles, in which case the bitwise representation of the floating point numbers is operated upon." } ;
231 { $values { "u" "a sequence of integers" } { "n" "a non-negative integer" } { "w" "a sequence of integers" } }
232 { $description "Shifts each element of " { $snippet "u" } " to the left by " { $snippet "n" } " bits." }
233 { $notes "Undefined behavior will result if " { $snippet "n" } " is negative." } ;
236 { $values { "u" "a sequence of integers" } { "n" "a non-negative integer" } { "w" "a sequence of integers" } }
237 { $description "Shifts each element of " { $snippet "u" } " to the right by " { $snippet "n" } " bits." }
238 { $notes "Undefined behavior will result if " { $snippet "n" } " is negative." } ;
241 { $values { "u" "a SIMD array" } { "n" "a non-negative integer" } { "w" "a SIMD array" } }
242 { $description "Shifts the entire SIMD array to the left by " { $snippet "n" } " bytes. This word may only be used in a context where the compiler can statically infer that the input is a SIMD array." } ;
245 { $values { "u" "a SIMD array" } { "n" "a non-negative integer" } { "w" "a SIMD array" } }
246 { $description "Shifts the entire SIMD array to the right by " { $snippet "n" } " bytes. This word may only be used in a context where the compiler can statically infer that the input is a SIMD array." } ;
249 { $values { "u" "a SIMD array" } { "n" "a non-negative integer" } { "v" "a SIMD array" } }
250 { $description "Outputs a new SIMD array of the same type as " { $snippet "u" } " where every element is equal to the " { $snippet "n" } "th element of " { $snippet "u" } "." }
253 "USING: alien.c-types math.vectors math.vectors.simd" "prettyprint ;"
255 "int-4{ 69 42 911 13 } 2 vbroadcast ."
256 "int-4{ 911 911 911 911 }"
261 { $values { "u" "a SIMD array" } { "perm" "an array of integers" } { "v" "a SIMD array" } }
262 { $description "Permutes the elements of a SIMD array. Duplicate entries are allowed in the permutation." }
265 "USING: alien.c-types math.vectors math.vectors.simd" "prettyprint ;"
267 "int-4{ 69 42 911 13 } { 1 3 2 3 } vshuffle ."
268 "int-4{ 42 13 911 13 }"
273 { $values { "v" "a sequence of numbers" } { "x" "a non-negative real number" } }
274 { $description "Computes the squared length of a mathematical vector." } ;
277 { $values { "v" "a sequence of numbers" } { "x" "a non-negative real number" } }
278 { $description "Computes the length of a mathematical vector." } ;
281 { $values { "u" "a sequence of numbers, not all zero" } { "v" "a sequence of numbers" } }
282 { $description "Outputs a vector with the same direction as " { $snippet "u" } " but length 1." } ;
285 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "x" "a non-negative real number" } }
286 { $description "Outputs the Euclidean distance between two vectors." } ;
289 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "axis" "a sequence of 0/1" } { "w" "a sequence of numbers" } }
290 { $description "Using " { $snippet "w" } " as a template, creates a new sequence containing corresponding elements from " { $snippet "u" } " in place of 0, and corresponding elements from " { $snippet "v" } " in place of 1." }
291 { $examples { $example "USING: math.vectors prettyprint ;" "{ 1 2 3 } { 4 5 6 } { 0 1 0 } set-axis ." "{ 1 5 3 }" } } ;
294 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
295 { $description "Compares each corresponding element of " { $snippet "u" } " and " { $snippet "v" } ", returning " { $link t } " in the result vector when the former is less than the latter or " { $link f } " otherwise." } ;
298 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
299 { $description "Compares each corresponding element of " { $snippet "u" } " and " { $snippet "v" } ", returning " { $link t } " in the result vector when the former is less than or equal to the latter or " { $link f } " otherwise." } ;
302 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
303 { $description "Compares each corresponding element of " { $snippet "u" } " and " { $snippet "v" } ", returning " { $link t } " in the result vector when they are equal or " { $link f } " otherwise." } ;
306 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
307 { $description "Compares each corresponding element of " { $snippet "u" } " and " { $snippet "v" } ", returning " { $link t } " in the result vector when the former is greater than the latter or " { $link f } " otherwise." } ;
310 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
311 { $description "Compares each corresponding element of " { $snippet "u" } " and " { $snippet "v" } ", returning " { $link t } " in the result vector when the former is greater than or equal to the latter or " { $link f } " otherwise." } ;
314 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
315 { $description "Compares each corresponding element of " { $snippet "u" } " and " { $snippet "v" } ", returning " { $link t } " in the result vector when either value is Not-a-Number or " { $link f } " otherwise." } ;
318 { $values { "u" "a sequence of booleans" } { "v" "a sequence of booleans" } { "w" "a sequence of booleans" } }
319 { $description "Takes the logical AND of each corresponding element of " { $snippet "u" } " and " { $snippet "v" } "." } ;
322 { $values { "u" "a sequence of booleans" } { "v" "a sequence of booleans" } { "w" "a sequence of booleans" } }
323 { $description "Takes the logical OR of each corresponding element of " { $snippet "u" } " and " { $snippet "v" } "." } ;
326 { $values { "u" "a sequence of booleans" } { "v" "a sequence of booleans" } { "w" "a sequence of booleans" } }
327 { $description "Takes the logical XOR of each corresponding element of " { $snippet "u" } " and " { $snippet "v" } "." } ;
330 { $values { "u" "a sequence of booleans" } { "w" "a sequence of booleans" } }
331 { $description "Takes the logical NOT of each element of " { $snippet "u" } "." } ;
334 { $values { "u" "a sequence of numbers" } { "?" "a sequence of booleans" } { "u'" "a sequence of numbers" } }
335 { $description "Returns a copy of " { $snippet "u" } " with the elements for which the corresponding element of " { $snippet "?" } " is false replaced by zero." } ;
338 { $values { "?" "a sequence of booleans" } { "true" "a sequence of numbers" } { "false" "a sequence of numbers" } { "w" "a sequence of numbers" } }
339 { $description "Creates a new sequence by selecting elements from the " { $snippet "true" } " and " { $snippet "false" } " sequences based on whether the corresponding element of the " { $snippet "?" } " sequence is true or false." } ;
341 { 2map v+ v- v* v/ } related-words
343 { 2reduce v. } related-words
345 { vs+ vs- vs* } related-words
347 { v< v<= v= v> v>= vunordered? vand vor vxor vnot vmask v? } related-words
349 { vbitand vbitandn vbitor vbitxor vbitnot } related-words