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 errors for LinearAlgebra operators of JuMP arrays #3476

Merged
merged 5 commits into from Sep 4, 2023

Conversation

odow
Copy link
Member

@odow odow commented Aug 31, 2023

Closes #3467

@codecov
Copy link

codecov bot commented Sep 1, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (99a67fa) 98.09% compared to head (7efc7f9) 98.09%.
Report is 4 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3476   +/-   ##
=======================================
  Coverage   98.09%   98.09%           
=======================================
  Files          37       37           
  Lines        5501     5518   +17     
=======================================
+ Hits         5396     5413   +17     
  Misses        105      105           
Files Changed Coverage Δ
src/nlp_expr.jl 99.26% <100.00%> (+0.02%) ⬆️

... and 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mlubin
Copy link
Member

mlubin commented Sep 1, 2023

Are these the only functions affected or are there others?

@odow
Copy link
Member Author

odow commented Sep 1, 2023

So do we want to do it for everything?

julia> using JuMP, LinearAlgebra

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x[1:2, 1:2])
2×2 Matrix{VariableRef}:
 x[1,1]  x[1,2]
 x[2,1]  x[2,2]

julia> ret = Any[]
Any[]

julia> for name in names(LinearAlgebra; all = true)
           f = getfield(LinearAlgebra, name)
           try
               f(x)
           catch err
               if !(err isa MethodError)
                   push!(ret, (name, typeof(err)))
               end
           end
       end


julia> 

julia> ret
19-element Vector{Any}:
 (:SymTridiagonal, ArgumentError)
 (:_rightappdimmismatch, DimensionMismatch)
 (:cholesky!, PosDefException)
 (:condskeel, TypeError)
 (:generic_norm1, StackOverflowError)
 (:generic_norm2, StackOverflowError)
 (:generic_normInf, StackOverflowError)
 (:generic_normMinusInf, StackOverflowError)
 (:indmaxcolumn, StackOverflowError)
 (:norm, StackOverflowError)
 (:norm1, StackOverflowError)
 (:norm2, StackOverflowError)
 (:normInf, StackOverflowError)
 (:normMinusInf, StackOverflowError)
 (:norm_sqr, StackOverflowError)
 (:normalize, StackOverflowError)
 (:normalize!, StackOverflowError)
 (:qr, TypeError)
 (:qrfactPivotedUnblocked!, StackOverflowError)

julia> using JuMP, LinearAlgebra

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x[1:2])
2-element Vector{VariableRef}:
 x[1]
 x[2]

julia> ret = Any[]
Any[]

julia> for name in names(LinearAlgebra; all = true)
           f = getfield(LinearAlgebra, name)
           try
               f(x)
           catch err
               if !(err isa MethodError)
                   push!(ret, (name, typeof(err)))
               end
           end
       end

julia> ret
17-element Vector{Any}:
 (:_rightappdimmismatch, DimensionMismatch)
 (:_triuppers_qr, BoundsError)
 (:checksquare, BoundsError)
 (:diag, ArgumentError)
 (:generic_norm1, StackOverflowError)
 (:generic_norm2, StackOverflowError)
 (:generic_normInf, StackOverflowError)
 (:generic_normMinusInf, StackOverflowError)
 (:norm, StackOverflowError)
 (:norm1, StackOverflowError)
 (:norm2, StackOverflowError)
 (:normInf, StackOverflowError)
 (:normMinusInf, StackOverflowError)
 (:norm_sqr, StackOverflowError)
 (:normalize, StackOverflowError)
 (:normalize!, StackOverflowError)
 (:qr, TypeError)

@mlubin
Copy link
Member

mlubin commented Sep 1, 2023

All the variants of norms look relevant to cover

@odow
Copy link
Member Author

odow commented Sep 1, 2023

So those norms are not exported and they don't have any documentation:

help?> LinearAlgebra.generic_norm1
  No documentation found.

  LinearAlgebra.generic_norm1 is a Function.

  # 1 method for generic function "generic_norm1":
  [1] generic_norm1(x) in LinearAlgebra at /Users/oscar/.julia/juliaup/julia-1.6.7+0.x64.apple.darwin14/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:477

