]> gitweb.factorcode.org Git - factor.git/blob - basis/math/vectors/vectors-docs.factor
break math.vectors docs into subsections
[factor.git] / basis / math / vectors / vectors-docs.factor
1 USING: help.markup help.syntax math math.functions sequences ;
2 IN: math.vectors
3
4 ARTICLE: "math-vectors-arithmetic" "Vector arithmetic"
5 "Vector/vector binary operations:"
6 { $subsection v+ }
7 { $subsection v- }
8 { $subsection v+- }
9 { $subsection v* }
10 { $subsection v/ }
11 "Vector unary operations:"
12 { $subsection vneg }
13 { $subsection vabs }
14 { $subsection vsqrt }
15 { $subsection vfloor }
16 { $subsection vceiling }
17 { $subsection vtruncate }
18 "Vector/scalar and scalar/vector binary operations:"
19 { $subsection vneg }
20 { $subsection v*n }
21 { $subsection n*v }
22 { $subsection v/n }
23 { $subsection n/v }
24 { $subsection v+n }
25 { $subsection n+v }
26 { $subsection v-n }
27 { $subsection n-v }
28 "Saturated arithmetic (only on " { $link "specialized-arrays" } "):"
29 { $subsection vs+ }
30 { $subsection vs- }
31 { $subsection vs* }
32 "Inner product and norm:"
33 { $subsection v. }
34 { $subsection norm }
35 { $subsection norm-sq }
36 { $subsection normalize }
37 "Comparing entire vectors:"
38 { $subsection distance }
39 { $subsection v~ } ;
40
41 ARTICLE: "math-vectors-logic" "Vector componentwise logic"
42 "Element comparisons:"
43 { $subsection v< }
44 { $subsection v<= }
45 { $subsection v= }
46 { $subsection v>= }
47 { $subsection v> }
48 { $subsection vunordered? }
49 { $subsection vmax }
50 { $subsection vmin }
51 { $subsection vsupremum }
52 { $subsection vinfimum }
53 "Bitwise operations:"
54 { $subsection vbitand }
55 { $subsection vbitandn }
56 { $subsection vbitor }
57 { $subsection vbitxor }
58 { $subsection vlshift }
59 { $subsection vrshift }
60 "Element logical operations:"
61 { $subsection vand }
62 { $subsection vor }
63 { $subsection vxor }
64 { $subsection vmask }
65 { $subsection v? }
66 "Element shuffling:"
67 { $subsection vshuffle } ;
68
69 ARTICLE: "math-vectors-misc" "Miscellaneous vector functions"
70 { $subsection trilerp }
71 { $subsection bilerp }
72 { $subsection vlerp }
73 { $subsection vnlerp }
74 { $subsection vbilerp } ;
75
76
77 ARTICLE: "math-vectors" "Vector operations"
78 "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."
79 { $subsection "math-vectors-arithmetic" }
80 { $subsection "math-vectors-logic" }
81 { $subsection "math-vectors-misc" } ;
82
83 ABOUT: "math-vectors"
84
85 HELP: vneg
86 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
87 { $description "Negates each element of " { $snippet "u" } "." } ;
88
89 HELP: vabs
90 { $values { "u" "a sequence of numbers" } { "v" "a sequence of non-negative real numbers" } }
91 { $description "Takes the absolute value of each element of " { $snippet "u" } "." } ;
92
93 HELP: vsqrt
94 { $values { "u" "a sequence of non-negative real numbers" } { "v" "a sequence of non-negative real numbers" } }
95 { $description "Takes the square root of each element of " { $snippet "u" } "." }
96 { $warning "For performance reasons, this does not work with negative inputs, unlike " { $link sqrt } "." } ;
97
98 HELP: vfloor
99 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } }
100 { $description "Takes the " { $link floor } " of each element of " { $snippet "u" } "." } ;
101
102 HELP: vceiling
103 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } }
104 { $description "Takes the " { $link ceiling } " of each element of " { $snippet "u" } "." } ;
105
106 HELP: vtruncate
107 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } }
108 { $description "Truncates each element of " { $snippet "u" } "." } ;
109
110 HELP: n+v
111 { $values { "n" "a number" } { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
112 { $description "Adds " { $snippet "n" } " to each element of " { $snippet "u" } "." } ;
113
114 HELP: v+n
115 { $values { "u" "a sequence of numbers" } { "n" "a number" } { "v" "a sequence of numbers" } }
116 { $description "Adds " { $snippet "n" } " to each element of " { $snippet "u" } "." } ;
117
118 HELP: n-v
119 { $values { "n" "a number" } { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
120 { $description "Subtracts each element of " { $snippet "u" } " from " { $snippet "n" } "." } ;
121
122 HELP: v-n
123 { $values { "u" "a sequence of numbers" } { "n" "a number" } { "v" "a sequence of numbers" } }
124 { $description "Subtracts " { $snippet "n" } " from each element of " { $snippet "u" } "." } ;
125
126 HELP: n*v
127 { $values { "n" "a number" } { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
128 { $description "Multiplies each element of " { $snippet "u" } " by " { $snippet "n" } "." } ;
129
130 HELP: v*n
131 { $values { "u" "a sequence of numbers" } { "n" "a number" } { "v" "a sequence of numbers" } }
132 { $description "Multiplies each element of " { $snippet "u" } " by " { $snippet "n" } "." } ;
133
134 HELP: n/v
135 { $values { "n" "a number" } { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } }
136 { $description "Divides " { $snippet "n" } " by each element of " { $snippet "u" } "." }
137 { $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
138
139 HELP: v/n
140 { $values { "u" "a sequence of numbers" } { "n" "a number" } { "v" "a sequence of numbers" } }
141 { $description "Divides each element of " { $snippet "u" } " by " { $snippet "n" } "." }
142 { $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
143
144 HELP: v+
145 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
146 { $description "Adds " { $snippet "u" } " and " { $snippet "v" } " component-wise." } ;
147
148 HELP: v-
149 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
150 { $description "Subtracts " { $snippet "v" } " from " { $snippet "u" } " component-wise." } ;
151
152 HELP: v+-
153 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
154 { $description "Adds and subtracts alternate elements of " { $snippet "v" } " and " { $snippet "u" } " component-wise." }
155 { $examples
156     { $example
157         "USING: math.vectors prettyprint ;"
158         "{ 1 2 3 } { 2 3 2 } v+- ."
159         "{ -1 5 1 }"
160     }
161 } ;
162
163 HELP: [v-]
164 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
165 { $description "Subtracts " { $snippet "v" } " from " { $snippet "u" } " component-wise; any components which become negative are set to zero." } ;
166
167 HELP: v*
168 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
169 { $description "Multiplies " { $snippet "u" } " and " { $snippet "v" } " component-wise." } ;
170
171 HELP: v/
172 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
173 { $description "Divides " { $snippet "u" } " by " { $snippet "v" } " component-wise." }
174 { $errors "May throw an error if a division by zero occurs; see " { $link "division-by-zero" } "." } ;
175
176 HELP: vmax
177 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
178 { $description "Creates a sequence where each element is the maximum of the corresponding elements from " { $snippet "u" } " and " { $snippet "v" } "." }
179 { $examples { $example "USING: math.vectors prettyprint ;" "{ 1 2 5 } { -7 6 3 } vmax ." "{ 1 6 5 }" } } ;
180
181 HELP: vmin
182 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
183 { $description "Creates a sequence where each element is the minimum of the corresponding elements from " { $snippet "u" } " and " { $snippet "v" } "." }
184 { $examples { $example "USING: math.vectors prettyprint ;" "{ 1 2 5 } { -7 6 3 } vmin ." "{ -7 2 3 }" } } ;
185
186 HELP: v.
187 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "x" "a real number" } }
188 { $description "Computes the dot product of two vectors." } ;
189
190 HELP: vs+
191 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
192 { $description "Adds " { $snippet "u" } " and " { $snippet "v" } " component-wise with saturation." }
193 { $examples
194     "With saturation:"
195     { $example
196         "USING: alien.c-types math.vectors prettyprint specialized-arrays ;"
197         "SPECIALIZED-ARRAY: uchar"
198         "uchar-array{ 100 200 150 } uchar-array{ 70 70 70 } vs+ ."
199         "uchar-array{ 170 255 220 }"
200     }
201     "Without saturation:"
202     { $example
203         "USING: alien.c-types math.vectors prettyprint specialized-arrays ;"
204         "SPECIALIZED-ARRAY: uchar"
205         "uchar-array{ 100 200 150 } uchar-array{ 70 70 70 } v+ ."
206         "uchar-array{ 170 14 220 }"
207     }
208 } ;
209
210 HELP: vs-
211 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
212 { $description "Subtracts " { $snippet "v" } " from " { $snippet "u" } " component-wise with saturation." } ;
213
214 HELP: vs*
215 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of numbers" } }
216 { $description "Multiplies " { $snippet "u" } " and " { $snippet "v" } " component-wise with saturation." } ;
217
218 HELP: vbitand
219 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
220 { $description "Takes the bitwise and of " { $snippet "u" } " and " { $snippet "v" } " component-wise." }
221 { $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." } ;
222
223 HELP: vbitandn
224 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
225 { $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" } "." }
226 { $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." } ;
227
228 HELP: vbitor
229 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
230 { $description "Takes the bitwise or of " { $snippet "u" } " and " { $snippet "v" } " component-wise." }
231 { $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." } ;
232
233 HELP: vbitxor
234 { $values { "u" "a sequence of real numbers" } { "v" "a sequence of real numbers" } { "w" "a sequence of real numbers" } }
235 { $description "Takes the bitwise exclusive or of " { $snippet "u" } " and " { $snippet "v" } " component-wise." }
236 { $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." } ;
237
238 HELP: vlshift
239 { $values { "u" "a sequence of integers" } { "n" "a non-negative integer" } { "w" "a sequence of integers" } }
240 { $description "Shifts each element of " { $snippet "u" } " to the left by " { $snippet "n" } " bits." }
241 { $notes "Undefined behavior will result if " { $snippet "n" } " is negative." } ;
242
243 HELP: vrshift
244 { $values { "u" "a sequence of integers" } { "n" "a non-negative integer" } { "w" "a sequence of integers" } }
245 { $description "Shifts each element of " { $snippet "u" } " to the right by " { $snippet "n" } " bits." }
246 { $notes "Undefined behavior will result if " { $snippet "n" } " is negative." } ;
247
248 HELP: hlshift
249 { $values { "u" "a SIMD array" } { "n" "a non-negative integer" } { "w" "a SIMD array" } }
250 { $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." } ;
251
252 HELP: hrshift
253 { $values { "u" "a SIMD array" } { "n" "a non-negative integer" } { "w" "a SIMD array" } }
254 { $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." } ;
255
256 HELP: vbroadcast
257 { $values { "u" "a SIMD array" } { "n" "a non-negative integer" } { "v" "a SIMD array" } }
258 { $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" } "." }
259 { $examples
260     { $example
261         "USING: alien.c-types math.vectors math.vectors.simd" "prettyprint ;"
262         "SIMD: int"
263         "int-4{ 69 42 911 13 } 2 vbroadcast ."
264         "int-4{ 911 911 911 911 }"
265     }
266 } ;
267
268 HELP: vshuffle
269 { $values { "u" "a SIMD array" } { "perm" "an array of integers" } { "v" "a SIMD array" } }
270 { $description "Permutes the elements of a SIMD array. Duplicate entries are allowed in the permutation." }
271 { $examples
272     { $example
273         "USING: alien.c-types math.vectors math.vectors.simd" "prettyprint ;"
274         "SIMD: int"
275         "int-4{ 69 42 911 13 } { 1 3 2 3 } vshuffle ."
276         "int-4{ 42 13 911 13 }"
277     }
278 } ;
279
280 HELP: norm-sq
281 { $values { "v" "a sequence of numbers" } { "x" "a non-negative real number" } }
282 { $description "Computes the squared length of a mathematical vector." } ;
283
284 HELP: norm
285 { $values { "v" "a sequence of numbers" } { "x" "a non-negative real number" } }
286 { $description "Computes the length of a mathematical vector." } ;
287
288 HELP: normalize
289 { $values { "u" "a sequence of numbers, not all zero" } { "v" "a sequence of numbers" } }
290 { $description "Outputs a vector with the same direction as " { $snippet "u" } " but length 1." } ;
291
292 HELP: distance
293 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "x" "a non-negative real number" } }
294 { $description "Outputs the Euclidean distance between two vectors." } ;
295
296 HELP: set-axis
297 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "axis" "a sequence of 0/1" } { "w" "a sequence of numbers" } }
298 { $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." }
299 { $examples { $example "USING: math.vectors prettyprint ;" "{ 1 2 3 } { 4 5 6 } { 0 1 0 } set-axis ." "{ 1 5 3 }" } } ;
300
301 HELP: v<
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 the former is less than the latter or " { $link f } " otherwise." } ;
304
305 HELP: v<=
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 less than or equal to the latter or " { $link f } " otherwise." } ;
308
309 HELP: v=
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 they are equal or " { $link f } " otherwise." } ;
312
313 HELP: v>
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 the former is greater than the latter or " { $link f } " otherwise." } ;
316
317 HELP: v>=
318 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
319 { $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." } ;
320
321 HELP: vunordered?
322 { $values { "u" "a sequence of numbers" } { "v" "a sequence of numbers" } { "w" "a sequence of booleans" } }
323 { $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." } ;
324
325 HELP: vand
326 { $values { "u" "a sequence of booleans" } { "v" "a sequence of booleans" } { "w" "a sequence of booleans" } }
327 { $description "Takes the logical AND of each corresponding element of " { $snippet "u" } " and " { $snippet "v" } "." } ;
328
329 HELP: vor
330 { $values { "u" "a sequence of booleans" } { "v" "a sequence of booleans" } { "w" "a sequence of booleans" } }
331 { $description "Takes the logical OR of each corresponding element of " { $snippet "u" } " and " { $snippet "v" } "." } ;
332
333 HELP: vxor
334 { $values { "u" "a sequence of booleans" } { "v" "a sequence of booleans" } { "w" "a sequence of booleans" } }
335 { $description "Takes the logical XOR of each corresponding element of " { $snippet "u" } " and " { $snippet "v" } "." } ;
336
337 HELP: vnot
338 { $values { "u" "a sequence of booleans" } { "w" "a sequence of booleans" } }
339 { $description "Takes the logical NOT of each element of " { $snippet "u" } "." } ;
340
341 HELP: vmask
342 { $values { "u" "a sequence of numbers" } { "?" "a sequence of booleans" } { "u'" "a sequence of numbers" } }
343 { $description "Returns a copy of " { $snippet "u" } " with the elements for which the corresponding element of " { $snippet "?" } " is false replaced by zero." } ;
344
345 HELP: v?
346 { $values { "?" "a sequence of booleans" } { "true" "a sequence of numbers" } { "false" "a sequence of numbers" } { "w" "a sequence of numbers" } }
347 { $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." } ;
348
349 { 2map v+ v- v* v/ } related-words
350
351 { 2reduce v. } related-words
352
353 { vs+ vs- vs* } related-words
354
355 { v< v<= v= v> v>= vunordered? vand vor vxor vnot vmask v? } related-words
356
357 { vbitand vbitandn vbitor vbitxor vbitnot } related-words