HMath.Tensor¶
Module index ยท Numerical conventions
Description
CPU tensors with immutable boxed backing arrays and shared, zero-copy
strided views. Logical indexing is zero based; strides and offsets count
elements. Scalars have shape [], one element; zero axes have no elements.
No implicit numeric promotion occurs: promoteTensor makes it explicit.
Garbage collection manages immutable backing storage and view lifetimes.
On this page¶
| API | Purpose |
|---|---|
| Tensor | Immutable boxed CPU tensor with signed element strides and shared view storage. |
| Layout | Tensor storage classification: canonical row major,column major,or general strided view. |
| RowMajor | Declaration and type contract below. |
| ColumnMajor | Declaration and type contract below. |
| Strided | Declaration and type contract below. |
| Device | Tensor execution/storage device; this release supports CPU. |
| CPU | Declaration and type contract below. |
| tensor | Given shape then flat row-major values, construct an immutable CPU tensor. |
| tensorColumnMajor | Given shape then values in column-major storage order, construct a column-major CPU tensor. |
| tensorValues | Return logical values in row-major coordinate order, honoring strides/offsets rather than exposing raw backing storage. |
| tensorShape | Return dimensions in axis order. |
| tensorStrides | Return signed element strides for each axis. |
| tensorOffset | Return backing-array offset in elements of the logical coordinate origin. |
| tensorDType | Return the Typeable element type name as a String. |
| tensorDevice | Return CPU, the supported device for this Haskell tensor implementation. |
| tensorLayout | Classify canonical row-major strides first, then column-major, otherwise Strided. |
| isContiguous | Test whether stored strides exactly match canonical row-major strides for this shape. |
| contiguous | Copy logical values into a new canonical row-major tensor of the same shape. |
| numel | Return logical element count (product of shape), including one for scalars and zero for any empty axis. |
| shapeSize | Validate nonnegative dimensions and return their product as Int. |
| inferBroadcast | Given two shapes, align trailing dimensions; equal sizes or a size-one dimension are compatible. |
| tensorZeros | Construct a row-major tensor of zeros with the supplied shape. |
| tensorOnes | Construct a row-major tensor of ones with the supplied shape. |
| tensorFull | Given shape then fill value, construct an immutable row-major tensor with every element equal to that value. |
| tensorArange | Given finite start, exclusive stop, and nonzero finite step, construct a rank-one arithmetic progression. |
| tensorLinspace | Given start, stop, then nonnegative count, return a rank-one evenly spaced sequence including both endpoints when count>=2. |
| tensorLogspace | Given base, starting exponent, ending exponent, then count, return base raised to tensorLinspace exponents. |
| tensorRandom | Explicit deterministic LCG seed. |
| indexTensor | Given tensor then complete zero-based coordinate list, return the element. |
| sliceTensor | Given axis,start,count,nonzero step,then tensor, return a zero-copy strided view. |
| fancyIndex | Given axis then index list then tensor, copy selected positions along that axis in index-list order, retaining duplicates. |
| booleanIndex | Given an identically shaped Bool mask then tensor, return a rank-one tensor of values whose mask is True, in logical row-major order. |
| gather | Alias of fancyIndex: given axis, zero-based index list, then tensor, copy those axis positions. |
| scatter | Given axis,index list,update tensor,then target, return a new target-shaped tensor with updates written to selected positions. |
| scatterAdd | Given axis,indices,updates,target, return a new target with updates added at selected coordinates. |
| reshapeTensor | Given new dimensions then tensor, preserve logical element order with unchanged count. |
| flattenTensor | Reshape to rank one with numel entries in logical row-major order. |
| squeeze | Remove every length-one axis as a shared view, preserving storage and logical values. |
| unsqueeze | Given insertion axis in [0,rank] then tensor, insert a length-one dimension as a shared view. |
| expand | Alias of broadcastTo: expand singleton dimensions to a requested compatible target shape without copying values. |
| permute | Given a permutation of all zero-based axes then tensor, return a zero-copy view with shape/strides reordered. |
| transposeTensor | Reverse every axis of a tensor as a shared view. |
| moveAxis | Given source axis,destination axis,then tensor, move one axis while preserving the order of others. |
| swapAxes | Given two existing axes then tensor, exchange their dimensions/strides in a shared view. |
| broadcastTo | Given target shape then tensor, return a shared zero-stride view where existing/trailing size-one axes can expand. |
| repeatTensor | Given axis,repetition count,then tensor, repeat each individual axis position consecutively. |
| tile | Given one nonnegative repetition count per existing axis then tensor, tile whole axis contents into repeated blocks. |
| concatenate | Given existing axis then nonempty tensor list, copy tensors end-to-end along that axis. |
| stack | Given insertion axis then nonempty list of equally shaped tensors, insert that axis and concatenate along it. |
| splitTensor | Given axis,nonnegative segment sizes,then tensor, return shared slices partitioning that axis exactly. |
| chunkTensor | Given axis,positive chunk count,then tensor, split into exactly that many near-equal shared slices; earlier chunks get the remainder. |
| padTensor | Given (before,after) nonnegative padding for every axis,fill value,then tensor, copy into a padded output. |
| roll | Given axis,signed shift,then tensor, cyclically shift elements along the axis, preserving shape. |
| flipTensor | Given axis then tensor, reverse that axis using a negative-stride shared slice. |
| rotate | Given two distinct axes,integer quarter-turn count,then tensor, repeatedly swap axes and flip the first. |
| sortTensor | Flatten logical values and return a rank-one tensor sorted ascending under Ord. |
| argsort | Flatten logical values and return a rank-one tensor of stable zero-based ascending-sort indices. |
| topk | Given k in [0,numel] then tensor, return rank-one (values,flat indices) for k largest values in descending order. |
| unique | Return sorted distinct logical values as a rank-one tensor. |
| nonzero | Return a count-by-rank tensor of zero-based coordinates of nonzero elements in row-major traversal order. |
| whereTensor | Given Bool mask,yes tensor,no tensor, broadcast all three shapes and select yes/no values elementwise. |
| maskedFill | Given broadcastable mask,fill value,then tensor, return a copied result with True positions replaced. |
| maskedSelect | Given mask then identically shaped tensor, return a rank-one copy of True-selected values in row-major order. |
| mapTensor | Apply scalar arithmetic, comparisons, logic, or elementary functions such as sin, exp, and log to each logical element. |
| zipTensor | Given binary element function then two tensors, broadcast compatible shapes and return a new contiguous result. |
| tensorAdd | Given two tensors, broadcast compatible shapes and add corresponding values, returning a new contiguous tensor. |
| tensorSubtract | Given two tensors, broadcast compatible shapes and subtract right values from left, returning a new contiguous tensor. |
| tensorMultiply | Given two tensors, broadcast compatible shapes and multiply corresponding values, returning a new contiguous tensor. |
| tensorDivide | Given two tensors, broadcast compatible shapes and divide left values by right, returning a new contiguous tensor. |
| tensorEqual | Given two tensors, broadcast compatible shapes and compare corresponding values for equality, returning a new contiguous tensor. |
| tensorLess | Given two tensors, broadcast compatible shapes and compare corresponding values with (<), returning a new contiguous tensor. |
| tensorAnd | Given two tensors, broadcast compatible shapes and combine Bool values with logical AND, returning a new contiguous tensor. |
| tensorOr | Given two tensors, broadcast compatible shapes and combine Bool values with logical OR, returning a new contiguous tensor. |
| tensorNot | Negate each Bool element, returning a new contiguous tensor of the same shape. |
| promoteTensor | Explicitly convert each Real element through exact Rational to the destination Fractional type, preserving shape. |
| sumAxes | Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by sum. |
| meanAxes | Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by arithmetic mean. |
| productAxes | Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by product. |
| minAxes | Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by minimum. |
| maxAxes | Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by maximum. |
| argmin | Return first zero-based flat row-major index of a minimum logical element. |
| argmax | Return first zero-based flat row-major index of a maximum logical element. |
| varianceAxes | Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by population variance (dividing by group size). |
| normAxes | Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by scaled Euclidean norm. |
| cumulativeSum | Given existing axis then tensor, return inclusive running sums along that axis, preserving shape. |
| cumulativeProduct | Given existing axis then tensor, return inclusive running products along that axis, preserving shape. |
| tensordot | Given contraction axes for the first tensor,axes for the second,then both tensors, sum products over matched axis pairs. |
| einsum | Explicit Einstein notation; each operand has one label per axis. |
| batchedMatmul | Multiply tensors on their final two dimensions and broadcast leading batch dimensions. |
| tensorKronecker | Return multidimensional Kronecker product, left-padding the lower-rank shape with ones and multiplying corresponding axis sizes. |
| serializeTensor | Serialize (shape,logical row-major values) using Haskell Show. |
| deserializeTensor | Read a complete Haskell (shape,values) encoding, permitting only trailing whitespace, and validate shapecount. |
| TensorGraph | Lazy expression trees defer computation. |
| inputGraph | Wrap an immutable tensor as a lazy TensorGraph leaf, retaining its backing storage without evaluating a new output array. |
| mapGraph | Given element function then graph, compose a lazy element transform, preserving shape. |
| zipGraph | Given binary function then two graphs, validate broadcast shapes and return a fused lazy graph. |
| evaluateGraph | Evaluate a lazy graph into one contiguous row-major tensor. |
Documentation
Tensor
Immutable boxed CPU tensor with signed element strides and shared view storage. Construct through tensor and checked view operations; garbage collection manages lifetime.
Layout
Tensor storage classification: canonical row major,column major,or general strided view.
Constructors
| RowMajor | ย |
| ColumnMajor | ย |
| Strided | ย |
Device
Tensor execution/storage device; this release supports CPU.
Constructors
| CPU | ย |
tensor
tensor :: [Int] -> [a] -> Either MathError (Tensor a) Source #
Given shape then flat row-major values, construct an immutable CPU tensor. Require exactly the shape product values; invalid dimensions/count return Left MathError. Generic values are not validated for finiteness.
tensorColumnMajor
tensorColumnMajor :: [Int] -> [a] -> Either MathError (Tensor a) Source #
Given shape then values in column-major storage order, construct a column-major CPU tensor. Validate shape/count as tensor; logical tensorValues still traverses row-major coordinates.
tensorValues
tensorValues :: Tensor a -> [a] Source #
Return logical values in row-major coordinate order, honoring strides/offsets rather than exposing raw backing storage.
tensorShape
tensorShape :: Tensor a -> [Int] Source #
Return dimensions in axis order. Scalar shape is []; empty axes are retained.
tensorStrides
tensorStrides :: Tensor a -> [Int] Source #
Return signed element strides for each axis. These describe shared views; they are not byte offsets and can be zero (broadcast) or negative (reverse slices).
tensorOffset
tensorOffset :: Tensor a -> Int Source #
Return backing-array offset in elements of the logical coordinate origin. Combine with tensorStrides for view indexing.
tensorDType
tensorDType :: Typeable a => Tensor a -> String Source #
Return the Typeable element type name as a String. This is diagnostic metadata, not an implicit conversion or stable external dtype code.
tensorDevice
tensorDevice :: Tensor a -> Device Source #
Return CPU, the supported device for this Haskell tensor implementation.
tensorLayout
tensorLayout :: Tensor a -> Layout Source #
Classify canonical row-major strides first, then column-major, otherwise Strided. Ambiguous single-axis layouts are classified RowMajor.
isContiguous
isContiguous :: Tensor a -> Bool Source #
Test whether stored strides exactly match canonical row-major strides for this shape. Offset does not prevent a contiguous view; column-major layouts generally return False.
contiguous
contiguous :: Tensor a -> Either MathError (Tensor a) Source #
Copy logical values into a new canonical row-major tensor of the same shape. Return checked construction result; existing views remain unchanged.
numel
numel :: Tensor a -> Int Source #
Return logical element count (product of shape), including one for scalars and zero for any empty axis.
shapeSize
shapeSize :: [Int] -> Either MathError Int Source #
Validate nonnegative dimensions and return their product as Int. Scalar [] has one element; a zero axis gives zero. Negative dimensions or an Int-overflowing product return Left InvalidArgument. Haskell tensors do not inherit the C ABI's smaller rank/element caps.
inferBroadcast
inferBroadcast :: [Int] -> [Int] -> Either MathError [Int] Source #
Given two shapes, align trailing dimensions; equal sizes or a size-one dimension are compatible. Return the broadcasted shape after nonnegative/Int-size validation; incompatible dimensions fail.
tensorZeros
tensorZeros :: Num a => [Int] -> Either MathError (Tensor a) Source #
Construct a row-major tensor of zeros with the supplied shape. Invalid dimensions or element-product overflow return Left MathError.
tensorOnes
tensorOnes :: Num a => [Int] -> Either MathError (Tensor a) Source #
Construct a row-major tensor of ones with the supplied shape. Scalars contain one 1; zero axes contain no values.
tensorFull
tensorFull :: [Int] -> a -> Either MathError (Tensor a) Source #
Given shape then fill value, construct an immutable row-major tensor with every element equal to that value. Invalid/overflowing shape fails.
tensorArange
tensorArange :: RealFloat a => a -> a -> a -> Either MathError (Tensor a) Source #
Given finite start, exclusive stop, and nonzero finite step, construct a rank-one arithmetic progression. A step pointing away from stop yields empty output. Reject invalid inputs or Int-size count overflow.
tensorLinspace
tensorLinspace :: Fractional a => a -> a -> Int -> Either MathError (Tensor a) Source #
Given start, stop, then nonnegative count, return a rank-one evenly spaced sequence including both endpoints when count>=2. Count zero gives empty, count one gives start. Negative count fails; generic arithmetic is unchecked.
tensorLogspace
tensorLogspace :: Floating a => a -> a -> a -> Int -> Either MathError (Tensor a) Source #
Given base, starting exponent, ending exponent, then count, return base raised to tensorLinspace exponents. Negative count fails; Floating domain/overflow behavior follows the element type.
tensorRandom
tensorRandom :: Integer -> [Int] -> Either MathError (Tensor Double, Integer) Source #
Explicit deterministic LCG seed. Values are uniform in [0,1); the returned seed continues the stream, and does not access global mutable generator state.
indexTensor
indexTensor :: Tensor a -> [Int] -> Either MathError a Source #
Given tensor then complete zero-based coordinate list, return the element. Wrong coordinate rank or out-of-range index returns Left InvalidArgument.
sliceTensor
sliceTensor :: Int -> Int -> Int -> Int -> Tensor a -> Either MathError (Tensor a) Source #
Given axis,start,count,nonzero step,then tensor, return a zero-copy strided view. Negative steps are supported with valid reachable coordinates. Invalid axis/extent, stride overflow or backing bounds return Left MathError.
fancyIndex
fancyIndex :: Int -> [Int] -> Tensor a -> Either MathError (Tensor a) Source #
Given axis then index list then tensor, copy selected positions along that axis in index-list order, retaining duplicates. Invalid axis/indices or output-size overflow fail.
booleanIndex
booleanIndex :: Tensor Bool -> Tensor a -> Either MathError (Tensor a) Source #
Given an identically shaped Bool mask then tensor, return a rank-one tensor of values whose mask is True, in logical row-major order. Mask mismatch fails; alias of maskedSelect.
gather
gather :: Int -> [Int] -> Tensor a -> Either MathError (Tensor a) Source #
Alias of fancyIndex: given axis, zero-based index list, then tensor, copy those axis positions. Invalid axis/indices fail; index-list order and duplicates are preserved.
scatter
scatter :: Int -> [Int] -> Tensor a -> Tensor a -> Either MathError (Tensor a) Source #
Given axis,index list,update tensor,then target, return a new target-shaped tensor with updates written to selected positions. Update shape replaces that axis with index-list length; invalid shape/indices fail. Repeated indices use the last update in traversal order.
scatterAdd
scatterAdd :: Num a => Int -> [Int] -> Tensor a -> Tensor a -> Either MathError (Tensor a) Source #
Given axis,indices,updates,target, return a new target with updates added at selected coordinates. Duplicate indices accumulate; update shape and indices follow scatter validation.
reshapeTensor
reshapeTensor :: [Int] -> Tensor a -> Either MathError (Tensor a) Source #
Given new dimensions then tensor, preserve logical element order with unchanged count. Contiguous input yields a shared view; non-contiguous input is copied. Invalid dimensions/count fail; no -1 inference.
flattenTensor
flattenTensor :: Tensor a -> Either MathError (Tensor a) Source #
Reshape to rank one with numel entries in logical row-major order. Shares contiguous storage and copies non-contiguous input; checked reshape errors propagate.
squeeze
squeeze :: Tensor a -> Either MathError (Tensor a) Source #
Remove every length-one axis as a shared view, preserving storage and logical values. A single-element all-one shape becomes scalar [].
unsqueeze
unsqueeze :: Int -> Tensor a -> Either MathError (Tensor a) Source #
Given insertion axis in [0,rank] then tensor, insert a length-one dimension as a shared view. Out-of-range axis returns Left InvalidArgument.
expand
expand :: [Int] -> Tensor a -> Either MathError (Tensor a) Source #
Alias of broadcastTo: expand singleton dimensions to a requested compatible target shape without copying values. Invalid broadcast shapes fail.
permute
permute :: [Int] -> Tensor a -> Either MathError (Tensor a) Source #
Given a permutation of all zero-based axes then tensor, return a zero-copy view with shape/strides reordered. Missing, duplicate or invalid axes fail.
transposeTensor
transposeTensor :: Tensor a -> Either MathError (Tensor a) Source #
Reverse every axis of a tensor as a shared view. For rank two this exchanges rows/columns; it is not restricted to matrices.
moveAxis
moveAxis :: Int -> Int -> Tensor a -> Either MathError (Tensor a) Source #
Given source axis,destination axis,then tensor, move one axis while preserving the order of others. Both axes must identify existing dimensions; invalid axes fail.
swapAxes
swapAxes :: Int -> Int -> Tensor a -> Either MathError (Tensor a) Source #
Given two existing axes then tensor, exchange their dimensions/strides in a shared view. Invalid axes return Left MathError.
broadcastTo
broadcastTo :: [Int] -> Tensor a -> Either MathError (Tensor a) Source #
Given target shape then tensor, return a shared zero-stride view where existing/trailing size-one axes can expand. Target must be exactly a valid broadcast destination; incompatible/overflowing shapes fail.
repeatTensor
repeatTensor :: Int -> Int -> Tensor a -> Either MathError (Tensor a) Source #
Given axis,repetition count,then tensor, repeat each individual axis position consecutively. Nonnegative count is required; zero yields an empty axis. Invalid axis/count or size overflow fails.
tile
tile :: [Int] -> Tensor a -> Either MathError (Tensor a) Source #
Given one nonnegative repetition count per existing axis then tensor, tile whole axis contents into repeated blocks. Rank mismatch, negative repetitions or resulting size overflow fail.
concatenate
concatenate :: Int -> [Tensor a] -> Either MathError (Tensor a) Source #
Given existing axis then nonempty tensor list, copy tensors end-to-end along that axis. All other dimensions must match; empty list, axis/shape mismatch or size overflow fails.
stack
stack :: Int -> [Tensor a] -> Either MathError (Tensor a) Source #
Given insertion axis then nonempty list of equally shaped tensors, insert that axis and concatenate along it. Invalid insertion axis or incompatible input shapes fail.
splitTensor
splitTensor :: Int -> [Int] -> Tensor a -> Either MathError [Tensor a] Source #
Given axis,nonnegative segment sizes,then tensor, return shared slices partitioning that axis exactly. Sizes must sum to the axis length; invalid axis/partition fails.
chunkTensor
chunkTensor :: Int -> Int -> Tensor a -> Either MathError [Tensor a] Source #
Given axis,positive chunk count,then tensor, split into exactly that many near-equal shared slices; earlier chunks get the remainder. Empty chunks are allowed if chunks exceed axis size. Invalid axis/count fails.
padTensor
padTensor :: [(Int, Int)] -> a -> Tensor a -> Either MathError (Tensor a) Source #
Given (before,after) nonnegative padding for every axis,fill value,then tensor, copy into a padded output. Rank mismatch, negative padding or output-size overflow fails.
roll
roll :: Int -> Int -> Tensor a -> Either MathError (Tensor a) Source #
Given axis,signed shift,then tensor, cyclically shift elements along the axis, preserving shape. Positive shifts move values toward larger indices. Invalid axis fails; the output is copied.
flipTensor
flipTensor :: Int -> Tensor a -> Either MathError (Tensor a) Source #
Given axis then tensor, reverse that axis using a negative-stride shared slice. Invalid axis returns Left MathError.
rotate
rotate :: Int -> Int -> Int -> Tensor a -> Either MathError (Tensor a) Source #
Given two distinct axes,integer quarter-turn count,then tensor, repeatedly swap axes and flip the first. Turns are reduced modulo four; invalid/equal axes fail.
sortTensor
sortTensor :: Ord a => Tensor a -> Either MathError (Tensor a) Source #
Flatten logical values and return a rank-one tensor sorted ascending under Ord. Original shape is not preserved.
argsort
argsort :: Ord a => Tensor a -> Either MathError (Tensor Int) Source #
Flatten logical values and return a rank-one tensor of stable zero-based ascending-sort indices. Ties preserve original order.
topk
topk :: Ord a => Int -> Tensor a -> Either MathError (Tensor a, Tensor Int) Source #
Given k in [0,numel] then tensor, return rank-one (values,flat indices) for k largest values in descending order. Equal values use reverse stable-sort order. Invalid k fails.
unique
unique :: Ord a => Tensor a -> Either MathError (Tensor a) Source #
Return sorted distinct logical values as a rank-one tensor. Input shape/order are not preserved.
nonzero
nonzero :: (Eq a, Num a) => Tensor a -> Either MathError (Tensor Int) Source #
Return a count-by-rank tensor of zero-based coordinates of nonzero elements in row-major traversal order. For a nonzero scalar the shape is [1,0].
whereTensor
whereTensor :: Tensor Bool -> Tensor a -> Tensor a -> Either MathError (Tensor a) Source #
Given Bool mask,yes tensor,no tensor, broadcast all three shapes and select yes/no values elementwise. Incompatible/overflowing broadcast shapes return Left MathError.
maskedFill
maskedFill :: Tensor Bool -> a -> Tensor a -> Either MathError (Tensor a) Source #
Given broadcastable mask,fill value,then tensor, return a copied result with True positions replaced. Incompatible mask broadcast or output construction fails.
maskedSelect
maskedSelect :: Tensor Bool -> Tensor a -> Either MathError (Tensor a) Source #
Given mask then identically shaped tensor, return a rank-one copy of True-selected values in row-major order. Shape mismatch returns Left InvalidArgument.
mapTensor
mapTensor :: (a -> b) -> Tensor a -> Tensor b Source #
Apply scalar arithmetic, comparisons, logic, or elementary functions such
as sin, exp, and log to each logical element. A contiguous output is
created; scalar numeric domain/overflow behavior follows the supplied function.
zipTensor
zipTensor :: (a -> b -> c) -> Tensor a -> Tensor b -> Either MathError (Tensor c) Source #
Given binary element function then two tensors, broadcast compatible shapes and return a new contiguous result. Shape/size failures return Left MathError; function evaluation/domain errors follow the supplied function.
tensorAdd
tensorAdd :: Num a => Tensor a -> Tensor a -> Either MathError (Tensor a) Source #
Given two tensors, broadcast compatible shapes and add corresponding values, returning a new contiguous tensor. Incompatible/overflowing broadcast shapes return Left MathError.
tensorSubtract
tensorSubtract :: Num a => Tensor a -> Tensor a -> Either MathError (Tensor a) Source #
Given two tensors, broadcast compatible shapes and subtract right values from left, returning a new contiguous tensor. Incompatible/overflowing broadcast shapes return Left MathError.
tensorMultiply
tensorMultiply :: Num a => Tensor a -> Tensor a -> Either MathError (Tensor a) Source #
Given two tensors, broadcast compatible shapes and multiply corresponding values, returning a new contiguous tensor. Incompatible/overflowing broadcast shapes return Left MathError.
tensorDivide
tensorDivide :: Fractional a => Tensor a -> Tensor a -> Either MathError (Tensor a) Source #
Given two tensors, broadcast compatible shapes and divide left values by right, returning a new contiguous tensor. Incompatible/overflowing broadcast shapes return Left MathError. Division-by-zero/non-finite behavior follows Fractional; it is not additionally checked.
tensorEqual
tensorEqual :: Eq a => Tensor a -> Tensor a -> Either MathError (Tensor Bool) Source #
Given two tensors, broadcast compatible shapes and compare corresponding values for equality, returning a new contiguous tensor. Incompatible/overflowing broadcast shapes return Left MathError.
tensorLess
tensorLess :: Ord a => Tensor a -> Tensor a -> Either MathError (Tensor Bool) Source #
Given two tensors, broadcast compatible shapes and compare corresponding values with (<), returning a new contiguous tensor. Incompatible/overflowing broadcast shapes return Left MathError.
tensorAnd
tensorAnd :: Tensor Bool -> Tensor Bool -> Either MathError (Tensor Bool) Source #
Given two tensors, broadcast compatible shapes and combine Bool values with logical AND, returning a new contiguous tensor. Incompatible/overflowing broadcast shapes return Left MathError.
tensorOr
tensorOr :: Tensor Bool -> Tensor Bool -> Either MathError (Tensor Bool) Source #
Given two tensors, broadcast compatible shapes and combine Bool values with logical OR, returning a new contiguous tensor. Incompatible/overflowing broadcast shapes return Left MathError.
tensorNot
tensorNot :: Tensor Bool -> Tensor Bool Source #
Negate each Bool element, returning a new contiguous tensor of the same shape. No broadcast or ownership mutation occurs.
promoteTensor
promoteTensor :: (Real a, Fractional b) => Tensor a -> Tensor b Source #
Explicitly convert each Real element through exact Rational to the destination Fractional type, preserving shape. Destination rounding/overflow follows its conversion; no implicit promotion occurs elsewhere.
sumAxes
sumAxes :: Num a => [Int] -> Bool -> Tensor a -> Either MathError (Tensor a) Source #
Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by sum. True retains reduced dimensions as size one; False removes them. Invalid/duplicate axes fail. Empty reduced groups use zero.
meanAxes
meanAxes :: Fractional a => [Int] -> Bool -> Tensor a -> Either MathError (Tensor a) Source #
Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by arithmetic mean. True retains reduced dimensions as size one; False removes them. Invalid/duplicate axes fail. Nonempty output with an empty reduced group fails.
productAxes
productAxes :: Num a => [Int] -> Bool -> Tensor a -> Either MathError (Tensor a) Source #
Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by product. True retains reduced dimensions as size one; False removes them. Invalid/duplicate axes fail. Empty reduced groups use one.
minAxes
minAxes :: Ord a => [Int] -> Bool -> Tensor a -> Either MathError (Tensor a) Source #
Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by minimum. True retains reduced dimensions as size one; False removes them. Invalid/duplicate axes fail. Empty reduced groups fail.
maxAxes
maxAxes :: Ord a => [Int] -> Bool -> Tensor a -> Either MathError (Tensor a) Source #
Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by maximum. True retains reduced dimensions as size one; False removes them. Invalid/duplicate axes fail. Empty reduced groups fail.
argmin
argmin :: Ord a => Tensor a -> Either MathError Int Source #
Return first zero-based flat row-major index of a minimum logical element. Empty input returns Left DomainError.
argmax
argmax :: Ord a => Tensor a -> Either MathError Int Source #
Return first zero-based flat row-major index of a maximum logical element. Empty input returns Left DomainError.
varianceAxes
varianceAxes :: Fractional a => [Int] -> Bool -> Tensor a -> Either MathError (Tensor a) Source #
Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by population variance (dividing by group size). True retains reduced dimensions as size one; False removes them. Invalid/duplicate axes fail. Empty reduced groups fail as meanAxes.
normAxes
normAxes :: RealFloat a => [Int] -> Bool -> Tensor a -> Either MathError (Tensor a) Source #
Given unique zero-based axis list,keep-dimensions Bool,then tensor, reduce those axes by scaled Euclidean norm. True retains reduced dimensions as size one; False removes them. Invalidduplicate axes fail. Empty groups have norm zero; IEEE NaNinfinity propagate.
cumulativeSum
cumulativeSum :: Num a => Int -> Tensor a -> Either MathError (Tensor a) Source #
Given existing axis then tensor, return inclusive running sums along that axis, preserving shape. Invalid axis fails; arithmetic follows Num.
cumulativeProduct
cumulativeProduct :: Num a => Int -> Tensor a -> Either MathError (Tensor a) Source #
Given existing axis then tensor, return inclusive running products along that axis, preserving shape. Invalid axis fails; arithmetic follows Num.
tensordot
tensordot :: Num a => [Int] -> [Int] -> Tensor a -> Tensor a -> Either MathError (Tensor a) Source #
Given contraction axes for the first tensor,axes for the second,then both tensors, sum products over matched axis pairs. Noncontracted left axes precede right axes in the output. Reject duplicate/out-of-range axes or mismatched contraction dimensions.
einsum
einsum :: Num a => [String] -> String -> [Tensor a] -> Either MathError (Tensor a) Source #
Explicit Einstein notation; each operand has one label per axis. Repeated labels select diagonals; absent output labels are summed. Ellipses are not accepted: callers express batch labels explicitly, preventing ambiguity.
batchedMatmul
batchedMatmul :: Num a => Tensor a -> Tensor a -> Either MathError (Tensor a) Source #
Multiply tensors on their final two dimensions and broadcast leading batch dimensions. Require rank>=2 and matching inner dimensions; incompatible broadcast or size overflow fails. Generic Num arithmetic is unchanged.
tensorKronecker
tensorKronecker :: Num a => Tensor a -> Tensor a -> Either MathError (Tensor a) Source #
Return multidimensional Kronecker product, left-padding the lower-rank shape with ones and multiplying corresponding axis sizes. Invalid/overflowing resulting dimensions fail.
serializeTensor
serializeTensor :: Show a => Tensor a -> String Source #
Serialize (shape,logical row-major values) using Haskell Show. Storage strides/offsets are not retained; the encoding requires a compatible Read instance for round-trip.
deserializeTensor
deserializeTensor :: Read a => String -> Either MathError (Tensor a) Source #
Read a complete Haskell (shape,values) encoding, permitting only trailing whitespace, and validate shapecount. Malformed input or invalid dimensionscount returns Left MathError.
TensorGraph
data TensorGraph a Source #
Lazy expression trees defer computation. Evaluation fuses adjacent maps and composes per-element expressions through broadcasted binary nodes; only the output array is allocated. Shape errors are checked before evaluation.
inputGraph
inputGraph :: Tensor a -> TensorGraph a Source #
Wrap an immutable tensor as a lazy TensorGraph leaf, retaining its backing storage without evaluating a new output array.
mapGraph
mapGraph :: (a -> b) -> TensorGraph a -> TensorGraph b Source #
Given element function then graph, compose a lazy element transform, preserving shape. No output tensor is allocated until evaluateGraph.
zipGraph
zipGraph :: (a -> b -> c) -> TensorGraph a -> TensorGraph b -> Either MathError (TensorGraph c) Source #
Given binary function then two graphs, validate broadcast shapes and return a fused lazy graph. Incompatible shapes return Left MathError before evaluation.
evaluateGraph
evaluateGraph :: TensorGraph a -> Either MathError (Tensor a) Source #
Evaluate a lazy graph into one contiguous row-major tensor. Adjacent map/zip operations are fused; scalar evaluation behavior follows supplied functions and checked output shape errors propagate.