{-
	Copyright (C) 2011 Dr. Alistair Ward

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program 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 PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]	Defines the /Chudnovsky/ series for /Pi/; <https://en.wikipedia.org/wiki/Pi>.
-}

module Factory.Math.Implementations.Pi.Ramanujan.Chudnovsky(
-- * Constants
	series
) where

-- import		Control.Arrow((***))
import			Data.Ratio((%))
-- import		Factory.Data.PrimeFactors((>/<), (>*<), (>^))
-- import qualified	Factory.Data.PrimeFactors				as Data.PrimeFactors
import qualified	Factory.Math.Factorial					as Math.Factorial
import qualified	Factory.Math.Implementations.Factorial			as Math.Implementations.Factorial
import qualified	Factory.Math.Implementations.Pi.Ramanujan.Series	as Math.Implementations.Pi.Ramanujan.Series
import qualified	Factory.Math.Power					as Math.Power
import qualified	Factory.Math.SquareRoot					as Math.SquareRoot

-- | Defines the parameters of the /Chudnovsky/ series.
series :: (
	Math.SquareRoot.Algorithmic	squareRootAlgorithm,
	Math.Factorial.Algorithmic	factorialAlgorithm
 ) => Math.Implementations.Pi.Ramanujan.Series.Series squareRootAlgorithm factorialAlgorithm
series :: Series squareRootAlgorithm factorialAlgorithm
series = MkSeries :: forall squareRootAlgorithm factorialAlgorithm.
(factorialAlgorithm -> [Rational])
-> (squareRootAlgorithm -> DecimalDigits -> Rational)
-> ConvergenceRate
-> Series squareRootAlgorithm factorialAlgorithm
Math.Implementations.Pi.Ramanujan.Series.MkSeries {
	terms :: factorialAlgorithm -> [Rational]
Math.Implementations.Pi.Ramanujan.Series.terms			= \factorialAlgorithm
factorialAlgorithm -> (Integer -> Integer -> Rational)
-> [Integer] -> [Integer] -> [Rational]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (
{-
		\n power -> let
			product'	= Data.PrimeFactors.product' (recip 2) 10
		in uncurry (%) . (
			(* (13591409 + 545140134 * n)) . product' *** (* power) . product'
		) $ Math.Implementations.Factorial.primeFactors (6 * n) >/< (
			Math.Implementations.Factorial.primeFactors (3 * n) >*< Math.Implementations.Factorial.primeFactors n >^ 3
		)
-}
		\Integer
n Integer
power -> (
			Integer -> Integer -> Integer
forall i. (Integral i, Show i) => i -> i -> i
Math.Implementations.Factorial.risingFactorial (Integer -> Integer
forall a. Enum a => a -> a
succ (Integer -> Integer) -> Integer -> Integer
forall a b. (a -> b) -> a -> b
$ Integer
3 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
n) (Integer
3 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
n) Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Integer -> Integer
forall n. Num n => n -> n
Math.Power.cube (factorialAlgorithm -> Integer -> Integer
forall algorithm i.
(Algorithmic algorithm, Integral i, Show i) =>
algorithm -> i -> i
Math.Factorial.factorial factorialAlgorithm
factorialAlgorithm Integer
n)
		) Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* (
			(Integer
13591409 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
545140134 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
n) Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Integer
power
		) -- CAVEAT: the order in which these terms are evaluated radically affects performance.
	) [Integer
0 ..] ([Integer] -> [Rational]) -> [Integer] -> [Rational]
forall a b. (a -> b) -> a -> b
$ (Integer -> Integer) -> Integer -> [Integer]
forall a. (a -> a) -> a -> [a]
iterate (Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* (Integer -> Integer
forall n. Num n => n -> n
Math.Power.cube (Integer -> Integer) -> Integer -> Integer
forall a b. (a -> b) -> a -> b
$ Integer -> Integer
forall n. Num n => n -> n
negate Integer
640320 :: Integer)) Integer
1,
	getSeriesScalingFactor :: squareRootAlgorithm -> DecimalDigits -> Rational
Math.Implementations.Pi.Ramanujan.Series.getSeriesScalingFactor	= \squareRootAlgorithm
squareRootAlgorithm DecimalDigits
decimalDigits -> Rational
426880 Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* squareRootAlgorithm -> DecimalDigits -> Integer -> Rational
forall algorithm operand.
(Algorithmic algorithm, Real operand, Show operand) =>
algorithm -> DecimalDigits -> operand -> Rational
Math.SquareRoot.squareRoot squareRootAlgorithm
squareRootAlgorithm DecimalDigits
decimalDigits (Integer
10005 :: Integer),
	convergenceRate :: ConvergenceRate
Math.Implementations.Pi.Ramanujan.Series.convergenceRate	= ConvergenceRate
10 ConvergenceRate -> ConvergenceRate -> ConvergenceRate
forall a. Floating a => a -> a -> a
** ConvergenceRate -> ConvergenceRate
forall n. Num n => n -> n
negate ConvergenceRate
14.0	-- Empirical.
}