Conjugate transpose: Difference between revisions
Content added Content deleted
m (added bullet points (2nd task requirement), used a larger font to make the formulae, italics, super- and subscripts, and symbols easier to read.) |
No edit summary |
||
Line 910: | Line 910: | ||
Normal: true |
Normal: true |
||
Unitary: true |
Unitary: true |
||
</pre> |
|||
=={{header|Haskell}}== |
|||
Slow implementation using lists. |
|||
<lang haskell>import Data.List (transpose) |
|||
import Data.Complex |
|||
type Matrix a = [[a]] |
|||
main :: IO () |
|||
main = |
|||
mapM_ (\a -> do |
|||
putStrLn "\nMatrix:" |
|||
mapM_ print a |
|||
putStrLn "Conjugate Transpose:" |
|||
mapM_ print (conjTranspose a) |
|||
putStrLn $ "Hermitian? " ++ show (isHermitianMatrix a) |
|||
putStrLn $ "Normal? " ++ show (isNormalMatrix a) |
|||
putStrLn $ "Unitary? " ++ show (isUnitaryMatrix a)) |
|||
([[[3, 2:+1], |
|||
[2:+(-1), 1 ]], |
|||
[[1, 1, 0], |
|||
[0, 1, 1], |
|||
[1, 0, 1]], |
|||
[[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, isNormalMatrix, isUnitaryMatrix :: RealFloat a => Matrix (Complex a) -> Bool |
|||
isHermitianMatrix a = a `approxEqualMatrix` conjTranspose a |
|||
isNormalMatrix a = (a `mmul` conjTranspose a) `approxEqualMatrix` (conjTranspose a `mmul` a) |
|||
isUnitaryMatrix a = (a `mmul` conjTranspose a) `approxEqualMatrix` ident (length a) |
|||
approxEqualMatrix :: (Fractional a, Ord a) => Matrix (Complex a) -> Matrix (Complex a) -> Bool |
|||
approxEqualMatrix a b = length a == length b && length (head a) == length (head b) && |
|||
and (zipWith approxEqualComplex (concat a) (concat b)) |
|||
where approxEqualComplex (rx :+ ix) (ry :+ iy) = abs (rx - ry) < eps && abs (ix - iy) < eps |
|||
eps = 1e-14 |
|||
mmul :: Num a => Matrix a -> Matrix a -> Matrix a |
|||
mmul a b = [[sum (zipWith (*) row column) | column <- transpose b] | row <- a] |
|||
ident :: Num a => Int -> Matrix a |
|||
ident size = [[fromIntegral $ div a b * div b a | a <- [1..size]] | b <- [1..size]] |
|||
conjTranspose :: Num a => Matrix (Complex a) -> Matrix (Complex a) |
|||
conjTranspose = map (map conjugate) . transpose</lang> |
|||
Output: |
|||
<pre> |
|||
Matrix: |
|||
[3.0 :+ 0.0,2.0 :+ 1.0] |
|||
[2.0 :+ (-1.0),1.0 :+ 0.0] |
|||
Conjugate Transpose: |
|||
[3.0 :+ (-0.0),2.0 :+ 1.0] |
|||
[2.0 :+ (-1.0),1.0 :+ (-0.0)] |
|||
Hermitian? True |
|||
Normal? True |
|||
Unitary? False |
|||
Matrix: |
|||
[1.0 :+ 0.0,1.0 :+ 0.0,0.0 :+ 0.0] |
|||
[0.0 :+ 0.0,1.0 :+ 0.0,1.0 :+ 0.0] |
|||
[1.0 :+ 0.0,0.0 :+ 0.0,1.0 :+ 0.0] |
|||
Conjugate Transpose: |
|||
[1.0 :+ (-0.0),0.0 :+ (-0.0),1.0 :+ (-0.0)] |
|||
[1.0 :+ (-0.0),1.0 :+ (-0.0),0.0 :+ (-0.0)] |
|||
[0.0 :+ (-0.0),1.0 :+ (-0.0),1.0 :+ (-0.0)] |
|||
Hermitian? False |
|||
Normal? True |
|||
Unitary? False |
|||
Matrix: |
|||
[0.7071067811865476 :+ 0.0,0.7071067811865476 :+ 0.0,0.0 :+ 0.0] |
|||
[0.0 :+ 0.7071067811865476,0.0 :+ (-0.7071067811865476),0.0 :+ 0.0] |
|||
[0.0 :+ 0.0,0.0 :+ 0.0,0.0 :+ 1.0] |
|||
Conjugate Transpose: |
|||
[0.7071067811865476 :+ (-0.0),0.0 :+ (-0.7071067811865476),0.0 :+ (-0.0)] |
|||
[0.7071067811865476 :+ (-0.0),0.0 :+ 0.7071067811865476,0.0 :+ (-0.0)] |
|||
[0.0 :+ (-0.0),0.0 :+ (-0.0),0.0 :+ (-1.0)] |
|||
Hermitian? False |
|||
Normal? True |
|||
Unitary? True |
|||
</pre> |
</pre> |
||
=={{header|J}}== |
=={{header|J}}== |