Contents

Description

Mathematics does not rigorously define a space, leaving library devs free to explore.

“But who can quantify the algebra of space, or weigh those worlds that swim each in its place? Who can outdo the dark? And what computer knows how beauty comes to birth - shell star and rose?

~ Technicians by Jean Kenward” ~ John Foster

Synopsis

# Usage

>>> :set -XRebindableSyntax
>>> :set -XNegativeLiterals
>>> import NumHask.Prelude
>>> import NumHask.Space
>>> Point 1 1
Point 1 1

>>> one :: Range Double
Range -0.5 0.5

>>> grid OuterPos (Range 0 50 :: Range Double) 5
[0.0,10.0,20.0,30.0,40.0,50.0]


# Space

Space is an interesting cross-section of many programming domains.

• A Range is a Space of numbers.
• A Rect is a Space of Points. It can also be a Space of Rects (but this is not yet coded up here).
• A time span is a Space containing moments of time.
• A Histogram is a divided Range with a count of elements within each division.

class Space s where Source #

A Space is a continuous set of numbers. Continuous here means that the set has an upper and lower bound, and an element that is between these two bounds is a member of the Space.

a union b == b union a
a intersection b == b intersection a
(a union b) intersection c == (a intersection b) union (a intersection c)
(a intersection b) union c == (a union b) intersection (a union c)
norm (norm a) = norm a
a |>| b == b |<| a
a |.| singleton a

Minimal complete definition

Associated Types

type Element s :: Type Source #

the underlying element in the space

Methods

lower :: s -> Element s Source #

lower boundary

upper :: s -> Element s Source #

upper boundary

singleton :: Element s -> s Source #

space containing a single element

intersection :: s -> s -> s Source #

the intersection of two spaces

default intersection :: Ord (Element s) => s -> s -> s Source #

union :: s -> s -> s Source #

the union of two spaces

default union :: Ord (Element s) => s -> s -> s Source #

normalise :: s -> s Source #

Normalise a space so that

lower a \/ upper a == lower a
lower a /\ upper a == upper a

(...) :: Element s -> Element s -> s infix 3 Source #

create a normalised space from two elements

default (...) :: Ord (Element s) => Element s -> Element s -> s Source #

(>.<) :: Element s -> Element s -> s infix 3 Source #

create a space from two elements without normalising

(|.|) :: Element s -> s -> Bool infixl 7 Source #

is an element in the space

default (|.|) :: Ord (Element s) => Element s -> s -> Bool Source #

(|>|) :: s -> s -> Bool infixl 7 Source #

is one space completely above the other

default (|>|) :: Ord (Element s) => s -> s -> Bool Source #

(|<|) :: s -> s -> Bool infixl 7 Source #

is one space completely below the other

default (|<|) :: Ord (Element s) => s -> s -> Bool Source #

#### Instances

Instances details
 (Eq a, Ord a) => Space (Range a) Source # Instance detailsDefined in NumHask.Space.Range Associated Typestype Element (Range a) Source # Methodslower :: Range a -> Element (Range a) Source #upper :: Range a -> Element (Range a) Source #singleton :: Element (Range a) -> Range a Source #intersection :: Range a -> Range a -> Range a Source #union :: Range a -> Range a -> Range a Source #normalise :: Range a -> Range a Source #(...) :: Element (Range a) -> Element (Range a) -> Range a Source #(>.<) :: Element (Range a) -> Element (Range a) -> Range a Source #(|.|) :: Element (Range a) -> Range a -> Bool Source #(|>|) :: Range a -> Range a -> Bool Source #(|<|) :: Range a -> Range a -> Bool Source # Ord a => Space (Rect a) Source # Instance detailsDefined in NumHask.Space.Rect Associated Typestype Element (Rect a) Source # Methodslower :: Rect a -> Element (Rect a) Source #upper :: Rect a -> Element (Rect a) Source #singleton :: Element (Rect a) -> Rect a Source #intersection :: Rect a -> Rect a -> Rect a Source #union :: Rect a -> Rect a -> Rect a Source #normalise :: Rect a -> Rect a Source #(...) :: Element (Rect a) -> Element (Rect a) -> Rect a Source #(>.<) :: Element (Rect a) -> Element (Rect a) -> Rect a Source #(|.|) :: Element (Rect a) -> Rect a -> Bool Source #(|>|) :: Rect a -> Rect a -> Bool Source #(|<|) :: Rect a -> Rect a -> Bool Source #

