Merge: Light FFI for the interpreter
[nit.git] / lib / standard / math.nit
index eec0cf6..fb6bb05 100644 (file)
@@ -4,14 +4,14 @@
 #
 # This file is free software, which comes along with NIT.  This software is
 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
-# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A 
+# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A
 # PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
 # is kept unaltered, and a notification of the changes is added.
 # You  are  allowed  to  redistribute it and sell it, alone or is a part of
 # another product.
 
 # Mathematical operations
-module math
+module math is ldflags "-lm"
 
 import kernel
 import collection
@@ -29,25 +29,37 @@ redef class Int
        #     assert 0x10.bin_and(0x01) == 0
        fun bin_and(i: Int): Int is extern "kernel_Int_Int_binand_0"
 
+       # Alias of `bin_and`
+       fun &(i: Int): Int do return bin_and(i)
+
        # Returns the result of a binary OR operation on `self` and `i`
        #
        #     assert 0x10.bin_or(0x01) == 0x11
        fun bin_or(i: Int): Int is extern "kernel_Int_Int_binor_0"
 
+       # Alias of `bin_or`
+       fun |(i: Int): Int do return bin_or(i)
+
        # Returns the result of a binary XOR operation on `self` and `i`
        #
        #     assert 0x101.bin_xor(0x110) == 0x11
        fun bin_xor(i: Int): Int is extern "kernel_Int_Int_binxor_0"
 
+       # Alias of `bin_xor`
+       fun ^(i: Int): Int do return bin_xor(i)
+
        # Returns the 1's complement of `self`
        #
        #     assert 0x2F.bin_not == -48
        fun bin_not: Int is extern "kernel_Int_Int_binnot_0"
 
+       # Alias of `bin_not`
+       fun ~: Int do return bin_not
+
        # Returns the square root of `self`
        #
        #     assert 16.sqrt == 4
-       fun sqrt: Int `{ return sqrt(recv); `}
+       fun sqrt: Int `{ return sqrt(self); `}
 
        # Returns the greatest common divisor of `self` and `o`
        #
@@ -84,6 +96,24 @@ redef class Int
        #     assert not 13.is_even
        fun is_odd: Bool do return not is_even
 
+       # Is self a prime number ?
+       #
+       # assert 3.is_prime
+       # assert not 1.is_prime
+       # assert not 12.is_prime
+       fun is_prime: Bool
+       do
+               if self == 2 then
+                       return true
+               else if self <= 1 or self.is_even then
+                       return false
+               end
+               for i in [3..self.sqrt[ do
+                       if self % i == 0 then return false
+               end
+               return true
+       end
+
        # Returns the `self` raised to the power of `e`.
        #
        #     assert 2 ** 3 == 8
@@ -113,6 +143,40 @@ redef class Int
        end
 end
 
+redef class Byte
+       # Returns the result of a binary AND operation on `self` and `i`
+       #
+       #     assert 0x10.bin_and(0x01) == 0
+       fun bin_and(i: Byte): Byte `{ return self & i; `}
+
+       # Alias of `bin_and`
+       fun &(i: Byte): Byte do return bin_and(i)
+
+       # Returns the result of a binary OR operation on `self` and `i`
+       #
+       #     assert 0x10.bin_or(0x01) == 0x11
+       fun bin_or(i: Byte): Byte `{ return self | i; `}
+
+       # Alias of `bin_or`
+       fun |(i: Byte): Byte do return bin_or(i)
+
+       # Returns the result of a binary XOR operation on `self` and `i`
+       #
+       #     assert 0x101.bin_xor(0x110) == 0x11
+       fun bin_xor(i: Byte): Byte `{ return self ^ i; `}
+
+       # Alias of `bin_xor`
+       fun ^(i: Byte): Byte do return bin_xor(i)
+
+       # Returns the 1's complement of `self`
+       #
+       #     assert 0x2F.bin_not == -48
+       fun bin_not: Byte `{ return ~self; `}
+
+       # Alias of `bin_not`
+       fun ~: Byte do return bin_not
+end
+
 redef class Float
 
        # Returns the non-negative square root of `self`.
@@ -158,7 +222,7 @@ redef class Float
        #     assert 12.0.abs == 12.0
        #     assert (-34.56).abs == 34.56
        #     assert -34.56.abs == -34.56
-       fun abs: Float `{ return fabs(recv); `}
+       fun abs: Float `{ return fabs(self); `}
 
        # Returns `self` raised at `e` power.
        #
@@ -167,12 +231,18 @@ redef class Float
        #     #assert 0.0.pow(9.0) == 0.0
        fun pow(e: Float): Float is extern "kernel_Float_Float_pow_1"
 
-       # Returns the logarithm of `self`.
+       # Natural logarithm of `self`.
        #
        #     assert 0.0.log.is_inf == -1
        #     #assert 1.0.log == 0.0
        fun log: Float is extern "kernel_Float_Float_log_0"
 
+       # Logarithm of `self` to base `base`.
+       #
+       #     assert 100.0.log_base(10.0) == 2.0
+       #     assert 256.0.log_base(2.0) == 8.0
+       fun log_base(base: Float): Float do return log/base.log
+
        # Returns *e* raised to `self`.
        fun exp: Float is extern "kernel_Float_Float_exp_0"
 
@@ -180,13 +250,13 @@ redef class Float
        #     assert 1.9.ceil == 2.0
        #     assert 2.0.ceil == 2.0
        #     assert (-1.5).ceil == -1.0
-       fun ceil: Float `{ return ceil(recv); `}
+       fun ceil: Float `{ return ceil(self); `}
 
        #     assert 1.1.floor == 1.0
        #     assert 1.9.floor == 1.0
        #     assert 2.0.floor == 2.0
        #     assert (-1.5).floor == -2.0
-       fun floor: Float `{ return floor(recv); `}
+       fun floor: Float `{ return floor(self); `}
 
        # Rounds the value of a float to its nearest integer value
        #
@@ -235,6 +305,11 @@ end
 redef class Collection[ E ]
        # Return a random element form the collection
        # There must be at least one element in the collection
+       #
+       # ~~~
+       # var x = [1,2,3].rand
+       # assert x == 1 or x == 2 or x == 3
+       # ~~~
        fun rand: E
        do
                if is_empty then abort
@@ -246,6 +321,19 @@ redef class Collection[ E ]
                end
                abort
        end
+
+       # Return a new array made of elements in a random order.
+       #
+       # ~~~
+       # var a = [1,2,1].to_shuffle
+       # assert a == [1,1,2] or a == [1,2,1] or a == [2,1,1]
+       # ~~~
+       fun to_shuffle: Array[E]
+       do
+               var res = self.to_a
+               res.shuffle
+               return res
+       end
 end
 
 redef class SequenceRead[E]
@@ -257,6 +345,36 @@ redef class SequenceRead[E]
        end
 end
 
+redef class AbstractArray[E]
+       # Reorder randomly the elements in self.
+       #
+       # ~~~
+       # var a = new Array[Int]
+       #
+       # a.shuffle
+       # assert a.is_empty
+       #
+       # a.add 1
+       # a.shuffle
+       # assert a == [1]
+       #
+       # a.add 2
+       # a.shuffle
+       # assert a == [1,2] or a == [2,1]
+       # ~~~
+       #
+       # ENSURE self.shuffle.has_exactly(old(self))
+       fun shuffle
+       do
+               for i in [0..length[ do
+                       var j = i + (length-i).rand
+                       var tmp = self[i]
+                       self[i] = self[j]
+                       self[j] = tmp
+               end
+       end
+end
+
 redef class Sys
        init
        do