Skip to content
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

Fix diagm_container for types without zero #35743

Merged
merged 1 commit into from
Jun 11, 2020

Conversation

blegat
Copy link
Contributor

@blegat blegat commented May 5, 2020

For some types, T, zero(T) isa T does not hold. This is the case for instance for T = JuMP.VariableRef for which zero(T) isa AffExpr. When the users call diagm with vectors of VariableRef, we should have a matrix of AffExpr but instead an error is thrown

MethodError: Cannot `convert` an object of type JuMP.AffExpr to an object of type JuMP.VariableRef
Closest candidates are:
  convert(::Type{T}, !Matched::T) where T at essentials.jl:171

because diagm_container calls zeros(JuMP.VariableRef, ...).
I think it is fair for zeros(JuMP.VariableRef, ...) to error and it would not be consistent for it to return a matrix of JuMP.AffExpr so it seems the issue is that diagm_container calls zeros(JuMP.VariableRef, ...) hence the fix in this PR.

Note that if you try it with the latest version of JuMP, it currently works as we redefine diagm_container just for this case:
https://github.com/JuliaOpt/MutableArithmetics.jl/blob/407a8fdfb5ef1810f3785a88e490291d37b9fb03/src/dispatch.jl#L27-L31
but we'd rather have it fixed upstream as other types might benefit from it (e.g. polynomial variables from MultivariatePolynomials).

See jump-dev/MutableArithmetics.jl#47 for more context.

@blegat
Copy link
Contributor Author

blegat commented Jun 8, 2020

Bump

Copy link
Member

@dkarrasch dkarrasch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me.

@dkarrasch dkarrasch added the linear algebra Linear algebra label Jun 8, 2020
@StefanKarpinski StefanKarpinski merged commit a3ef188 into JuliaLang:master Jun 11, 2020
simeonschaub pushed a commit to simeonschaub/julia that referenced this pull request Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear algebra Linear algebra
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants