]> gitweb.factorcode.org Git - factor.git/blob - basis/math/functions/functions-docs.factor
benchmark.sockets: don't hang if an error occurs
[factor.git] / basis / math / functions / functions-docs.factor
1 USING: help.markup help.syntax kernel math math.order
2 sequences quotations math.functions.private ;
3 IN: math.functions
4
5 ARTICLE: "integer-functions" "Integer functions"
6 { $subsection align }
7 { $subsection gcd }
8 { $subsection log2 }
9 { $subsection next-power-of-2 }
10 "Modular exponentiation:"
11 { $subsection ^mod }
12 { $subsection mod-inv }
13 "Tests:"
14 { $subsection power-of-2? }
15 { $subsection even? }
16 { $subsection odd? }
17 { $subsection divisor? } ;
18
19 ARTICLE: "arithmetic-functions" "Arithmetic functions"
20 "Computing additive and multiplicative inverses:"
21 { $subsection neg }
22 { $subsection recip }
23 "Complex conjugation:"
24 { $subsection conjugate }
25 "Tests:"
26 { $subsection zero? }
27 { $subsection between? }
28 "Control flow:"
29 { $subsection if-zero }
30 { $subsection when-zero }
31 { $subsection unless-zero }
32 "Sign:"
33 { $subsection sgn }
34 "Rounding:"
35 { $subsection ceiling }
36 { $subsection floor }
37 { $subsection truncate }
38 { $subsection round }
39 "Inexact comparison:"
40 { $subsection ~ }
41 "Numbers implement the " { $link "math.order" } ", therefore operations such as " { $link min } " and " { $link max } " can be used with numbers." ;
42
43 ARTICLE: "power-functions" "Powers and logarithms"
44 "Squares:"
45 { $subsection sq }
46 { $subsection sqrt }
47 "Exponential and natural logarithm:"
48 { $subsection exp }
49 { $subsection cis }
50 { $subsection log }
51 "Other logarithms:"
52 { $subsection log1+ }
53 { $subsection log10 }
54 "Raising a number to a power:"
55 { $subsection ^ }
56 { $subsection 10^ }
57 "Converting between rectangular and polar form:"
58 { $subsection abs }
59 { $subsection absq }
60 { $subsection arg }
61 { $subsection >polar }
62 { $subsection polar> } ;
63
64 ARTICLE: "trig-hyp-functions" "Trigonometric and hyperbolic functions"
65 "Trigonometric functions:"
66 { $subsection cos }
67 { $subsection sin }
68 { $subsection tan }
69 "Reciprocals:"
70 { $subsection sec }
71 { $subsection cosec }
72 { $subsection cot }
73 "Inverses:"
74 { $subsection acos }
75 { $subsection asin }
76 { $subsection atan }
77 "Inverse reciprocals:"
78 { $subsection asec }
79 { $subsection acosec }
80 { $subsection acot }
81 "Hyperbolic functions:"
82 { $subsection cosh }
83 { $subsection sinh }
84 { $subsection tanh }
85 "Reciprocals:"
86 { $subsection sech }
87 { $subsection cosech }
88 { $subsection coth }
89 "Inverses:"
90 { $subsection acosh }
91 { $subsection asinh }
92 { $subsection atanh }
93 "Inverse reciprocals:"
94 { $subsection asech }
95 { $subsection acosech }
96 { $subsection acoth } ;
97
98 ARTICLE: "math-functions" "Mathematical functions"
99 { $subsection "integer-functions" }
100 { $subsection "arithmetic-functions" }
101 { $subsection "power-functions" }
102 { $subsection "trig-hyp-functions" } ;
103
104 ABOUT: "math-functions"
105
106 HELP: rect>
107 { $values { "x" real } { "y" real } { "z" number } }
108 { $description "Creates a complex number from real and imaginary components. If " { $snippet "z" } " is an integer zero, this will simply output " { $snippet "x" } "." } ;
109
110 HELP: >rect
111 { $values { "z" number } { "x" real } { "y" real } }
112 { $description "Extracts the real and imaginary components of a complex number." } ;
113
114 HELP: align
115 { $values { "m" integer } { "w" "a power of 2" } { "n" "an integer multiple of " { $snippet "w" } } }
116 { $description "Outputs the least multiple of " { $snippet "w" } " greater than " { $snippet "m" } "." }
117 { $notes "This word will give an incorrect result if " { $snippet "w" } " is not a power of 2." } ;
118
119 HELP: exp
120 { $values { "x" number } { "y" number } }
121 { $description "Exponential function, " { $snippet "y=e^x" } "." } ;
122
123 HELP: log
124 { $values { "x" number } { "y" number } }
125 { $description "Natural logarithm function. Outputs negative infinity if " { $snippet "x" } " is 0." } ;
126
127 HELP: log1+
128 { $values { "x" number } { "y" number } }
129 { $description "Takes the natural logarithm of " { $snippet "1 + x" } ". Outputs negative infinity if " { $snippet "1 + x" } " is zero. This word may be more accurate than " { $snippet "1 + log" } " for very small values of " { $snippet "x" } "." } ;
130
131 HELP: log10
132 { $values { "x" number } { "y" number } }
133 { $description "Logarithm function base 10. Outputs negative infinity if " { $snippet "x" } " is 0." } ;
134
135 HELP: sqrt
136 { $values { "x" number } { "y" number } }
137 { $description "Square root function." } ;
138
139 HELP: cosh
140 $values-x/y
141 { $description "Hyperbolic cosine." } ;
142
143 HELP: sech
144 $values-x/y
145 { $description "Hyperbolic secant." } ;
146
147 HELP: sinh
148 $values-x/y
149 { $description "Hyperbolic sine." } ;
150
151 HELP: cosech
152 $values-x/y
153 { $description "Hyperbolic cosecant." } ;
154
155 HELP: tanh
156 $values-x/y
157 { $description "Hyperbolic tangent." } ;
158
159 HELP: coth
160 $values-x/y
161 { $description "Hyperbolic cotangent." } ;
162
163 HELP: cos
164 $values-x/y
165 { $description "Trigonometric cosine." } ;
166
167 HELP: sec
168 $values-x/y
169 { $description "Trigonometric secant." } ;
170
171 HELP: sin
172 $values-x/y
173 { $description "Trigonometric sine." } ;
174
175 HELP: cosec
176 $values-x/y
177 { $description "Trigonometric cosecant." } ;
178
179 HELP: tan
180 $values-x/y
181 { $description "Trigonometric tangent." } ;
182
183 HELP: cot
184 $values-x/y
185 { $description "Trigonometric cotangent." } ;
186
187 HELP: acosh
188 $values-x/y
189 { $description "Inverse hyperbolic cosine." } ;
190
191 HELP: asech
192 $values-x/y
193 { $description "Inverse hyperbolic secant." } ;
194
195 HELP: asinh
196 $values-x/y
197 { $description "Inverse hyperbolic sine." } ;
198
199 HELP: acosech
200 $values-x/y
201 { $description "Inverse hyperbolic cosecant." } ;
202
203 HELP: atanh
204 $values-x/y
205 { $description "Inverse hyperbolic tangent." } ;
206
207 HELP: acoth
208 $values-x/y
209 { $description "Inverse hyperbolic cotangent." } ;
210
211 HELP: acos
212 $values-x/y
213 { $description "Inverse trigonometric cosine." } ;
214
215 HELP: asec
216 $values-x/y
217 { $description "Inverse trigonometric secant." } ;
218
219 HELP: asin
220 $values-x/y
221 { $description "Inverse trigonometric sine." } ;
222
223 HELP: acosec
224 $values-x/y
225 { $description "Inverse trigonometric cosecant." } ;
226
227 HELP: atan
228 $values-x/y
229 { $description "Inverse trigonometric tangent." } ;
230
231 HELP: acot
232 $values-x/y
233 { $description "Inverse trigonometric cotangent." } ;
234
235 HELP: conjugate
236 { $values { "z" number } { "z*" number } }
237 { $description "Computes the complex conjugate by flipping the sign of the imaginary part of " { $snippet "z" } "." } ;
238
239 HELP: arg
240 { $values { "z" number } { "arg" "a number in the interval " { $snippet "(-pi,pi]" } } }
241 { $description "Computes the complex argument." } ;
242
243 HELP: >polar
244 { $values { "z" number } { "abs" "a non-negative real number" } { "arg" "a number in the interval " { $snippet "(-pi,pi]" } } }
245 { $description "Converts a complex number into an absolute value and argument (polar form)." } ;
246
247 HELP: cis
248 { $values { "arg" "a real number" } { "z" "a complex number on the unit circle" } }
249 { $description "Computes a point on the unit circle using Euler's formula for " { $snippet "exp(arg*i)" } "." } ;
250
251 { cis exp } related-words
252
253 HELP: polar>
254 { $values { "z" number } { "abs" "a non-negative real number" } { "arg" real } }
255 { $description "Converts an absolute value and argument (polar form) to a complex number." } ;
256
257 HELP: [-1,1]?
258 { $values { "x" number } { "?" "a boolean" } }
259 { $description "Tests if " { $snippet "x" } " is a real number between -1 and 1, inclusive." } ;
260
261 HELP: abs
262 { $values { "x" number } { "y" "a non-negative real number" } }
263 { $description "Computes the absolute value of a complex number." } ;
264
265 HELP: absq
266 { $values { "x" number } { "y" "a non-negative real number" } }
267 { $description "Computes the squared absolute value of a complex number. This is marginally more efficient than " { $link abs } "." } ;
268
269 HELP: ^
270 { $values { "x" number } { "y" number } { "z" number } }
271 { $description "Raises " { $snippet "x" } " to the power of " { $snippet "y" } ". If " { $snippet "y" } " is an integer the answer is computed exactly, otherwise a floating point approximation is used." }
272 { $errors "Throws an error if " { $snippet "x" } " and " { $snippet "y" } " are both integer 0." } ;
273
274 HELP: 10^
275 { $values { "x" number } { "y" number } }
276 { $description "Raises " { $snippet "x" } " to the power of 10. If " { $snippet "x" } " is an integer the answer is computed exactly, otherwise a floating point approximation is used." } ;
277
278 HELP: gcd
279 { $values { "x" integer } { "y" integer } { "a" integer } { "d" integer } }
280 { $description "Computes the positive greatest common divisor " { $snippet "d" } " of " { $snippet "x" } " and " { $snippet "y" } ", and another value " { $snippet "a" } " satisfying:" { $code "a*y = d mod x" } }
281 { $notes "If " { $snippet "d" } " is 1, then " { $snippet "a" } " is the inverse of " { $snippet "y" } " modulo " { $snippet "x" } "." } ;
282
283 HELP: divisor?
284 { $values { "m" integer } { "n" integer } { "?" "a boolean" } }
285 { $description "Tests if " { $snippet "n" } " is a divisor of " { $snippet "m" } ". This is the same thing as asking if " { $snippet "m" } " is divisible by " { $snippet "n" } "." }
286 { $notes "Returns t for both negative and positive divisors, as well as for trivial and non-trivial divisors." } ;
287
288 HELP: mod-inv
289 { $values { "x" integer } { "n" integer } { "y" integer } }
290 { $description "Outputs an integer " { $snippet "y" } " such that " { $snippet "xy = 1 (mod n)" } "." }
291 { $errors "Throws an error if " { $snippet "n" } " is not invertible modulo " { $snippet "n" } "." }
292 { $examples
293     { $example "USING: math.functions prettyprint ;" "173 1119 mod-inv ." "815" }
294     { $example "USING: math prettyprint ;" "173 815 * 1119 mod ." "1" }
295 } ;
296
297 HELP: ~
298 { $values { "x" real } { "y" real } { "epsilon" real } { "?" "a boolean" } }
299 { $description "Tests if " { $snippet "x" } " and " { $snippet "y" } " are approximately equal to each other. There are three possible comparison tests, chosen based on the sign of " { $snippet "epsilon" } ":"
300     { $list
301         { { $snippet "epsilon" } " is zero: exact comparison." }
302         { { $snippet "epsilon" } " is positive: absolute distance test." }
303         { { $snippet "epsilon" } " is negative: relative distance test." }
304     }
305 } ;
306
307
308 HELP: truncate
309 { $values { "x" real } { "y" "a whole real number" } }
310 { $description "Outputs the number that results from subtracting the fractional component of " { $snippet "x" } "." }
311 { $notes "The result is not necessarily an integer." } ;
312
313 HELP: floor
314 { $values { "x" real } { "y" "a whole real number" } }
315 { $description "Outputs the greatest whole number smaller than or equal to " { $snippet "x" } "." }
316 { $notes "The result is not necessarily an integer." } ;
317
318 HELP: ceiling
319 { $values { "x" real } { "y" "a whole real number" } }
320 { $description "Outputs the least whole number greater than or equal to " { $snippet "x" } "." }
321 { $notes "The result is not necessarily an integer." } ;
322
323 HELP: round
324 { $values { "x" real } { "y" "a whole real number" } }
325 { $description "Outputs the whole number closest to " { $snippet "x" } "." }
326 { $notes "The result is not necessarily an integer." } ;