diff --git a/NEWS.md b/NEWS.md index 495bc1015ea93..ec9d542f38197 100644 --- a/NEWS.md +++ b/NEWS.md @@ -528,6 +528,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 9b417d90922c4..41445ee70b44e 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -2076,6 +2076,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 cc4e01f647709..cdfc50f9a272f 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 84e7e3ed29790..eed1f18fb545c 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1255,7 +1255,6 @@ export sparse, sparsevec, spdiagm, - speye, spones, sprand, sprandn, diff --git a/base/linalg/arnoldi.jl b/base/linalg/arnoldi.jl index 70e696c83eb66..fe05312d94f27 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 e68417cad7a36..bf83ad849c79c 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 4bffa183a8fbb..af3b16bc873dc 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 27818df9d14e9..cee0f6d56f839 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) = 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 @@ -1568,6 +1522,7 @@ function Base.isone(A::SparseMatrixCSC) return true end +sparse(s::UniformScaling, m::Integer, n::Integer) = SparseMatrixCSC(s, Dims((m, n))) # TODO: More appropriate location? conj!(A::SparseMatrixCSC) = (@inbounds broadcast!(conj, A.nzval, A.nzval); A) @@ -3136,13 +3091,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...) @@ -3594,6 +3549,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 0bc4a53a53f8f..916b8695c5099 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 d00334b3cf1a0..d5f0ae3fef876 100644 --- a/doc/src/stdlib/arrays.md +++ b/doc/src/stdlib/arrays.md @@ -187,8 +187,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 a78bffc27d3a4..428d7b6859750 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1017,7 +1017,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 e2f277ab5b61c..703dcb9a41253 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 e6950f32255d4..7ec173bcd018b 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 bc1cfb0f34baf..295882508eaf8 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 6184b7a10f995..6e6fb23d598f5 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 04ba5663d8ab6..d0db3b0eca6a9 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 80559921c9eba..63940cdf1cf9d 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 71a9577899615..ddcafdb872500 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 f3aa5fe341049..fab2d33e1a47f 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(sparse(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)) @@ -738,11 +738,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 @@ -788,7 +789,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 4eddb17ed1605..3e716a95bbd80 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 fa189d41e38a2..bdce9ef415a2b 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -49,7 +49,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 @@ -72,7 +72,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))) @@ -86,11 +86,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 @@ -102,7 +102,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 @@ -178,7 +178,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) @@ -201,7 +201,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()) @@ -210,7 +210,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()) @@ -219,7 +219,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()) @@ -228,7 +228,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()) @@ -237,7 +237,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()) @@ -246,7 +246,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()) @@ -256,12 +256,12 @@ 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()) # test error throwing for bwdTrisolve @test_throws DimensionMismatch a\Matrix{Float64}(6, 6) - 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()) # test error throwing for fwdTrisolve @@ -526,7 +526,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 @@ -848,66 +848,68 @@ end @test count(!iszero, A) == nA @test A == B - A = speye(Int, 5) - I=1:10 + A = sparse(1I, 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(1I, 5, 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 @@ -1139,7 +1141,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 @@ -1158,7 +1160,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) @@ -1301,7 +1303,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 @@ -1313,7 +1315,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 @@ -1360,16 +1362,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 @@ -1435,7 +1436,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 @@ -1475,7 +1476,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] @@ -1516,7 +1517,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 @@ -1527,14 +1528,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 @@ -1547,7 +1548,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 @@ -1573,8 +1574,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 @@ -1652,7 +1653,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 @@ -1729,7 +1730,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 @@ -1906,7 +1907,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 @@ -2075,7 +2076,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) @@ -2085,7 +2086,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 c61654377fcb6..cbb0a97d18448 100644 --- a/test/sparse/sparsevector.jl +++ b/test/sparse/sparsevector.jl @@ -922,9 +922,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 a99dac1c92413..fe4193f82cefb 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))