help?> LinearAlgebra.norm1
  No documentation found.

  LinearAlgebra.norm1 is a Function.

  # 2 methods for generic function "norm1":
  [1] norm1(x::Union{DenseVector{T}, Base.ReinterpretArray{T, 1, S, A, IsReshaped} where {A<:Union{SubArray{T, N, A, I, true} where {T, N, A<:DenseArray, I<:Union{Tuple{Vararg{Real, N} where N}, Tuple{AbstractUnitRange, Vararg{Any, N} where N}}}, DenseArray}, IsReshaped, S}, Base.ReshapedArray{T, 1, A, MI} where {A<:Union{Base.ReinterpretArray{T, N, S, A, IsReshaped} where {T, N, A<:Union{SubArray{T, N, A, I, true} where {T, N, A<:DenseArray, I<:Union{Tuple{Vararg{Real, N} where N}, Tuple{AbstractUnitRange, Vararg{Any, N} where N}}}, DenseArray}, IsReshaped, S}, SubArray{T, N, A, I, true} where {T, N, A<:DenseArray, I<:Union{Tuple{Vararg{Real, N} where N}, Tuple{AbstractUnitRange, Vararg{Any, N} where N}}}, DenseArray}, MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64}, N} where N}}, SubArray{T, 1, A, I, L} where {A<:Union{Base.ReinterpretArray{T, N, S, A, IsReshaped} where {T, N, A<:Union{SubArray{T, N, A, I, true} where {T, N, A<:DenseArray, I<:Union{Tuple{Vararg{Real, N} where N}, Tuple{AbstractUnitRange, Vararg{Any, N} where N}}}, DenseArray}, IsReshaped, S}, Base.ReshapedArray{T, N, A, MI} where {T, N, A<:Union{Base.ReinterpretArray{T, N, S, A, IsReshaped} where {T, N, A<:Union{SubArray{T, N, A, I, true} where {T, N, A<:DenseArray, I<:Union{Tuple{Vararg{Real, N} where N}, Tuple{AbstractUnitRange, Vararg{Any, N} where N}}}, DenseArray}, IsReshaped, S}, SubArray{T, N, A, I, true} where {T, N, A<:DenseArray, I<:Union{Tuple{Vararg{Real, N} where N}, Tuple{AbstractUnitRange, Vararg{Any, N} where N}}}, DenseArray}, MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64}, N} where N}}, DenseArray}, I<:Tuple{Vararg{Union{Int64, AbstractRange{Int64}, Base.AbstractCartesianIndex, Base.ReshapedArray{T, N, A, Tuple{}} where {T, N, A<:AbstractUnitRange}}, N} where N}, L}, Array{T, N} where N}) where T<:Union{Float32, Float64} in LinearAlgebra at /Users/oscar/.julia/juliaup/julia-1.6.7+0.x64.apple.darwin14/share/julia/stdlib/v1.6/LinearAlgebra/src/dense.jl:102
  [2] norm1(x) in LinearAlgebra at /Users/oscar/.julia/juliaup/julia-1.6.7+0.x64.apple.darwin14/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:557

help?> LinearAlgebra.norm2
  No documentation found.

  LinearAlgebra.norm2 is a Function.

  # 2 methods for generic function "norm2":

This is the exported list: #3467 (comment), and normalize now throws an error because it uses norm.

@mlubin
Copy link
Member

mlubin commented Sep 1, 2023

Ok that's fine, we shouldn't don't need to define fallbacks for methods that aren't exported.

@odow
Copy link
Member Author

odow commented Sep 1, 2023

The matrix ones are more problematic:

