|
| 1 | +## Option 1 |
| 2 | + |
| 3 | + |
| 4 | +abstract type BasisFunction <: Function end |
| 5 | +struct LegendreP <: BasisFunction |
| 6 | + n::Int |
| 7 | +end |
| 8 | +Base.transpose(x::Legendre) = x |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +B = transpose(LegendreP.(1:∞)) |
| 13 | +v = Vcat([1,2,3], Zeros(∞)) |
| 14 | + |
| 15 | +f = Mul(B, v) |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +## Option 2 |
| 20 | +using LazyArrays, InfiniteArrays, LinearAlgebra |
| 21 | +import Base: size, getindex, show, +, *, -, convert, copyto! |
| 22 | +import LinearAlgebra: adjoint, SymTridiagonal |
| 23 | +import InfiniteArrays: Infinity |
| 24 | + |
| 25 | +abstract type QuasiMatrix <: AbstractMatrix{Any} end |
| 26 | + |
| 27 | +const Vec{QM<:QuasiMatrix} = Mul{<:Any,<:Any,<:Any,QM} |
| 28 | + |
| 29 | +Base.IndexStyle(::QuasiMatrix) = Base.IndexLinear() |
| 30 | + |
| 31 | +for op in (:+, :-) |
| 32 | + @eval $op(f::Vec{BASIS}, g::Vec{BASIS}) where BASIS <: QuasiMatrix = |
| 33 | + Mul(f.A, $op(f.B , g.B)) |
| 34 | +end |
| 35 | + |
| 36 | +*(c::Number, f::Vec) = Mul(f.A, c*f.B) |
| 37 | + |
| 38 | +adjoint(f::Vec) = Adjoint(f) |
| 39 | + |
| 40 | +show(io::IO, f::Adjoint{<:Any,<:Vec}) = print(io, "Bra") |
| 41 | +show(io::IO, ::MIME"text/plain", f::Adjoint{<:Any,<:Vec}) = print(io, "Bra $(typeof(f))") |
| 42 | + |
| 43 | +show(io::IO, B::QuasiMatrix) = print(io, string(typeof(B))) |
| 44 | +show(io::IO, ::MIME"text/plain", B::QuasiMatrix) = print(io, string(typeof(B))) |
| 45 | + |
| 46 | +pad(v, n::Infinity) = Vcat(v, Zeros(n-length(v))) |
| 47 | +pad(v, n) = vcat(v, Zeros(n-length(v))) |
| 48 | + |
| 49 | +getindex(B::QuasiMatrix, k) = Mul(B, pad([Zeros(k-1); 1], size(B,2))) |
| 50 | + |
| 51 | +struct DiracDelta |
| 52 | + x::Float64 |
| 53 | +end |
| 54 | + |
| 55 | +*(δ::DiracDelta, f::Vec) = (δ*f.A) * f.B |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +struct Legendre <: QuasiMatrix end |
| 60 | + |
| 61 | +size(::Legendre) = (1,∞) |
| 62 | + |
| 63 | + |
| 64 | +struct LinearSpline <: QuasiMatrix |
| 65 | + points::Vector{Float64} |
| 66 | +end |
| 67 | + |
| 68 | +size(B::LinearSpline) = (1,length(B.points)) |
| 69 | + |
| 70 | + |
| 71 | +function convert(::Type{SymTridiagonal}, AB::Mul{<:Any,<:Any,<:Any, <:Adjoint{<:Any,LinearSpline},<:LinearSpline}) |
| 72 | + Ac,B = AB.A, AB.B |
| 73 | + A = parent(Ac) |
| 74 | + @assert A.points == B.points |
| 75 | + x = A.points |
| 76 | + SymTridiagonal(x, x/2) # TODO fix |
| 77 | +end |
| 78 | + |
| 79 | +SymTridiagonal(AB::Mul) = convert(SymTridiagonal, AB) |
| 80 | + |
| 81 | + |
| 82 | +copyto!(C::AbstractMatrix, AB::Mul{<:Any,<:Any,<:Any, <:Adjoint{<:Any,LinearSpline},<:LinearSpline}) = |
| 83 | + copyto!(C, SymTridiagonal(AB)) |
| 84 | + |
| 85 | +*(Ac::Adjoint{<:Any,LinearSpline}, B::LinearSpline) = SymTridiagonal(Mul(Ac, B)) |
| 86 | + |
| 87 | +function *(δ::DiracDelta, B::LinearSpline) |
| 88 | + x = δ.x |
| 89 | + @assert B.points[1] ≤ B.points[2] |
| 90 | + [(B.points[2]-B.points[1])*(x-B.points[1]);Zeros(size(B,2)-1)]' |
| 91 | +end |
| 92 | + |
| 93 | +(f::Vec{LinearSpline})(x) = DiracDelta(x) * f |
| 94 | + |
| 95 | + |
| 96 | +SymTridiagonal(Mul(B', B)) |
| 97 | + |
| 98 | + |
| 99 | +C = Array{Float64}(undef, 3, 3) |
| 100 | + |
| 101 | +B = LinearSpline([1,2,3]) |
| 102 | +size(B) |
| 103 | + |
| 104 | + |
| 105 | +copyto!(C, Mul(B', B)) |
| 106 | + |
| 107 | + |
| 108 | +C .= Mul(B', B) |
| 109 | + |
| 110 | + |
| 111 | +size(Mul(B', B)) |
| 112 | + |
| 113 | + |
| 114 | +A = randn(5,5) |
| 115 | +B = randn(5,5) |
| 116 | +C = similar(A) |
| 117 | + |
| 118 | +C .= 2.0 .* Mul(A,B) .+ 3.0 .* C |
| 119 | +δ = DiracDelta(0) |
| 120 | + |
| 121 | +δ*f |
| 122 | +f(0.5) |
| 123 | + |
| 124 | +f = Mul(B, [1,2,3]) |
| 125 | + |
| 126 | +1B[1] + 2B[2] + 3B[3] - f |
| 127 | + |
| 128 | + |
| 129 | +DiracDelta(2) * B |
| 130 | + |
| 131 | +DiracDelta(2) * f |
| 132 | + |
| 133 | + |
| 134 | +B = Legendre() |
| 135 | +Bt = B'; (B[1]') |
| 136 | + |
| 137 | +B' * B |
| 138 | + |
| 139 | +Eye(∞) |
| 140 | + |
| 141 | + |
| 142 | +B = LinearSpline([1,2,3]) |
| 143 | + |
| 144 | +B'*B |
| 145 | + |
| 146 | +typeof(B') |
| 147 | +typeof(B) |
| 148 | + |
| 149 | + |
| 150 | +factorize(B) |
0 commit comments