numhask
Safe HaskellNone
LanguageGHC2024

NumHask.Algebra.Field

Description

field classes

Synopsis

Documentation

type SemiField a = (Distributive a, Divisive a) Source #

A Semifield is a field with no subtraction.

Since: 0.12

type Field a = (Ring a, Divisive a) Source #

A Field is a set on which addition, subtraction, multiplication, and division are defined. It is also assumed that multiplication is distributive over addition.

A summary of the rules inherited from super-classes of Field:

zero + a == a
a + zero == a
((a + b) + c) (a + (b + c))
a + b == b + a
a - a == zero
negate a == zero - a
negate a + a == zero
a + negate a == zero
one * a == a
a * one == a
((a * b) * c) == (a * (b * c))
(a * (b + c)) == (a * b + a * c)
((a + b) * c) == (a * c + b * c)
a * zero == zero
zero * a == zero
a / a == one || a == zero
recip a == one / a || a == zero
recip a * a == one || a == zero
a * recip a == one || a == zero

class Field a => ExpField a where Source #

A hyperbolic field class

a < zero || (sqrt . (**2)) a == a
a < zero || (log . exp) a ~= a
b < zero || a <= zero || a == 1 || abs (a ** logBase a b - b) < 10 * epsilon
>>> (sqrt . (**2)) (4.0 :: Double) == 4.0
True
>>> (log . exp) (1.0 :: Double) ~= 1.0
True
>>> 2 ** logBase 2 8 ~= (8 :: Double)
True

Minimal complete definition

exp, log

Methods

exp :: a -> a Source #

log :: a -> a Source #

(**) :: a -> a -> a Source #

logBase :: a -> a -> a Source #

log to the base of

>>> logBase 2 8
2.9999999999999996

sqrt :: a -> a Source #

square root

>>> sqrt 4
2.0

Instances

Instances details
ExpField Double Source # 
Instance details

Defined in NumHask.Algebra.Field

ExpField Float Source # 
Instance details

Defined in NumHask.Algebra.Field

(TrigField a, ExpField a) => ExpField (EuclideanPair a) Source # 
Instance details

Defined in NumHask.Algebra.Metric

(TrigField a, ExpField a) => ExpField (Complex a) Source # 
Instance details

Defined in NumHask.Data.Complex

ExpField a => ExpField (Wrapped a) Source # 
Instance details

Defined in NumHask.Data.Wrapped

ExpField b => ExpField (a -> b) Source # 
Instance details

Defined in NumHask.Algebra.Field

Methods

exp :: (a -> b) -> a -> b Source #

log :: (a -> b) -> a -> b Source #

(**) :: (a -> b) -> (a -> b) -> a -> b Source #

logBase :: (a -> b) -> (a -> b) -> a -> b Source #

sqrt :: (a -> b) -> a -> b Source #

class SemiField a => QuotientField a where Source #

Quotienting of a Field into a Ring

See Field of fractions

\a -> a - one < floor a <= a <= ceiling a < a + one
>>> properFraction (1.5 :: Double)
Quotient {whole = 1, fraction = 0.5}
>>> fromQuotient (properFraction (1.5 :: Double))
1.5

Minimal complete definition

properFraction

Associated Types

type Whole a Source #

Methods

properFraction :: a -> Quotient (Whole a) a Source #

Split into a whole part and a fractional tail in the canonical range 0 <= fraction < one.

The floor decomposition, not Prelude's toward-zero split: the fraction is never negative, so properFraction (-1.5) = Quotient {whole = -2, fraction = 0.5}. Deliberate departure from Prelude — the floor basis keeps the sign in whole, a clean change-of-basis to compute with, instead of compounding sign into the fraction.

>>> properFraction (-1.5 :: Double)
Quotient {whole = -2, fraction = 0.5}

fromWhole :: Whole a -> a Source #

Embed the whole-number type back into the field.

The recombination inverse of properFraction. Scalar fields default to fromIntegral; compound fields override it (e.g. Whole (Complex a) = Complex (Whole a) lifts component-wise).