julia> la_y
29-element Vector{Any}:
 (:bunchkaufman, MethodError(VariableRef, (1,), 0x0000000000007424))
 (:cholesky, MethodError(VariableRef, (1,), 0x0000000000007424))
 (:cholesky!, PosDefException(-1))
 (:cond, MethodError(LinearAlgebra.svdvals!, (Any[y[1,1] y[1,2]; y[2,1] y[2,2]],), 0x0000000000007424))
 (:condskeel, TypeError(:typeassert, "", NonlinearExpr, y[1,1]))
 (:eigen, MethodError(LinearAlgebra.var"#eigen!##kw"(), ((permute = true, scale = true, sortby = LinearAlgebra.eigsortby), LinearAlgebra.eigen!, Any[y[1,1] y[1,2]; y[2,1] y[2,2]]), 0x0000000000007424))
 (:eigmax, MethodError(LinearAlgebra.var"#eigvals!##kw"(), ((permute = true, scale = true), LinearAlgebra.eigvals!, Any[y[1,1] y[1,2]; y[2,1] y[2,2]]), 0x0000000000007424))
 (:eigmin, MethodError(LinearAlgebra.var"#eigvals!##kw"(), ((permute = true, scale = true), LinearAlgebra.eigvals!, Any[y[1,1] y[1,2]; y[2,1] y[2,2]]), 0x0000000000007424))
 (:eigvals, MethodError(LinearAlgebra.eigvals!, (Any[y[1,1] y[1,2]; y[2,1] y[2,2]],), 0x0000000000007424))
 (:eigvecs, MethodError(LinearAlgebra.var"#eigen!##kw"(), ((permute = true, scale = true, sortby = LinearAlgebra.eigsortby), LinearAlgebra.eigen!, Any[y[1,1] y[1,2]; y[2,1] y[2,2]]), 0x0000000000007424))
 (:factorize, MethodError(VariableRef, (1,), 0x0000000000007424))
 (:hessenberg, MethodError(LinearAlgebra.hessenberg!, (Any[y[1,1] y[1,2]; y[2,1] y[2,2]],), 0x0000000000007424))
 (:logabsdet, MethodError(VariableRef, (1,), 0x0000000000007424))
 (:logdet, MethodError(VariableRef, (1,), 0x0000000000007424))
 (:lu, MethodError(VariableRef, (1,), 0x0000000000007424))
 (:lu!, MethodError(isless, (abs(y[1,1]), abs(y[2,1])), 0x0000000000007424))
 (:nullspace, MethodError(AbstractFloat, (1,), 0x0000000000007424))
 (:opnorm, MethodError(AbstractFloat, (0,), 0x0000000000007424))
 (:pinv, MethodError(AbstractFloat, (1,), 0x0000000000007424))
 (:qr, TypeError(:typeassert, "", NonlinearExpr, y[1,1]))
 (:qr!, MethodError(convert, (VariableRef, 0), 0x0000000000007424))
 (:rank, MethodError(AbstractFloat, (1,), 0x0000000000007424))
 (:schur, MethodError(LinearAlgebra.schur!, (Any[y[1,1] y[1,2]; y[2,1] y[2,2]],), 0x0000000000007424))
 (:svd, MethodError(LinearAlgebra.var"#svd!##kw"(), ((full = false, alg = LinearAlgebra.DivideAndConquer()), LinearAlgebra.svd!, Any[y[1,1] y[1,2]; y[2,1] y[2,2]]), 0x0000000000007424))
 (:svdvals, MethodError(LinearAlgebra.svdvals!, (Any[y[1,1] y[1,2]; y[2,1] y[2,2]],), 0x0000000000007424))
 (:tril, MethodError(convert, (VariableRef, 0), 0x0000000000007424))
 (:tril!, MethodError(convert, (VariableRef, 0), 0x0000000000007424))
 (:triu, MethodError(convert, (VariableRef, 0), 0x0000000000007424))
 (:triu!, MethodError(convert, (VariableRef, 0), 0x0000000000007424))

No overflows, but do we want to add fallbacks for things like LinearAlgebra.triu(::Matrix{<:AbstractJuMPScalar})?

src/nlp_expr.jl Outdated Show resolved Hide resolved
src/nlp_expr.jl Outdated Show resolved Hide resolved
@odow
Copy link
Member Author

odow commented Sep 3, 2023

Let's go with this for now, and see what feedback we get?

@odow odow merged commit 7b98ffc into master Sep 4, 2023
12 checks passed
@odow odow deleted the od/linear-algebra-errors branch September 4, 2023 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

StackOverflow for norm of vector of variables
3 participants