Skip to content

Overload Base.map #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nsajko opened this issue Dec 26, 2022 · 3 comments
Open

Overload Base.map #74

nsajko opened this issue Dec 26, 2022 · 3 comments

Comments

@nsajko
Copy link

nsajko commented Dec 26, 2022

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?

@JeffFessler
Copy link
Member

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.

@nsajko
Copy link
Author

nsajko commented Jan 1, 2023

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.

@JeffFessler
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants