{-# LANGUAGE MagicHash, BangPatterns, CPP, FlexibleContexts #-}
module Math.NumberTheory.Powers.Cubes
( integerCubeRoot
, integerCubeRoot'
, exactCubeRoot
, isCube
, isCube'
, isPossibleCube
) where
#include "MachDeps.h"
import Control.Monad.ST
import Data.Bits
import qualified Data.Vector.Unboxed as V
import qualified Data.Vector.Unboxed.Mutable as MV
import GHC.Base
import GHC.Integer
import GHC.Integer.GMP.Internals
import GHC.Integer.Logarithms (integerLog2#)
import Numeric.Natural
{-# SPECIALISE integerCubeRoot :: Int -> Int,
Word -> Word,
Integer -> Integer,
Natural -> Natural
#-}
integerCubeRoot :: Integral a => a -> a
integerCubeRoot 0 = 0
integerCubeRoot n
| n > 0 = integerCubeRoot' n
| otherwise =
let m = negate n
r = if m < 0
then negate . fromInteger $ integerCubeRoot' (negate $ fromIntegral n)
else negate (integerCubeRoot' m)
in if r*r*r == n then r else (r-1)
{-# RULES
"integerCubeRoot'/Int" integerCubeRoot' = cubeRootInt'
"integerCubeRoot'/Word" integerCubeRoot' = cubeRootWord
"integerCubeRoot'/Integer" integerCubeRoot' = cubeRootIgr
#-}
{-# INLINE [1] integerCubeRoot' #-}
integerCubeRoot' :: Integral a => a -> a
integerCubeRoot' 0 = 0
integerCubeRoot' n = newton3 n (approxCuRt n)
{-# SPECIALISE exactCubeRoot :: Int -> Maybe Int,
Word -> Maybe Word,
Integer -> Maybe Integer,
Natural -> Maybe Natural
#-}
exactCubeRoot :: Integral a => a -> Maybe a
exactCubeRoot 0 = Just 0
exactCubeRoot n
| n < 0 =
if m < 0
then fmap (negate . fromInteger) $ exactCubeRoot (negate $ fromIntegral n)
else fmap negate (exactCubeRoot m)
| isPossibleCube n && r*r*r == n = Just r
| otherwise = Nothing
where
m = negate n
r = integerCubeRoot' n
{-# SPECIALISE isCube :: Int -> Bool,
Word -> Bool,
Integer -> Bool,
Natural -> Bool
#-}
isCube :: Integral a => a -> Bool
isCube 0 = True
isCube n
| n > 0 = isCube' n
| m > 0 = isCube' m
| otherwise = isCube' (negate (fromIntegral n) :: Integer)
where
m = negate n
{-# SPECIALISE isCube' :: Int -> Bool,
Word -> Bool,
Integer -> Bool,
Natural -> Bool
#-}
isCube' :: Integral a => a -> Bool
isCube' !n = isPossibleCube n
&& (r*r*r == n)
where
r = integerCubeRoot' n
{-# SPECIALISE isPossibleCube :: Int -> Bool,
Word -> Bool,
Integer -> Bool,
Natural -> Bool
#-}
isPossibleCube :: Integral a => a -> Bool
isPossibleCube !n
= V.unsafeIndex cr512 (fromIntegral n .&. 511)
&& V.unsafeIndex cubeRes837 (fromIntegral (n `rem` 837))
&& V.unsafeIndex cubeRes637 (fromIntegral (n `rem` 637))
&& V.unsafeIndex cubeRes703 (fromIntegral (n `rem` 703))
cubeRootInt' :: Int -> Int
cubeRootInt' 0 = 0
cubeRootInt' n
| n < c || c < 0 = r-1
| 0 < d && d < n = r+1
| otherwise = r
where
x = fromIntegral n :: Double
r = truncate (x ** (1/3))
c = r*r*r
d = c+3*r*(r+1)
cubeRootWord :: Word -> Word
cubeRootWord 0 = 0
cubeRootWord w
#if WORD_SIZE_IN_BITS == 64
| r > 2642245 = 2642245
#else
| r > 1625 = 1625
#endif
| w < c = r-1
| c < w && e < w && c < e = r+1
| otherwise = r
where
r = truncate ((fromIntegral w) ** (1/3) :: Double)
c = r*r*r
d = 3*r*(r+1)
e = c+d
cubeRootIgr :: Integer -> Integer
cubeRootIgr 0 = 0
cubeRootIgr n = newton3 n (approxCuRt n)
{-# SPECIALISE newton3 :: Integer -> Integer -> Integer #-}
newton3 :: Integral a => a -> a -> a
newton3 n a = go (step a)
where
step k = (2*k + n `quot` (k*k)) `quot` 3
go k
| m < k = go m
| otherwise = k
where
m = step k
{-# SPECIALISE approxCuRt :: Integer -> Integer #-}
approxCuRt :: Integral a => a -> a
approxCuRt 0 = 0
approxCuRt n = fromInteger $ appCuRt (fromIntegral n)
#if WORD_SIZE_IN_BITS == 64
#define THRESH 5
#else
#define THRESH 9
#endif
appCuRt :: Integer -> Integer
appCuRt (S# i#) = case double2Int# (int2Double# i# **## (1.0## /## 3.0##)) of
r# -> S# r#
appCuRt n@(Jp# bn#)
| isTrue# ((sizeofBigNat# bn#) <# THRESH#) =
floor (fromInteger n ** (1.0/3.0) :: Double)
| otherwise = case integerLog2# n of
l# -> case (l# `quotInt#` 3#) -# 51# of
h# -> case shiftRInteger n (3# *# h#) of
m -> case floor (fromInteger m ** (1.0/3.0) :: Double) of
r -> shiftLInteger r h#
appCuRt _ = error "integerCubeRoot': negative argument"
cr512 :: V.Vector Bool
cr512 = runST $ do
ar <- MV.replicate 512 True
let note s i
| i < 512 = MV.unsafeWrite ar i False >> note s (i+s)
| otherwise = return ()
note 4 2
note 8 4
note 32 16
note 64 32
note 256 128
MV.unsafeWrite ar 256 False
V.unsafeFreeze ar
cubeRes837 :: V.Vector Bool
cubeRes837 = runST $ do
ar <- MV.replicate 837 False
let note 837 = return ()
note k = MV.unsafeWrite ar ((k*k*k) `rem` 837) True >> note (k+1)
note 0
V.unsafeFreeze ar
cubeRes637 :: V.Vector Bool
cubeRes637 = runST $ do
ar <- MV.replicate 637 False
let note 637 = return ()
note k = MV.unsafeWrite ar ((k*k*k) `rem` 637) True >> note (k+1)
note 0
V.unsafeFreeze ar
cubeRes703 :: V.Vector Bool
cubeRes703 = runST $ do
ar <- MV.replicate 703 False
let note 703 = return ()
note k = MV.unsafeWrite ar ((k*k*k) `rem` 703) True >> note (k+1)
note 0
V.unsafeFreeze ar