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

gradient of function with Sparse Matrix fails: #198

Open
longemen3000 opened this issue Dec 7, 2021 · 0 comments
Open

gradient of function with Sparse Matrix fails: #198

longemen3000 opened this issue Dec 7, 2021 · 0 comments

Comments

@longemen3000
Copy link

reproducer:

using FiniteDifferences,LinearAlgebra;SparseArrays,Random
grad(central_fdm(5,1),norm,B)
I_n = spdiagm(ones(n))
Random.seed!(0)
B = sprand(n, n, 0.5) * 0.2 + I_n
FiniteDifferences.grad(central_fdm(5,1),norm,B)

it gives:

ERROR: TypeError: in new, expected Int64, got a value of type Float64Stacktrace:
  [1] macro expansion
    @ C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\to_vec.jl:0 [inlined]
  [2] _force_construct
    @ C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\to_vec.jl:27 [inlined]
  [3] (::FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}})(v::Vector{Float64})
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\to_vec.jl:53
  [4] (::ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}})(x::Vector{Float64}; kw::Base.Pairs{Symbol, 
Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Base .\operators.jl:1085
  [5] ComposedFunction
    @ .\operators.jl:1085 [inlined]
  [6] (::FiniteDifferences.var"#64#66"{Int64, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, Vector{Float64}})(ε::Float64)
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:19
  [7] #10
    @ .\broadcast.jl:329 [inlined]
  [8] macro expansion
    @ C:\Users\user\.julia\packages\StaticArrays\OWJK7\src\broadcast.jl:126 [inlined]
  [9] _broadcast
    @ C:\Users\user\.julia\packages\StaticArrays\OWJK7\src\broadcast.jl:100 [inlined]
 [10] copy
    @ C:\Users\user\.julia\packages\StaticArrays\OWJK7\src\broadcast.jl:27 [inlined]
 [11] materialize
    @ .\broadcast.jl:860 [inlined]
 [12] _eval_function(m::FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}, f::FiniteDifferences.var"#64#66"{Int64, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, 
Vector{Float64}}, x::Float64, step::Float64)
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\methods.jl:249
 [13] _estimate_magnitudes(m::FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}, f::FiniteDifferences.var"#64#66"{Int64, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, Vector{Float64}}, x::Float64)
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\methods.jl:378
 [14] estimate_step(m::FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}, f::FiniteDifferences.var"#64#66"{Int64, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, Vector{Float64}}, x::Float64)
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\methods.jl:365
 [15] (::FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}})(f::FiniteDifferences.var"#64#66"{Int64, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, Vector{Float64}}, x::Float64)    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\methods.jl:193
 [16] #63
    @ C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:16 [inlined]
 [17] iterate
    @ .\generator.jl:47 [inlined]
 [18] _collect(c::Base.OneTo{Int64}, itr::Base.Generator{Base.OneTo{Int64}, FiniteDifferences.var"#63#65"{FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, Vector{Float64}}}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
    @ Base .\array.jl:744
 [19] collect_similar(cont::Base.OneTo{Int64}, itr::Base.Generator{Base.OneTo{Int64}, FiniteDifferences.var"#63#65"{FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, Vector{Float64}}})
    @ Base .\array.jl:653
 [20] map(f::Function, A::Base.OneTo{Int64})
    @ Base .\abstractarray.jl:2849
 [21] #jacobian#62
    @ C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:15 [inlined]
 [22] jacobian
    @ C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:10 [inlined]
 [23] _j′vp(fdm::FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}, f::Function, ȳ::Vector{Int64}, x::Vector{Float64})
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:80
 [24] j′vp(fdm::FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}, f::Function, ȳ::Int64, x::SparseMatrixCSC{Float64, Int64})
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:73
 [25] grad(fdm::FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}, f::Function, xs::SparseMatrixCSC{Float64, Int64})
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:88
 [26] top-level scope
    @ REPL[61]:1