newtype Union a Source #

a convex hull

Constructors

 Union FieldsgetUnion :: a

#### Instances

Instances details
 Space a => Semigroup (Union a) Source # Instance detailsDefined in NumHask.Space.Types Methods(<>) :: Union a -> Union a -> Union a #sconcat :: NonEmpty (Union a) -> Union a #stimes :: Integral b => b -> Union a -> Union a #

newtype Intersection a Source #

Constructors

 Intersection FieldsgetIntersection :: a

#### Instances

Instances details
 Space a => Semigroup (Intersection a) Source # Instance detailsDefined in NumHask.Space.Types Methods(<>) :: Intersection a -> Intersection a -> Intersection a #stimes :: Integral b => b -> Intersection a -> Intersection a #

class (Space s, Field (Element s)) => FieldSpace s where Source #

a space that can be divided neatly

space1 (grid OuterPos s g) == s
getUnion (sconcat (Union <$> (gridSpace s g))) == s Associated Types type Grid s :: Type Source # Methods grid :: Pos -> s -> Grid s -> [Element s] Source # create equally-spaced elements across a space gridSpace :: s -> Grid s -> [s] Source # create equally-spaced spaces from a space #### Instances Instances details  (Field a, Ord a, FromIntegral a Int) => FieldSpace (Range a) Source # Instance detailsDefined in NumHask.Space.Range Associated Typestype Grid (Range a) Source # Methodsgrid :: Pos -> Range a -> Grid (Range a) -> [Element (Range a)] Source #gridSpace :: Range a -> Grid (Range a) -> [Range a] Source # (FromIntegral a Int, Field a, Ord a) => FieldSpace (Rect a) Source # Instance detailsDefined in NumHask.Space.Rect Associated Typestype Grid (Rect a) Source # Methodsgrid :: Pos -> Rect a -> Grid (Rect a) -> [Element (Rect a)] Source #gridSpace :: Rect a -> Grid (Rect a) -> [Rect a] Source # mid :: (Space s, Field (Element s)) => s -> Element s Source # middle element of the space project :: (Space s, Field (Element s)) => s -> s -> Element s -> Element s Source # project an element from one space to another, preserving relative position. project o n (lower o) = lower n project o n (upper o) = upper n project o n (mid o) = mid n project a a x = x data Pos Source # Pos suggests where points should be placed in forming a grid across a field space. Constructors  OuterPos include boundaries InnerPos don't include boundaries LowerPos include the lower boundary UpperPos include the upper boundary MidPos use the mid-point of the space #### Instances Instances details  Source # Instance detailsDefined in NumHask.Space.Types Methods(==) :: Pos -> Pos -> Bool #(/=) :: Pos -> Pos -> Bool # Source # Instance detailsDefined in NumHask.Space.Types MethodsshowsPrec :: Int -> Pos -> ShowS #show :: Pos -> String #showList :: [Pos] -> ShowS # space1 :: (Space s, Traversable f) => f (Element s) -> s Source # the containing space of a non-empty Traversable all$ space1 a contains <$> a randomS :: (Space s, RandomGen g, Random (Element s)) => s -> g -> (Element s, g) Source # supply a random element within a Space randomSs :: (Space s, RandomGen g, Random (Element s)) => s -> g -> [Element s] Source # supply an (infinite) list of random elements within a Space >>> let g = mkStdGen 42 >>> take 3$ randomSs (one :: Range Double) g
[0.43085240252163404,-6.472345419562497e-2,0.3854692674681801]

