hgeometry-combinatorial
Copyright(C) Frank Staals
Licensesee the LICENSE file
MaintainerFrank Staals
Safe HaskellNone
LanguageGHC2021

HGeometry.Number.Real.Symbolic

Description

Defines a type 'Symbolic i r' that represents "indexed" numbers of type r that have been "symbolically" perturbed by some arbitraryily small \(\varepsilon\).

With indexed numbers we mean that every number has some index, and the size of the pertubation \(\varepsilon\) is proportional to this index.

This is useful for implementing a more general form of "Simulation of Simplicity" as described in:

Synopsis

Documentation

data EpsFold i Source #

Let \(\mathcal{I}\) be a bag with indices, let \(c\) be an upper bound on the number of times a single item may occur in (mathcal{I}), and let \(\varepsilon\) be a function mapping indices to real numbers that satisfies:

  1. \(0 < \varepsilon(j) < 1\), for all \(1 \leq j\),
  2. \(\prod_{0 \leq i \leq j} \varepsilon(i)^c > \varepsilon(k)\), for all \(1 \leq j < k\)

Note that such a function exists:

begin{lemma} label{lem:condition_2} Let \(\delta \in (0,1)\) and \(d \geq c+1\). The function \(\varepsilon(i) = \delta^{d^i}\) satisfies condition 2. end{lemma}

begin{proof} By transitivity it suffices to argue this for \(k=j+1\):

begin{align*} &qquad prod_{0 leq i leq j} varepsilon(i)^c > varepsilon(j+1) \ equiv &qquad prod_{0 leq i leq j} (delta^{d^i})^c > delta^{d^{j+1}}\ equiv &qquad prod_{0 leq i leq j} delta^{cd^i} > delta^{d^{j+1}} \ equiv &qquad delta^{sum_{0 leq i leq j} cd^i} > delta^{d^{j+1}} & text{using } delta in (0,1)\ equiv &qquad sum_{0 leq i leq j} cd^i < d^{j+1} \ equiv &qquad csum_{0 leq i leq j} d^i < d^{j+1} \ end{align*}

We prove this by induction.

For the base case \(j=0\): we have \(0 < 1\), which is trivially true.

For the step case we have the induction hypothesis \(c\sum_{0 \leq i \leq j} d^i < d^{j+1}\), and we have to prove that \(c\sum_{0 \leq i \leq j+1} d^i < d^{j+2}\):

begin{align*} csum_{0 leq i leq j+1} d^i &= cd^{j+1} + csum_{0 leq i leq j} d^i \ &< cd^{j+1} + d^{j+1} & text{using IH} \ &= (c+1)d^{j+1} & text{using that } c+1 leq d \ &leq dd^{j+1} \ &=d^{j+2} end{align*} This completes the proof. end{proof}

An EpsFold now represents the term

\[ \prod_{i \in \mathcal{I}} \varepsilon(i) \]

for some bag \(\mathcal{I}\).

Let \(\mathcal{J}\) be some sub-bag of \(\mathcal{I}\). Note that condition 2 implies that:

\(\prod_{i \in \mathcal{J}} \varepsilon(i) > \varepsilon(k)\), for all \(1 \leq j < k\)

This means that when comparing two EpsFolds, say \(e_1\) and \(e_2\), representing bags \(\mathcal{I}_1\) and \(\mathcal{I}_2\), respectively. It suffices to compare the largest index (j in mathcal{I}_1setminusmathcal{I}_2) with the largest index (k in mathcal{I}_2setminusmathcal{I}_1). We have that

\(e_1 > e_2\) if and only if \(j < k\).

Instances

Instances details
Ord i => Monoid (EpsFold i) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

mempty :: EpsFold i #

mappend :: EpsFold i -> EpsFold i -> EpsFold i #

mconcat :: [EpsFold i] -> EpsFold i #

Ord i => Semigroup (EpsFold i) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

(<>) :: EpsFold i -> EpsFold i -> EpsFold i #

sconcat :: NonEmpty (EpsFold i) -> EpsFold i #

stimes :: Integral b => b -> EpsFold i -> EpsFold i #

Show i => Show (EpsFold i) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

showsPrec :: Int -> EpsFold i -> ShowS #

show :: EpsFold i -> String #

showList :: [EpsFold i] -> ShowS #

Ord i => Eq (EpsFold i) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

(==) :: EpsFold i -> EpsFold i -> Bool #

(/=) :: EpsFold i -> EpsFold i -> Bool #

Ord i => Ord (EpsFold i) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

compare :: EpsFold i -> EpsFold i -> Ordering #

(<) :: EpsFold i -> EpsFold i -> Bool #

(<=) :: EpsFold i -> EpsFold i -> Bool #

(>) :: EpsFold i -> EpsFold i -> Bool #

(>=) :: EpsFold i -> EpsFold i -> Bool #

max :: EpsFold i -> EpsFold i -> EpsFold i #

min :: EpsFold i -> EpsFold i -> EpsFold i #

eps :: i -> EpsFold i Source #

Creates the term \(\varepsilon(i)\)

mkEpsFold :: Ord i => [i] -> EpsFold i Source #

Constructs an epsfold from a list of elements

evalEps :: (Fractional r, Integral i, Integral j) => j -> r -> EpsFold i -> r Source #

Evaluate an eps-fold using the choice of eps from the lemma above, i.e. \(\varepsilon(i) = \delta^{d^i}\) >>> evalEps 2 (1/2) $ mkEpsFold [1] 0.25 >>> evalEps 2 (1/2) $ mkEpsFold [2] 6.25e-2 >>> evalEps 2 (1/2) $ mkEpsFold [1,2] 1.5625e-2