caused by: MethodError: no method matching SparseMatrixCSC{Float64, Int64}(::Float64, ::Float64, ::Vector{Float64}, ::Vector{Float64}, ::Vector{Float64})
Closest candidates are:
  SparseMatrixCSC{Tv, Ti}(::Integer, ::Integer, ::Vector{Ti}, ::Vector{Ti}, ::Vector{Tv}) where {Tv, Ti<:Integer} at C:\Users\user\.julia\juliaup\julia-1.7.0+0~x64\share\julia\stdlib\v1.7\SparseArrays\src\sparsematrix.jl:26
Stacktrace:
  [1] (::FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}})(v::Vector{Float64})
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\to_vec.jl:51
  [2] (::ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}})(x::Vector{Float64}; kw::Base.Pairs{Symbol, 
Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Base .\operators.jl:1085
  [3] ComposedFunction
    @ .\operators.jl:1085 [inlined]
  [4] (::FiniteDifferences.var"#64#66"{Int64, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, Vector{Float64}})(ε::Float64)
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:19
  [5] #10
    @ .\broadcast.jl:329 [inlined]
  [6] macro expansion
    @ C:\Users\user\.julia\packages\StaticArrays\OWJK7\src\broadcast.jl:126 [inlined]
  [7] _broadcast
    @ C:\Users\user\.julia\packages\StaticArrays\OWJK7\src\broadcast.jl:100 [inlined]
  [8] copy
    @ C:\Users\user\.julia\packages\StaticArrays\OWJK7\src\broadcast.jl:27 [inlined]
  [9] materialize
    @ .\broadcast.jl:860 [inlined]
 [10] _eval_function(m::FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}, f::FiniteDifferences.var"#64#66"{Int64, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, 
Vector{Float64}}, x::Float64, step::Float64)
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\methods.jl:249
 [11] _estimate_magnitudes(m::FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}, f::FiniteDifferences.var"#64#66"{Int64, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, Vector{Float64}}, x::Float64)
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\methods.jl:378
 [12] estimate_step(m::FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}, f::FiniteDifferences.var"#64#66"{Int64, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, Vector{Float64}}, x::Float64)
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\methods.jl:365
 [13] (::FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}})(f::FiniteDifferences.var"#64#66"{Int64, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, Vector{Float64}}, x::Float64)    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\methods.jl:193
 [14] #63
    @ C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:16 [inlined]
 [15] iterate
    @ .\generator.jl:47 [inlined]
 [16] _collect(c::Base.OneTo{Int64}, itr::Base.Generator{Base.OneTo{Int64}, FiniteDifferences.var"#63#65"{FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, Vector{Float64}}}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
    @ Base .\array.jl:744
 [17] collect_similar(cont::Base.OneTo{Int64}, itr::Base.Generator{Base.OneTo{Int64}, FiniteDifferences.var"#63#65"{FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}, ComposedFunction{ComposedFunction{ComposedFunction{typeof(first), typeof(to_vec)}, typeof(f2)}, FiniteDifferences.var"#structtype_from_vec#29"{SparseMatrixCSC{Float64, Int64}, FiniteDifferences.var"#Tuple_from_vec#48"{NTuple{5, Int64}, NTuple{5, Int64}, NTuple{5, typeof(identity)}}, Tuple{FiniteDifferences.var"#Real_from_vec#20", FiniteDifferences.var"#Real_from_vec#20", typeof(identity), typeof(identity), typeof(identity)}}}, Vector{Float64}}})
    @ Base .\array.jl:653
 [18] map(f::Function, A::Base.OneTo{Int64})
    @ Base .\abstractarray.jl:2849
 [19] #jacobian#62
    @ C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:15 [inlined]
 [20] jacobian
    @ C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:10 [inlined]
 [21] _j′vp(fdm::FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}, f::Function, ȳ::Vector{Int64}, x::Vector{Float64})
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:80
 [22] j′vp(fdm::FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}, f::Function, ȳ::Int64, x::SparseMatrixCSC{Float64, Int64})
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:73
 [23] grad(fdm::FiniteDifferences.AdaptedFiniteDifferenceMethod{5, 1, FiniteDifferences.UnadaptedFiniteDifferenceMethod{7, 5}}, f::Function, xs::SparseMatrixCSC{Float64, Int64})
    @ FiniteDifferences C:\Users\user\.julia\packages\FiniteDifferences\iFBBx\src\grad.jl:88
 [24] top-level scope
    @ REPL[61]:1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant