on master help sign in
Added (.|) and (./)
commitf888838128084d51bb6e2a33d7ce45cf4bf1aa84 authorArmando Santos <armandoifsantos@gmail.com> dateSat 22 Feb 2020, 10:35 (7 years ago) parent1a1cf28
4 files changed+62−4
app/Quantum.hs+1−3 src/LAoP/Matrix/Internal.hs+25−1 src/LAoP/Matrix/Nat.hs+18 src/LAoP/Matrix/Type.hs+18
app/Quantum.hs+1−3
@@ -23,9 +23,7 @@ ccnot = kr fstM (fromF f)
2323 tp f g (a,b) = (f a, g b)
2424
2525had :: Matrix (Complex Double) Bool Bool
26−had = fromLists l
27− where
28− l = map (map (*(1/sqrt 2))) [[1, 1], [1, -1]]
26+had = (1/sqrt 2) .| fromLists [[1, 1], [1, -1]]
2927
3028bell :: Matrix (Complex Double) (Bool, Bool) (Bool, Bool)
3129bell = cnot `comp` (had >< iden)
src/LAoP/Matrix/Internal.hs+25−1
@@ -81,6 +81,10 @@ module LAoP.Matrix.Internal
8181 -- ** Matrix Transposition
8282 tr,
8383
84+ -- ** Scalar multiplication/division of matrices
85+ (.|),
86+ (./),
87+
8488 -- ** Selective operator
8589 select,
8690 branch,
@@ -344,7 +348,7 @@ instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a
344348 fromLists l@(h : t) =
345349 let lh = length h
346350 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
347− condition = all (== lh) (map length t)
351+ condition = all ((== lh) . length) t
348352 in if lh > 0 && condition
349353 then Fork (fromLists (take rowsC l)) (fromLists (drop rowsC l))
350354 else error "Not all rows have the same length"
@@ -497,6 +501,26 @@ comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
497501 "comp/iden2" forall m. comp iden m = m
498502#-}
499503
504+-- Scalar multiplication of matrices
505+
506+infixl 7 .|
507+-- | Scalar multiplication of matrices.
508+(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
509+(.|) _ Empty = Empty
510+(.|) e (One a) = One (e * a)
511+(.|) e (Join a b) = Join (e .| a) (e .| b)
512+(.|) e (Fork a b) = Fork (e .| a) (e .| b)
513+
514+-- Scalar division of matrices
515+
516+infixl 7 ./
517+-- | Scalar multiplication of matrices.
518+(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
519+(./) Empty _ = Empty
520+(./) (One a) e = One (a / e)
521+(./) (Join a b) e = Join (a ./ e) (b ./ e)
522+(./) (Fork a b) e = Fork (a ./ e) (b ./ e)
523+
500524-- Projections
501525
502526-- | Biproduct first component projection
src/LAoP/Matrix/Nat.hs+18
@@ -98,6 +98,10 @@ module LAoP.Matrix.Nat
9898 -- ** Matrix Transposition
9999 tr,
100100
101+ -- ** Scalar multiplication/division of matrices
102+ (.|),
103+ (./),
104+
101105 -- ** Selective operator
102106 select,
103107
@@ -308,6 +312,20 @@ iden = M I.iden
308312comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
309313comp (M a) (M b) = M (I.comp a b)
310314
315+-- Scalar multiplication of matrices
316+
317+infixl 7 .|
318+-- | Scalar multiplication of matrices.
319+(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
320+(.|) e (M m) = M (e I..| m)
321+
322+-- Scalar division of matrices
323+
324+infixl 7 ./
325+-- | Scalar multiplication of matrices.
326+(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
327+(./) (M m) e = M (m I../ e)
328+
311329p1 ::
312330 ( Num e,
313331 CountableDimensionsN n m,
src/LAoP/Matrix/Type.hs+18
@@ -118,6 +118,10 @@ module LAoP.Matrix.Type
118118 -- ** Matrix Transposition
119119 tr,
120120
121+ -- ** Scalar multiplication/division of matrices
122+ (.|),
123+ (./),
124+
121125 -- ** McCarthy's Conditional
122126 cond,
123127
@@ -443,6 +447,20 @@ comp (M a) (M b) = M (I.comp a b)
443447 "comp/iden2" forall m. comp iden m = m
444448#-}
445449
450+-- Scalar multiplication of matrices
451+
452+infixl 7 .|
453+-- | Scalar multiplication of matrices.
454+(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
455+(.|) e (M m) = M (e I..| m)
456+
457+-- Scalar division of matrices
458+
459+infixl 7 ./
460+-- | Scalar multiplication of matrices.
461+(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
462+(./) (M m) e = M (m I../ e)
463+
446464-- | Biproduct first component projection
447465p1 ::
448466 ( Num e,