Fusion Arrays
Push-pull arrays with automatic fusion support.
Description
The fusion-arrays module implements push-pull array computing with automatic fusion. Unlike traditional array libraries that create intermediate arrays during computation, fusion arrays build expression trees and evaluate them index-by-index without materializing intermediate results.
This approach provides reduced allocation overhead for intermediate computations and blending of lazy expressions and materialized arrays.
Type System
Fusion arrays support multiple element types:
- 'f64 - Double precision floating point (default)
- 'f32 - Single precision floating point
- 's64 - 64-bit signed integer
- 's32 - 32-bit signed integer
- 'u32 - 32-bit unsigned integer
- 'generic - Any Scheme value (slower)
Type promotion follows standard rules: - Integers promote to floating point on division - Transcendental functions produce floating point - Operations promote to highest type in the expression
Core API
Array Creation
(make-array type size #!optional (fill-value 0))Create a new materialized array of specified type filled with fill-value.
(array-from-list lst #!optional target-type)Create array from list with automatic type inference.
(array-linspace start end n)Create linearly spaced array with n points from start to end.
(array-zeros n #!optional type)Create array of zeros.
(array-ones n #!optional type)Create array of ones.
(array-constant n value #!optional type)Create array filled with constant value.
(array-range n #!optional (start 0) (step 1) (type 'f64))Create range array.
(array-random-normal n #!optional (mean 0.0) (std 1.0) (seed #f))Generate normally distributed random array using Box-Muller transform.
(array-meshgrid x-arr y-arr)Create coordinate grids for 2D evaluation. Returns two arrays (XX, YY) in row-major order.
Array Properties
(array-size arr)Get number of elements in array (works for materialized, fusion expressions, and reductions).
(array-ref arr index)Get element at index (forces evaluation for fusion expressions).
(array->list arr)Convert array to list (forces full materialization).
Basic Operations
(compute! expr #!optional target-type)Force computation of fusion expression, returning materialized array. Optionally convert to target type.
(force! expr)Alias for compute!.
(convert-array-type arr target-type)Convert array to target type if needed.
Arithmetic Operations
All operations are lazy by default and create fusion expressions.
Element-wise binary operations. All operands must have same size.
Element-wise scalar operations.
Element-wise unary operations.
Comparison Operations
Element-wise comparison operations (return 1.0 for true, 0.0 for false).
Element-wise logical operations (0.0 = false, non-zero = true).
Element-wise min/max.
Mapping
(array-map func arr)Lazy map operation - applies function to each element. Result type inferred from sample evaluation.
Reductions
(array-reduce op arr #!optional params)Create lazy reduction operation. Supported operations: - 'sum - Sum of elements - 'mean - Mean of elements - 'min/'max - Min/max value - 'var - Variance (params = ddof) - 'std - Standard deviation (params = ddof) - 'fold - General fold (params = (func init))
(array-fold func init arr)Lazy fold operation.
Statistical Operations
Convenience functions for common statistical operations.
Lazy moving window operations.
Filtering and Selection
(array-slice arr start end #!optional step)Create lazy slice with step support.
Take/drop first n elements.
(array-filter predicate arr)Lazy filter operation (returns sparse representation).
(array-zip func . arrays)Combine multiple arrays element-wise with function.
Functional Helpers
(array-diff arr #!optional n)Discrete difference (n-th order).
(array-cumsum arr)Cumulative sum (materialized).
(array-select arr indices)Select elements at given indices.
(array-where predicate arr)Return indices where predicate is true.
(array-unique arr)Return unique elements (materialized).
(array-group-by key-func arr)Group elements by key function (materialized).
Boolean Operations
Predicate operations.
Signal Processing
(array-convolve signal kernel #!optional (mode 'valid))Lazy convolution operation. Modes: - 'valid - No zero padding - 'same - Output same size as input - 'full - Full convolution
(array-gaussian-filter arr sigma)Apply Gaussian filter using lazy convolution.
Cross-correlation and autocorrelation.
(array-fft arr)Forward FFT of real-valued array. Input size must be power of 2. Returns (values real-part imag-part).
(array-ifft real-arr imag-arr)Inverse FFT from separate real and imaginary parts. Returns (values real-part imag-part) scaled by 1/n.
(array-integrate arr dx)Numerical integration using trapezoidal rule.
Pipeline Operations
The data-last operations can be used in pipeline style with the data-first variants:
Pipeline Macro
(array-pipeline arr (op . args) rest ...)Macro for readable chaining of array operations.
(array-pipeline arr
(array-scale 2.0)
(array-sin)
(array-exp)
(array+ other-array))Fusion Analysis
(array-complexity expr)Calculate computational complexity of expression.
(fusion-plan expr)Show the fusion plan for an expression.
(pp-fusion expr)Pretty print fusion expression tree.
Memory Allocation Statistics
Performance monitoring functions.
Performance Notes
- Fusion arrays are lazy, that is operations build expression trees;
- Computation happens only when compute! or array-ref is called;
- No intermediate arrays are created during fusion;
- Use array-pipeline for more readable chained operations;
- For best performance, materialize early when reusing results.
Example
;; Create arrays
(define x (array-linspace 0 (* 2 3.14159) 1000))
(define noise (array-random-normal 1000 0.0 0.1))
;; Build fusion expression (no allocation)
(define signal
(array-pipeline x
(array-scale 10.0)
(array+ 5.0)
(array+ noise)
(array-sin)
(array+ (array-exp (array-scale x -0.1)))))
;; Force computation
(compute! signal)
;; Compute statistics
(array-mean signal)
(array-std signal)
;; Moving statistics
(array-moving-average signal 50)Author
Repository
https://github.com/iraikov/fusion-arrays
Version History
- 1.1
- Ported to CHICKEN 6
- 1.0
- Initial release
License
GPL-3