ConstantArrays.jl

Julia type for (nearly memoryless) arrays filled with a constant value.
Author JeffFessler
Popularity
0 Stars
Updated Last
11 Months Ago
Started In
July 2020

ConstantArrays.jl

Build Status Codecov.io

https://github.com/JeffFessler/ConstantArrays.jl

A Julia data type that is a subtype of AbstractArray where every element is the same constant.

The "constant" in the name has two meaning: every element of the array has the same constant value, and the array is immutable (setindex! is unsupported).

Caution: This Package No Longer Maintained!

This package is now a subset of the existing FillArrays.jl package (that I did not know about when I wrote it). My original version had a extra couple features but I contributed those to FillArrays. So now I will no longer maintain this package and I strongly recommend you use FillArrays instead.

Installation

At the Julia REPL run: using Pkg; Pkg.add("ConstantArrays").

Documentation

At the Julia REPL execute: using ConstantArrays, then type ?ConstantArray and press enter to get help.

Primary usage example:

x = ConstantArray(42, (5,7))

makes a "lazy" constant "array" functionally equivalent to fill(42, (5,7)) but essentially requires only the memory need to store a struct with the value 42 and the dimensions (5,7).

The motivating use of this type is for the "masks" used in tomographic image reconstruction that are often uniform but also often patient conforming. The default one-argument usage mask = ConstantArray((4,6)) uses true (i.e., one(Bool)) as the constant value for this purpose.

The idea here is somewhat analogous to the UniformScaling type (I) in the LinearAlgebra package. Arguably that I is non-essential because one could accomplish something similar using Diagonal(ones(N)) but I requires much less memory. Likewise, arguably ConstantArray is non-essential, but ConstantArray(true, (100,100,100)) uses about a million times less memory than trues(100,100,100).

A better analogy might be a sparse array, where only the nonzero values are stored to save memory. A ConstantArray needs only to store only a single value.

The most useful operations are probably x .* y and y[x], both of which are faster with a ConstantArray than with trues(dim).

Developed by Jeff Fessler at the University of Michigan, with some inspiration from ReadOnlyArrays.jl.