numhask-space-0.11.1.0: Numerical spaces.
Safe HaskellSafe-Inferred
LanguageGHC2021

NumHask.Space

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
>>> 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.

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

lower, upper, (>.<)

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
Ord a => Space (Range a) Source # 
Instance details

Defined in NumHask.Space.Range

Associated Types

type Element (Range a) Source #

Ord a => Space (Rect a) Source # 
Instance details

Defined in NumHask.Space.Rect

Associated Types

type Element (Rect a) Source #

Methods

lower :: 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 

Fields

Instances

Instances details
Space a => Semigroup (Union a) Source # 
Instance details

Defined 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 #

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

a space that can be divided neatly

unsafeSpace1 (grid OuterPos s g) == s
getUnion (sconcat (Union <$> (gridSpace s g))) == s

Associated Types

type Grid s :: Type Source #

the type that divides or quotients the space

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 details

Defined in NumHask.Space.Range

Associated Types

type Grid (Range a) Source #

Methods

grid :: 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 details

Defined in NumHask.Space.Rect

Associated Types

type Grid (Rect a) Source #

Methods

grid :: 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

interpolate :: (Space s, Ring (Element s)) => s -> Element s -> Element s Source #

interpolate a space

interpolate s x == project s (zero ... one) x

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
Show Pos Source # 
Instance details

Defined in NumHask.Space.Types

Methods

showsPrec :: Int -> Pos -> ShowS #

show :: Pos -> String #

showList :: [Pos] -> ShowS #

Eq Pos Source # 
Instance details

Defined in NumHask.Space.Types

Methods

(==) :: Pos -> Pos -> Bool #

(/=) :: Pos -> Pos -> Bool #

space1 :: (Space s, Traversable f) => f (Element s) -> Maybe s Source #

Maybe containing space of a traversable.

unsafeSpace1 :: (Space s, Traversable f) => f (Element s) -> s Source #

the containing space of a non-empty Traversable.

partial function.

all $ unsafeSpace1 a `contains` <$> a

randomS :: (Space s, RandomGen g, UniformRange (Element s)) => s -> g -> (Element s, g) Source #

supply a random element within a Space

>>> randomS (one :: Range Double) g
(0.43085240252163404,StdGen {unStdGen = SMGen 4530528345362647137 13679457532755275413})

randomSM :: (UniformRange (Element s), StatefulGen g m, Space s) => s -> g -> m (Element s) Source #

StatefulGen version of randomS

>>> import Control.Monad
>>> runStateGen_ g (randomSM (one :: Range Double))
0.43085240252163404

randomSs :: (Space s, RandomGen g, UniformRange (Element s)) => Int -> s -> g -> ([Element s], g) Source #

list of n random elements within a Space

>>> let g = mkStdGen 42
>>> fst (randomSs 3 (one :: Range Double) g)
[0.43085240252163404,-6.472345419562497e-2,0.3854692674681801]
>>> fst (randomSs 3 (Rect 0 10 0 10 :: Rect Int) g)
[Point 0 7,Point 0 2,Point 1 7]

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 

Fields

  • ta :: !a
     
  • tb :: !a
     
  • tc :: !a
     
  • td :: !a
     
  • te :: !a
     
  • tf :: !a
     

Instances

Instances details
Foldable Transform Source # 
Instance details

Defined in NumHask.Space.Types

Methods

fold :: 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 #

Traversable Transform Source # 
Instance details

Defined in NumHask.Space.Types

Methods

traverse :: 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) #

Functor Transform Source # 
Instance details

Defined in NumHask.Space.Types

Methods

fmap :: (a -> b) -> Transform a -> Transform b #

(<$) :: a -> Transform b -> Transform a #

Show a => Show (Transform a) Source # 
Instance details

Defined in NumHask.Space.Types

Eq a => Eq (Transform a) Source # 
Instance details

Defined in NumHask.Space.Types

Methods

(==) :: Transform a -> Transform a -> Bool #

(/=) :: Transform a -> Transform a -> Bool #

(Multiplicative a, Additive a) => Affinity (Transform a) a Source # 
Instance details

Defined in NumHask.Space.Types

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 (Line a) a Source # 
Instance details

Defined in NumHask.Space.Point

Methods

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

(Multiplicative a, Additive a) => Affinity (Point a) a Source # 
Instance details

Defined in NumHask.Space.Point

Methods

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

(Multiplicative a, Additive a) => Affinity (Transform a) a Source # 
Instance details

Defined in NumHask.Space.Types

(|.) :: 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)

Instances