lib/core/math: add `Collection::sample`
[nit.git] / lib / core / math.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 #
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
11 # another product.
12
13 # Mathematical operations
14 module math is ldflags "-lm"
15
16 import kernel
17 import collection
18
19 in "C header" `{
20 #include <stdlib.h>
21 #include <math.h>
22 #include <time.h>
23 `}
24
25 in "C" `{
26 /* Is rand shortcut? */
27 static int nit_rand_seeded;
28 /* Current rand seed if seeded */
29 static unsigned int nit_rand_seed;
30
31 #define NIT_RAND_MAX 0x7fffffff
32
33 /* This algorithm is mentioned in the ISO C standard, here extended
34 for 32 bits. */
35 static int
36 nit_rand(void) {
37 unsigned int next = nit_rand_seed;
38 int result;
39
40 next *= 1103515245;
41 next += 12345;
42 result = (unsigned int) (next / 65536) % 2048;
43
44 next *= 1103515245;
45 next += 12345;
46 result <<= 10;
47 result ^= (unsigned int) (next / 65536) % 1024;
48
49 next *= 1103515245;
50 next += 12345;
51 result <<= 10;
52 result ^= (unsigned int) (next / 65536) % 1024;
53
54 nit_rand_seed = next;
55
56 return result;
57 }
58 `}
59
60 redef class Int
61 # Returns a random `Int` in `[0 .. self[`.
62 fun rand: Int `{
63 if (nit_rand_seeded) return (long)(((double)self)*nit_rand()/(NIT_RAND_MAX+1.0));
64 return (long)(((double)self)*rand()/(RAND_MAX+1.0));
65 `}
66
67 # Returns the result of a binary AND operation on `self` and `i`
68 #
69 # assert 0x10 & 0x01 == 0
70 fun &(i: Int): Int is intern do return band(i)
71
72 private fun band(i: Int): Int `{ return self & i; `}
73
74 # Returns the result of a binary OR operation on `self` and `i`
75 #
76 # assert 0x10 | 0x01 == 0x11
77 fun |(i: Int): Int is intern do return bor(i)
78
79 private fun bor(i: Int): Int `{ return self | i; `}
80
81 # Returns the result of a binary XOR operation on `self` and `i`
82 #
83 # assert 0x101 ^ 0x110 == 0x11
84 fun ^(i: Int): Int `{ return self ^ i; `}
85
86 # Returns the 1's complement of `self`
87 #
88 # assert ~0x2F == -48
89 fun ~: Int `{ return ~self; `}
90
91 # Returns the square root of `self`
92 #
93 # assert 16.sqrt == 4
94 fun sqrt: Int `{ return sqrt(self); `}
95
96 # Returns the greatest common divisor of `self` and `o`
97 #
98 # assert 54.gcd(24) == 6
99 # assert -54.gcd(-24) == 6
100 # assert 54.gcd(-24) == -6
101 # assert -54.gcd(24) == -6
102 # assert 12.gcd(6) == 6
103 fun gcd(o: Int): Int
104 do
105 if self < 0 then return -(-self).gcd(o)
106 if o < 0 then return -(self.gcd(-o))
107 if self == 0 or o == self then return o
108 if o == 0 then return self
109 if self & 1 == 0 then
110 if o & 1 == 1 then
111 return (self >> 1).gcd(o)
112 else
113 return (self >> 1).gcd(o >> 1) << 1
114 end
115 end
116 if o & 1 == 0 then return self.gcd(o >> 1)
117 if self > o then return ((self - o) >> 1).gcd(o)
118 return ((o - self) >> 1).gcd(self)
119 end
120
121 # Is `self` even ?
122 #
123 # assert 12.is_even
124 fun is_even: Bool do return self % 2 == 0
125
126 # Is `self` odd ?
127 #
128 # assert not 13.is_even
129 fun is_odd: Bool do return not is_even
130
131 # Is self a prime number ?
132 #
133 # assert 3.is_prime
134 # assert not 1.is_prime
135 # assert not 12.is_prime
136 fun is_prime: Bool
137 do
138 if self == 2 then
139 return true
140 else if self <= 1 or self.is_even then
141 return false
142 end
143 for i in [3..self.sqrt[ do
144 if self % i == 0 then return false
145 end
146 return true
147 end
148
149 # Returns the `self` raised to the power of `e`.
150 #
151 # assert 2 ** 3 == 8
152 fun **(e: Int): Int
153 do
154 return self.to_f.pow(e.to_f).to_i
155 end
156
157 # The factorial of `self` (aka `self!`)
158 #
159 # Returns `1 * 2 * 3 * ... * self-1 * self`
160 #
161 # assert 0.factorial == 1 # by convention for an empty product
162 # assert 1.factorial == 1
163 # assert 4.factorial == 24
164 # assert 9.factorial == 362880
165 fun factorial: Int
166 do
167 assert self >= 0
168 var res = 1
169 var n = self
170 while n > 0 do
171 res = res * n
172 n -= 1
173 end
174 return res
175 end
176 end
177
178 redef class Byte
179 # Returns the result of a binary AND operation on `self` and `i`
180 #
181 # assert 0x10u8 & 0x01u8 == 0u8
182 fun &(i: Byte): Byte is intern do return band(i)
183
184 private fun band(i: Byte): Byte `{ return self & i; `}
185
186 # Returns the result of a binary OR operation on `self` and `i`
187 #
188 # assert 0x10u8 | 0x01u8 == 0x11u8
189 fun |(i: Byte): Byte `{ return self | i; `}
190
191 # Returns the result of a binary XOR operation on `self` and `i`
192 #
193 # assert 0x101u8 ^ 0x110u8 == 0x11u8
194 fun ^(i: Byte): Byte `{ return self ^ i; `}
195
196 # Returns the 1's complement of `self`
197 #
198 # assert ~0x2Fu8 == 0xD0u8
199 fun ~: Byte `{ return ~self; `}
200 end
201
202 redef class Float
203
204 # Returns the non-negative square root of `self`.
205 #
206 # assert 9.0.sqrt == 3.0
207 # #assert 3.0.sqrt == 1.732
208 # assert 1.0.sqrt == 1.0
209 # assert 0.0.sqrt == 0.0
210 fun sqrt: Float `{ return sqrt(self); `}
211
212 # Computes the cosine of `self` (expressed in radians).
213 #
214 # #assert pi.cos == -1.0
215 fun cos: Float `{ return cos(self); `}
216
217 # Computes the sine of `self` (expressed in radians).
218 #
219 # #assert pi.sin == 0.0
220 fun sin: Float `{ return sin(self); `}
221
222 # Computes the cosine of x (expressed in radians).
223 #
224 # #assert 0.0.tan == 0.0
225 fun tan: Float `{ return tan(self); `}
226
227 # Computes the arc cosine of `self`.
228 #
229 # #assert 0.0.acos == pi / 2.0
230 fun acos: Float `{ return acos(self); `}
231
232 # Computes the arc sine of `self`.
233 #
234 # #assert 1.0.asin == pi / 2.0
235 fun asin: Float `{ return asin(self); `}
236
237 # Computes the arc tangent of `self`.
238 #
239 # #assert 0.0.tan == 0.0
240 fun atan: Float `{ return atan(self); `}
241
242 # Returns the absolute value of `self`.
243 #
244 # assert 12.0.abs == 12.0
245 # assert (-34.56).abs == 34.56
246 # assert -34.56.abs == -34.56
247 fun abs: Float `{ return fabs(self); `}
248
249 # Returns `self` raised at `e` power.
250 #
251 # #assert 2.0.pow(0.0) == 1.0
252 # #assert 2.0.pow(3.0) == 8.0
253 # #assert 0.0.pow(9.0) == 0.0
254 fun pow(e: Float): Float `{ return pow(self, e); `}
255
256 # Natural logarithm of `self`.
257 #
258 # assert 0.0.log.is_inf == -1
259 # #assert 1.0.log == 0.0
260 fun log: Float `{ return log(self); `}
261
262 # Logarithm of `self` to base `base`.
263 #
264 # assert 100.0.log_base(10.0) == 2.0
265 # assert 256.0.log_base(2.0) == 8.0
266 fun log_base(base: Float): Float do return log/base.log
267
268 # Returns *e* raised to `self`.
269 fun exp: Float `{ return exp(self); `}
270
271 # assert 1.1.ceil == 2.0
272 # assert 1.9.ceil == 2.0
273 # assert 2.0.ceil == 2.0
274 # assert (-1.5).ceil == -1.0
275 fun ceil: Float `{ return ceil(self); `}
276
277 # assert 1.1.floor == 1.0
278 # assert 1.9.floor == 1.0
279 # assert 2.0.floor == 2.0
280 # assert (-1.5).floor == -2.0
281 fun floor: Float `{ return floor(self); `}
282
283 # Rounds the value of a float to its nearest integer value
284 #
285 # assert 1.67.round == 2.0
286 # assert 1.34.round == 1.0
287 # assert -1.34.round == -1.0
288 # assert -1.67.round == -2.0
289 fun round: Float `{ return round(self); `}
290
291 # Returns a random `Float` in `[0.0 .. self[`.
292 fun rand: Float `{
293 if (nit_rand_seeded) return ((self)*nit_rand())/(NIT_RAND_MAX+1.0);
294 return ((self)*rand())/(RAND_MAX+1.0);
295 `}
296
297 # Returns the euclidean distance from `b`.
298 fun hypot_with(b: Float): Float `{ return hypotf(self, b); `}
299
300 # Returns true is self is not a number.
301 #
302 # As `nan != nan`, `is_nan` should be used to test if a float is the special *not a number* value.
303 #
304 # ~~~
305 # assert nan != nan # By IEEE 754
306 # assert nan.is_nan
307 # assert not 10.0.is_nan
308 # ~~~
309 fun is_nan: Bool `{ return isnan(self); `}
310
311 # Is the float an infinite value
312 # this function returns:
313 #
314 # * 1 if self is positive infinity
315 # * -1 if self is negative infinity
316 # * 0 otherwise
317 #
318 # ~~~
319 # assert 10.0.is_inf == 0
320 # assert inf.is_inf == 1
321 # assert (-inf).is_inf == -1
322 # ~~~
323 fun is_inf: Int do
324 if native_is_inf then
325 if self < 0.0 then return -1
326 return 1
327 end
328 return 0
329 end
330
331 private fun native_is_inf: Bool `{ return isinf(self); `}
332
333 # Linear interpolation between `a` and `b` using `self` as weight
334 #
335 # ~~~
336 # assert 0.0.lerp(0.0, 128.0) == 0.0
337 # assert 0.5.lerp(0.0, 128.0) == 64.0
338 # assert 1.0.lerp(0.0, 128.0) == 128.0
339 # assert -0.5.lerp(0.0, 128.0) == -64.0
340 # ~~~
341 fun lerp(a, b: Float): Float do return (1.0 - self) * a + self * b
342
343 # Quadratic Bézier interpolation between `a` and `b` with an `handle` using `self` as weight
344 #
345 # ~~~
346 # assert 0.00.qerp(0.0, 32.0, 128.0) == 0.0
347 # assert 0.25.qerp(0.0, 32.0, 128.0) == 20.0
348 # assert 0.50.qerp(0.0, 32.0, 128.0) == 48.0
349 # assert 0.75.qerp(0.0, 32.0, 128.0) == 84.0
350 # assert 1.00.qerp(0.0, 32.0, 128.0) == 128.0
351 # ~~~
352 fun qerp(a, handle, b: Float): Float do
353 var p = self
354 var i = 1.0 - p
355 var r = i*i * a +
356 2.0*i*p * handle +
357 p*p * b
358 return r
359 end
360
361 # Cubic Bézier interpolation between `a` and `b` with two handles using `self` as weight
362 #
363 # The Cubic Bézier interpolation is the most common one and use two control points.
364 #
365 # ~~~
366 # assert 0.00.cerp(0.0, 32.0, 128.0, 64.0) == 0.0
367 # assert 0.25.cerp(0.0, 32.0, 128.0, 64.0) == 32.5
368 # assert 0.50.cerp(0.0, 32.0, 128.0, 64.0) == 68.0
369 # assert 0.75.cerp(0.0, 32.0, 128.0, 64.0) == 85.5
370 # assert 1.00.cerp(0.0, 32.0, 128.0, 64.0) == 64.0
371 # ~~~
372 fun cerp(a, a_handle, b_handle, b: Float): Float do
373 var p = self
374 var i = 1.0 - p
375 var r = i*i*i * a +
376 3.0*i*i*p * a_handle +
377 3.0*i*p*p * b_handle +
378 p*p*p * b
379 return r
380 end
381 end
382
383 # Positive float infinite (IEEE 754)
384 #
385 # assert inf > 10.0
386 # assert inf.is_inf == 1
387 #
388 # `inf` follows the arithmetic of infinites
389 #
390 # assert (inf - 1.0) == inf
391 # assert (inf - inf).is_nan
392 #
393 # The negative infinite can be used as `-inf`.
394 #
395 # assert -inf < -10.0
396 # assert (-inf).is_inf == -1
397 fun inf: Float do return 1.0 / 0.0
398
399 # Not a Number, representation of an undefined or unrepresentable float (IEEE 754).
400 #
401 # `nan` is not comparable with itself, you should use `Float::is_nan` to test it.
402 #
403 # ~~~
404 # assert nan.is_nan
405 # assert nan != nan # By IEEE 754
406 # ~~~
407 #
408 # `nan` is the quiet result of some undefined operations.
409 #
410 # ~~~
411 # assert (1.0 + nan).is_nan
412 # assert (0.0 / 0.0).is_nan
413 # assert (inf - inf).is_nan
414 # assert (inf / inf).is_nan
415 # assert (-1.0).sqrt.is_nan
416 # ~~~
417 fun nan: Float do return 0.0 / 0.0
418
419 redef class Collection[ E ]
420 # Return a random element form the collection
421 # There must be at least one element in the collection
422 #
423 # ~~~
424 # var x = [1,2,3].rand
425 # assert x == 1 or x == 2 or x == 3
426 # ~~~
427 fun rand: E
428 do
429 if is_empty then abort
430 var rand_index = length.rand
431
432 for e in self do
433 if rand_index == 0 then return e
434 rand_index -= 1
435 end
436 abort
437 end
438
439 # Return a new array made of elements in a random order.
440 #
441 # ~~~
442 # var a = [1,2,1].to_shuffle
443 # assert a == [1,1,2] or a == [1,2,1] or a == [2,1,1]
444 # ~~~
445 fun to_shuffle: Array[E]
446 do
447 var res = self.to_a
448 res.shuffle
449 return res
450 end
451
452 # Return a new array made of (at most) `length` elements randomly chosen.
453 #
454 # ~~~
455 # var a = [1,2,1].sample(2)
456 # assert a == [1,1] or a == [1,2] or a == [2,1]
457 # ~~~
458 #
459 # If there is not enough elements, then the result only contains them in a random order.
460 # See `to_shuffle`.
461 #
462 # ENSURE `result.length == self.length.min(length)`
463 #
464 # Note: the default implementation uses the Reservoir Algorithm
465 fun sample(length: Int): Array[E]
466 do
467 if length >= self.length then return to_shuffle
468
469 var res = new Array[E].with_capacity(length)
470 var it = iterator
471 for i in [0..length[ do
472 res[i] = it.item
473 it.next
474 end
475 res.shuffle
476 for i in [length+1..self.length] do
477 var j = i.rand
478 if j < length then
479 res[j] = it.item
480 end
481 it.next
482 end
483 return res
484 end
485 end
486
487 redef class SequenceRead[E]
488 # Optimized for large collections using `[]`
489 redef fun rand
490 do
491 assert not is_empty
492 return self[length.rand]
493 end
494 end
495
496 redef class AbstractArray[E]
497 # Reorder randomly the elements in self.
498 #
499 # ~~~
500 # var a = new Array[Int]
501 #
502 # a.shuffle
503 # assert a.is_empty
504 #
505 # a.add 1
506 # a.shuffle
507 # assert a == [1]
508 #
509 # a.add 2
510 # a.shuffle
511 # assert a == [1,2] or a == [2,1]
512 # ~~~
513 #
514 # ENSURE self.shuffle.has_exactly(old(self))
515 fun shuffle
516 do
517 for i in [0..length[ do
518 var j = i + (length-i).rand
519 var tmp = self[i]
520 self[i] = self[j]
521 self[j] = tmp
522 end
523 end
524 end
525
526 redef class Sys
527 init
528 do
529 srand
530 end
531 end
532
533 # Computes the arc tangent given `x` and `y`.
534 #
535 # assert atan2(-0.0, 1.0) == -0.0
536 # assert atan2(0.0, 1.0) == 0.0
537 fun atan2(x: Float, y: Float): Float `{ return atan2(x, y); `}
538
539 # Approximate value of **pi**.
540 fun pi: Float do return 3.14159265
541
542 # Initialize the pseudo-random generator with the given seed.
543 # The pseudo-random generator is used by the method `rand` and other to generate sequence of numbers.
544 # These sequences are repeatable by calling `srand_from` with a same seed value.
545 #
546 # ~~~~
547 # srand_from(0)
548 # var a = 10.rand
549 # var b = 100.rand
550 # srand_from(0)
551 # assert 10.rand == a
552 # assert 100.rand == b
553 # ~~~~
554 fun srand_from(x: Int) `{ nit_rand_seeded = 1; nit_rand_seed = x; `}
555
556 # Reinitialize the pseudo-random generator used by the method `rand` and other.
557 # This method is automatically invoked at the begin of the program, so usually, there is no need to manually invoke it.
558 # The only exception is in conjunction with `srand_from` to reset the pseudo-random generator.
559 fun srand `{ nit_rand_seeded = 0; srand(time(NULL)); `}