Bases

Basic functionality

LinearCombinations.AbstractBasisType
AbstractBasis{T,N} <: AbstractArray{T,N}

The supertype of all types representing bases whose elements are of type T. Bases are needed for linear combinations of type DenseLinear.

All subtypes of AbstractBasis must implement the abstract arrays interface. For a given basis b, b[i] is the i-th basis element. Mapping from basis elements to indices is one via the toindex function. The parameter N == ndims(b) specifies how many indices are used to index elements of a basis b.

See also Basis, TensorBasis, toindex, Base.ndims.

source
LinearCombinations.BasisType
Basis{T,N} <: AbstractBasis{T,N}

Basis(iter)

Construct a Basis whose elements are the elements of the AbstractArray or iterator iter. Internally, basis elements are stored in the given AbstractArray or otherwise in the Array obtained from collect(iter).

See also AbstractBasis, TensorBasis.

Examples

julia> b = Basis(['a', 'b', 'x', 'y', 'z'])
Basis(['a', 'b', 'x', 'y', 'z'])

julia> length(b)
5

julia> i = toindex(b, 'x')
CartesianIndex(3,)

julia> b[i], b[3]
('x', 'x')

julia> length(Basis(Char[]))
0
source
LinearCombinations.TensorBasisType
TensorBasis{T,N} <: AbstractBasis{T,N}

TensorBasis(bases...)

Construct a TensorBasis out of the given bases. The elements of the TensorBasis are of type Tensor, where the i-th tensor component is from the i-th basis.

See also AbstractBasis, Basis.

Examples

julia> b1, b2 = Basis('a':'c'), Basis(["x", "y", "z"])
(Basis('a':1:'c'), Basis(["x", "y", "z"]))

julia> b3 = TensorBasis(b1, b2)
TensorBasis(Basis('a':1:'c'), Basis(["x", "y", "z"]))

julia> length(b3)
9

julia> toindex(b3, Tensor('b', "z"))
CartesianIndex(2, 3)

julia> b4 = TensorBasis(b3, b1)
TensorBasis(TensorBasis(Basis('a':1:'c'), Basis(["x", "y", "z"])), Basis('a':1:'c'))

julia> ndims(b4)
3

julia> x = first(b4)
(a⊗x)⊗a

julia> toindex(b4, x)
CartesianIndex(1, 1, 1)

julia> b0 = TensorBasis(); length(b0), Tensor() in b0
(1, true)
source
Base.getindexMethod
getindex(b::AbstractBasis{T,N}, ii::Vararg{Int,N}) -> T

b[ii...] -> T

Return the basis element indexed by the indices ii.

See also AbstractBasis, toindex, Base.CartesianIndex.

source

AbstractBasis interface

A new subtype of AbstractBasis must provide a method for toindex and satisfy the abstract arrays interface for read-only arrays. The latter means that size and getindex must be supported.

As an example, here is subtype of AbstractBasis that turns ranges into bases. The advantage over Basis is that no dictionary lookup is needed for toindex.

import Base: show, size, getindex
import LinearCombinations: toindex

struct RangeBasis{T,R<:AbstractRange{T}} <: AbstractBasis{T,1}
    range::R
end

show(io::IO, b::RangeBasis) = print(io, "RangeBasis(", b.range, ')')

size(b::RangeBasis) = size(b.range)

getindex(b::RangeBasis, i::Int) = b.range[i]

function toindex(b::RangeBasis, x)
    d, r = divrem(x-first(b.range), step(b.range))
    d += 1
    if r == 0 && firstindex(b.range) <= d <= lastindex(b.range)
        CartesianIndex(d)
    else
        error("$x is not an element of the basis $b")
    end
end