-
Notifications
You must be signed in to change notification settings - Fork 1
/
Harmonic.hs
62 lines (51 loc) · 2.09 KB
/
Harmonic.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
-- | A module defining the algebra of the harmonic oscillator over a field of complex variables
-- Note that this is not yet tested!
module Harmonic where
import Prelude hiding ( (*>) )
import Math.Algebras.VectorSpace
import Math.Algebras.Structures
import Variables
import Data.Char.SScript
infixl 5 :*:
infixr 6 :^:
data HarmonicBasis = I
| B
| BH
| HarmonicBasis :*: HarmonicBasis
| HarmonicBasis :^: Int
deriving (Eq, Ord)
instance Show HarmonicBasis where
show I = "I"
show B = "a"
show BH = "c"
show (u :*: v) = show u ++ "*" ++ show v
-- the code below seems to lead to double superscript.
-- only map first?
show (u :^: n) = show u ++ map superscript (show n)
type Harmonic = Vect Var HarmonicBasis
instance Algebra Var HarmonicBasis where
unit x = x *> return I
mult = linear mult'
where mult' (I, u) = return u
mult' (u, I) = return u
mult' (B, BH) = id' + mult' (BH, B)
mult' (B, B) = return (B :^: 2)
mult' (BH, BH) = return (BH :^: 2)
mult' (u, v :*: w) | u == v = mult' (u :^: 2, w)
| v == w = mult' (u, v :^: 2)
| otherwise = return u * mult' (v, w)
mult' (u :^: n, v) | u == v = return $ u :^: (n+1)
mult' (u :^: n, v :^: m) | u == v = return $ u :^: (n+m)
mult' (u, v :^: n) | u == v = return $ u :^: (n+1)
mult' (u :*: v, w) | v == w = return (u :*: (v :^: 2))
| u == v = return (u :^: 2 :*: w)
| otherwise = return u * mult' (v, w)
mult' (u,v) = return (u :*: v)
id',a,c :: Harmonic
id' = return I
a = return B
c = return BH