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