{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
#ifndef MIN_VERSION_base
#define MIN_VERSION_base(x,y,z) 1
#endif
module Data.Complex.Lens
( _realPart
, _imagPart
, _polar
, _magnitude
, _phase
, _conjugate
) where
import Control.Applicative
import Control.Lens
import Data.Complex
#if MIN_VERSION_base(4,4,0)
_realPart :: Lens' (Complex a) a
#else
_realPart :: RealFloat a => Lens' (Complex a) a
#endif
_realPart f (a :+ b) = (:+ b) <$> f a
{-# INLINE _realPart #-}
#if MIN_VERSION_base(4,4,0)
_imagPart :: Lens' (Complex a) a
#else
_imagPart :: RealFloat a => Lens' (Complex a) a
#endif
_imagPart f (a :+ b) = (a :+) <$> f b
{-# INLINE _imagPart #-}
_polar :: RealFloat a => Iso' (Complex a) (a,a)
_polar = iso polar (uncurry mkPolar)
{-# INLINE _polar #-}
_magnitude :: RealFloat a => Lens' (Complex a) a
_magnitude f c = setMag <$> f r
where setMag r' | r /= 0 = c * (r' / r :+ 0)
| otherwise = r' :+ 0
r = magnitude c
{-# INLINE _magnitude #-}
_phase :: RealFloat a => Lens' (Complex a) a
_phase f c = setPhase <$> f theta
where setPhase theta' = c * cis (theta' - theta)
theta = phase c
{-# INLINE _phase #-}
_conjugate :: RealFloat a => Iso' (Complex a) (Complex a)
_conjugate = involuted conjugate
{-# INLINE _conjugate #-}