-
Notifications
You must be signed in to change notification settings - Fork 40
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
doesn't work
#25
Comments
Thanks for the proposal. function diagm{T}(v::AbstractVector{T}, k::Integer=0)
n = length(v) + abs(k)
A = zeros(T,n,n)
A[diagind(A,k)] = v
A
end Please see #12 for more details. |
The other important part I think is the indexing. The current A sketch of something that would solve both problems: function diagm2{T}(v::AbstractVector{T})
S = promote_type(T, typeof(zero(T)))
A = similar(v, S, first(indices(v)), first(indices(v)))
for i in eachindex(v), j in eachindex(v)
A[i, j] = i == j ? v[i] : zero(T)
end
A
end with which using OffsetArrays, JuMP
m = Model()
@variable(m, v[1 : 3])
diagm2(OffsetArray(v, -3)) produces
Problems of course are that That brings up a different question: what should |
That improved
Seems like one might want an interface |
Interesting. That does seem hard to make consistent with the current behavior for regular Another option is to change the last index for each dimension, so -2:1x-2:1 for both |
Cross-ref JuliaLang/julia#24047: I don't think we can call |
The specific example listed in the OP seems to work now julia> A = OffsetArray(rand(3), -3)
3-element OffsetArray(::Array{Float64,1}, -2:0) with eltype Float64 with indices -2:0:
0.5009784688952947
0.09987117852205074
0.6784469095270629
julia> diagm(A)
3×3 Array{Float64,2}:
0.500978 0.0 0.0
0.0 0.0998712 0.0
0.0 0.0 0.678447 Closing as old. Please reopen if further discussion on this is necessary. |
results in
on nightly. This should probably be fixed in Base, but I wanted to get your input first.
As for the desired output type of
diagm
, I guess it should always be?
The text was updated successfully, but these errors were encountered: