diff --git a/Project.toml b/Project.toml index 652b8c46..ca1122ee 100644 --- a/Project.toml +++ b/Project.toml @@ -13,6 +13,8 @@ julia = "1" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +DiffEqDiffTools = "01453d9d-ee7c-5054-8395-0335cb756afa" [targets] -test = ["Test"] +test = ["Test", "LinearAlgebra", "DiffEqDiffTools"] diff --git a/src/matrix2graph.jl b/src/matrix2graph.jl index 8175b468..b8803b73 100644 --- a/src/matrix2graph.jl +++ b/src/matrix2graph.jl @@ -10,7 +10,7 @@ sparse matrix, columns are represented with vertices and 2 vertices are connected with an edge only if the two columns are mutually orthogonal. """ -function matrix2graph(SparseMatrix::SparseMatrixCSC{Int64,Int64}) +function matrix2graph(SparseMatrix::SparseMatrixCSC{T,Int}) where T<:Number dropzeros(SparseMatrix) (rows_index, cols_index, val) = findnz(SparseMatrix) diff --git a/test/runtests.jl b/test/runtests.jl index da0ff461..0117be07 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -5,3 +5,4 @@ using Test @testset "Exact coloring via contraction" begin include("test_contraction.jl") end @testset "Greedy distance-1 coloring" begin include("test_greedy_d1.jl") end @testset "Matrix to graph conversion" begin include("test_matrix2graph.jl") end +@testset "Integration test" begin include("test_integration.jl") end diff --git a/test/test_integration.jl b/test/test_integration.jl new file mode 100644 index 00000000..ae7862a8 --- /dev/null +++ b/test/test_integration.jl @@ -0,0 +1,49 @@ +using SparseDiffTools +using LinearAlgebra +using DiffEqDiffTools +using Test + +fcalls = 0 +function f(dx,x) + global fcalls += 1 + for i in 2:length(x)-1 + dx[i] = x[i-1] - 2x[i] + x[i+1] + end + dx[1] = -2x[1] + x[2] + dx[end] = x[end-1] - 2x[end] + nothing +end + +function second_derivative_stencil(N) + A = zeros(N,N) + for i in 1:N, j in 1:N + (j-i==-1 || j-i==1) && (A[i,j]=1) + j-i==0 && (A[i,j]=-2) + end + A +end + +function generate_sparsity_pattern(N::Integer) + dl = repeat([1.0],N-1) + du = repeat([1.0],N-1) + d = repeat([-2.0],N) + return Tridiagonal(dl,d,du) +end + +sparsity_pattern = sparse(generate_sparsity_pattern(30)) + +_graph = matrix2graph(sparsity_pattern) +coloring_vector = greedy_d1(_graph) +@test coloring_vector == repeat(1:3,10) + +#Jacobian computed without coloring vector +J = DiffEqDiffTools.finite_difference_jacobian(f, rand(30)) +@test J ≈ second_derivative_stencil(30) +@test fcalls == 31 + +#Jacobian computed with coloring vectors +fcalls = 0 +_J = sparsity_pattern +DiffEqDiffTools.finite_difference_jacobian!(_J, f, rand(30), color = coloring_vector) +@test fcalls == 4 +@test _J ≈ J