If diag is passed a vector rather than a matrix, we already give a good error message:
julia> diag([1, 2, 3, 4]) ERROR: use diagm instead of diag to construct a diagonal matrix in diag at linalg/generic.jl:49 It wouldn't hurt to have this in the docs, though. On Sunday, April 27, 2014 4:07:52 PM UTC-4, Andreas Noack Jensen wrote: > > I agree. It would probably avoid some confusion if the documentation was a > little longer and pointed to diagm and Diagonal. > > > 2014-04-27 22:02 GMT+02:00 Ivar Nesje <[email protected] <javascript:>>: > >> This difference should be explained in the documentation for diag >> >> The current documentation is kind of short: >> >> Base.diag(M[, k]) >> The "k"-th diagonal of a matrix, as a vector. >> >> Ivar >> >> kl. 21:54:43 UTC+2 søndag 27. april 2014 skrev John Code følgende: >> >>> Thank you. >>> >>> On Sunday, April 27, 2014 11:49:12 PM UTC+4, Andreas Noack Jensen wrote: >>>> >>>> Hi John >>>> >>>> In julia, the function diag extract the diagonal of a matrix and if the >>>> matrix is rectangular, it extracts the diagonal of the largest square sub >>>> matrix. Note that in julia, [1 2 3 4] is not vector but a matrix. To >>>> construct a matrix from a vector you can either use the function diagm, >>>> which does what you expected diag did, >>>> >>>> julia> diagm([1,2,3,4]) >>>> 4x4 Array{Int64,2}: >>>> 1 0 0 0 >>>> 0 2 0 0 >>>> 0 0 3 0 >>>> 0 0 0 4 >>>> >>>> but it is often better to use Diagonal, which creates a special >>>> Diagonal matrix, >>>> >>>> julia> Diagonal([1,2,3,4]) >>>> >>>> 4x4 Diagonal{Int64}: >>>> 1 0 0 0 >>>> 0 2 0 0 >>>> 0 0 3 0 >>>> 0 0 0 4 >>>> >>>> >>>> 2014-04-27 21:40 GMT+02:00 John Code <[email protected]>: >>>> > >>>> > Hi all, >>>> > I would like to ask why there is a difference between Octave diag >>>> function >>>> > and the function that julia provide. For example, in the following >>>> Octave session I get: >>>> > >>>> > ============================ >>>> > octave:1> v = [1 2 3 4] >>>> > v = >>>> > >>>> > 1 2 3 4 >>>> > >>>> > octave:2> a = diag(v) >>>> > a = >>>> > >>>> > Diagonal Matrix >>>> > >>>> > 1 0 0 0 >>>> > 0 2 0 0 >>>> > 0 0 3 0 >>>> > 0 0 0 4 >>>> > ============================= >>>> > >>>> > But in Julia I get: >>>> > >>>> > ============================= >>>> > julia> v = [1 2 3 4] >>>> > 1x4 Array{Int64,2}: >>>> > 1 2 3 4 >>>> > >>>> > julia> a = diag(v) >>>> > 1-element Array{Int64,1}: >>>> > 1 >>>> > >>>> > >>>> > ============================= >>>> > >>>> > >>>> > Why is this the case and how to get a similar effect of the octave >>>> code. >>>> > Thank you. >>>> >>>> >>>> >>>> >>>> -- >>>> Med venlig hilsen >>>> >>>> Andreas Noack Jensen >>>> >>> > > > -- > Med venlig hilsen > > Andreas Noack Jensen >
