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