diff --git a/NEWS.md b/NEWS.md index 1126cb2d117962..8c7fbc1a523b88 100644 --- a/NEWS.md +++ b/NEWS.md @@ -509,6 +509,9 @@ Deprecated or removed particularly, consider instead `triu!(copy(parent(A)))`. On `LowerTriangular` matrices `A` particularly, consider instead `tril!(copy(parent(A)))` ([#24250]). + * `speye` has been deprecated in favor of `I`, `sparse`, and `SparseMatrixCSC` + constructor methods ([#24356]). + * Calling `union` with no arguments is deprecated; construct an empty set with an appropriate element type using `Set{T}()` instead ([#23144]). diff --git a/base/deprecated.jl b/base/deprecated.jl index ee81e772f9360a..8fc2b2c1e554cf 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -2070,6 +2070,55 @@ end # deprecate bits to bitstring (#24263, #24281) @deprecate bits bitstring +# deprecate speye +export speye +function speye(n::Integer) + depwarn(string("`speye(n::Integer)` has been deprecated in favor of `I`, `sparse`, and ", + "`SparseMatrixCSC` constructor methods. For a direct replacement, consider ", + "`sparse(1.0I, n, n)`, `SparseMatrixCSC(1.0I, n, n)`, or `SparseMatrixCSC{Float64}(I, n, n)`. ", + "If `Float64` element type is not necessary, consider the shorter `sparse(I, n, n)` ", + "or `SparseMatrixCSC(I, n, n)` (with default `eltype(I)` of `Bool`)."), :speye) + return sparse(1.0I, n, n) +end +function speye(m::Integer, n::Integer) + depwarn(string("`speye(m::Integer, n::Integer)` has been deprecated in favor of `I`, ", + "`sparse`, and `SparseMatrixCSC` constructor methods. For a direct ", + "replacement, consider `sparse(1.0I, m, n)`, `SparseMatrixCSC(1.0I, m, n)`, ", + "or `SparseMatrixCSC{Float64}(I, m, n)`. If `Float64` element type is not ", + " necessary, consider the shorter `sparse(I, m, n)` or `SparseMatrixCSC(I, m, n)` ", + "(with default `eltype(I)` of `Bool`)."), :speye) + return sparse(1.0I, m, n) +end +function speye(::Type{T}, n::Integer) where T + depwarn(string("`speye(T, n::Integer)` has been deprecated in favor of `I`, `sparse`, and ", + "`SparseMatrixCSC` constructor methods. For a direct replacement, consider ", + "`sparse(T(1)I, n, n)` if `T` is concrete or `SparseMatrixCSC{T}(I, n, n)` ", + "if `T` is either concrete or abstract. If element type `T` is not necessary, ", + "consider the shorter `sparse(I, n, n)` or `SparseMatrixCSC(I, n, n)` ", + "(with default `eltype(I)` of `Bool`)."), :speye) + return SparseMatrixCSC{T}(I, n) +end +function speye(::Type{T}, m::Integer, n::Integer) where T + depwarn(string("`speye(T, m::Integer, n::Integer)` has been deprecated in favor of `I`, ", + "`sparse`, and `SparseMatrixCSC` constructor methods. For a direct ", + "replacement, consider `sparse(T(1)I, m, n)` if `T` is concrete or ", + "`SparseMatrixCSC{T}(I, m, n)` if `T` is either concrete or abstract. ", + "If element type `T` is not necessary, consider the shorter ", + "`sparse(I, m, n)` or `SparseMatrixCSC(I, m, n)` (with default `eltype(I)` ", + "of `Bool`)."), :speye) + return SparseMatrixCSC{T}(I, m, n) +end +function speye(S::SparseMatrixCSC{T}) where T + depwarn(string("`speye(S::SparseMatrixCSC{T})` has been deprecated in favor of `I`, ", + "`sparse`, and `SparseMatrixCSC` constructor methods. For a direct ", + "replacement, consider `sparse(T(1)I, size(S)...)` if `T` is concrete or ", + "`SparseMatrixCSC{eltype(S)}(I, size(S))` if `T` is either concrete or abstract. ", + "If preserving element type `T` is not necessary, consider the shorter ", + "`sparse(I, size(S)...)` or `SparseMatrixCSC(I, size(S))` (with default ", + "`eltype(I)` of `Bool`)."), :speye) + return SparseMatrixCSC{T}(I, m, n) +end + # issue #24167 @deprecate EnvHash EnvDict diff --git a/base/essentials.jl b/base/essentials.jl index cc4e01f647709c..cdfc50f9a272f0 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -119,7 +119,7 @@ true Similarly, if `T` is a composite type and `x` a related instance, the result of `convert(T, x)` may alias part or all of `x`. ```jldoctest -julia> x = speye(5); +julia> x = sparse(1.0I, 5, 5); julia> typeof(x) SparseMatrixCSC{Float64,Int64} diff --git a/base/exports.jl b/base/exports.jl index 0e30396010261f..df357ab5e28b33 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1256,7 +1256,6 @@ export sparse, sparsevec, spdiagm, - speye, spones, sprand, sprandn, diff --git a/base/linalg/arnoldi.jl b/base/linalg/arnoldi.jl index 70e696c83eb669..fe05312d94f27e 100644 --- a/base/linalg/arnoldi.jl +++ b/base/linalg/arnoldi.jl @@ -145,7 +145,7 @@ final residual vector `resid`. # Examples ```jldoctest -julia> A = speye(4, 4); B = Diagonal(1:4); +julia> A = sparse(1.0I, 4, 4); B = Diagonal(1:4); julia> λ, ϕ = eigs(A, B, nev = 2); diff --git a/base/sparse/cholmod.jl b/base/sparse/cholmod.jl index 4939f1af57890b..49114148e96c3d 100644 --- a/base/sparse/cholmod.jl +++ b/base/sparse/cholmod.jl @@ -16,7 +16,7 @@ export Factor, Sparse -import ..SparseArrays: AbstractSparseMatrix, SparseMatrixCSC, increment, indtype, sparse, speye, +import ..SparseArrays: AbstractSparseMatrix, SparseMatrixCSC, increment, indtype, sparse, spzeros, nnz ######### diff --git a/base/sparse/sparse.jl b/base/sparse/sparse.jl index 4bffa183a8fbb9..af3b16bc873dcb 100644 --- a/base/sparse/sparse.jl +++ b/base/sparse/sparse.jl @@ -31,7 +31,7 @@ import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh, export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector, SparseMatrixCSC, SparseVector, blkdiag, droptol!, dropzeros!, dropzeros, - issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm, speye, spones, + issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm, spones, sprand, sprandn, spzeros, nnz, permute include("abstractsparse.jl") diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 32c242a7a19841..0d80afc3a8f3d7 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -59,11 +59,11 @@ Returns the number of stored (filled) elements in a sparse array. # Examples ```jldoctest -julia> A = speye(3) -3×3 SparseMatrixCSC{Float64,Int64} with 3 stored entries: - [1, 1] = 1.0 - [2, 2] = 1.0 - [3, 3] = 1.0 +julia> A = sparse(2I, 3, 3) +3×3 SparseMatrixCSC{Int64,Int64} with 3 stored entries: + [1, 1] = 2 + [2, 2] = 2 + [3, 3] = 2 julia> nnz(A) 3 @@ -83,17 +83,17 @@ modifications to the returned vector will mutate `A` as well. See # Examples ```jldoctest -julia> A = speye(3) -3×3 SparseMatrixCSC{Float64,Int64} with 3 stored entries: - [1, 1] = 1.0 - [2, 2] = 1.0 - [3, 3] = 1.0 +julia> A = sparse(2I, 3, 3) +3×3 SparseMatrixCSC{Int64,Int64} with 3 stored entries: + [1, 1] = 2 + [2, 2] = 2 + [3, 3] = 2 julia> nonzeros(A) -3-element Array{Float64,1}: - 1.0 - 1.0 - 1.0 +3-element Array{Int64,1}: + 2 + 2 + 2 ``` """ nonzeros(S::SparseMatrixCSC) = S.nzval @@ -108,11 +108,11 @@ nonzero values. See also [`nonzeros`](@ref) and [`nzrange`](@ref). # Examples ```jldoctest -julia> A = speye(3) -3×3 SparseMatrixCSC{Float64,Int64} with 3 stored entries: - [1, 1] = 1.0 - [2, 2] = 1.0 - [3, 3] = 1.0 +julia> A = sparse(2I, 3, 3) +3×3 SparseMatrixCSC{Int64,Int64} with 3 stored entries: + [1, 1] = 2 + [2, 2] = 2 + [3, 3] = 2 julia> rowvals(A) 3-element Array{Int64,1}: @@ -1453,8 +1453,6 @@ julia> spones(A) [3, 3] = 1.0 [2, 4] = 1.0 ``` - -Note the difference from [`speye`](@ref). """ spones(S::SparseMatrixCSC{T}) where {T} = SparseMatrixCSC(S.m, S.n, copy(S.colptr), copy(S.rowval), ones(T, S.colptr[end]-1)) @@ -1487,55 +1485,11 @@ function spzeros(::Type{Tv}, ::Type{Ti}, sz::Tuple{Integer,Integer}) where {Tv, spzeros(Tv, Ti, sz[1], sz[2]) end -speye(n::Integer) = speye(Float64, n) -speye(::Type{T}, n::Integer) where {T} = speye(T, n, n) -speye(m::Integer, n::Integer) = speye(Float64, m, n) - -""" - speye(S) - -Create a sparse identity matrix with the same size as `S`. - -# Examples -```jldoctest -julia> A = sparse([1,2,3,4],[2,4,3,1],[5.,4.,3.,2.]) -4×4 SparseMatrixCSC{Float64,Int64} with 4 stored entries: - [4, 1] = 2.0 - [1, 2] = 5.0 - [3, 3] = 3.0 - [2, 4] = 4.0 - -julia> speye(A) -4×4 SparseMatrixCSC{Float64,Int64} with 4 stored entries: - [1, 1] = 1.0 - [2, 2] = 1.0 - [3, 3] = 1.0 - [4, 4] = 1.0 -``` - -Note the difference from [`spones`](@ref). -""" -speye(S::SparseMatrixCSC{T}) where {T} = speye(T, size(S, 1), size(S, 2)) -eye(S::SparseMatrixCSC) = speye(S) - -""" - speye([type,]m[,n]) - -Create a sparse identity matrix of size `m x m`. When `n` is supplied, -create a sparse identity matrix of size `m x n`. The type defaults to [`Float64`](@ref) -if not specified. - -`sparse(I, m, n)` is equivalent to `speye(Int, m, n)`, and -`sparse(α*I, m, n)` can be used to efficiently create a sparse -multiple `α` of the identity matrix. -""" -speye(::Type{T}, m::Integer, n::Integer) where {T} = SparseMatrixCSC{T}(UniformScaling(one(T)), Dims((m, n))) -sparse(s::UniformScaling, m::Integer, n::Integer=m) = SparseMatrixCSC(s, Dims((m, n))) +eye(S::SparseMatrixCSC{T}) where {T} = SparseMatrixCSC{T}(I, size(S)) function one(S::SparseMatrixCSC{T}) where T - m,n = size(S) - if m != n; throw(DimensionMismatch("multiplicative identity only defined for square matrices")); end - speye(T, m) + S.m == S.n || throw(DimensionMismatch("multiplicative identity only defined for square matrices")) + return SparseMatrixCSC{T}(I, S.m, S.n) end ## SparseMatrixCSC construction from UniformScaling @@ -1573,6 +1527,7 @@ function Base.isone(A::SparseMatrixCSC) return true end +sparse(s::UniformScaling, m::Integer, n::Integer=m) = SparseMatrixCSC(s, m, n) # TODO: More appropriate location? conj!(A::SparseMatrixCSC) = (@inbounds broadcast!(conj, A.nzval, A.nzval); A) @@ -3141,13 +3096,13 @@ Concatenate matrices block-diagonally. Currently only implemented for sparse mat # Examples ```jldoctest -julia> blkdiag(speye(3), 2*speye(2)) -5×5 SparseMatrixCSC{Float64,Int64} with 5 stored entries: - [1, 1] = 1.0 - [2, 2] = 1.0 - [3, 3] = 1.0 - [4, 4] = 2.0 - [5, 5] = 2.0 +julia> blkdiag(sparse(2I, 3, 3), sparse(4I, 2, 2)) +5×5 SparseMatrixCSC{Int64,Int64} with 5 stored entries: + [1, 1] = 2 + [2, 2] = 2 + [3, 3] = 2 + [4, 4] = 4 + [5, 5] = 4 ``` """ function blkdiag(X::SparseMatrixCSC...) @@ -3599,6 +3554,6 @@ end ## Uniform matrix arithmetic -(+)(A::SparseMatrixCSC, J::UniformScaling) = A + J.λ * speye(A) -(-)(A::SparseMatrixCSC, J::UniformScaling) = A - J.λ * speye(A) -(-)(J::UniformScaling, A::SparseMatrixCSC) = J.λ * speye(A) - A +(+)(A::SparseMatrixCSC, J::UniformScaling) = A + sparse(J, size(A)...) +(-)(A::SparseMatrixCSC, J::UniformScaling) = A - sparse(J, size(A)...) +(-)(J::UniformScaling, A::SparseMatrixCSC) = sparse(J, size(A)...) - A diff --git a/doc/src/manual/arrays.md b/doc/src/manual/arrays.md index 0bc4a53a53f8fb..916b8695c5099f 100644 --- a/doc/src/manual/arrays.md +++ b/doc/src/manual/arrays.md @@ -781,19 +781,13 @@ stored zeros. (See [Sparse Matrix Storage](@ref man-csc).). ### Sparse Vector and Matrix Constructors -The simplest way to create sparse arrays is to use functions equivalent to the [`zeros`](@ref) -and [`eye`](@ref) functions that Julia provides for working with dense arrays. To produce -sparse arrays instead, you can use the same names with an `sp` prefix: +The simplest way to create a sparse array is to use a function equivalent to the [`zeros`](@ref) +function that Julia provides for working with dense arrays. To produce a +sparse array instead, you can use the same name with an `sp` prefix: ```jldoctest julia> spzeros(3) 3-element SparseVector{Float64,Int64} with 0 stored entries - -julia> speye(3,5) -3×5 SparseMatrixCSC{Float64,Int64} with 3 stored entries: - [1, 1] = 1.0 - [2, 2] = 1.0 - [3, 3] = 1.0 ``` The [`sparse`](@ref) function is often a handy way to construct sparse arrays. For @@ -867,7 +861,7 @@ You can go in the other direction using the [`Array`](@ref) constructor. The [`i function can be used to query if a matrix is sparse. ```jldoctest -julia> issparse(speye(5)) +julia> issparse(spzeros(5)) true ``` @@ -895,7 +889,7 @@ section of the standard library reference. |:-------------------------- |:---------------------- |:--------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [`spzeros(m,n)`](@ref) | [`zeros(m,n)`](@ref) | Creates a *m*-by-*n* matrix of zeros. ([`spzeros(m,n)`](@ref) is empty.) | | [`spones(S)`](@ref) | [`ones(m,n)`](@ref) | Creates a matrix filled with ones. Unlike the dense version, [`spones`](@ref) has the same sparsity pattern as *S*. | -| [`speye(n)`](@ref) | [`eye(n)`](@ref) | Creates a *n*-by-*n* identity matrix. | +| [`sparse(I, n, n)`](@ref) | [`eye(n)`](@ref) | Creates a *n*-by-*n* identity matrix. | | [`Array(S)`](@ref) | [`sparse(A)`](@ref) | Interconverts between dense and sparse formats. | | [`sprand(m,n,d)`](@ref) | [`rand(m,n)`](@ref) | Creates a *m*-by-*n* random matrix (of density *d*) with iid non-zero elements distributed uniformly on the half-open interval ``[0, 1)``. | | [`sprandn(m,n,d)`](@ref) | [`randn(m,n)`](@ref) | Creates a *m*-by-*n* random matrix (of density *d*) with iid non-zero elements distributed according to the standard normal (Gaussian) distribution. | diff --git a/doc/src/stdlib/arrays.md b/doc/src/stdlib/arrays.md index 0073510805c5f7..03669888f18f93 100644 --- a/doc/src/stdlib/arrays.md +++ b/doc/src/stdlib/arrays.md @@ -188,8 +188,6 @@ Base.SparseArrays.issparse Base.SparseArrays.nnz Base.SparseArrays.spzeros Base.SparseArrays.spones -Base.SparseArrays.speye(::Type, ::Integer, ::Integer) -Base.SparseArrays.speye(::SparseMatrixCSC) Base.SparseArrays.spdiagm Base.SparseArrays.sprand Base.SparseArrays.sprandn diff --git a/test/arrayops.jl b/test/arrayops.jl index 7fb1ef21890f3e..89aeb76bf3201c 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1008,7 +1008,7 @@ end @test m[1,2] == ([2,4],) # issue #21123 - @test mapslices(nnz, speye(3), 1) == [1 1 1] + @test mapslices(nnz, sparse(1.0I, 3, 3), 1) == [1 1 1] end @testset "single multidimensional index" begin diff --git a/test/hashing.jl b/test/hashing.jl index e2f277ab5b61ce..703dcb9a412532 100644 --- a/test/hashing.jl +++ b/test/hashing.jl @@ -73,7 +73,7 @@ vals = Any[ Dict(x => x for x in 1:10), Dict(7=>7,9=>9,4=>4,10=>10,2=>2,3=>3,8=>8,5=>5,6=>6,1=>1), [], [1], [2], [1, 1], [1, 2], [1, 3], [2, 2], [1, 2, 2], [1, 3, 3], - zeros(2, 2), spzeros(2, 2), eye(2, 2), speye(2, 2), + zeros(2, 2), spzeros(2, 2), eye(2, 2), sparse(1.0I, 2, 2), sparse(ones(2, 2)), ones(2, 2), sparse([0 0; 1 0]), [0 0; 1 0], [-0. 0; -0. 0.], SparseMatrixCSC(2, 2, [1, 3, 3], [1, 2], [-0., -0.]) ] diff --git a/test/linalg/arnoldi.jl b/test/linalg/arnoldi.jl index e6950f32255d44..7ec173bcd018be 100644 --- a/test/linalg/arnoldi.jl +++ b/test/linalg/arnoldi.jl @@ -178,7 +178,7 @@ let # Adjust the tolerance a bit since matrices with repeated eigenvalues # can be very stressful to ARPACK and this may therefore fail with # info = 3 if the tolerance is too small - @test eigs(speye(50), nev=10, tol = 5e-16)[1] ≈ ones(10) #Issue 4246 + @test eigs(sparse(1.0I, 50, 50), nev=10, tol = 5e-16)[1] ≈ ones(10) #Issue 4246 end @testset "real svds" begin diff --git a/test/linalg/special.jl b/test/linalg/special.jl index bc1cfb0f34bafa..295882508eaf82 100644 --- a/test/linalg/special.jl +++ b/test/linalg/special.jl @@ -178,7 +178,7 @@ end annotations = testfull ? (triannotations..., symannotations...) : (LowerTriangular, Symmetric) # Concatenations involving these types, un/annotated, should yield sparse arrays spvec = spzeros(N) - spmat = speye(N) + spmat = sparse(1.0I, N, N) diagmat = Diagonal(ones(N)) bidiagmat = Bidiagonal(ones(N), ones(N-1), :U) tridiagmat = Tridiagonal(ones(N-1), ones(N), ones(N-1)) diff --git a/test/linalg/uniformscaling.jl b/test/linalg/uniformscaling.jl index c506f7c7ef5e70..82833b19f9fb73 100644 --- a/test/linalg/uniformscaling.jl +++ b/test/linalg/uniformscaling.jl @@ -82,7 +82,7 @@ let @test B + I == B + eye(B) @test I + B == B + eye(B) AA = randn(2, 2) - for SS in (sprandn(3,3, 0.5), speye(Int, 3)) + for SS in (sprandn(3,3, 0.5), sparse(Int(1)I, 3, 3)) for (A, S) in ((AA, SS), (view(AA, 1:2, 1:2), view(SS, 1:3, 1:3))) @test @inferred(A + I) == A + eye(A) @test @inferred(I + A) == A + eye(A) diff --git a/test/perf/kernel/getdivgrad.jl b/test/perf/kernel/getdivgrad.jl index 04ba5663d8ab6a..d0db3b0eca6a9d 100644 --- a/test/perf/kernel/getdivgrad.jl +++ b/test/perf/kernel/getdivgrad.jl @@ -5,9 +5,9 @@ #----------------- Get the A matrix function getDivGrad(n1,n2,n3) # the Divergence - D1 = kron(speye(n3),kron(speye(n2),ddx(n1))) - D2 = kron(speye(n3),kron(ddx(n2),speye(n1))) - D3 = kron(ddx(n3),kron(speye(n2),speye(n1))) + D1 = kron(sparse(1.0I, n3, n3), kron(sparse(1.0I, n2), ddx(n1))) + D2 = kron(sparse(1.0I, n3, n3), kron(ddx(n2), sparse(1.0I, n1, n1))) + D3 = kron(ddx(n3), kron(sparse(1.0I, n2, n2), sparse(1.0I, n1, n1))) # DIV from faces to cell-centers Div = [D1 D2 D3] diff --git a/test/perf/sparse/fem.jl b/test/perf/sparse/fem.jl index 80559921c9eba1..63940cdf1cf9d5 100644 --- a/test/perf/sparse/fem.jl +++ b/test/perf/sparse/fem.jl @@ -7,7 +7,7 @@ function fdlaplacian(N) # create a 1D laplacian and a sparse identity fdl1 = spdiagm(-1 => ones(N-1), 0 => -2*ones(N), 1 => ones(N-1)) # laplace operator on the full grid - return kron(speye(N), fdl1) + kron(fdl1, speye(N)) + return kron(sparse(1.0I, N, N), fdl1) + kron(fdl1, sparse(1.0I, N, N)) end # get the list of boundary dof-indices diff --git a/test/show.jl b/test/show.jl index a3075d722ba16f..97aba3140b2ff3 100644 --- a/test/show.jl +++ b/test/show.jl @@ -492,7 +492,7 @@ end # issue #12960 mutable struct T12960 end let - A = speye(3) + A = sparse(1.0I, 3, 3) B = similar(A, T12960) @test sprint(show, B) == "\n [1, 1] = #undef\n [2, 2] = #undef\n [3, 3] = #undef" @test sprint(print, B) == "\n [1, 1] = #undef\n [2, 2] = #undef\n [3, 3] = #undef" diff --git a/test/sparse/cholmod.jl b/test/sparse/cholmod.jl index 256732c71c3083..410d54090e8d1a 100644 --- a/test/sparse/cholmod.jl +++ b/test/sparse/cholmod.jl @@ -188,7 +188,7 @@ end end @testset "Issue #9915" begin - @test speye(2)\speye(2) == eye(2) + @test sparse(1.0I, 2, 2) \ sparse(1.0I, 2, 2) == eye(2) end @testset "test Sparse constructor Symmetric and Hermitian input (and issymmetric and ishermitian)" begin @@ -660,12 +660,12 @@ end end @testset "Further issue with promotion #14894" begin - @test cholfact(speye(Float16, 5))\ones(5) == ones(5) - @test cholfact(Symmetric(speye(Float16, 5)))\ones(5) == ones(5) - @test cholfact(Hermitian(speye(Complex{Float16}, 5)))\ones(5) == ones(Complex{Float64}, 5) - @test_throws MethodError cholfact(speye(BigFloat, 5)) - @test_throws MethodError cholfact(Symmetric(speye(BigFloat, 5))) - @test_throws MethodError cholfact(Hermitian(speye(Complex{BigFloat}, 5))) + @test cholfact(sparse(Float16(1)I, 5, 5))\ones(5) == ones(5) + @test cholfact(Symmetric(sparse(Float16(1)I, 5, 5)))\ones(5) == ones(5) + @test cholfact(Hermitian(sparse(Complex{Float16}(1)I, 5, 5)))\ones(5) == ones(Complex{Float64}, 5) + @test_throws MethodError cholfact(sprase(BigFloat(1)I, 5, 5)) + @test_throws MethodError cholfact(Symmetric(sparse(BigFloat(1)I, 5, 5))) + @test_throws MethodError cholfact(Hermitian(sparse(Complex{BigFloat}(1)I, 5, 5))) end @testset "test \\ for Factor and StridedVecOrMat" begin @@ -684,7 +684,7 @@ end @testset "Make sure that ldltfact performs an LDLt (Issue #19032)" begin m, n = 400, 500 A = sprandn(m, n, .2) - M = [speye(n) A'; A -speye(m)] + M = [I A'; A -I] b = M * ones(m + n) F = ldltfact(M) s = unsafe_load(pointer(F)) @@ -730,11 +730,12 @@ end end @testset "sparse right multiplication of Symmetric and Hermitian matrices #21431" begin - @test issparse(speye(2)*speye(2)*speye(2)) + S = sparse(1.0I, 2, 2) + @test issparse(S*S*S) for T in (Symmetric, Hermitian) - @test issparse(speye(2)*T(speye(2))*speye(2)) - @test issparse(speye(2)*(T(speye(2))*speye(2))) - @test issparse((speye(2)*T(speye(2)))*speye(2)) + @test issparse(S*T(S)*S) + @test issparse(S*(T(S)*S)) + @test issparse((S*T(S))*S) end end @@ -780,7 +781,7 @@ end @testset "Issue #22335" begin local A, F - A = speye(3) + A = sparse(1.0I, 3, 3) @test LinAlg.issuccess(cholfact(A)) A[3, 3] = -1 F = cholfact(A) diff --git a/test/sparse/higherorderfns.jl b/test/sparse/higherorderfns.jl index 4eddb17ed1605e..3e716a95bbd809 100644 --- a/test/sparse/higherorderfns.jl +++ b/test/sparse/higherorderfns.jl @@ -269,7 +269,7 @@ end @testset "sparse map/broadcast with result eltype not a concrete subtype of Number (#19561/#19589)" begin N = 4 - A, fA = speye(N), eye(N) + A, fA = sparse(1.0I, N, N), eye(N) B, fB = spzeros(1, N), zeros(1, N) intorfloat_zeropres(xs...) = all(iszero, xs) ? zero(Float64) : Int(1) stringorfloat_zeropres(xs...) = all(iszero, xs) ? zero(Float64) : "hello" diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index 29a611454affaf..769c88f66a0900 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -52,7 +52,7 @@ end @test SparseMatrixCSC{Float64,Int32}(0I, 3, 3)::SparseMatrixCSC{Float64,Int32} == spzeros(Float64, Int32, 3, 3) end -se33 = speye(3) +se33 = SparseMatrixCSC{Float64}(I, 3, 3) do33 = ones(3) @testset "sparse binary operations" begin @@ -75,7 +75,7 @@ do33 = ones(3) end @testset "concatenation tests" begin - sp33 = speye(3, 3) + sp33 = sparse(1.0I, 3, 3) @testset "horizontal concatenation" begin @test all([se33 se33] == sparse([1, 2, 3, 1, 2, 3], [1, 2, 3, 4, 5, 6], ones(6))) @@ -89,11 +89,11 @@ end @test length(([sp33; 0I]).nzval) == 3 end - se44 = speye(4) + se44 = sparse(1.0I, 4, 4) sz42 = spzeros(4, 2) sz41 = spzeros(4, 1) sz34 = spzeros(3, 4) - se77 = speye(7) + se77 = sparse(1.0I, 7, 7) @testset "h+v concatenation" begin @test all([se44 sz42 sz41; sz34 se33] == se77) @test length(([sp33 0I; 1I 0I]).nzval) == 6 @@ -105,7 +105,7 @@ end @testset "concatenation promotion" begin sz41_f32 = spzeros(Float32, 4, 1) - se33_i32 = speye(Int32, 3, 3) + se33_i32 = sparse(Int32(1)I, 3, 3) @test all([se44 sz42 sz41_f32; sz34 se33_i32] == se77) end @@ -181,7 +181,7 @@ end @testset "complex matrix-vector multiplication and left-division" begin if Base.USE_GPL_LIBS for i = 1:5 - a = speye(5) + 0.1*sprandn(5, 5, 0.2) + a = I + 0.1*sprandn(5, 5, 0.2) b = randn(5,3) + im*randn(5,3) c = randn(5) + im*randn(5) d = randn(5) + im*randn(5) @@ -204,7 +204,7 @@ end @test_throws DimensionMismatch α*a.'*c + β*c @test_throws DimensionMismatch α*a.'*ones(5) + β*c - a = speye(5) + 0.1*sprandn(5, 5, 0.2) + 0.1*im*sprandn(5, 5, 0.2) + a = I + 0.1*sprandn(5, 5, 0.2) + 0.1*im*sprandn(5, 5, 0.2) b = randn(5,3) @test (maximum(abs.(a*b - Array(a)*b)) < 100*eps()) @test (maximum(abs.(a'b - Array(a)'b)) < 100*eps()) @@ -213,7 +213,7 @@ end @test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps()) @test (maximum(abs.(a.'\b - Array(a.')\b)) < 1000*eps()) - a = speye(5) + tril(0.1*sprandn(5, 5, 0.2)) + a = I + tril(0.1*sprandn(5, 5, 0.2)) b = randn(5,3) + im*randn(5,3) @test (maximum(abs.(a*b - Array(a)*b)) < 100*eps()) @test (maximum(abs.(a'b - Array(a)'b)) < 100*eps()) @@ -222,7 +222,7 @@ end @test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps()) @test (maximum(abs.(a.'\b - Array(a.')\b)) < 1000*eps()) - a = speye(5) + tril(0.1*sprandn(5, 5, 0.2) + 0.1*im*sprandn(5, 5, 0.2)) + a = I + tril(0.1*sprandn(5, 5, 0.2) + 0.1*im*sprandn(5, 5, 0.2)) b = randn(5,3) @test (maximum(abs.(a*b - Array(a)*b)) < 100*eps()) @test (maximum(abs.(a'b - Array(a)'b)) < 100*eps()) @@ -231,7 +231,7 @@ end @test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps()) @test (maximum(abs.(a.'\b - Array(a.')\b)) < 1000*eps()) - a = speye(5) + triu(0.1*sprandn(5, 5, 0.2)) + a = I + triu(0.1*sprandn(5, 5, 0.2)) b = randn(5,3) + im*randn(5,3) @test (maximum(abs.(a*b - Array(a)*b)) < 100*eps()) @test (maximum(abs.(a'b - Array(a)'b)) < 100*eps()) @@ -240,7 +240,7 @@ end @test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps()) @test (maximum(abs.(a.'\b - Array(a.')\b)) < 1000*eps()) - a = speye(5) + triu(0.1*sprandn(5, 5, 0.2) + 0.1*im*sprandn(5, 5, 0.2)) + a = I + triu(0.1*sprandn(5, 5, 0.2) + 0.1*im*sprandn(5, 5, 0.2)) b = randn(5,3) @test (maximum(abs.(a*b - Array(a)*b)) < 100*eps()) @test (maximum(abs.(a'b - Array(a)'b)) < 100*eps()) @@ -249,7 +249,7 @@ end @test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps()) @test (maximum(abs.(a.'\b - Array(a.')\b)) < 1000*eps()) - a = speye(5) + triu(0.1*sprandn(5, 5, 0.2)) + a = I + triu(0.1*sprandn(5, 5, 0.2)) b = randn(5,3) + im*randn(5,3) @test (maximum(abs.(a*b - Array(a)*b)) < 100*eps()) @test (maximum(abs.(a'b - Array(a)'b)) < 100*eps()) @@ -259,10 +259,10 @@ end @test (maximum(abs.(a.'\b - Array(a.')\b)) < 1000*eps()) # UpperTriangular/LowerTriangular solve - a = UpperTriangular(speye(5) + triu(0.1*sprandn(5, 5, 0.2))) + a = UpperTriangular(I + triu(0.1*sprandn(5, 5, 0.2))) b = sprandn(5, 5, 0.2) @test (maximum(abs.(a\b - Array(a)\Array(b))) < 1000*eps()) - a = LowerTriangular(speye(5) + tril(0.1*sprandn(5, 5, 0.2))) + a = LowerTriangular(I + tril(0.1*sprandn(5, 5, 0.2))) b = sprandn(5, 5, 0.2) @test (maximum(abs.(a\b - Array(a)\Array(b))) < 1000*eps()) @@ -523,7 +523,7 @@ end end @testset "issue described in https://groups.google.com/d/msg/julia-users/Yq4dh8NOWBQ/GU57L90FZ3EJ" begin - A = speye(Bool, 5) + A = sparse(I, 5, 5) @test find(A) == find(x -> x == true, A) == find(Array(A)) end @@ -838,66 +838,68 @@ end @test count(!iszero, A) == nA @test A == B - A = speye(Int, 5) - I=1:10 + A = sparse(Int(1)I, 5, 5) + lininds = 1:10 X=reshape([trues(10); falses(15)],5,5) - @test A[I] == A[X] == [1,0,0,0,0,0,1,0,0,0] - A[I] = [1:10;] - @test A[I] == A[X] == collect(1:10) - A[I] = zeros(Int, 10) + @test A[lininds] == A[X] == [1,0,0,0,0,0,1,0,0,0] + A[lininds] = [1:10;] + @test A[lininds] == A[X] == collect(1:10) + A[lininds] = zeros(Int, 10) @test nnz(A) == 13 @test count(!iszero, A) == 3 - @test A[I] == A[X] == zeros(Int, 10) + @test A[lininds] == A[X] == zeros(Int, 10) c = collect(11:20); c[1] = c[3] = 0 - A[I] = c + A[lininds] = c @test nnz(A) == 13 @test count(!iszero, A) == 11 - @test A[I] == A[X] == c - A = speye(Int, 5) - A[I] = c + @test A[lininds] == A[X] == c + A = sparse(Int(1)I, 5) + A[lininds] = c @test nnz(A) == 12 @test count(!iszero, A) == 11 - @test A[I] == A[X] == c - - S = sprand(50, 30, 0.5, x -> round.(Int, rand(x) * 100)) - I = sprand(Bool, 50, 30, 0.2) - FS = Array(S) - FI = Array(I) - @test sparse(FS[FI]) == S[I] == S[FI] - @test sum(S[FI]) + sum(S[.!FI]) == sum(S) - @test count(!iszero, I) == count(I) - - sumS1 = sum(S) - sumFI = sum(S[FI]) - nnzS1 = nnz(S) - S[FI] = 0 - sumS2 = sum(S) - cnzS2 = count(!iszero, S) - @test sum(S[FI]) == 0 - @test nnz(S) == nnzS1 - @test (sum(S) + sumFI) == sumS1 - - S[FI] = 10 - nnzS3 = nnz(S) - @test sum(S) == sumS2 + 10*sum(FI) - S[FI] = 0 - @test sum(S) == sumS2 - @test nnz(S) == nnzS3 - @test count(!iszero, S) == cnzS2 - - S[FI] = [1:sum(FI);] - @test sum(S) == sumS2 + sum(1:sum(FI)) - - S = sprand(50, 30, 0.5, x -> round.(Int, rand(x) * 100)) - N = length(S) >> 2 - I = randperm(N) .* 4 - J = randperm(N) - sumS1 = sum(S) - sumS2 = sum(S[I]) - S[I] = 0 - @test sum(S) == (sumS1 - sumS2) - S[I] = J - @test sum(S) == (sumS1 - sumS2 + sum(J)) + @test A[lininds] == A[X] == c + + let # prevent assignment to I from overwriting UniformSampling in enclosing scope + S = sprand(50, 30, 0.5, x -> round.(Int, rand(x) * 100)) + I = sprand(Bool, 50, 30, 0.2) + FS = Array(S) + FI = Array(I) + @test sparse(FS[FI]) == S[I] == S[FI] + @test sum(S[FI]) + sum(S[.!FI]) == sum(S) + @test count(!iszero, I) == count(I) + + sumS1 = sum(S) + sumFI = sum(S[FI]) + nnzS1 = nnz(S) + S[FI] = 0 + sumS2 = sum(S) + cnzS2 = count(!iszero, S) + @test sum(S[FI]) == 0 + @test nnz(S) == nnzS1 + @test (sum(S) + sumFI) == sumS1 + + S[FI] = 10 + nnzS3 = nnz(S) + @test sum(S) == sumS2 + 10*sum(FI) + S[FI] = 0 + @test sum(S) == sumS2 + @test nnz(S) == nnzS3 + @test count(!iszero, S) == cnzS2 + + S[FI] = [1:sum(FI);] + @test sum(S) == sumS2 + sum(1:sum(FI)) + + S = sprand(50, 30, 0.5, x -> round.(Int, rand(x) * 100)) + N = length(S) >> 2 + I = randperm(N) .* 4 + J = randperm(N) + sumS1 = sum(S) + sumS2 = sum(S[I]) + S[I] = 0 + @test sum(S) == (sumS1 - sumS2) + S[I] = J + @test sum(S) == (sumS1 - sumS2 + sum(J)) + end end @testset "dropstored!" begin @@ -1129,7 +1131,7 @@ for (tup, rval, rind) in [((1,), ["b"], [2])] end @testset "findn" begin - b = findn( speye(4) ) + b = findn( sparse(1.0I, 4, 4) ) @test (length(b[1]) == 4) @test (length(b[2]) == 4) end @@ -1148,7 +1150,7 @@ end #ensure we have preserved the correct dimensions! - a = speye(3,5) + a = sparse(1.0I, 3, 5) @test size(rot180(a)) == (3,5) @test size(rotr90(a)) == (5,3) @test size(rotl90(a)) == (5,3) @@ -1291,7 +1293,7 @@ end @test Array(sparse([])) == zeros(0) @test_throws BoundsError sparse([])[1] @test_throws BoundsError sparse([])[1] = 1 - x = speye(100) + x = sparse(1.0I, 100, 100) @test_throws BoundsError x[-10:10] end @@ -1303,7 +1305,7 @@ end @testset "issue #10411" begin for (m,n) in ((2,-2),(-2,2),(-2,-2)) @test_throws ArgumentError spzeros(m,n) - @test_throws ArgumentError speye(m,n) + @test_throws ArgumentError sparse(1.0I, m, n) @test_throws ArgumentError sprand(m,n,0.2) end end @@ -1350,16 +1352,15 @@ end @testset "sparse" begin local A = sparse(ones(5, 5)) @test sparse(A) == A - @test sparse([1:5;], [1:5;], 1) == speye(5) + @test sparse([1:5;], [1:5;], 1) == sparse(1.0I, 5, 5) end -@testset "speye and one" begin +@testset "sparse(I, ...) and one" begin local A = sparse(ones(5, 5)) - @test speye(A) == speye(5) - @test eye(A) == speye(5) - @test one(A) == speye(5) + @test sparse(I, 5, 5) == eye(A) + @test one(A) == eye(A) @test_throws DimensionMismatch one(sprand(5, 6, 0.2)) - @test eltype(speye(Real, 5, 5)) === Real + @test eltype(SparseMatrixCSC{Real}(I, 5, 5)) === Real end @testset "istriu/istril" begin @@ -1425,7 +1426,7 @@ end @testset "trace" begin @test_throws DimensionMismatch trace(sparse(ones(5,6))) - @test trace(speye(5)) == 5 + @test trace(sparse(1.0I, 5, 5)) == 5 end @testset "spdiagm" begin @@ -1465,7 +1466,7 @@ end end @testset "expandptr" begin - local A = speye(5) + local A = sparse(1.0I, 5, 5) @test Base.SparseArrays.expandptr(A.colptr) == collect(1:5) A[1,2] = 1 @test Base.SparseArrays.expandptr(A.colptr) == [1; 2; 2; 3; 4; 5] @@ -1506,7 +1507,7 @@ end @testset "ishermitian/issymmetric" begin local A # real matrices - A = speye(5,5) + A = sparse(1.0I, 5, 5) @test ishermitian(A) == true @test issymmetric(A) == true A[1,3] = 1.0 @@ -1517,14 +1518,14 @@ end @test issymmetric(A) == true # complex matrices - A = speye(5,5) + im*speye(5,5) + A = sparse((1.0 + 1.0im)I, 5, 5) @test ishermitian(A) == false @test issymmetric(A) == true A[1,4] = 1.0 + im @test ishermitian(A) == false @test issymmetric(A) == false - A = speye(Complex128, 5,5) + A = sparse(Complex128(1)I, 5, 5) A[3,2] = 1.0 + im @test ishermitian(A) == false @test issymmetric(A) == false @@ -1537,7 +1538,7 @@ end @test issymmetric(A) == true # explicit zeros - A = speye(Complex128, 5,5) + A = sparse(Complex128(1)I, 5, 5) A[3,1] = 2 A.nzval[2] = 0.0 @test ishermitian(A) == true @@ -1563,8 +1564,8 @@ end end @testset "equality ==" begin - A1 = speye(10) - A2 = speye(10) + A1 = sparse(1.0I, 10, 10) + A2 = sparse(1.0I, 10, 10) nonzeros(A1)[end]=0 @test A1!=A2 nonzeros(A1)[end]=1 @@ -1642,7 +1643,7 @@ end # make certain entries in nzval beyond # the range specified in colptr do not # impact vecnorm of a sparse matrix - foo = speye(4) + foo = sparse(1.0I, 4, 4) resize!(foo.nzval, 5) setindex!(foo.nzval, NaN, 5) @test vecnorm(foo) == 2.0 @@ -1719,7 +1720,7 @@ end end @testset "spones" begin - local A = 2. * speye(5, 5) + local A = sparse(2.0I, 5, 5) @test Array(spones(A)) == eye(Array(A)) end @@ -1896,7 +1897,7 @@ end end @testset "issue #14398" begin - @test transpose(view(speye(10), 1:5, 1:5)) ≈ eye(5,5) + @test transpose(view(sparse(I, 10, 10), 1:5, 1:5)) ≈ eye(5) end @testset "dropstored issue #20513" begin @@ -2065,7 +2066,7 @@ end end @testset "similar with type conversion" begin - local A = speye(5) + local A = sparse(1.0I, 5, 5) @test size(similar(A, Complex128, Int)) == (5, 5) @test typeof(similar(A, Complex128, Int)) == SparseMatrixCSC{Complex128, Int} @test size(similar(A, Complex128, Int8)) == (5, 5) @@ -2075,7 +2076,7 @@ end end @testset "similar for SparseMatrixCSC" begin - local A = speye(5) + local A = sparse(1.0I, 5, 5) # test similar without specifications (preserves stored-entry structure) simA = similar(A) @test typeof(simA) == typeof(A) diff --git a/test/sparse/sparsevector.jl b/test/sparse/sparsevector.jl index c622a67bf83ef1..8e9e908eb075bf 100644 --- a/test/sparse/sparsevector.jl +++ b/test/sparse/sparsevector.jl @@ -904,9 +904,9 @@ end sparsecomplexvecs = SparseVector[SparseVector(m, sprvec.nzind, complex.(sprvec.nzval, sprvec.nzval)) for sprvec in sparsefloatvecs] sprmat = sprand(m, m, 0.2) - sparsefloatmat = speye(m) + sprmat/(2m) - sparsecomplexmat = speye(m) + SparseMatrixCSC(m, m, sprmat.colptr, sprmat.rowval, complex.(sprmat.nzval, sprmat.nzval)/(4m)) - sparseintmat = speye(Int, m)*10m + SparseMatrixCSC(m, m, sprmat.colptr, sprmat.rowval, round.(Int, sprmat.nzval*10)) + sparsefloatmat = I + sprmat/(2m) + sparsecomplexmat = I + SparseMatrixCSC(m, m, sprmat.colptr, sprmat.rowval, complex.(sprmat.nzval, sprmat.nzval)/(4m)) + sparseintmat = 10m*I + SparseMatrixCSC(m, m, sprmat.colptr, sprmat.rowval, round.(Int, sprmat.nzval*10)) denseintmat = I*10m + rand(1:m, m, m) densefloatmat = I + randn(m, m)/(2m) diff --git a/test/sparse/umfpack.jl b/test/sparse/umfpack.jl index a99dac1c92413c..fe4193f82cefb4 100644 --- a/test/sparse/umfpack.jl +++ b/test/sparse/umfpack.jl @@ -1,7 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license @testset "UMFPACK wrappers" begin - se33 = speye(3) + se33 = sparse(1.0I, 3, 3) do33 = ones(3) @test isequal(se33 \ do33, do33) @@ -98,7 +98,7 @@ end @testset "Issue #4523 - complex sparse \\" begin - x = speye(2) + im * speye(2) + x = sparse((1.0 + 1.0im)I, 2, 2) @test (x*(lufact(x) \ ones(2))) ≈ ones(2) @test det(sparse([1,3,3,1], [1,1,3,3], [1,1,1,1])) == 0 @@ -142,13 +142,13 @@ @testset "Test aliasing" begin a = rand(5) - @test_throws ArgumentError Base.SparseArrays.UMFPACK.solve!(a, lufact(speye(5,5)), a, Base.SparseArrays.UMFPACK.UMFPACK_A) + @test_throws ArgumentError Base.SparseArrays.UMFPACK.solve!(a, lufact(sparse(1.0I, 5, 5)), a, Base.SparseArrays.UMFPACK.UMFPACK_A) aa = complex(a) - @test_throws ArgumentError Base.SparseArrays.UMFPACK.solve!(aa, lufact(complex(speye(5,5))), aa, Base.SparseArrays.UMFPACK.UMFPACK_A) + @test_throws ArgumentError Base.SparseArrays.UMFPACK.solve!(aa, lufact(sparse((1.0im)I, 5, 5)), aa, Base.SparseArrays.UMFPACK.UMFPACK_A) end @testset "Issues #18246,18244 - lufact sparse pivot" begin - A = speye(4) + A = sparse(1.0I, 4, 4) A[1:2,1:2] = [-.01 -200; 200 .001] F = lufact(A) @test F[:p] == [3 ; 4 ; 2 ; 1] @@ -157,7 +157,7 @@ @testset "Test that A[c|t]_ldiv_B!{T<:Complex}(X::StridedMatrix{T}, lu::UmfpackLU{Float64}, B::StridedMatrix{T}) works as expected." begin N = 10 p = 0.5 - A = N*speye(N) + sprand(N, N, p) + A = N*I + sprand(N, N, p) X = zeros(Complex{Float64}, N, N) B = complex.(rand(N, N), rand(N, N)) luA, lufA = lufact(A), lufact(Array(A))