HMath.Complex¶
Module index · Numerical conventions
Description
Checked finite complex arithmetic over Data.Complex. Angles are radians,
positive rotations are counterclockwise, and logarithms/powers use the principal
branch (argument in (-pi,pi], with IEEE signed-zero behavior inherited from
Data.Complex). Poles, zero reciprocals, and nonfinite results are errors.
Matrices use the existing row-major M.Matrix; FFTs use the existing negative
forward-exponent convention. The reference matrix budget is 256 per dimension.
On this page¶
| API | Purpose |
|---|---|
| Complex | A data type representing complex numbers. |
| complex | Construct a finite number from real and imaginary parts. |
| realPart | Extracts the real part of a complex number. |
| imagPart | Extracts the imaginary part of a complex number. |
| magnitude | The non-negative magnitude of a complex number. |
| phase | The phase of a complex number, in the range (-pi, pi]. |
| conjugate | The conjugate of a complex number. |
| reciprocal | Reciprocal using scaled division to avoid squaring a large norm. |
| add | Checked sum. |
| subtractComplex | Checked difference. |
| multiply | Checked product. |
| divide | Smith scaled division. |
| power | Principal complex power. 0^0 is 1; other zero-base exponents must be positive real. |
| Elementary | Elementary functions with principal inverse-function branches. |
| Exp | Declaration and type contract below. |
| Log | Declaration and type contract below. |
| Sqrt | Declaration and type contract below. |
| Sin | Declaration and type contract below. |
| Cos | Declaration and type contract below. |
| Tan | Declaration and type contract below. |
| Sinh | Declaration and type contract below. |
| Cosh | Declaration and type contract below. |
| Tanh | Declaration and type contract below. |
| Asin | Declaration and type contract below. |
| Acos | Declaration and type contract below. |
| Atan | Declaration and type contract below. |
| Asinh | Declaration and type contract below. |
| Acosh | Declaration and type contract below. |
| Atanh | Declaration and type contract below. |
| elementary | Apply an elementary function, rejecting poles and nonfinite results. |
| fromPolar | Polar construction with nonnegative finite radius. |
| toPolar | Radius and principal phase; the phase of zero is defined by Data.Complex. |
| rotate | Counterclockwise rotation in the complex plane. |
| complexMatrix | Checked finite complex matrix, at most 256 rows and columns. |
| multiplyMatrices | Matrix multiplication with finite-result validation. |
| conjugateTranspose | Hermitian transpose with finite-input validation. |
| complexFFT | Existing checked mixed-radix/Bluestein forward FFT. |
| complexIFFT | Existing checked inverse FFT, normalized by sample count. |
Documentation
Complex
A data type representing complex numbers.
You can read about complex numbers on wikipedia.
In haskell, complex numbers are represented as a :+ b which can be thought of
as representing \(a + bi\). For a complex number z, is a number with the abs zmagnitude of z,
but oriented in the positive real direction, whereas
has the signum zphase of z, but unit magnitude.
Apart from the loss of precision due to IEEE754 floating point numbers,
it holds that z == .abs z * signum z
Note that Complex's instances inherit the deficiencies from the type
parameter's. For example, Complex Float's Ord instance has similar
problems to Float's.
As can be seen in the examples, the Foldable
and Traversable instances traverse the real part first.
Examples
>>>(5.0 :+ 2.5) + 6.511.5 :+ 2.5
>>>abs (1.0 :+ 1.0) - sqrt 2.00.0 :+ 0.0
>>>abs (signum (4.0 :+ 3.0))1.0 :+ 0.0
>>>foldr (:) [] (1 :+ 2)[1,2]
>>>mapM print (1 :+ 2)1 2
Instances
| MonadZip Complex | Since: base-4.15.0.0 | ||||
| Foldable1 Complex | Since: base-4.18.0.0 | ||||
Defined in Data.Foldable1 Methods fold1 :: Semigroup m => Complex m -> m # foldMap1 :: Semigroup m => (a -> m) -> Complex a -> m # foldMap1' :: Semigroup m => (a -> m) -> Complex a -> m # toNonEmpty :: Complex a -> NonEmpty a # maximum :: Ord a => Complex a -> a # minimum :: Ord a => Complex a -> a # foldrMap1 :: (a -> b) -> (a -> b -> b) -> Complex a -> b # foldlMap1' :: (a -> b) -> (b -> a -> b) -> Complex a -> b # foldlMap1 :: (a -> b) -> (b -> a -> b) -> Complex a -> b # foldrMap1' :: (a -> b) -> (a -> b -> b) -> Complex a -> b # | |||||
| Eq1 Complex |
Since: base-4.16.0.0 | ||||
| Read1 Complex |
Since: base-4.16.0.0 | ||||
Defined in Data.Functor.Classes | |||||
| Show1 Complex |
Since: base-4.16.0.0 | ||||
| Applicative Complex | Since: base-4.9.0.0 | ||||
| Functor Complex | Since: base-4.9.0.0 | ||||
| Monad Complex | Since: base-4.9.0.0 | ||||
| MonadFix Complex | Since: base-4.15.0.0 | ||||
Defined in Data.Complex | |||||
| Foldable Complex | Since: base-4.9.0.0 | ||||
Defined in Data.Complex Methods fold :: Monoid m => Complex m -> m # foldMap :: Monoid m => (a -> m) -> Complex a -> m # foldMap' :: Monoid m => (a -> m) -> Complex a -> m # foldr :: (a -> b -> b) -> b -> Complex a -> b # foldr' :: (a -> b -> b) -> b -> Complex a -> b # foldl :: (b -> a -> b) -> b -> Complex a -> b # foldl' :: (b -> a -> b) -> b -> Complex a -> b # foldr1 :: (a -> a -> a) -> Complex a -> a # foldl1 :: (a -> a -> a) -> Complex a -> a # elem :: Eq a => a -> Complex a -> Bool # maximum :: Ord a => Complex a -> a # minimum :: Ord a => Complex a -> a # | |||||
| Traversable Complex | Since: base-4.9.0.0 | ||||
| Generic1 Complex | |||||
Defined in Data.Complex Associated Types
| |||||
| Data a => Data (Complex a) | Since: base-2.1 | ||||
Defined in Data.Complex Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Complex a -> c (Complex a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Complex a) # toConstr :: Complex a -> Constr # dataTypeOf :: Complex a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Complex a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Complex a)) # gmapT :: (forall b. Data b => b -> b) -> Complex a -> Complex a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Complex a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Complex a -> r # gmapQ :: (forall d. Data d => d -> u) -> Complex a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Complex a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Complex a -> m (Complex a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Complex a -> m (Complex a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Complex a -> m (Complex a) # | |||||
| RealFloat a => Floating (Complex a) | Since: base-2.1 | ||||
Defined in Data.Complex Methods exp :: Complex a -> Complex a # log :: Complex a -> Complex a # sqrt :: Complex a -> Complex a # (**) :: Complex a -> Complex a -> Complex a # logBase :: Complex a -> Complex a -> Complex a # sin :: Complex a -> Complex a # cos :: Complex a -> Complex a # tan :: Complex a -> Complex a # asin :: Complex a -> Complex a # acos :: Complex a -> Complex a # atan :: Complex a -> Complex a # sinh :: Complex a -> Complex a # cosh :: Complex a -> Complex a # tanh :: Complex a -> Complex a # asinh :: Complex a -> Complex a # acosh :: Complex a -> Complex a # atanh :: Complex a -> Complex a # log1p :: Complex a -> Complex a # expm1 :: Complex a -> Complex a # | |||||
| Storable a => Storable (Complex a) | Since: base-4.8.0.0 | ||||
Defined in Data.Complex | |||||
| Generic (Complex a) | |||||
Defined in Data.Complex Associated Types
| |||||
| RealFloat a => Num (Complex a) | Since: base-2.1 | ||||
| Read a => Read (Complex a) | Since: base-2.1 | ||||
| RealFloat a => Fractional (Complex a) | Since: base-2.1 | ||||
| Show a => Show (Complex a) | Since: base-2.1 | ||||
| Eq a => Eq (Complex a) | Since: base-2.1 | ||||
| Field (Complex Double) Source # | |||||
| FloatingPoint (Complex Double) Source # | |||||
Defined in HMath.API.Algebra | |||||
| Numeric (Complex Double) Source # | |||||
| Ring (Complex Double) Source # | |||||
| Scalar (Complex Double) Source # | |||||
Defined in HMath.API.Algebra | |||||
| Element (Complex Double) Source # | |||||
| Element (Complex Float) Source # | |||||
| type Rep1 Complex | Since: base-4.9.0.0 | ||||
Defined in Data.Complex type Rep1 Complex = D1 ('MetaData "Complex" "Data.Complex" "base" 'False) (C1 ('MetaCons ":+" ('InfixI 'NotAssociative 6) 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1)) | |||||
| type Rep (Complex a) | Since: base-4.9.0.0 | ||||
Defined in Data.Complex type Rep (Complex a) = D1 ('MetaData "Complex" "Data.Complex" "base" 'False) (C1 ('MetaCons ":+" ('InfixI 'NotAssociative 6) 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a))) | |||||
complex
complex :: Double -> Double -> Either MathError (Complex Double) Source #
Construct a finite number from real and imaginary parts.
realPart
Extracts the real part of a complex number.
Examples
>>>realPart (5.0 :+ 3.0)5.0
>>>realPart ((5.0 :+ 3.0) * (2.0 :+ 3.0))1.0
imagPart
Extracts the imaginary part of a complex number.
Examples
>>>imagPart (5.0 :+ 3.0)3.0
>>>imagPart ((5.0 :+ 3.0) * (2.0 :+ 3.0))21.0
magnitude
magnitude :: RealFloat a => Complex a -> a #
The non-negative magnitude of a complex number.
Examples
>>>magnitude (1.0 :+ 1.0)1.4142135623730951
>>>magnitude (1.0 + 0.0)1.0
>>>magnitude (0.0 :+ (-5.0))5.0
phase
conjugate
conjugate :: Num a => Complex a -> Complex a #
The conjugate of a complex number.
conjugate (conjugate x) = x
Examples
>>>conjugate (3.0 :+ 3.0)3.0 :+ (-3.0)
>>>conjugate ((3.0 :+ 3.0) * (2.0 :+ 2.0))0.0 :+ (-12.0)
reciprocal
reciprocal :: Complex Double -> Either MathError (Complex Double) Source #
Reciprocal using scaled division to avoid squaring a large norm.
add
add :: Complex Double -> Complex Double -> Either MathError (Complex Double) Source #
Checked sum.
subtractComplex
subtractComplex :: Complex Double -> Complex Double -> Either MathError (Complex Double) Source #
Checked difference.
multiply
multiply :: Complex Double -> Complex Double -> Either MathError (Complex Double) Source #
Checked product.
divide
divide :: Complex Double -> Complex Double -> Either MathError (Complex Double) Source #
Smith scaled division. A zero divisor is rejected.
power
power :: Complex Double -> Complex Double -> Either MathError (Complex Double) Source #
Principal complex power. 0^0 is 1; other zero-base exponents must be positive real.
Elementary
data Elementary Source #
Elementary functions with principal inverse-function branches.
Instances
| Show Elementary Source # | |
Defined in HMath.Complex Methods showsPrec :: Int -> Elementary -> ShowS # show :: Elementary -> String # showList :: [Elementary] -> ShowS # | |
| Eq Elementary Source # | |
Defined in HMath.Complex | |
elementary
elementary :: Elementary -> Complex Double -> Either MathError (Complex Double) Source #
Apply an elementary function, rejecting poles and nonfinite results.
fromPolar
fromPolar :: Double -> Double -> Either MathError (Complex Double) Source #
Polar construction with nonnegative finite radius.
toPolar
toPolar :: Complex Double -> Either MathError (Double, Double) Source #
Radius and principal phase; the phase of zero is defined by Data.Complex.
rotate
rotate :: Double -> Complex Double -> Either MathError (Complex Double) Source #
Counterclockwise rotation in the complex plane.
complexMatrix
complexMatrix :: Int -> Int -> [Complex Double] -> Either MathError (Matrix (Complex Double)) Source #
Checked finite complex matrix, at most 256 rows and columns.
multiplyMatrices
multiplyMatrices :: Matrix (Complex Double) -> Matrix (Complex Double) -> Either MathError (Matrix (Complex Double)) Source #
Matrix multiplication with finite-result validation.
conjugateTranspose
conjugateTranspose :: Matrix (Complex Double) -> Either MathError (Matrix (Complex Double)) Source #
Hermitian transpose with finite-input validation.