>>> take 3 $randomSs (Rect 0 10 0 10 :: Rect Word8) g [Point 8 0,Point 6 4,Point 5 3]  memberOf :: Space s => Element s -> s -> Bool Source # is an element contained within a space contains :: Space s => s -> s -> Bool Source # is a space contained within another? (a union b) contains a (a union b) contains b disjoint :: Space s => s -> s -> Bool Source # are two spaces disjoint? width :: (Space s, Subtractive (Element s)) => s -> Element s Source # distance between boundaries (+/-) :: (Space s, Subtractive (Element s)) => Element s -> Element s -> s infixl 6 Source # create a space centered on a plus or minus b monotone :: (Space a, Space b) => (Element a -> Element b) -> a -> b Source # lift a monotone function (increasing or decreasing) over a given space eps :: (Space s, FromRational (Element s), Field (Element s)) => Element s -> Element s -> s Source # a small space widen :: (Space s, Ring (Element s)) => Element s -> s -> s Source # widen a space widenEps :: (Space s, FromRational (Element s), Ring (Element s)) => Element s -> s -> s Source # widen by a small amount scale :: (Multiplicative (Element s), Space s) => Element s -> s -> s Source # Scale a Space. (scalar multiplication) move :: (Additive (Element s), Space s) => Element s -> s -> s Source # Move a Space. (scalar addition) data Transform a Source # linear transform + translate of a point-like number (x, y) -> (ax + by + c, dx + ey + d) or $\begin{pmatrix} a & b & c\\ d & e & f\\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x\\ y\\ 1 \end{pmatrix}$ Constructors  Transform Fieldsta :: !a tb :: !a tc :: !a td :: !a te :: !a tf :: !a #### Instances Instances details  Source # Instance detailsDefined in NumHask.Space.Types Methodsfmap :: (a -> b) -> Transform a -> Transform b #(<$) :: a -> Transform b -> Transform a # Source # Instance detailsDefined in NumHask.Space.Types Methodsfold :: Monoid m => Transform m -> m #foldMap :: Monoid m => (a -> m) -> Transform a -> m #foldMap' :: Monoid m => (a -> m) -> Transform a -> m #foldr :: (a -> b -> b) -> b -> Transform a -> b #foldr' :: (a -> b -> b) -> b -> Transform a -> b #foldl :: (b -> a -> b) -> b -> Transform a -> b #foldl' :: (b -> a -> b) -> b -> Transform a -> b #foldr1 :: (a -> a -> a) -> Transform a -> a #foldl1 :: (a -> a -> a) -> Transform a -> a #toList :: Transform a -> [a] #null :: Transform a -> Bool #length :: Transform a -> Int #elem :: Eq a => a -> Transform a -> Bool #maximum :: Ord a => Transform a -> a #minimum :: Ord a => Transform a -> a #sum :: Num a => Transform a -> a #product :: Num a => Transform a -> a # Source # Instance detailsDefined in NumHask.Space.Types Methodstraverse :: Applicative f => (a -> f b) -> Transform a -> f (Transform b) #sequenceA :: Applicative f => Transform (f a) -> f (Transform a) #mapM :: Monad m => (a -> m b) -> Transform a -> m (Transform b) #sequence :: Monad m => Transform (m a) -> m (Transform a) # Eq a => Eq (Transform a) Source # Instance detailsDefined in NumHask.Space.Types Methods(==) :: Transform a -> Transform a -> Bool #(/=) :: Transform a -> Transform a -> Bool # Show a => Show (Transform a) Source # Instance detailsDefined in NumHask.Space.Types MethodsshowsPrec :: Int -> Transform a -> ShowS #show :: Transform a -> String #showList :: [Transform a] -> ShowS # (Multiplicative a, Additive a) => Affinity (Transform a) a Source # Instance detailsDefined in NumHask.Space.Types Methodstransform :: Transform a -> Transform a -> Transform a Source #

inverseTransform :: (Eq a, Field a) => Transform a -> Maybe (Transform a) Source #

Calculate the inverse of a transformation.

class Affinity a b | a -> b where Source #

An Affinity is something that can be subjected to an affine transformation in 2-dimensional space, where affine means a linear matrix operation or a translation (+).

https://en.wikipedia.org/wiki/Affine_transformation

Methods

transform :: Transform b -> a -> a Source #

#### Instances

Instances details
 (Multiplicative a, Additive a) => Affinity (Transform a) a Source # Instance detailsDefined in NumHask.Space.Types Methodstransform :: Transform a -> Transform a -> Transform a Source # (Multiplicative a, Additive a) => Affinity (Line a) a Source # Instance detailsDefined in NumHask.Space.Point Methodstransform :: Transform a -> Line a -> Line a Source # (Multiplicative a, Additive a) => Affinity (Point a) a Source # Instance detailsDefined in NumHask.Space.Point Methodstransform :: Transform a -> Point a -> Point a Source #

(|.) :: Affinity a b => Transform b -> a -> a infix 3 Source #

Apply a Transform to an Affinity

rotate :: TrigField a => a -> Transform a Source #

Rotate an Affinity (counter-clockwise)