module Numeric.Combinatorics ( choose
, doubleFactorial
, catalan
, factorial
, derangement
, permutations
, maxRegions
, stirling2
) where
import Foreign.C
import Foreign.Ptr
import Numeric.GMP.Raw.Unsafe (mpz_clear)
import Numeric.GMP.Types
import Numeric.GMP.Utils
import System.IO.Unsafe (unsafePerformIO)
foreign import ccall unsafe double_factorial_ats :: CInt -> IO (Ptr MPZ)
foreign import ccall unsafe factorial_ats :: CInt -> IO (Ptr MPZ)
foreign import ccall unsafe choose_ats :: CInt -> CInt -> IO (Ptr MPZ)
foreign import ccall unsafe catalan_ats :: CInt -> IO (Ptr MPZ)
foreign import ccall unsafe derangements_ats :: CInt -> IO (Ptr MPZ)
foreign import ccall unsafe permutations_ats :: CInt -> CInt -> IO (Ptr MPZ)
foreign import ccall unsafe max_regions_ats :: CInt -> IO (Ptr MPZ)
foreign import ccall unsafe stirling2_ats :: CInt -> CInt -> IO (Ptr MPZ)
conjugateMPZ :: (CInt -> IO (Ptr MPZ)) -> Int -> Integer
conjugateMPZ f n = unsafePerformIO $ do
mPtr <- f (fromIntegral n)
peekInteger mPtr <* mpz_clear mPtr
conjugateMPZ' :: (CInt -> CInt -> IO (Ptr MPZ)) -> Int -> Int -> Integer
conjugateMPZ' f n k = unsafePerformIO $ do
mPtr <- f (fromIntegral n) (fromIntegral k)
peekInteger mPtr <* mpz_clear mPtr
derangement :: Int -> Integer
derangement = conjugateMPZ derangements_ats
catalan :: Int -> Integer
catalan = conjugateMPZ catalan_ats
choose :: Int -> Int -> Integer
choose = conjugateMPZ' choose_ats
permutations :: Int -> Int -> Integer
permutations = conjugateMPZ' permutations_ats
stirling2 :: Int -> Int -> Integer
stirling2 = conjugateMPZ' stirling2_ats
factorial :: Int -> Integer
factorial = conjugateMPZ factorial_ats
doubleFactorial :: Int -> Integer
doubleFactorial = conjugateMPZ double_factorial_ats
maxRegions :: Int
-> Integer
maxRegions = conjugateMPZ max_regions_ats