tile
Safe HaskellNone
LanguageGHC2024

Tile.Affine

Description

An AffineRankSpace maps logical coordinates to integer ranks via an offset and per-dimension strides. The type is general: any combination of offset, sizes, and strides is representable.

Functions divide into two classes:

Unconditional
rankOf and rankOfMaybe compute offset + sum (zipWith (*) coord strides) for any strides.
Row-major invariant required
pointOf and pointOfMaybe recover coordinates by mixed-radix division and require strides[k] = product(sizes[k+1..]) at every level. rowMajor establishes this invariant; select and fixDim preserve it.
Synopsis

Affine rank spaces

data AffineRankSpace Source #

An affine map from logical coordinates to ranks.

For a coordinate coord, the rank is:

offset + sum (zipWith (*) coord strides)

Constructors

AffineRankSpace 

Fields

  • offset :: Int

    Rank of the origin coordinate.

  • sizes :: [Int]

    Extents, or shape, of the affine rank space.

  • strides :: [Int]

    Rank stride for each logical dimension.

Instances

Instances details
Eq AffineRankSpace Source # 
Instance details

Defined in Tile.Affine

Show AffineRankSpace Source # 
Instance details

Defined in Tile.Affine

type Point = [Int] Source #

A logical coordinate in an affine rank space.

rowMajor :: Shape -> AffineRankSpace Source #

Construct the row-major affine rank space for a shape.

Strides satisfy strides[k] = product(sizes[k+1..]), which is the row-major invariant required by pointOf and pointOfMaybe. select and fixDim preserve this invariant.

Coordinate/rank conversion

rankOf :: AffineRankSpace -> Point -> Int Source #

Convert a coordinate to a rank.

Unconditional: works for any AffineRankSpace. Throws an error if the coordinate has the wrong dimension or is out of bounds. Use rankOfMaybe for a total variant.

rankOfMaybe :: AffineRankSpace -> Point -> Maybe Int Source #

Convert a coordinate to a rank, returning Nothing for invalid coordinates.

Unconditional: works for any AffineRankSpace.

pointOf :: AffineRankSpace -> Int -> Point Source #

Convert a rank to a coordinate.

Precondition: strides must satisfy the row-major invariant (strides[k] = sizes[k+1] * strides[k+1]). All spaces produced by rowMajor, select, and fixDim satisfy this. A hand-constructed AffineRankSpace with arbitrary strides may produce wrong coordinates without error.

Throws an error if the rank is outside the affine rank space. Use pointOfMaybe for a total variant.

pointOfMaybe :: AffineRankSpace -> Int -> Maybe Point Source #

Convert a rank to a coordinate, returning Nothing for ranks outside the affine rank space.

Precondition: strides must satisfy the row-major invariant (strides[k] = sizes[k+1] * strides[k+1]). See pointOf.

Queries

spaceExtent :: AffineRankSpace -> Int Source #

Number of logical points in an affine rank space.

points :: Shape -> [Point] Source #

Enumerate all logical coordinates for a shape.

Coordinates are scanned in row-major order: the last dimension varies fastest. This is the coordinate grid used by ranks.

ranks :: AffineRankSpace -> [Int] Source #

Enumerate all ranks in logical coordinate order.

Unconditional: works for any AffineRankSpace. Ranks are ordered by the row-major scan of the coordinate grid via points.

Slicing

select :: AffineRankSpace -> Int -> Int -> Int -> Int -> Maybe AffineRankSpace Source #

Select a strided interval along one dimension.

The selected dimension remains present with a reduced extent. Preserves the row-major stride invariant, so the result is safe to pass to pointOf. Returns Nothing for an invalid dimension, interval, or step.

fixDim :: AffineRankSpace -> Int -> Int -> Maybe AffineRankSpace Source #

Select one index along a dimension.

The fixed dimension remains present with extent 1. Preserves the row-major stride invariant via select.