{-
	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 parameters of a series used in a /Spigot/-table to generate /Pi/.
-}

module Factory.Math.Implementations.Pi.Spigot.Series(
-- * Types
-- ** Data-types
	Series(..),
-- * Functions
	bases
) where

import			Data.Ratio((%))
import qualified	Data.Ratio
import qualified	Factory.Math.Precision	as Math.Precision

{- |
	* Defines a series composed from a sum of terms, each one of which is the product of a coefficient and a base.

	* The coefficents and bases of the series are described in /Horner form/; @Pi = c1 + (b1 * (c2 + b2 * (c3 + b3 * (...))))@.
-}
data Series i	= MkSeries {
	Series i -> [i]
coefficients		:: [i],
	Series i -> [i]
baseNumerators		:: [i],
	Series i -> [i]
baseDenominators	:: [i],
	Series i -> DecimalDigits -> DecimalDigits
nTerms			:: Math.Precision.DecimalDigits -> Int	-- ^ The width of the spigot-table, required to accurately generate the requested number of digits.
}

-- | Combines 'baseNumerators' and 'baseDenominators', and as a side-effect, expresses the ratio in lowest terms.
bases :: Integral i => Series i -> [Data.Ratio.Ratio i]
bases :: Series i -> [Ratio i]
bases MkSeries {
	baseNumerators :: forall i. Series i -> [i]
baseNumerators		= [i]
n,
	baseDenominators :: forall i. Series i -> [i]
baseDenominators	= [i]
d
} = (i -> i -> Ratio i) -> [i] -> [i] -> [Ratio i]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith i -> i -> Ratio i
forall a. Integral a => a -> a -> Ratio a
(%) [i]
n [i]
d