-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
LinearAlgebra diag() returns error for 1- and 0-dimensional matrices #727
Comments
Note that both julia> a = fill(5, 1, 1) # 1x1 matrix
1×1 Array{Int64,2}:
5
julia> diag(a)
1-element Array{Int64,1}:
5
julia> a = fill(5, 0, 0) # 0x0 matrix
0×0 Array{Int64,2}
julia> diag(a)
0-element Array{Int64,1} |
Similar behavior with numpy as well actually; [] turns into a 1D numpy array, so this should be >>> numpy.diag([[1]])
array([1])
>>> numpy.diag([[]])
array([], dtype=float64) The table is also wrong for >>> numpy.diag([1])
array([[1]]) |
We can contrast Linear Algebra to Julia's Implementation of linear algebra. Is [5] a vector or an array? From a Linear Algebra perspective, it's both, or either. The Julia Implementation can store [5] in two different data structures (vector or array). The storage data structure shouldn't break the Linear Algebra. Furthermore, what does Julia tell me about vector [5]?
It informs the user that it is a 1-element Array. It doesn't say Vector, it says Array. I've corrected the table for the Numpy result based on the other comment. |
Examples are still wrong, because you are not calling the same functionality; All languages and libraries I'm aware of, except matlab, is consistent on this; 2D input, 1D output, even when the size in each dimension is less than 2: julia> diag(ones((1,1)))
1-element Array{Float64,1}:
1.0 Numpy: >>> diag(ones((1,1))) # or diag([[1]])
array([1.]) Mathematica: In[4]:= Diagonal[ConstantArray[1, {1, 1}]] (* or Diagonal[{{1}}] *)
Out[4]= {1} R > diag(matrix(1, nrow=1))
[1] 1 The only difference is syntax for constructing 2D like things. Matlab/octave is the odd one out, as it decides to give special treatment and disallow true 1D things by padding on a second dimension. The ambiguity of the math is a bad thing. Julia tells you that [5] is a (1) dimensional thing, and not a 1x1 dimensional thing. |
Python Numpy:
Julia is telling me it is storing it as a (1) dimensional thing, not a 1x1 dimensional thing. [5] is a special case of a (1) dimensional thing and a 1x1 dimensional thing. You're explained why Julia does it the way it does it. But . . . how should it be done? You see how I think it should be done under "desired behaviors". |
As documented, julia> diag([1, 2, 3])
ERROR: ArgumentError: use diagm instead of diag to construct a diagonal matrix This is helpful since people coming from other systems may be expecting |
Eh, they're not using the wrong function, they're using the wrong type. The user truly wanted the diagonal of [5]. It's cumbersome in the REPL to create a 1 dimensional matrix.
Just considering the user interface, it would be friendlier to a statistician typing in the REPL if diag([n]) returned [n] and diag([]) returned [].
I don't see why this is the case. It would just apply to 1-dimensional and 0-dimensional vectors. However, if you don't want to modify the behavior of the function, then I would request that the error message change.
The current error message is assuming the user is trying to construct a diagonal matrix. If the user was just trying to get the diagonal of a 1-dimensional matrix, this error is confusing. The user error is feeding diag() a vector and not an array. The error message should say that. Perhaps something like:
That would be a helpful error message. |
We don't do that sort of thing. Making a function that works when a vector has certain sizes but not others is how you end up with a language that's impossible to write reliable code in.
Yes, that would be a reasonable change as well since some users may have wanted to take the diagonal of a vector as though it were a column matrix. |
To be clear, it's also reasonable to make |
I propose closing this. |
Problem Statement
The diag() function in Linear Algebra returns an error for 1- and 0-dimensional matrices.
Desired Behavior
In both cases, the input matrix can simply be returned
Error Example: 1 dimensional case
Error Example: 0 dimensional case
What do other languages do?
* the numpy error returned says that diagonal() requires at least a 2x2 matrix; however, it works for a 1-dimensional matrix.
† result corrected from comment below.
System information for above examples:
The text was updated successfully, but these errors were encountered: