SpecialMatrices.jl icon indicating copy to clipboard operation
SpecialMatrices.jl copied to clipboard

Overload Base.map

Open nsajko opened this issue 2 years ago • 3 comments

I think it would make sense for some types, like Vandermonde and Companion, to overload Base.map. The goal is to make things like map(Float32, Vandermonde([0.5, 3])) isa Vandermonde{Float32} hold.

Any objections?

nsajko avatar Dec 26 '22 11:12 nsajko

Thanks for the suggestion. I have a couple questions. For A = Vandermonde(1:4), currently map(x -> x+1, A) returns a dense matrix, as it should. So does map(Float32, A) and I can see why you'd prefer it to return a Vandermonde{Float32}.

  • How would you determine which functions f for map(f, A) would be "Vandermonde preserving"?
  • Out of curiosity, what is the advantage of changing the type after construction, rather than simply using Vandermonde(Float32[0.5,3])) in the first place?

BTW, since you have some interest in this package, would you mind taking a look at #17 and offering a code review? I would like another pair of eyes on it before merging and no one has responded to my request.

JeffFessler avatar Dec 27 '22 15:12 JeffFessler

How would you determine which functions f for map(f, A) would be "Vandermonde preserving"?

We can't know anything about a Function, but the overload for Base.map could have a signature like this, to restrict the first argument only to some known-good types:

Base.map(::Type{T}, m::Vandermonde) where {T <: Number}

This would be similar to the Base.map overload for UnitRanges:

julia> map(Float64, 3:5)
3.0:1.0:5.0

julia> @which map(Float64, 3:5)
map(::Type{T}, r::UnitRange) where T<:Real in Base at abstractarray.jl:1176

what is the advantage of changing the type after construction

Just convenience.

would you mind taking a look at #17 and offering a code review

I don't actually know anything about Cauchy matrices, so I'm not sure of how much help I would be, sorry.

nsajko avatar Jan 01 '23 16:01 nsajko

restrict the first argument only to some known-good types

Ah, that seems fine then. Should be an easy PR. Please include a test for any type where you add this extension.

JeffFessler avatar Jan 01 '23 17:01 JeffFessler