diff --git a/lib/CanonicalMoments/ext/SymbolicsExt.jl b/lib/CanonicalMoments/ext/SymbolicsExt.jl index 8949d76..451b742 100644 --- a/lib/CanonicalMoments/ext/SymbolicsExt.jl +++ b/lib/CanonicalMoments/ext/SymbolicsExt.jl @@ -1,8 +1,6 @@ module SymbolicsExt using Symbolics -import Symbolics: unwrap, wrap, array_term using CanonicalMoments -using LinearAlgebra @register_array_symbolic (alg::CanonicalMoments.EigvalSupportAlg)(M::AbstractMatrix{<:Real}) begin size = (size(M)[1],) @@ -14,26 +12,24 @@ end eltype = eltype(M) end +# `AbstractVecOrMat` is a `Union` alias, which `@register_array_symbolic` cannot +# introspect; `AbstractArray` covers both the vector and matrix cases. @register_array_symbolic (alg::CanonicalMoments.EigvecWeightAlg)( M::AbstractMatrix{<:Real}, - λ::AbstractVecOrMat{<:Real}, + λ::AbstractArray{<:Real}, ) begin size = size(M) eltype = eltype(M) end -function CanonicalMoments._companion_matrix(B::Vector{Num}, C::Vector{Num}) - dv = -B - ndims = length(B) - ev = sqrt.(C[2:end]) # Probably use views here - _comp_mat_unwrapped = array_term( - SymTridiagonal, - unwrap.(dv), - unwrap.(ev); - eltype = Real, - size = (ndims, ndims), - ) - return wrap(_comp_mat_unwrapped) +# Registering the companion matrix constructor keeps it lazy for symbolic inputs +# while deferring to the numeric implementation otherwise. +@register_array_symbolic CanonicalMoments._companion_matrix( + B::AbstractVector{<:Real}, + C::AbstractVector{<:Real}, +) begin + size = (length(B), length(B)) + eltype = Real end end