{-# LANGUAGE ScopedTypeVariables #-}
----------------------------------------------------------------------
-- |
-- Module      :  Graphics.FieldTrip.Misc
-- Copyright   :  (c) Conal Elliott 2008
-- License     :  BSD3
-- 
-- Maintainer  :  conal@conal.net
-- Stability   :  experimental
-- 
-- Miscellany for FieldTrip graphics
----------------------------------------------------------------------

module Graphics.FieldTrip.Misc
  (
    R, fracPart, Unop, Binop, logMemo
  ) where

import Data.MemoTrie

-- | Values commonly used for coordinates, coefficients, etc.
type R = Float

-- | Fractional portion, @x - floor x@
fracPart :: R -> R
fracPart x = x - fromIntegral (floor x :: Int)

-- | Unary operations
type Unop  a = a -> a

-- | Binary operations
type Binop a = a -> a -> a

-- | Memoize an approximation to a given function, discretizing its domain
-- by scaling.  An input @x@ discretizes into @round (logBase base x)@.
logMemo :: forall s a. (Floating s, RealFrac s) => s -> (s->a) -> (s->a)
logMemo base f = memo (f . unapprox) . approx
 where
   approx :: s -> Int
   approx = round . logBase base
   unapprox :: Int -> s
   unapprox = (base **) . fromIntegral