{-
	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@]	Implements a /Ramanujan/-type series for /Pi/; <https://en.wikipedia.org/wiki/Srinivasa_Ramanujan>.
-}

module Factory.Math.Implementations.Pi.Ramanujan.Implementation(
-- * Functions
	openR
) where

import qualified	Control.Parallel.Strategies
import qualified	Factory.Math.Implementations.Pi.Ramanujan.Series	as Math.Implementations.Pi.Ramanujan.Series
import qualified	Factory.Math.Precision					as Math.Precision
import qualified	Factory.Math.Summation					as Math.Summation

-- | Returns /Pi/, accurate to the specified number of decimal digits.
openR
	:: Math.Implementations.Pi.Ramanujan.Series.Series squareRootAlgorithm factorialAlgorithm	-- ^ This /Pi/-algorithm is parameterised by the type of other algorithms to use.
	-> squareRootAlgorithm										-- ^ The specific /square-root/ algorithm to apply to the above series.
	-> factorialAlgorithm										-- ^ The specific /factorial/-algorithm to apply to the above series.
	-> Math.Precision.DecimalDigits									-- ^ The number of decimal digits required.
	-> Rational
openR :: Series squareRootAlgorithm factorialAlgorithm
-> squareRootAlgorithm
-> factorialAlgorithm
-> DecimalDigits
-> Rational
openR Math.Implementations.Pi.Ramanujan.Series.MkSeries {
	terms :: forall squareRootAlgorithm factorialAlgorithm.
Series squareRootAlgorithm factorialAlgorithm
-> factorialAlgorithm -> [Rational]
Math.Implementations.Pi.Ramanujan.Series.terms			= factorialAlgorithm -> [Rational]
terms,
	getSeriesScalingFactor :: forall squareRootAlgorithm factorialAlgorithm.
Series squareRootAlgorithm factorialAlgorithm
-> squareRootAlgorithm -> DecimalDigits -> Rational
Math.Implementations.Pi.Ramanujan.Series.getSeriesScalingFactor	= squareRootAlgorithm -> DecimalDigits -> Rational
getSeriesScalingFactor,
	convergenceRate :: forall squareRootAlgorithm factorialAlgorithm.
Series squareRootAlgorithm factorialAlgorithm -> ConvergenceRate
Math.Implementations.Pi.Ramanujan.Series.convergenceRate	= ConvergenceRate
convergenceRate
} squareRootAlgorithm
squareRootAlgorithm factorialAlgorithm
factorialAlgorithm DecimalDigits
decimalDigits	= (Rational -> Rational -> Rational)
-> (Rational, Rational) -> Rational
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
(/) ((Rational, Rational) -> Rational)
-> (Rational, Rational) -> Rational
forall a b. (a -> b) -> a -> b
$ Strategy (Rational, Rational)
-> (Rational, Rational) -> (Rational, Rational)
forall a. Strategy a -> a -> a
Control.Parallel.Strategies.withStrategy (
		Strategy Rational
-> Strategy Rational -> Strategy (Rational, Rational)
forall a b. Strategy a -> Strategy b -> Strategy (a, b)
Control.Parallel.Strategies.parTuple2 Strategy Rational
forall a. NFData a => Strategy a
Control.Parallel.Strategies.rdeepseq Strategy Rational
forall a. NFData a => Strategy a
Control.Parallel.Strategies.rdeepseq
	) (
		squareRootAlgorithm -> DecimalDigits -> Rational
getSeriesScalingFactor squareRootAlgorithm
squareRootAlgorithm DecimalDigits
decimalDigits,
		DecimalDigits -> [Rational] -> Rational
forall i.
(Integral i, NFData i) =>
DecimalDigits -> [Ratio i] -> Ratio i
Math.Summation.sumR DecimalDigits
64 ([Rational] -> Rational)
-> ([Rational] -> [Rational]) -> [Rational] -> Rational
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DecimalDigits -> [Rational] -> [Rational]
forall a. DecimalDigits -> [a] -> [a]
take (
			ConvergenceRate -> DecimalDigits -> DecimalDigits
forall i. Integral i => ConvergenceRate -> DecimalDigits -> i
Math.Precision.getTermsRequired ConvergenceRate
convergenceRate DecimalDigits
decimalDigits
		) ([Rational] -> Rational) -> [Rational] -> Rational
forall a b. (a -> b) -> a -> b
$ factorialAlgorithm -> [Rational]
terms factorialAlgorithm
factorialAlgorithm
	) -- Pair.