diff --git a/base/deprecated.jl b/base/deprecated.jl index 2236f3c8081d76..3a110cfbbf8bfb 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -2056,6 +2056,13 @@ end # deprecate bits to bitstring (#24263, #24281) @deprecate bits bitstring +# deprecate speye +@deprecate speye(n::Integer) sparse(1.0I, n) +@deprecate speye(m::Integer, n::Integer) sparse(1.0I, m, n) +@deprecate speye(::Type{T}, n::Integer) where {T} sparse(UniformScaling(one(T)), n) +@deprecate speye(::Type{T}, m::Integer, n::Integer) where {T} sparse(UniformScaling(one(T)), m, n) +@deprecate speye(S::SparseMatrixCSC{T}) where {T} sparse(UniformScaling(one(T)), size(S)...) + # issue #24167 @deprecate EnvHash EnvDict diff --git a/base/essentials.jl b/base/essentials.jl index cc4e01f647709c..b76783dd40bce2 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); julia> typeof(x) SparseMatrixCSC{Float64,Int64} diff --git a/base/exports.jl b/base/exports.jl index cbebc40c25be45..d96162a8154fd6 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1267,7 +1267,6 @@ export sparse, sparsevec, spdiagm, - speye, spones, sprand, sprandn, diff --git a/base/linalg/arnoldi.jl b/base/linalg/arnoldi.jl index 70e696c83eb669..7d575abb555ed6 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); 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 b9a4f05de6000d..831104f745e5b3 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(I, 3) +3×3 SparseMatrixCSC{Int64,Int64} with 3 stored entries: + [1, 1] = 1 + [2, 2] = 1 + [3, 3] = 1 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(I, 3) +3×3 SparseMatrixCSC{Int64,Int64} with 3 stored entries: + [1, 1] = 1 + [2, 2] = 1 + [3, 3] = 1 julia> nonzeros(A) -3-element Array{Float64,1}: - 1.0 - 1.0 - 1.0 +3-element Array{Int64,1}: + 1 + 1 + 1 ``` """ 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(I, 3) +3×3 SparseMatrixCSC{Int64,Int64} with 3 stored entries: + [1, 1] = 1 + [2, 2] = 1 + [3, 3] = 1 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,54 +1485,12 @@ 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} = speye_scaled(T, oneunit(T), m, n) +eye(S::SparseMatrixCSC{T}) where {T} = sparse(UniformScaling{T}(one(T)), 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) + speye_scaled(one(T), m, m) end speye_scaled(diag, m::Integer, n::Integer) = speye_scaled(typeof(diag), diag, m, n) @@ -3120,13 +3076,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(I, 3), sparse(2I, 2)) +5×5 SparseMatrixCSC{Int64,Int64} with 5 stored entries: + [1, 1] = 1 + [2, 2] = 1 + [3, 3] = 1 + [4, 4] = 2 + [5, 5] = 2 ``` """ function blkdiag(X::SparseMatrixCSC...) @@ -3578,6 +3534,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..d1ebf3d7c1d12f 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)`](@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..4c38a4dade0637 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), 1) == [1 1 1] end @testset "single multidimensional index" begin diff --git a/test/hashing.jl b/test/hashing.jl index e2f277ab5b61ce..dfae4514496136 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), 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 d8c4e032f42806..82d30898b799c8 100644 --- a/test/linalg/arnoldi.jl +++ b/test/linalg/arnoldi.jl @@ -163,7 +163,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), 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..cfe1bf9eeadc65 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) 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 d3ce6677b4be9f..2c4e2cb3b98e9e 100644 --- a/test/linalg/uniformscaling.jl +++ b/test/linalg/uniformscaling.jl @@ -81,7 +81,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)) 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..47615a2d963945 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), kron(sparse(1.0I, n2), ddx(n1))) + D2 = kron(sparse(1.0I, n3), kron(ddx(n2), sparse(1.0I, n1))) + D3 = kron(ddx(n3), kron(sparse(1.0I, n2), sparse(1.0I, 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..1634da4698b65c 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), fdl1) + kron(fdl1, sparse(1.0I, N)) end # get the list of boundary dof-indices diff --git a/test/show.jl b/test/show.jl index a3075d722ba16f..dd200810761712 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) 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 48744801989bea..b1bf3b7a3d59aa 100644 --- a/test/sparse/cholmod.jl +++ b/test/sparse/cholmod.jl @@ -178,7 +178,7 @@ let # Issue 9160 end # Issue #9915 -@test speye(2)\speye(2) == eye(2) +@test sparse(1.0I, 2)\sparse(1.0I, 2) == eye(2) # having `foo == I` would be nice # test eltype @test eltype(Dense(ones(3))) == Float64 @@ -634,12 +634,12 @@ Fnew = deserialize(b) @test SparseArrays.CHOLMOD.Dense(ones(Complex{Float32}, 5, 2)) == ones(5, 2) # Further issue with promotion #14894 -@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))\ones(5) == ones(5) +@test cholfact(Symmetric(sparse(Float16(1)I, 5)))\ones(5) == ones(5) +@test cholfact(Hermitian(sparse(Complex{Float16}(1)I, 5)))\ones(5) == ones(Complex{Float64}, 5) +@test_throws MethodError cholfact(sparse(BigFloat(1)I, 5)) +@test_throws MethodError cholfact(Symmetric(sparse(BigFloat(1)I, 5))) +@test_throws MethodError cholfact(Hermitian(sparse(Complex{BigFloat}(1)I, 5))) # test \ for Factor and StridedVecOrMat let x = rand(5), @@ -657,7 +657,7 @@ end # Make sure that ldltfact performs an LDLt (Issue #19032) let m = 400, n = 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)) @@ -702,11 +702,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) + @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 @@ -751,7 +752,7 @@ end @testset "Issue #22335" begin local A, F - A = speye(3) + A = sparse(1.0I, 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..cc718fb254a62e 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), 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 23689de033215d..32650c27c971e3 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -22,7 +22,7 @@ end @test sparse(Any[1,2,3], Any[1,2,3], Any[1,1,1], 5, 4) == sparse([1,2,3], [1,2,3], [1,1,1], 5, 4) end -se33 = speye(3) +se33 = sparse(1.0I, 3) do33 = ones(3) @testset "sparse binary operations" begin @@ -45,7 +45,7 @@ do33 = ones(3) end @testset "concatenation tests" begin - sp33 = speye(3, 3) + sp33 = sparse(1.0I, 3) @testset "horizontal concatenation" begin @test all([se33 se33] == sparse([1, 2, 3, 1, 2, 3], [1, 2, 3, 4, 5, 6], ones(6))) @@ -59,11 +59,11 @@ end @test length(([sp33; 0I]).nzval) == 3 end - se44 = speye(4) + se44 = sparse(1.0I, 4) sz42 = spzeros(4, 2) sz41 = spzeros(4, 1) sz34 = spzeros(3, 4) - se77 = speye(7) + se77 = sparse(1.0I, 7) @testset "h+v concatenation" begin @test all([se44 sz42 sz41; sz34 se33] == se77) @test length(([sp33 0I; 1I 0I]).nzval) == 6 @@ -75,7 +75,7 @@ end @testset "concatenation promotion" begin sz41_f32 = spzeros(Float32, 4, 1) - se33_i32 = speye(Int32, 3, 3) + se33_i32 = sparse(UniformScaling{Int32}(1), 3) # hm @test all([se44 sz42 sz41_f32; sz34 se33_i32] == se77) end @@ -151,7 +151,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) @@ -174,7 +174,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()) @@ -183,7 +183,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()) @@ -192,7 +192,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()) @@ -201,7 +201,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()) @@ -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) + 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()) @@ -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) + 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()) @@ -229,10 +229,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()) @@ -493,7 +493,7 @@ end end @testset "issue described in https://groups.google.com/d/msg/julia-users/Yq4dh8NOWBQ/GU57L90FZ3EJ" begin - A = speye(Bool, 5) + A = sparse(UniformScaling{Bool}(true), 5) # hm @test find(A) == find(x -> x == true, A) == find(Array(A)) end @@ -808,7 +808,7 @@ end @test count(!iszero, A) == nA @test A == B - A = speye(Int, 5) + A = sparse(Int(1)I, 5) I=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] @@ -823,7 +823,7 @@ end @test nnz(A) == 13 @test count(!iszero, A) == 11 @test A[I] == A[X] == c - A = speye(Int, 5) + A = sparse(Int(1)I, 5) A[I] = c @test nnz(A) == 12 @test count(!iszero, A) == 11 @@ -1099,7 +1099,7 @@ for (tup, rval, rind) in [((1,), ["b"], [2])] end @testset "findn" begin - b = findn( speye(4) ) + b = findn( sparse(1.0I, 4) ) @test (length(b[1]) == 4) @test (length(b[2]) == 4) end @@ -1118,7 +1118,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) @@ -1261,7 +1261,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) @test_throws BoundsError x[-10:10] end @@ -1273,7 +1273,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 @@ -1320,16 +1320,16 @@ 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) 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) == eye(A) + @test one(A) == eye(A) @test_throws DimensionMismatch one(sprand(5, 6, 0.2)) - @test eltype(speye(Real, 5, 5)) === Real + # presently: @test_throws MethodError SparseMatrixCSC{Real}(I, 5, 5) + # but want: @test eltype(SparseMatrixCSC{Real}(I, 5, 5)) === Real end @testset "istriu/istril" begin @@ -1395,7 +1395,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 end @testset "spdiagm" begin @@ -1435,7 +1435,7 @@ end end @testset "expandptr" begin - local A = speye(5) + local A = sparse(1.0I, 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] @@ -1476,7 +1476,7 @@ end @testset "ishermitian/issymmetric" begin local A # real matrices - A = speye(5,5) + A = sparse(1.0I, 5) @test ishermitian(A) == true @test issymmetric(A) == true A[1,3] = 1.0 @@ -1487,14 +1487,14 @@ end @test issymmetric(A) == true # complex matrices - A = speye(5,5) + im*speye(5,5) + A = sparse((1.0 + 1.0im)I, 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) A[3,2] = 1.0 + im @test ishermitian(A) == false @test issymmetric(A) == false @@ -1507,7 +1507,7 @@ end @test issymmetric(A) == true # explicit zeros - A = speye(Complex128, 5,5) + A = sparse(Complex128(1)I, 5) A[3,1] = 2 A.nzval[2] = 0.0 @test ishermitian(A) == true @@ -1533,8 +1533,8 @@ end end @testset "equality ==" begin - A1 = speye(10) - A2 = speye(10) + A1 = sparse(1.0I, 10) + A2 = sparse(1.0I, 10) nonzeros(A1)[end]=0 @test A1!=A2 nonzeros(A1)[end]=1 @@ -1612,7 +1612,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) resize!(foo.nzval, 5) setindex!(foo.nzval, NaN, 5) @test vecnorm(foo) == 2.0 @@ -1688,7 +1688,7 @@ end end @testset "spones" begin - local A = 2. * speye(5, 5) + local A = sparse(2.0I, 5) @test Array(spones(A)) == eye(Array(A)) end @@ -1865,7 +1865,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), 1:5, 1:5)) ≈ eye(5) # having `foo ≈ I` would be nice end @testset "dropstored issue #20513" begin @@ -2034,7 +2034,7 @@ end end @testset "similar with type conversion" begin - local A = speye(5) + local A = sparse(1.0I, 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) @@ -2044,7 +2044,7 @@ end end @testset "similar for SparseMatrixCSC" begin - local A = speye(5) + local A = sparse(1.0I, 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 f7f8bdb77bae7a..abc0fde0ccc67f 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 = eye(Int, m)*10m + rand(1:m, m, m) densefloatmat = eye(m) + randn(m, m)/(2m) diff --git a/test/sparse/umfpack.jl b/test/sparse/umfpack.jl index a99dac1c92413c..dd6906482e9664 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) 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) @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)), 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)), aa, Base.SparseArrays.UMFPACK.UMFPACK_A) end @testset "Issues #18246,18244 - lufact sparse pivot" begin - A = speye(4) + A = sparse(1.0I, 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))