{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE PatternSynonyms #-}
#endif
module Data.Complex.Lens
( _realPart
, _imagPart
, _polar
, _magnitude
, _phase
, _conjugate
#if __GLASGOW_HASKELL__ >= 710
, pattern Polar
, pattern Real
, pattern Imaginary
, pattern Conjugate
#endif
) where
import Prelude ()
import Control.Lens
import Control.Lens.Internal.Prelude
import Data.Complex
_realPart :: Lens' (Complex a) a
_realPart :: (a -> f a) -> Complex a -> f (Complex a)
_realPart a -> f a
f (a
a :+ a
b) = (a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
b) (a -> Complex a) -> f a -> f (Complex a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
a
{-# INLINE _realPart #-}
_imagPart :: Lens' (Complex a) a
_imagPart :: (a -> f a) -> Complex a -> f (Complex a)
_imagPart a -> f a
f (a
a :+ a
b) = (a
a a -> a -> Complex a
forall a. a -> a -> Complex a
:+) (a -> Complex a) -> f a -> f (Complex a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
b
{-# INLINE _imagPart #-}
_polar :: RealFloat a => Iso' (Complex a) (a,a)
_polar :: Iso' (Complex a) (a, a)
_polar = (Complex a -> (a, a))
-> ((a, a) -> Complex a) -> Iso' (Complex a) (a, a)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Complex a -> (a, a)
forall a. RealFloat a => Complex a -> (a, a)
polar ((a -> a -> Complex a) -> (a, a) -> Complex a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> a -> Complex a
forall a. Floating a => a -> a -> Complex a
mkPolar)
{-# INLINE _polar #-}
#if __GLASGOW_HASKELL__ >= 710
pattern $bPolar :: a -> a -> Complex a
$mPolar :: forall r a.
RealFloat a =>
Complex a -> (a -> a -> r) -> (Void# -> r) -> r
Polar m theta <- (view _polar -> (m, theta)) where
Polar a
m a
theta = AReview (Complex a) (a, a) -> (a, a) -> Complex a
forall b (m :: * -> *) t. MonadReader b m => AReview t b -> m t
review AReview (Complex a) (a, a)
forall a. RealFloat a => Iso' (Complex a) (a, a)
_polar (a
m, a
theta)
pattern $bReal :: a -> Complex a
$mReal :: forall r a.
(Eq a, Num a) =>
Complex a -> (a -> r) -> (Void# -> r) -> r
Real r = r :+ 0
pattern $bImaginary :: a -> Complex a
$mImaginary :: forall r a.
(Eq a, Num a) =>
Complex a -> (a -> r) -> (Void# -> r) -> r
Imaginary i = 0 :+ i
#endif
_magnitude :: RealFloat a => Lens' (Complex a) a
_magnitude :: Lens' (Complex a) a
_magnitude a -> f a
f Complex a
c = a -> Complex a
setMag (a -> Complex a) -> f a -> f (Complex a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
r
where setMag :: a -> Complex a
setMag a
r' | a
r a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0 = Complex a
c Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
* (a
r' a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
r a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
0)
| Bool
otherwise = a
r' a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
0
r :: a
r = Complex a -> a
forall a. RealFloat a => Complex a -> a
magnitude Complex a
c
{-# INLINE _magnitude #-}
_phase :: RealFloat a => Lens' (Complex a) a
_phase :: Lens' (Complex a) a
_phase a -> f a
f Complex a
c = a -> Complex a
setPhase (a -> Complex a) -> f a -> f (Complex a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
theta
where setPhase :: a -> Complex a
setPhase a
theta' = Complex a
c Complex a -> Complex a -> Complex a
forall a. Num a => a -> a -> a
* a -> Complex a
forall a. Floating a => a -> Complex a
cis (a
theta' a -> a -> a
forall a. Num a => a -> a -> a
- a
theta)
theta :: a
theta = Complex a -> a
forall a. RealFloat a => Complex a -> a
phase Complex a
c
{-# INLINE _phase #-}
_conjugate :: RealFloat a => Iso' (Complex a) (Complex a)
_conjugate :: Iso' (Complex a) (Complex a)
_conjugate = (Complex a -> Complex a) -> Iso' (Complex a) (Complex a)
forall a. (a -> a) -> Iso' a a
involuted Complex a -> Complex a
forall a. Num a => Complex a -> Complex a
conjugate
{-# INLINE _conjugate #-}
#if __GLASGOW_HASKELL__ >= 710
pattern $bConjugate :: Complex a -> Complex a
$mConjugate :: forall r a.
Num a =>
Complex a -> (Complex a -> r) -> (Void# -> r) -> r
Conjugate a <- (conjugate -> a) where
Conjugate Complex a
a = Complex a -> Complex a
forall a. Num a => Complex a -> Complex a
conjugate Complex a
a
#endif