-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Rewrite spdiagm with pairs #23757
Rewrite spdiagm with pairs #23757
Conversation
Actually, the single vector case could better be written as |
2be114e
to
6e0d456
Compare
Here are some datapoints that support the changes here, specifically the removal of the option to supply
|
base/deprecated.jl
Outdated
@deprecate spdiagm(x::AbstractVector) sparse(Diagonal(x)) | ||
@deprecate spdiagm(x::AbstractVector, d::Number) spdiagm(x => d) | ||
@deprecate spdiagm(x, d) spdiagm((x[i] => d[i] for i in 1:length(x))...) | ||
@deprecate spdiagm(x, d, m::Integer, n::Integer) spdiagm((x[i] => d[i] for i in 1:length(x))...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, the latter now always yields a square matrix whereas the former yields an m
-by-n
matrix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea. I should rewrite these deprecations such that this change won't be breaking.
i += numel | ||
end | ||
|
||
return (I,J,V) | ||
return I, J, V | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The former descriptive variable names made this code a bit easier to follow :).
base/sparse/sparsematrix.jl
Outdated
one diagonal, `B` can be a vector (instead of a tuple) and `d` can be the diagonal position | ||
(instead of a tuple), defaulting to 0 (diagonal). Optionally, `m` and `n` specify the size | ||
of the resulting sparse matrix. | ||
Construct a sparse diagonal matrix from pairs of vectors and diagonals. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"pairs" -> "Pair
s"?
Off topic, I also wish there was a better way to name this and similar functions. It is really annoying to have the |
The obvious solution would be something like |
|
6e0d456
to
3b107ae
Compare
base/deprecated.jl
Outdated
@deprecate spdiagm(x::AbstractVector) sparse(Diagonal(x)) | ||
function spdiagm(x::AbstractVector, d::Number) | ||
depwarn(string("spdiagm(x::AbstractVector, d::Number) is deprecated, use ", | ||
"spdiagm(x => d) instead, which now return a square matrix. To preserve the old ", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"return" -> "returns"?
base/deprecated.jl
Outdated
end | ||
function spdiagm(x, d) | ||
depwarn(string("spdiagm((x1, x2, ...), (d1, d2, ...)) is deprecated, use ", | ||
"spdiagm(x1 => d1, x2 => d2, ...) instead, which now return a square matrix. ", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"return" => "returns"?
base/deprecated.jl
Outdated
end | ||
function spdiagm(x, d, m::Integer, n::Integer) | ||
depwarn(string("spdiagm((x1, x2, ...), (d1, d2, ...), m, n) is deprecated, use ", | ||
"spdiagm(x1 => d1, x2 => d2, ...) instead, which now return a square matrix. ", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"return" -> "returns"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes strike me as a step forward, independent of where diagm
and friends go eventually. Much thanks for thinking through the design here and in upstream threads @fredrikekre! :)
A minor, subjective point: I find k => v
more natural than v => k
, particularly with the pairs consistently identified as kv
. But I'm sure this impression varies, and practically the difference is likely negligible :).
I read |
Either orientation sounds good on this end :). |
I don't particularly care either. Perhaps other people have some opinions? |
A matrix can be viewed as an association mapping a number (the |
Also, the same vector might be used for different diagonals, but not vice versa. |
instead of using one tuple with diagonals and one tuple with vectors
3b107ae
to
f07157b
Compare
Should be ready now. Wanna take another look @Sacha0 ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm from a rapid skim of this iteration! :)
Do we still have to call |
I think this is nicer than the previous
spdiagm(tuple_of_vectors, tuple_of_diags)
API. And the same should also be implemented fordiagm
. See #23320 and specifically #23320 (comment)This also deals with #23320 (comment) such that we always return a square matrix.
Some things to decide:
vector => diagonal
ordiagonal => vector
? I thinkvector => diagonal
is nicer.spdiagm(v::AbstractVector) = spdiagm(0 => v)
. I imagine that case being quite common and it might be nice to have that?Still need to implement proper deprecations and such.