Conjugate transpose: Difference between revisions
m
→{{header|Haskell}}: Applied hindent for Rosetta 80 char width. Slightly foregrounded shared predicate structure.
m (Removed unused member functions) |
m (→{{header|Haskell}}: Applied hindent for Rosetta 80 char width. Slightly foregrounded shared predicate structure.) |
||
Line 1,127:
=={{header|Haskell}}==
Slow implementation using lists.
<lang haskell>import Data.
import Data.
type Matrix a = [[a]]
Line 1,134:
main :: IO ()
main =
(\a -> do
putStrLn "\nMatrix:"▼
putStrLn
mapM_
putStrLn
([ [[3, 2 :+
]
isHermitianMatrix, isNormalMatrix, isUnitaryMatrix
:: RealFloat a
▲ [0, 1, 1],
▲ [1, 0, 1]],
isNormalMatrix = mTest mmct (mmul =<< conjTranspose)
▲ [[sqrt 2/2:+0, sqrt 2/2:+0, 0 ],
▲ [0:+sqrt 2/2, 0:+ (-sqrt 2/2), 0 ],
▲ [0, 0, 0:+1]]] :: [Matrix (Complex Double)])
▲isHermitianMatrix a = a `approxEqualMatrix` conjTranspose a
mTest
approxEqualMatrix :: (Fractional a, Ord a) => Matrix (Complex a) -> Matrix (Complex a) -> Bool▼
:: RealFloat a
approxEqualMatrix a b = length a == length b && length (head a) == length (head b) &&▼
=> (a2 -> Matrix (Complex a)) -> (a2 -> Matrix (Complex a)) -> a2 -> Bool
and (zipWith approxEqualComplex (concat a) (concat b))▼
mTest f g = (approxEqualMatrix . f) <*> g
where approxEqualComplex (rx :+ ix) (ry :+ iy) = abs (rx - ry) < eps && abs (ix - iy) < eps▼
eps = 1e-14▼
mmct
mmul :: Num a => Matrix a -> Matrix a -> Matrix a▼
:: RealFloat a
mmul a b = [[sum (zipWith (*) row column) | column <- transpose b] | row <- a]▼
=> Matrix (Complex a) -> Matrix (Complex a)
mmct = mmul <*> conjTranspose
approxEqualMatrix
ident :: Num a => Int -> Matrix a▼
:: (Fractional a, Ord a)
ident size = [[fromIntegral $ div a b * div b a | a <- [1..size]] | b <- [1..size]]▼
approxEqualMatrix a b =
length a == length b &&
where
approxEqualComplex (rx :+ ix) (ry :+ iy) =
mmul
▲conjTranspose :: Num a => Matrix (Complex a) -> Matrix (Complex a)
:: Num a
mmul a b =
| column <- transpose b ]
| row <- a ]
ident
:: Num a
ident size =
| b <- [1 .. size] ]
conjTranspose
:: Num a
=> Matrix (Complex a) -> Matrix (Complex a)
conjTranspose = map (map conjugate) . transpose</lang>
Output:
|