import Graphics.Dynamic.Plot.R2 import Data.Default.Class import Data.VectorSpace import Math.Polynomial import Math.Polynomial.Legendre type ℝ = Double sawtooth :: ℝ -> ℝ sawtooth x = x - fromIntegral (round x) l2 :: (ℝ->ℝ) -> (ℝ->ℝ) -> ℝ l2 f g = (f (-1)*g (-1) / 2 + sum [ f x*g x | x <- take (n-1) [2/n-1, 4/n-1 ..] ] + f 1*g 1 / 2 ) / n where n :: Num n=>n n = 256 [l2 (evalLegendre i) (evalLegendre i) * (2*fromIntegral i+1) | i<-[0..5]] coeffs :: [ℝ] coeffs = [l2 sawtooth (evalLegendre i) * (2*fromIntegral i+1) | i<-[0..] ] approxSaw :: Poly ℝ approxSaw = (/10) . fromIntegral . round . (*10) <$> sumV [legendre i ^* c | (i,c) <- zip [0..20] coeffs ] plotPrerender def [ continFnPlot sawtooth, continFnPlot $ evalPoly approxSaw , forceXRange (-1,1), forceYRange (-3/4,3/4) ] plotPrerender def [ continFnPlot sawtooth, continFnPlot $ evalPoly approxSaw , forceXRange (-0.7,1.3), forceYRange (-1,0.4) ] {-# LANGUAGE OverloadedStrings #-} import Text.LaTeX import Text.LaTeX.Packages.AMSMath mathDisplay (sum [ fromRational (fromInteger (round $ c*10)/10) * "x"**fromIntegral i | (i,c) <- zip [0..] $ polyCoeffs LE approxSaw , abs c > 0.1 ] :: LaTeX)