module TBit.Hamiltonian.Builder.Matrification (mixedSum, toMatrix) where
import Prelude.Listless
import Data.List
import Numeric.LinearAlgebra.HMatrix
import Data.Graph.Inductive
import Data.Maybe
mixedAdd :: Maybe (Matrix (Complex Double))
-> Maybe (Matrix (Complex Double))
-> Maybe (Matrix (Complex Double))
mixedAdd (Just a) (Just b) | dim a == dim b = Just $ a + b
| dim a == 2 * dim b = Just $ a + expand b
| dim b == 2 * dim a = Just $ expand a + b
| otherwise = Nothing
where dim = fst . size
expand m = m `kronecker` ident 2
mixedAdd _ _ = Nothing
mixedSum :: [Matrix (Complex Double)] -> Maybe (Matrix (Complex Double))
mixedSum = foldl1 mixedAdd . map Just
toMatrix :: Gr (Matrix (Complex Double)) (Matrix (Complex Double))
-> Matrix (Complex Double)
toMatrix gr = fromJust $ mixedSum [offd, ond]
where es = map (\(i,j,k) -> ((i,j),k)) $ labEdges gr
n = noNodes gr
ns = labNodes gr
d i j = if i==j then 1 else 0
offd = fromBlocks [[ sum $ map snd
$ filter ((==) (i,j) . fst) es | i <- [1..n]]
| j <- [1..n]]
ond = fromBlocks [[ (*) (d i j)
$ sum $ map snd $ filter ((==) j . fst) ns
| i <- [1..n]]
| j <- [1..n]]