-
-
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
diagm for non-square matrices #31654
Conversation
I suppose this is too late for 1.2, so I should wait until 1.3-dev to add a NEWS entry, I guess? |
Yeah, we'll be branching for 1.2 shortly. |
I'm all for the functionality here. It's a little unfortunate that we have to use a keyword argument for the size when we use either positional arguments everywhere else for specifying the size. Can't really come up with a better suggestion right now, though. |
Why not as a positional argument? This is currently a method error: julia> diagm(rand(3), (3, 4))
ERROR: MethodError: no method matching diagm(::Array{Float64,1}, ::Tuple{Int64,Int64}) What should this do if you call |
@StefanKarpinski, I think it's pretty clear it should pad with zeros, and raise an error if the (m,n) size is too small to hold the requested diagonal. (That's the behavior in the current PR.) If we use a positional argument, it would be more consistent with our other APIs to pass |
IMO last, similar to e.g. |
The implementation would certainly be easier if |
Another possibility would be a julia> diagm(0=>[1,2,3], 1=>[4,5,6], tight=true)
3×4 Array{Int64,2}:
1 4 0 0
0 2 5 0
0 0 3 6 Even if we allow a general (Not sure if there is a better word than |
8ef0ad1
to
d70278b
Compare
Updated to use |
AppVeyor i686 "Error in testset Profile" seems unrelated (#29880). |
Co-Authored-By: andreasnoack <[email protected]>
This PR adds
aoptionalsize=(m,n)
keyword argumentm,n
initial arguments todiagm
andspdiagm
so that they can be used to create non-square matrices.I mainly care about this capability for
spdiagm
, which was removed in #23757: I often create non-square matrices as a part of creating finite-difference operators via Kronecker products, and the current workaround is to callSparseArrays.spdiagm_internal
. But ifspdiagm
gets this feature thandiagm
clearly should as well.Instead ofIt could also havesize=(m,n)
argument, I suppose it could acceptnrows
andncols
or similar. I don't have strong feelings, butsize=(m,n)
seemed cleaner and more comprehensible to me, since if you calldiagm(...; nrows=N)
it is very unclear what to expect forncols
(should it=nrows
or should it be the minimum number of columns for the given diagonals?).m,n
as the first positional arguments instead of as a keyword, I suppose.