{-
	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 /Borwein/ series for /Pi/; <https://en.wikipedia.org/wiki/Borwein%27s_algorithm>
-}

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

import qualified	Control.Arrow
import qualified	Control.Parallel.Strategies
import qualified	Factory.Math.Implementations.Pi.Borwein.Series	as Math.Implementations.Pi.Borwein.Series
import qualified	Factory.Math.Precision				as Math.Precision

-- | Returns /Pi/, accurate to the specified number of decimal digits.
openR
	:: Math.Implementations.Pi.Borwein.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.Borwein.Series.MkSeries {
	terms :: forall squareRootAlgorithm factorialAlgorithm.
Series squareRootAlgorithm factorialAlgorithm
-> squareRootAlgorithm
-> factorialAlgorithm
-> DecimalDigits
-> (Rational, [Rational])
Math.Implementations.Pi.Borwein.Series.terms		= squareRootAlgorithm
-> factorialAlgorithm -> DecimalDigits -> (Rational, [Rational])
terms,
	convergenceRate :: forall squareRootAlgorithm factorialAlgorithm.
Series squareRootAlgorithm factorialAlgorithm -> ConvergenceRate
Math.Implementations.Pi.Borwein.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, Rational))
-> (Rational, [Rational])
-> Rational
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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
	) ((Rational, Rational) -> (Rational, Rational))
-> ((Rational, [Rational]) -> (Rational, Rational))
-> (Rational, [Rational])
-> (Rational, Rational)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Rational] -> Rational)
-> (Rational, [Rational]) -> (Rational, Rational)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
Control.Arrow.second (
		[Rational] -> Rational
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([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, [Rational]) -> Rational
forall a b. (a -> b) -> a -> b
$ squareRootAlgorithm
-> factorialAlgorithm -> DecimalDigits -> (Rational, [Rational])
terms squareRootAlgorithm
squareRootAlgorithm factorialAlgorithm
factorialAlgorithm DecimalDigits
decimalDigits