default fromWhole :: FromIntegral a (Whole a) => Whole a -> a Source #

round :: a -> Whole a Source #

round to the nearest Int

Exact ties are managed by rounding down ties if the whole component is even.

>>> round (1.5 :: Double)
2
>>> round (2.5 :: Double)
2

default round :: (Ord a, Eq (Whole a), Integral (Whole a)) => a -> Whole a Source #

ceiling :: a -> Whole a Source #

supply the next upper whole component

>>> ceiling (1.001 :: Double)
2

default ceiling :: (Ord a, Distributive (Whole a)) => a -> Whole a Source #

floor :: a -> Whole a Source #

supply the previous lower whole component

>>> floor (1.001 :: Double)
1

truncate :: a -> Whole a Source #

supply the whole component closest to zero

>>> floor (-1.001 :: Double)
-2
>>> truncate (-1.001 :: Double)
-1

default truncate :: Ord a => a -> Whole a Source #

Instances

Instances details
QuotientField Double Source # 
Instance details

Defined in NumHask.Algebra.Field

Associated Types

type Whole Double 
Instance details

Defined in NumHask.Algebra.Field

QuotientField Float Source # 
Instance details

Defined in NumHask.Algebra.Field

Associated Types

type Whole Float 
Instance details

Defined in NumHask.Algebra.Field

type Whole Float = Int
QuotientField a => QuotientField (EuclideanPair a) Source # 
Instance details

Defined in NumHask.Algebra.Metric

Associated Types

type Whole (EuclideanPair a) 
Instance details

Defined in NumHask.Algebra.Metric

(Subtractive a, QuotientField a) => QuotientField (Complex a) Source #

A complex field quotients and recombines component-wise.

>>> fromQuotient (properFraction (Complex (1.5, 2.5) :: Complex Double))
Complex {complexPair = (1.5,2.5)}

Can't use DerivingVia due to extra Whole constraints.

Instance details

Defined in NumHask.Data.Complex

Associated Types

type Whole (Complex a) 
Instance details

Defined in NumHask.Data.Complex

type Whole (Complex a) = Complex (Whole a)
QuotientField (Positive Double) Source # 
Instance details

Defined in NumHask.Data.Positive

Associated Types

type Whole (Positive Double) 
Instance details

Defined in NumHask.Data.Positive

(Ord a, EndoBased a, Absolute a, ToInt a, FromIntegral a Int, Integral a, Ring a) => QuotientField (Ratio a) Source # 
Instance details

Defined in NumHask.Data.Rational

Associated Types

type Whole (Ratio a) 
Instance details

Defined in NumHask.Data.Rational

type Whole (Ratio a) = Int
(Ord a, Eq (Whole a), Integral (Whole a), Subtractive (Whole a), Subtractive a, QuotientField a) => QuotientField (Wrapped a) Source # 
Instance details

Defined in NumHask.Data.Wrapped

Associated Types

type Whole (Wrapped a) 
Instance details

Defined in NumHask.Data.Wrapped

type Whole (Wrapped a) = Whole a

data Quotient w a Source #

Whole-and-fraction pair. properFraction is the normalizer (Prelude toward-zero). The constructor is raw; the range is not enforced.

Constructors

Quotient 

Fields

Instances

Instances details
(Eq w, Eq a) => Eq (Quotient w a) Source # 
Instance details

Defined in NumHask.Algebra.Field

Methods

(==) :: Quotient w a -> Quotient w a -> Bool #

(/=) :: Quotient w a -> Quotient w a -> Bool #

(Show w, Show a) => Show (Quotient w a) Source # 
Instance details

Defined in NumHask.Algebra.Field

Methods

showsPrec :: Int -> Quotient w a -> ShowS #

show :: Quotient w a -> String #

showList :: [Quotient w a] -> ShowS #

toQuotient :: QuotientField a => a -> Quotient (Whole a) a Source #

properFraction as the named normalizer into Quotient.

The canonical range is the Prelude toward-zero split: fraction ∈ (-1, 1) with the sign of the input, and fromQuotient (toQuotient a) == a.

