Getting started
Installation
In a Julia environment:
using Pkg
Pkg.add("FastHartleyTransform")Julia 1.10 or later is required. The maintenance baseline is Julia 1.10.12 LTS. The Enzyme and Reactant APIs described here target AbstractFastHartleyTransforms 0.1.3 and FastHartleyTransform 0.1.2. Check the installed versions before using these optional extensions.
Forward and inverse transforms
CPU examples use dense Float32 or Float64 arrays. Nonmutating calls preserve their input:
using FastHartleyTransform
x = reshape(collect(1.0:12.0), 3, 4)
original = copy(x)
# The forward transform is unnormalized and leaves x unchanged.
y = fht(x)
@assert x == original
@assert ifht(y) ≈ x # The inverse includes the normalization factor.
size(y)(3, 4)A name ending in ! overwrites its input:
# Keep the original data because both calls overwrite their input.
scratch = copy(x)
@assert fht!(scratch) === scratch
@assert scratch ≈ y
ifht!(scratch)
@assert scratch ≈ xSpatial axes and batches
Without dims, all axes are transformed. Select only spatial axes when other axes represent channels or independent samples:
using FastHartleyTransform
# Layout: two spatial axes, one channel axis, and one batch axis.
x = reshape(sin.(1:120), 4, 5, 2, 3)
dims = (1, 2) # Transform each spatial slice independently.
y = fht(x, dims)
@assert size(y) == (4, 5, 2, 3)
@assert ifht(y, dims) ≈ x
@assert y[:, :, 1, 2] ≈ fht(x[:, :, 1, 2])
size(y)(4, 5, 2, 3)Here each of the six spatial slices is independent. The inverse scale is 1/(4*5), with no contribution from the channel and batch axes.
For repeated transforms, use Plans and buffers. For automatic differentiation, continue with Differentiation and compiled execution.