module Numeric.Interpolation.Piecewise (
   interpolate,
   interpolateConstantExt,
   ) where

import qualified Numeric.Interpolation.NodeList as Nodes
import qualified Numeric.Interpolation.Type as Type


{- |
It is a checked error to interpolate outside of the range of nodes.
-}
interpolate :: (Ord x) => Type.T x y ny -> Nodes.T x ny -> x -> y
interpolate typ ns x =
   case Nodes.lookup ns x of
      (Just p0, Just p1) -> Type.interpolatePiece typ p0 p1 x
      _ -> error "interpolate: argument outside range"

{- |
Outside the range of nodes the interpolation function
takes the value of the respective border.
-}
interpolateConstantExt ::
   (Ord x) => Type.T x y ny -> Nodes.T x ny -> x -> y
interpolateConstantExt typ ns x =
   case Nodes.lookup ns x of
      (Just p0, Just p1) -> Type.interpolatePiece typ p0 p1 x
      (Just p, Nothing) -> Type.valueFromNode typ $ snd p
      (Nothing, Just p) -> Type.valueFromNode typ $ snd p
      (Nothing, Nothing) -> error "interpolateConstantExt: empty node list"