fromQuotient :: QuotientField a => Quotient (Whole a) a -> a Source #

Recombine a Quotient. Law: fromQuotient (toQuotient a) == a.

mulQuotient :: QuotientField a => Quotient (Whole a) a -> Quotient (Whole a) a -> Quotient (Whole a) a Source #

Bilinear multiplication of quotients: recombine, multiply, re-split.

(w1 + f1) * (w2 + f2) expands to the four terms w1*w2 + w1*f2 + f1*w2 + f1*f2, and toQuotient pushes any overflow back into whole. The same bilinear-then-normalize pattern as complex multiplication.

>>> mulQuotient (toQuotient (0.5 :: Double)) (toQuotient (3.0 :: Double))
Quotient {whole = 1, fraction = 0.5}
>>> mulQuotient (toQuotient (1.5 :: Double)) (toQuotient (2.0 :: Double)) == toQuotient (1.5 * 2.0 :: Double)
True

infinity :: SemiField a => a Source #

infinity is defined for any Field.

>>> one / zero + infinity
Infinity
>>> infinity + 1
Infinity

negInfinity :: Field a => a Source #

negative infinity

>>> negInfinity + infinity
NaN

nan :: SemiField a => a Source #

nan is defined as zero/zero

but note the (social) law:

>>> nan == zero / zero
False

class Field a => TrigField a where Source #

Trigonometric Field

The list of laws is quite long: trigonometric identities

Minimal complete definition

pi, sin, cos, asin, acos, atan, atan2, sinh, cosh, asinh, acosh, atanh

Methods

pi :: a Source #

sin :: a -> a Source #

cos :: a -> a Source #

tan :: a -> a Source #

asin :: a -> a Source #

acos :: a -> a Source #

atan :: a -> a Source #

atan2 :: a -> a -> a Source #

sinh :: a -> a Source #

cosh :: a -> a Source #

tanh :: a -> a Source #

asinh :: a -> a Source #

acosh :: a -> a Source #

atanh :: a -> a Source #

Instances

Instances details
TrigField Double Source # 
Instance details

Defined in NumHask.Algebra.Field

TrigField Float Source # 
Instance details

Defined in NumHask.Algebra.Field

TrigField a => TrigField (Wrapped a) Source # 
Instance details

Defined in NumHask.Data.Wrapped

TrigField b => TrigField (a -> b) Source # 
Instance details

Defined in NumHask.Algebra.Field

Methods

pi :: a -> b Source #

sin :: (a -> b) -> a -> b Source #

cos :: (a -> b) -> a -> b Source #

tan :: (a -> b) -> a -> b Source #

asin :: (a -> b) -> a -> b Source #

acos :: (a -> b) -> a -> b Source #

atan :: (a -> b) -> a -> b Source #

atan2 :: (a -> b) -> (a -> b) -> a -> b Source #

sinh :: (a -> b) -> a -> b Source #

cosh :: (a -> b) -> a -> b Source #

tanh :: (a -> b) -> a -> b Source #

asinh :: (a -> b) -> a -> b Source #

acosh :: (a -> b) -> a -> b Source #

atanh :: (a -> b) -> a -> b Source #

half :: (Additive a, Divisive a) => a Source #

A half of one

>>> half :: Double
0.5

modF :: (Eq a, Field a, FromIntegral a (Whole a), QuotientField a) => a -> a -> a Source #

Approximate modulo for fields

>>> modF 1.5 1.2
0.30000000000000004

Since: 0.13

divF :: (Eq a, Field a, FromIntegral a (Whole a), QuotientField a) => a -> a -> a Source #

Approximate diviso for fields.

Compared with div, divF returns the original type rather than the Whole type.

>>> divF 1.5 1.2
1.0

Since: 0.13

divModF :: (Eq a, Field a, FromIntegral a (Whole a), QuotientField a) => a -> a -> DivMod a Source #

Approximate divMod for fields.

>>> divModF 1.5 1.2
DivMod {div_ = 1.0, mod_ = 0.30000000000000004}

Since: 0.13