hasNoPertubation :: EpsFold i -> Bool Source #

Test if the epsfold has no pertubation at all (i.e. if it is \(\Pi_{\emptyset}\)

factors :: EpsFold i -> Bag i Source #

Gets the factors

suitableBase :: EpsFold i -> Int Source #

computes a base d that can be used as:

\( \varepsilon(i) = \varepsilon^{d^i} \)

data Term i r Source #

A term 'Term c es' represents a term:

\[ c \Pi_{i \in es} \varepsilon(i) \]

for a constant c and an arbitrarily small value \(\varepsilon\), parameterized by i.

Constructors

Term !r (EpsFold i) 

Instances

Instances details
Functor (Term i) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

fmap :: (a -> b) -> Term i a -> Term i b #

(<$) :: a -> Term i b -> Term i a #

(Show i, Show r) => Show (Term i r) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

showsPrec :: Int -> Term i r -> ShowS #

show :: Term i r -> String #

showList :: [Term i r] -> ShowS #

(Ord i, Eq r) => Eq (Term i r) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

(==) :: Term i r -> Term i r -> Bool #

(/=) :: Term i r -> Term i r -> Bool #

(Ord i, Ord r, Num r) => Ord (Term i r) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

compare :: Term i r -> Term i r -> Ordering #

(<) :: Term i r -> Term i r -> Bool #

(<=) :: Term i r -> Term i r -> Bool #

(>) :: Term i r -> Term i r -> Bool #

(>=) :: Term i r -> Term i r -> Bool #

max :: Term i r -> Term i r -> Term i r #

min :: Term i r -> Term i r -> Term i r #

term :: r -> i -> Term i r Source #

Creates a singleton term

constantFactor :: forall i r f. Functor f => (r -> f r) -> Term i r -> f (Term i r) Source #

Lens to access the constant c in the term.

data Symbolic i r Source #

Represents a Sum of terms, i.e. given a sequence of terms Term \(c_i\) \(I_i\) s where \(c_i\) is a constant and \(I_i\) is a set of indices, a Symbolic represents an expression of the form:

\[ \sum c_i \Pi_{j in I_i} \varepsilon(j) \]

The terms are represented in order of decreasing significance.

The main idea in this type is that, if symbolic values contains \(\varepsilon(i)\) terms we can always order them. That is, two Symbolic terms will be equal only if:

  • they contain *only* a constant term (that is equal)
  • they contain the exact same \(\varepsilon\)-fold.

Instances

Instances details
Functor (Symbolic i) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

fmap :: (a -> b) -> Symbolic i a -> Symbolic i b #

(<$) :: a -> Symbolic i b -> Symbolic i a #

(Ord i, Num r, Eq r) => Num (Symbolic i r) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

(+) :: Symbolic i r -> Symbolic i r -> Symbolic i r #

(-) :: Symbolic i r -> Symbolic i r -> Symbolic i r #

(*) :: Symbolic i r -> Symbolic i r -> Symbolic i r #

negate :: Symbolic i r -> Symbolic i r #

abs :: Symbolic i r -> Symbolic i r #

signum :: Symbolic i r -> Symbolic i r #

fromInteger :: Integer -> Symbolic i r #

(Show i, Show r) => Show (Symbolic i r) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

showsPrec :: Int -> Symbolic i r -> ShowS #

show :: Symbolic i r -> String #

showList :: [Symbolic i r] -> ShowS #

(Ord i, Eq r, Num r) => Eq (Symbolic i r) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

(==) :: Symbolic i r -> Symbolic i r -> Bool #

(/=) :: Symbolic i r -> Symbolic i r -> Bool #

(Ord i, Ord r, Num r) => Ord (Symbolic i r) Source # 
Instance details

Defined in HGeometry.Number.Real.Symbolic

Methods

compare :: Symbolic i r -> Symbolic i r -> Ordering #

(<) :: Symbolic i r -> Symbolic i r -> Bool #

(<=) :: Symbolic i r -> Symbolic i r -> Bool #

(>) :: Symbolic i r -> Symbolic i r -> Bool #

(>=) :: Symbolic i r -> Symbolic i r -> Bool #

max :: Symbolic i r -> Symbolic i r -> Symbolic i r #

min :: Symbolic i r -> Symbolic i r -> Symbolic i r #

constant :: Ord i => r -> Symbolic i r Source #

Creates a constant symbolic value

symbolic :: Ord i => r -> i -> Symbolic i r Source #

Creates a symbolic vlaue with a single indexed term. If you just need a constant (i.e. non-indexed), use constant

perturb :: (Num r, Ord i) => r -> i -> Symbolic i r Source #

given the value c and the index i, creates the perturbed value \(c + \varepsilon(i)\)

toTerms :: Symbolic i r -> [Term i r] Source #

Produces a list of terms, in decreasing order of significance

signOf :: (Num r, Eq r) => Symbolic i r -> Maybe Sign Source #

Computing the Sign of an expression. (Nothing represents zero)

roundToConstant :: Num r => Symbolic i r -> r Source #

Drops all the epsilon terms

type SoSRational i r = GRatio (Symbolic i r) Source #

Number type representing

sosRational :: (Ord i, Eq r, Num r) => Symbolic i r -> Symbolic i r -> SoSRational i r Source #

Helper to construct sosRationals