brush-strokes-0.1.0.0: src/lib/Math/Root/Isolation/Utils.hs
{-# LANGUAGE ScopedTypeVariables #-}
-- | Utilities for root isolation
module Math.Root.Isolation.Utils
( fromComponents, matMulVec, boxMidpoint )
where
-- base
import Prelude hiding ( unzip )
import Data.Foldable
( toList )
import Data.Functor
( unzip )
-- brush-strokes
import Math.Interval
import Math.Linear
--------------------------------------------------------------------------------
fromComponents
:: forall n
. ( Representable π ( πβ n ), n ~ RepDim ( πβ n ) )
=> ( Fin n -> [ ( π, Bool ) ] ) -> [ ( πβ n, Vec n Bool ) ]
fromComponents f = do
( xs, bs ) <- unzip <$> traverse f ( universe @n )
return $ ( tabulate $ \ i -> xs ! i, bs )
-- TODO: this could be more efficient.
{-# INLINEABLE fromComponents #-}
-- | The midpoint of a box.
boxMidpoint :: ( Representable π ( πβ n ), Representable Double ( β n ) ) => πβ n -> β n
boxMidpoint box =
tabulate $ \ i -> midpoint ( box `index` i )
{-# INLINEABLE boxMidpoint #-}
-- | Matrix multiplication \( A v \).
matMulVec
:: forall n m
. ( Representable π ( πβ n ), Representable π ( πβ m )
, Representable Double ( β n ), Representable Double ( β m )
)
=> Vec m ( β n ) -- ^ columns of the matrix \( A )
-> πβ m -- ^ vector \( v \)
-> πβ n
matMulVec as v = tabulate $ \ r ->
sum [ scaleInterval ( a `index` r ) ( index v c )
| ( c, a ) <- toList ( (,) <$> universe @m <*> as )
]
{-# INLINEABLE matMulVec #-}