1 USING: help.markup help.syntax math math.functions sequences ;
4 ARTICLE: "math-vectors-arithmetic" "Vector arithmetic"
5 "Vector/vector binary operations:"
13 "Vector unary operations:"
22 "Vector/scalar and scalar/vector binary operations:"
34 "Saturated arithmetic (only on " { $link "specialized-arrays" } "):"
40 "Inner product and norm:"
47 "Comparing entire vectors:"
53 ARTICLE: "math-vectors-logic" "Vector componentwise logic"
54 "Element comparisons:"
76 "Element logical operations:"
85 { $subsections vshuffle } ;
87 ARTICLE: "math-vectors-misc" "Miscellaneous vector functions"
97 ARTICLE: "math-vectors" "Vector operations"
98 "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."
100 "math-vectors-arithmetic"
105 ABOUT: "math-vectors"
108 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
109 { $description "Negates each element of " { $snippet "u" } "." } ;
112 { $values { "u" "a sequence of numbers" } { "v" "a sequence of non-negative real numbers" } }
113 { $description "Takes the absolute value of each element of " { $snippet "u" } "." } ;
116 { $values { "u" "a sequence of non-negative real numbers" } { "v" "a sequence of non-negative real numbers" } }
117 { $description "Takes the square root of each element of " { $snippet "u" } "." }
118 { $warning "For performance reasons, this does not work with negative inputs, unlike " { $link sqrt } "." } ;
121 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } }
122 { $description "Takes the " { $link floor } " of each element of " { $snippet "u" } "." } ;
125 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } }
126 { $description "Takes the " { $link ceiling } " of each element of " { $snippet "u" } "." } ;
129 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } }
130 { $description "Truncates each element of " { $snippet "u" } "." } ;
133 { $values { "n" "a number" } { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
134 { $description "Adds " { $snippet "n" } " to each element of " { $snippet "u" } "." } ;
137 { $values { "u" "a sequence of numbers" } { "n" "a number" } { "v" "a sequence of numbers" } }
138 { $description "Adds " { $snippet "n" } " to each element of " { $snippet "u" } "." } ;
141 { $values { "n" "a number" } { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
142 { $description "Subtracts each element of " { $snippet "u" } " from " { $snippet "n" } "." } ;
145 { $values { "u" "a sequence of numbers" } { "n" "a number" } { "v" "a sequence of numbers" } }
146 { $description "Subtracts " { $snippet "n" } " from each element of " { $snippet "u" } "." } ;
149 { $values { "n" "a number" } { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
150 { $description "Multiplies each element of " { $snippet "u" } " by " { $snippet "n" } "." } ;
153 { $values { "u" "a sequence of numbers" } { "n" "a number" } { "v" "a sequence of numbers" } }
154 { $description "Multiplies each element of " { $snippet "u" } " by " { $snippet "n" } "." } ;
157 { $values { "n" "a number" } { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
158 { $description "Divides " { $snippet "n" } " by each element of " { $snippet "u" } "." }
159 { $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
162 { $values { "u" "a sequence of numbers" } { "n" "a number" } { "v" "a sequence of numbers" } }
163 { $description "Divides each element of " { $snippet "u" } " by " { $snippet "n" } "." }
164 { $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
167 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
168 { $description "Adds " { $snippet "u" } " and " { $snippet "v" } " component-wise." } ;
171 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
172 { $description "Subtracts " { $snippet "v" } " from " { $snippet "u" } " component-wise." } ;
175 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
176 { $description "Adds and subtracts alternate elements of " { $snippet "v" } " and " { $snippet "u" } " component-wise." }
179 "USING: math.vectors prettyprint ;"
180 "{ 1 2 3 } { 2 3 2 } v+- ."
186 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
187 { $description "Subtracts " { $snippet "v" } " from " { $snippet "u" } " component-wise; any components which become negative are set to zero." } ;
190 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
191 { $description "Multiplies " { $snippet "u" } " and " { $snippet "v" } " component-wise." } ;
194 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
195 { $description "Divides " { $snippet "u" } " by " { $snippet "v" } " component-wise." }
196 { $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
199 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
200 { $description "Creates a sequence where each element is the maximum of the corresponding elements from " { $snippet "u" } " and " { $snippet "v" } "." }
201 { $examples { $example "USING: math.vectors prettyprint ;" "{ 1 2 5 } { -7 6 3 } vmax ." "{ 1 6 5 }" } } ;
204 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
205 { $description "Creates a sequence where each element is the minimum of the corresponding elements from " { $snippet "u" } " and " { $snippet "v" } "." }
206 { $examples { $example "USING: math.vectors prettyprint ;" "{ 1 2 5 } { -7 6 3 } vmin ." "{ -7 2 3 }" } } ;
209 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "x" "a real number" } }
210 { $description "Computes the dot product of two vectors." } ;
213 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
214 { $description "Adds " { $snippet "u" } " and " { $snippet "v" } " component-wise with saturation." }
218 "USING: alien.c-types math.vectors prettyprint specialized-arrays ;"
219 "SPECIALIZED-ARRAY: uchar"
220 "uchar-array{ 100 200 150 } uchar-array{ 70 70 70 } vs+ ."
221 "uchar-array{ 170 255 220 }"
223 "Without saturation:"
225 "USING: alien.c-types math.vectors prettyprint specialized-arrays ;"
226 "SPECIALIZED-ARRAY: uchar"
227 "uchar-array{ 100 200 150 } uchar-array{ 70 70 70 } v+ ."
228 "uchar-array{ 170 14 220 }"
233 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
234 { $description "Subtracts " { $snippet "v" } " from " { $snippet "u" } " component-wise with saturation." } ;
237 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
238 { $description "Multiplies " { $snippet "u" } " and " { $snippet "v" } " component-wise with saturation." } ;
241 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
242 { $description "Takes the bitwise and of " { $snippet "u" } " and " { $snippet "v" } " component-wise." }
243 { $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." } ;
246 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
247 { $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" } "." }
248 { $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." } ;
251 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
252 { $description "Takes the bitwise or of " { $snippet "u" } " and " { $snippet "v" } " component-wise." }
253 { $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." } ;
256 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
257 { $description "Takes the bitwise exclusive or of " { $snippet "u" } " and " { $snippet "v" } " component-wise." }
258 { $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." } ;
261 { $values { "u" "a sequence of integers" } { "n" "a non-negative integer" } { "w" "a sequence of integers" } }
262 { $description "Shifts each element of " { $snippet "u" } " to the left by " { $snippet "n" } " bits." }
263 { $notes "Undefined behavior will result if " { $snippet "n" } " is negative." } ;
266 { $values { "u" "a sequence of integers" } { "n" "a non-negative integer" } { "w" "a sequence of integers" } }
267 { $description "Shifts each element of " { $snippet "u" } " to the right by " { $snippet "n" } " bits." }
268 { $notes "Undefined behavior will result if " { $snippet "n" } " is negative." } ;
271 { $values { "u" "a SIMD array" } { "n" "a non-negative integer" } { "w" "a SIMD array" } }
272 { $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." } ;
275 { $values { "u" "a SIMD array" } { "n" "a non-negative integer" } { "w" "a SIMD array" } }
276 { $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." } ;
279 { $values { "u" "a SIMD array" } { "n" "a non-negative integer" } { "v" "a SIMD array" } }
280 { $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" } "." }
283 "USING: alien.c-types math.vectors math.vectors.simd" "prettyprint ;"
285 "int-4{ 69 42 911 13 } 2 vbroadcast ."
286 "int-4{ 911 911 911 911 }"
291 { $values { "u" "a SIMD array" } { "perm" "an array of integers" } { "v" "a SIMD array" } }
292 { $description "Permutes the elements of a SIMD array. Duplicate entries are allowed in the permutation." }
295 "USING: alien.c-types math.vectors math.vectors.simd" "prettyprint ;"
297 "int-4{ 69 42 911 13 } { 1 3 2 3 } vshuffle ."
298 "int-4{ 42 13 911 13 }"
303 { $values { "v" "a sequence of numbers" } { "x" "a non-negative real number" } }
304 { $description "Computes the squared length of a mathematical vector." } ;
307 { $values { "v" "a sequence of numbers" } { "x" "a non-negative real number" } }
308 { $description "Computes the length of a mathematical vector." } ;
311 { $values { "u" "a sequence of numbers, not all zero" } { "v" "a sequence of numbers" } }
312 { $description "Outputs a vector with the same direction as " { $snippet "u" } " but length 1." } ;
315 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "x" "a non-negative real number" } }
316 { $description "Outputs the Euclidean distance between two vectors." } ;
319 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "axis" "a sequence of 0/1" } { "w" "a sequence of numbers" } }
320 { $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." }
321 { $examples { $example "USING: math.vectors prettyprint ;" "{ 1 2 3 } { 4 5 6 } { 0 1 0 } set-axis ." "{ 1 5 3 }" } } ;
324 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
325 { $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." } ;
328 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
329 { $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." } ;
332 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
333 { $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." } ;
336 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
337 { $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." } ;
340 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
341 { $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." } ;
344 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
345 { $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." } ;
348 { $values { "u" "a sequence of booleans" } { "v" "a sequence of booleans" } { "w" "a sequence of booleans" } }
349 { $description "Takes the logical AND of each corresponding element of " { $snippet "u" } " and " { $snippet "v" } "." } ;
352 { $values { "u" "a sequence of booleans" } { "v" "a sequence of booleans" } { "w" "a sequence of booleans" } }
353 { $description "Takes the logical OR of each corresponding element of " { $snippet "u" } " and " { $snippet "v" } "." } ;
356 { $values { "u" "a sequence of booleans" } { "v" "a sequence of booleans" } { "w" "a sequence of booleans" } }
357 { $description "Takes the logical XOR of each corresponding element of " { $snippet "u" } " and " { $snippet "v" } "." } ;
360 { $values { "u" "a sequence of booleans" } { "w" "a sequence of booleans" } }
361 { $description "Takes the logical NOT of each element of " { $snippet "u" } "." } ;
364 { $values { "u" "a sequence of numbers" } { "?" "a sequence of booleans" } { "u'" "a sequence of numbers" } }
365 { $description "Returns a copy of " { $snippet "u" } " with the elements for which the corresponding element of " { $snippet "?" } " is false replaced by zero." } ;
368 { $values { "?" "a sequence of booleans" } { "true" "a sequence of numbers" } { "false" "a sequence of numbers" } { "w" "a sequence of numbers" } }
369 { $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." } ;
371 { 2map v+ v- v* v/ } related-words
373 { 2reduce v. } related-words
375 { vs+ vs- vs* } related-words
377 { v< v<= v= v> v>= vunordered? vand vor vxor vnot vmask v? } related-words
379 { vbitand vbitandn vbitor vbitxor vbitnot } related-words