Advertisement
PonaFly

6

Dec 4th, 2016
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE FlexibleInstances, TypeFamilies #-}
  2.  
  3. import Data.Ratio   -- рациональные числа
  4. import Data.Complex -- комплексные числа
  5. -- Класс абелевой группы
  6. class AbelGroup a where
  7.   (^+^) :: a -> a -> a
  8.   zero :: a
  9.   negate' :: a -> a  
  10.  negate' a = zero ^-^ a
  11.   (^-^) :: a -> a -> a
  12.   (^-^) el1 el2 = el1 ^+^ negate' el2
  13.  
  14. ---------------------------------------------
  15. -- Класс линейного пространства над числовым полем типа Numeric a
  16. class AbelGroup a => LinearSpace a where
  17.  type Numeric a :: *     -- Numeric - тип числового поля
  18.  (^*^) :: Numeric a -> a -> a
  19.  
  20.  
  21. --- Тип векторов
  22. data TVector = Vector { x::Double, y::Double }
  23.  deriving(Eq,Ord)
  24.  
  25. instance Show TVector where
  26.  show (Vector x y) = "(" ++ show x ++ ";" ++ show y ++ ")"
  27.  
  28. instance AbelGroup TVector where
  29.  (Vector x1 y1) ^+^ (Vector x2 y2) =
  30.      Vector (x1+x2) (y1+y2)
  31.  zero = Vector 0 0
  32.  negate' (Vector x y) = Vector (-x) (-y)
  33.  
  34. instance LinearSpace TVector where
  35.   type Numeric TVector = Double
  36.   a ^*^ v = Vector (a*x v) (a*y v)  
  37.  
  38.  
  39. class LinearSpace a => HilbertSpace a where
  40.   (%) ::  a -> a -> Numeric a
  41.  
  42. instance HilbertSpace TVector where
  43.   (Vector x1 y1) % (Vector x2 y2) =  (x1)*(x2) + (y1)*(y2)
  44.  
  45.  
  46. data TRealFunc = RealFunc (Double->Double)
  47. (^$^) :: TRealFunc -> Double -> Double
  48. (RealFunc f) ^$^ x = f x
  49.  
  50. instance AbelGroup TRealFunc where  
  51.   (RealFunc f1) ^+^ (RealFunc f2) =
  52.       RealFunc (\x -> f1 x + f2 x)
  53.   zero = RealFunc (\_ -> 0)
  54.   negate' (RealFunc f) = RealFunc (\x -> -f x)  
  55.  
  56. instance LinearSpace TRealFunc where
  57.  type Numeric TRealFunc = Double
  58.  a ^*^ (RealFunc f) = RealFunc (\x -> a*f x)  
  59.  
  60. instance HilbertSpace TRealFunc where
  61. (RealFunc f1) % (RealFunc f2) =
  62.  sum $ map (\x -> (f1 x)*(f2 x)*(1/100)) [1/100,2/100..100/100]
  63.  
  64. class MyMonoid a  where
  65.  (^**^) :: a -> a -> a
  66.  neutral :: a
  67.  
  68. instance MyMonoid [a] where
  69. l1 ^**^ l2 = l1 ++ l2
  70. neutral = []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement