ImplicitArrays.jl

The ImplicitArrays.jl package provides multiple implicit array types for the Julia programming language. In many situations, the implicit array types can be used as drop-in replacements for regular Julia arrays, while carrying a smaller memory footprints as their explicit counterparts.

Block matrices

This section presents a block matrix type for equally-sized blocks (sharing a common element type). The BlockMatrix type can be used to encapsulate and mix different matrix blocks, e.g., regular matrices and implicit matrices at the same time.

ImplicitArrays.BlockMatrixType
BlockMatrix{T} <: AbstractArray{T, 2}

Implicit block matrix of equally-sized matrices with elements of type T.

Constructors

BlockMatrix(blocks)
BlockMatrix{T}(blocks)
BlockMatrix{T}(rows, cols[, blocks...])
BlockMatrix(rows, cols, first_block[, more_blocks...])

Create a new block matrix from the given matrix of matrices. Alternatively, create the block matrix from a sequence of matrices, given the number of rows and cols.

Examples

julia> A = [1 2; 3 4]; B = zeros(Int, 2, 2);

julia> C = [x * y for x in 2:3, y in 3:4];

julia> BlockMatrix(1, 4, A, B, B, C)
2×8 BlockMatrix{Int64}:
 1  2  0  0  0  0  6   8
 3  4  0  0  0  0  9  12
Note

This type is not suitable for high-performance purposes, as matrix blocks are stored in an abstract container to allow the composition of arbitrary matrix types. For more details, please refer to the performance tips in the official Julia documentation.

source

Fixed-value arrays

Fixed-value arrays "replicate" a single value across arbitrary array dimensions. The resulting arrays have a constant memory footprint (i.e., the represented value and the array dimensions) and can only be read from.

ImplicitArrays.FixedValueArrayType
FixedValueArray{T, N} <: AbstractArray{T, N}

Implicit and arbitrarily-sized array with a common value of type T for all elements.

Constructors

FixedValueArray(val, dims)
FixedValueArray(val, dims...)

Create a new matrix of the given dimensions dims, where each element is represented by the given value val.

Examples

julia> FixedValueArray(0, (2, 5))
2×5 FixedValueArray{Int64, 2}:
 0  0  0  0  0
 0  0  0  0  0

julia> FixedValueArray("(^_^) Hi!", 1, 2)
1×2 FixedValueArray{String, 2}:
 "(^_^) Hi!"  "(^_^) Hi!"

julia> FixedValueArray(FixedValueArray(5.0, (1, 2)), (1, 3))
1×3 FixedValueArray{FixedValueArray{Float64, 2}, 2}:
 [5.0 5.0]  [5.0 5.0]  [5.0 5.0]

julia> length(FixedValueArray(1, 1_000_000_000, 1_000_000_000))
1000000000000000000
source

Interaction matrices

An interaction matrix M of size n × m consist of a list of n elements rᵢ ∈ R (row elements), a list of m elements cⱼ ∈ C (column elements), and a interaction function f: R × C → T, such that each element Mᵢⱼ of the matrix can be computed as f(rᵢ, cⱼ).

Interaction functions

ImplicitArrays.ConstInteractionFunctionType
struct ConstInteractionFunction{R, C, T} <: InteractionFunction{R, C, T}

Interaction function R × C → T that ignores its arguments and returns a fixed value.

Constructor

ConstInteractionFunction{R, C, T}(val)

Create a constant interaction function that always returns val.

Examples

julia> f = ConstInteractionFunction{Float64, Float64, Int64}(42);

julia> f(1.0, 1.5)
42
source
ImplicitArrays.GenericInteractionFunctionType
GenericInteractionFunction{R, C, T} <: InteractionFunction{R, C, T}

Interaction function R × C → T acting as a type-safe wrapper for a given function.

Constructor

GenericInteractionFunction{R, C, T}(fun)

Create an interaction function from the given function fun.

Examples

julia> f = GenericInteractionFunction{Int64, Int64, Int64}(+); f(9, 3)
12

julia> g = GenericInteractionFunction{Int64, Int64, Float64}(/); g(9, 3)
3.0

julia> h = GenericInteractionFunction{String, Int64, String}(repeat); h("abc", 3)
"abcabcabc"
Note

The interaction function can only be evaluated if there is a method fun(::R, ::C) that returns a value of type T. Otherwise, a MethodError or TypeError is thrown.

source

Interaction matrices

ImplicitArrays.InteractionMatrixType
InteractionMatrix{T, R, C, F <: InteractionFunction{R, C, T}} <: AbstractArray{T, 2}

Interaction matrix with elements of type T, generated from row elements of type R, column elements of type C, and an interaction function F: R × C → T.

Constructors

InteractionMatrix(rowelems, colelems, interact)
InteractionMatrix{T}(rowelems, colelems, interact)

Create an interaction matrix for the given row and column element arrays as well as an interaction function.

InteractionMatrix(rowelems, colelems, val)

Create an interaction matrix for the given (yet unused) row and column element arrays and a constant interaction function always returning val. In this form, the interaction matrix acts as a replacement for a two-dimensional FixedValueArray.

Examples

julia> InteractionMatrix{Int64}([1, 2, 3], [10, 20, 30], +)
3×3 InteractionMatrix{Int64, Int64, Int64, GenericInteractionFunction{Int64, Int64, Int64}}:
 11  21  31
 12  22  32
 13  23  33

julia> InteractionMatrix([1, 2, 3], [10, 20, 30], 42)
3×3 InteractionMatrix{Int64, Int64, Int64, ConstInteractionFunction{Int64, Int64, Int64}}:
 42  42  42
 42  42  42
 42  42  42

julia> struct DivFun <: InteractionFunction{Int, Int, Float64} end

julia> (::DivFun)(x::Int, y::Int) = x / y

julia> InteractionMatrix([10, 20, 30], [5, 2, 1], DivFun())
3×3 InteractionMatrix{Float64, Int64, Int64, DivFun}:
 2.0   5.0  10.0
 4.0  10.0  20.0
 6.0  15.0  30.0
source

Row-projection arrays

Row-projection arrays represent views on existing vectors or matrices, where the projections are created from a sequence of valid row indices corresponding to the vector or matrix. Row indices can occur multiple times and at arbitrary positions in the sequence.

ImplicitArrays.RowProjectionVectorType
RowProjectionVector{T} <: AbstractArray{T, 1}

Implicit vector projecting a sequence of rows of a given vector with elements of type T.

Constructors

RowProjectionVector(base, rows)
RowProjectionVector(base, rows...)
RowProjectionVector{T}(base, rows)

Create a new row-projected vector from the given base vector and a list or sequence of rows corresponding to it. Rows can be used multiple times and in any order.

Examples

julia> v = [10, 20, 30, 40, 50];

julia> RowProjectionVector(v, [1, 3, 5])
3-element RowProjectionVector{Int64}:
 10
 30
 50

julia> RowProjectionVector(v, 2, 3, 2)
3-element RowProjectionVector{Int64}:
 20
 30
 20
Note

Changing an element through RowProjectionVector actually changes the element in the underlying array. The changes are still reflected in the RowProjectionVector. Thus, caution is advised when changing elements of rows that are contained multiple times in the projection, in order to avoid unwanted side effects.

source
ImplicitArrays.RowProjectionMatrixType
RowProjectionMatrix{T} <: AbstractArray{T, 2}

Implicit matrix projecting a sequence of rows of a given matrix with elements of type T.

Constructors

RowProjectionMatrix(base, rows)
RowProjectionMatrix(base, rows...)
RowProjectionMatrix{T}(base, rows)

Create a new row-projected matrix from the given base matrix and a list or sequence of rows corresponding to it. Rows can be used multiple times and in any order.

Examples

julia> M = [10 20 30; 40 50 60; 70 80 90];

julia> RowProjectionMatrix(M, [1, 3])
2×3 RowProjectionMatrix{Int64}:
 10  20  30
 70  80  90

julia> RowProjectionMatrix(M, 2, 3, 2)
3×3 RowProjectionMatrix{Int64}:
 40  50  60
 70  80  90
 40  50  60
Note

Changing an element through RowProjectionMatrix actually changes the element in the underlying array. The changes are still reflected in the RowProjectionMatrix. Thus, caution is advised when changing elements of rows that are contained multiple times in the projection, in order to avoid unwanted side effects.

source