]> gitweb.michael.orlitzky.com - spline3.git/blob - src/RealFunction.hs
Rename the spline project to spline3.
[spline3.git] / src / RealFunction.hs
1 {-# LANGUAGE TypeSynonymInstances #-}
2
3 module RealFunction
4 where
5
6
7 type RealFunction a = (a -> Double)
8
9 instance Show (RealFunction a) where
10 show _ = "Real Function"
11
12 instance Eq (RealFunction a) where
13 _ == _ = False
14
15 instance Num (RealFunction a) where
16 f1 + f2 = \x -> (f1 x) + (f2 x)
17 f1 - f2 = \x -> (f1 x) - (f2 x)
18 f1 * f2 = \x -> (f1 x) * (f2 x)
19 negate f = \x -> -1 * (f x)
20 abs f = \x -> abs (f x)
21 signum f = \x -> signum (f x)
22 fromInteger i = \_ -> (fromInteger i)
23
24
25 -- Takes a constant, and a function as arguments. Returns a new
26 -- function representing the original function times the constant.
27 cmult :: Double -> (RealFunction a) -> (RealFunction a)
28 cmult coeff f = (*coeff) . f
29
30 -- Takes a function f and an exponent n. Returns a new function, f^n.
31 fexp :: (RealFunction a) -> Int -> (RealFunction a)
32 fexp f n
33 | n == 0 = (\_ -> 1)
34 | otherwise = \x -> (f x)^n