Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Two-dimensional lookup tables.
Synopsis
- data LookupTable2 a
- data Grid = Grid {}
- createLookupTable2 :: Grid -> (Vec2 -> a) -> LookupTable2 a
- lookupNearest :: LookupTable2 Double -> Vec2 -> Double
- lookupBilinear :: LookupTable2 Double -> Vec2 -> Double
- forLookupTable2_ :: Monad f => LookupTable2 a -> (a -> Vec2 -> IVec2 -> f b) -> f ()
- data IVec2 = IVec2 !Int !Int
- data CIVec2 = CIVec2 !Double !Double
- roundCIVec2 :: CIVec2 -> IVec2
- fromGrid :: Grid -> IVec2 -> Vec2
- toGrid :: Grid -> Vec2 -> CIVec2
- valueTable :: Grid -> (Vec2 -> a) -> Vector (Vector a)
Function cache
data LookupTable2 a Source #
Lookup table for a two-dimensional function. Created with lookupTable2
.
Instances
Show a => Show (LookupTable2 a) Source # | |
Defined in Geometry.LookupTable.Lookup2 showsPrec :: Int -> LookupTable2 a -> ShowS # show :: LookupTable2 a -> String # showList :: [LookupTable2 a] -> ShowS # | |
NFData a => NFData (LookupTable2 a) Source # | |
Defined in Geometry.LookupTable.Lookup2 rnf :: LookupTable2 a -> () # | |
Eq a => Eq (LookupTable2 a) Source # | |
Defined in Geometry.LookupTable.Lookup2 (==) :: LookupTable2 a -> LookupTable2 a -> Bool # (/=) :: LookupTable2 a -> LookupTable2 a -> Bool # | |
Ord a => Ord (LookupTable2 a) Source # | |
Defined in Geometry.LookupTable.Lookup2 compare :: LookupTable2 a -> LookupTable2 a -> Ordering # (<) :: LookupTable2 a -> LookupTable2 a -> Bool # (<=) :: LookupTable2 a -> LookupTable2 a -> Bool # (>) :: LookupTable2 a -> LookupTable2 a -> Bool # (>=) :: LookupTable2 a -> LookupTable2 a -> Bool # max :: LookupTable2 a -> LookupTable2 a -> LookupTable2 a # min :: LookupTable2 a -> LookupTable2 a -> LookupTable2 a # |
Specification of a discrete grid, used for sampling contour lines.
Subdivide the unit square with 50 squares (51 steps!) in x direction, and 30 (31 steps!) in y direction:
Grid
(Vec2
0 0,Vec2
1 1) (50, 30)
createLookupTable2 :: Grid -> (Vec2 -> a) -> LookupTable2 a Source #
Build a 2D lookup table, suitable for caching function calls. Values are initialized lazily, so that only repeated computations are sped up.
Example: lookup table for \(f(x,y) = x\cdot y\)
grid =Grid
(Vec2
(-10) (-10),Vec2
10 10), (100, 100) f (Vec2
x y) = x*y table =createLookupTable2
grid f
lookupNearest :: LookupTable2 Double -> Vec2 -> Double Source #
Nearest neigbour lookup in a two-dimensional lookup table. Lookup outside of the lookup table’s domain is clamped to the table’s edges.
Compared to lookupBilinear
this function works on types that don’t support
arithmetic on them, and is faster. The downside is of course that only the
values on the grid points are accessible, without any interpolation.
lookupBilinear :: LookupTable2 Double -> Vec2 -> Double Source #
Bilinear lookup in a two-dimensional lookup table. Lookup outside of the lookup table’s domain is clamped to the table’s edges, so while it will not make the program crash, the values are not useful.
This lookup approximates a function in the sense that
lookupBilinear
(createLookupTable2
grid f) x ≈ f x
forLookupTable2_ :: Monad f => LookupTable2 a -> (a -> Vec2 -> IVec2 -> f b) -> f () Source #
Perform an action for each entry in the lookup table. Can be handy for plotting its contents.
grid =Grid
(Vec2
0 0,Vec2
100, 100) (100, 100) f (Vec2
x y) = x +sin
y table =createLookupTable2
grid fforLookupTable2_
table $ val pos _ ->moveToVec
posshowTextAligned
HCenter
VCenter
(show
val)
Technical utilities
Discrete Vec2
. Useful as coordinate in a
.Vector
(Vector
a)
Continuous version of IVec2
. Type-wise the same as Vec2
, but it shows
fractional grid coodrdinates, so we can express the fact that our lookup might
be »between i and i+1« and we can interpolate.
roundCIVec2 :: CIVec2 -> IVec2 Source #
Round a »continuous integral« coordinate to a »proper integral« coordinate.
Map a coordinate from the discrete grid to continuous space.
:: Grid | |
-> Vec2 | Continuous coordinate |
-> CIVec2 | Continuous coordinate, scaled and clamped to grid dimensions.
Suitable to be rounded to an |
valueTable :: Grid -> (Vec2 -> a) -> Vector (Vector a) Source #
A raw value table, filled (lazily) by a function applied to the underlying
Grid
.
We first index by i
and then j
, so that vec!i!j
has the intuitive meaning
of »go in i
x
direction and then in j
y
. The drawback is that this makes
the table look like downward columns of y
values, indexed by x
. The more
common picture for at least me is to have line numbers and then rows in each
line.