Demonstration of Operator Algebras and Kron

Let M, D, F be matrix-based, diagonal-matrix-based, and function-based SciMLOperators respectively. Here are some examples of composing operators in order to build more complex objects and using their operations.

using SciMLOperators, LinearAlgebra
N = 4
function f(v, u, p, t)
    u .* v
end
function f(w, v, u, p, t)
    w .= u .* v
end

u = rand(4)
p = nothing # parameter struct
t = 0.0     # time

M = MatrixOperator(rand(N, N))
D = DiagonalOperator(rand(N))
F = FunctionOperator(f, zeros(N), zeros(N); u, p, t)
FunctionOperator(4 × 4)

Then, the following codes just work.

L1 = 2M + 3F + LinearAlgebra.I + rand(N, N)
L2 = D * F * M'
L3 = kron(M, D, F)
L4 = lu(M) \ D
L5 = [M; D]' * [M F; F D] * [F; D]
((((MatrixOperator(4 × 4) * MatrixOperator(4 × 4)) + (DiagonalOperator(4 × 4) * FunctionOperator(4 × 4))) * FunctionOperator(4 × 4)) + (((MatrixOperator(4 × 4) * FunctionOperator(4 × 4)) + (DiagonalOperator(4 × 4) * DiagonalOperator(4 × 4))) * DiagonalOperator(4 × 4)))

Each L# can be applied to AbstractVectors of appropriate sizes:

v = rand(N)
w = L1(v, u, p, t) # == L1 * v

v_kron = rand(N^3)
w_kron = L3(v_kron, u, p, t) # == L3 * v_kron
64-element reshape(transpose(::Matrix{Float64}), 64) with eltype Float64:
 0.00240501976623395
 0.0018647713090822098
 0.00033381412175475465
 0.001998600329018631
 0.6590191379674176
 0.7388864111714507
 0.08540489046779746
 0.39410458989049385
 0.15518825934407332
 0.14271241464156237
 ⋮
 0.23278854419542924
 0.09086170040203434
 0.07610199272132212
 0.02054024106931269
 0.12170263340014809
 0.05908452408881985
 0.025243294168007722
 0.00942711507803353
 0.08065092790331628

For mutating operator evaluations, call cache_operator to generate an in-place cache, so the operation is nonallocating.

α, β = rand(2)

# allocate cache
L2 = cache_operator(L2, u)
L4 = cache_operator(L4, u)

# allocation-free evaluation
L2(w, v, u, p, t) # == mul!(w, L2, v)
L4(w, v, u, p, t, α, β) # == mul!(w, L4, v, α, β)
4-element Vector{Float64}:
   1.1903271336942034
 -15.134920431116203
  -1.1987182836732002
  15.911933495743899