diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 6fcddbfa153d0..eb81315bb6f8f 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -31,12 +31,12 @@ lengths of dimensions you asked for. # Examples ```jldoctest -julia> A = ones(2,3,4); +julia> A = fill(1, (2,3,4)); julia> size(A, 2) 3 -julia> size(A,3,2) +julia> size(A, 3, 2) (4, 3) ``` """ @@ -51,9 +51,9 @@ Return the valid range of indices for array `A` along dimension `d`. # Examples ```jldoctest -julia> A = ones(5,6,7); +julia> A = fill(1, (5,6,7)); -julia> axes(A,2) +julia> axes(A, 2) Base.OneTo(6) ``` """ @@ -69,7 +69,7 @@ Return the tuple of valid indices for array `A`. # Examples ```jldoctest -julia> A = ones(5,6,7); +julia> A = fill(1, (5,6,7)); julia> axes(A) (Base.OneTo(5), Base.OneTo(6), Base.OneTo(7)) @@ -104,7 +104,7 @@ exploit linear indexing. # Examples ```jldoctest -julia> A = ones(5,6,7); +julia> A = fill(1, (5,6,7)); julia> b = linearindices(A); @@ -131,7 +131,7 @@ Return the number of dimensions of `A`. # Examples ```jldoctest -julia> A = ones(3,4,5); +julia> A = fill(1, (3,4,5)); julia> ndims(A) 3 @@ -225,7 +225,7 @@ Return the distance in memory (in number of elements) between adjacent elements # Examples ```jldoctest -julia> A = ones(3,4,5); +julia> A = fill(1, (3,4,5)); julia> stride(A,2) 3 @@ -252,7 +252,7 @@ Return a tuple of the memory strides in each dimension. # Examples ```jldoctest -julia> A = ones(3,4,5); +julia> A = fill(1, (3,4,5)); julia> strides(A) (1, 3, 12) diff --git a/base/array.jl b/base/array.jl index 5a54e3de6eca6..fe6f45772d769 100644 --- a/base/array.jl +++ b/base/array.jl @@ -77,11 +77,11 @@ types. # Examples ```jldoctest -julia> eltype(ones(Float32,2,2)) +julia> eltype(fill(1f0, (2,2))) Float32 -julia> eltype(ones(Int8,2,2)) -Int8 +julia> eltype(fill(0x1, (2,2))) +UInt8 ``` """ eltype(::Type) = Any @@ -343,7 +343,7 @@ fill(v, dims::Integer...) = fill!(Array{typeof(v)}(uninitialized, dims...), v) zeros([T=Float64,] dims...) Create an `Array`, with element type `T`, of all zeros with size specified by `dims`. -See also [`ones`](@ref), [`similar`](@ref). +See also [`fill`](@ref), [`ones`](@ref). # Examples ```jldoctest @@ -363,7 +363,7 @@ function zeros end ones([T=Float64,] dims...) Create an `Array`, with element type `T`, of all ones with size specified by `dims`. -See also [`zeros`](@ref), [`similar`](@ref). +See also: [`fill`](@ref), [`zeros`](@ref). # Examples ```jldoctest diff --git a/base/bitarray.jl b/base/bitarray.jl index 93f4973f3ee56..6088e1c8909ea 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -1812,7 +1812,7 @@ function vcat(A::BitMatrix...) nrowsA = [size(a, 1) for a in A] Ac = [a.chunks for a in A] pos_d = 1 - pos_s = ones(Int, nargs) + pos_s = fill(1, nargs) for j = 1:ncols, k = 1:nargs copy_chunks!(Bc, pos_d, Ac[k], pos_s[k], nrowsA[k]) pos_s[k] += nrowsA[k] diff --git a/base/combinatorics.jl b/base/combinatorics.jl index 369832e28dd18..a90084b4ff4e0 100644 --- a/base/combinatorics.jl +++ b/base/combinatorics.jl @@ -239,7 +239,7 @@ function nextprod(a::Vector{Int}, x) throw(ArgumentError("unsafe for x > typemax(Int), got $x")) end k = length(a) - v = ones(Int, k) # current value of each counter + v = fill(1, k) # current value of each counter mx = [nextpow(ai,x) for ai in a] # maximum value of each counter v[1] = mx[1] # start at first case that is >= x p::widen(Int) = mx[1] # initial value of product in this case diff --git a/base/docs/basedocs.jl b/base/docs/basedocs.jl index 053c6ec633155..6fc647cfae03a 100644 --- a/base/docs/basedocs.jl +++ b/base/docs/basedocs.jl @@ -926,14 +926,14 @@ An indexing operation into an array, `a`, tried to access an out-of-bounds eleme # Examples ```jldoctest -julia> A = ones(7); +julia> A = fill(1.0, 7); julia> A[8] ERROR: BoundsError: attempt to access 7-element Array{Float64,1} at index [8] Stacktrace: [1] getindex(::Array{Float64,1}, ::Int64) at ./array.jl:758 -julia> B = ones(2, 3); +julia> B = fill(1.0, (2,3)); julia> B[2, 4] ERROR: BoundsError: attempt to access 2×3 Array{Float64,2} at index [2, 4] diff --git a/base/indices.jl b/base/indices.jl index 9911e42cc6e36..e05d489380a6d 100644 --- a/base/indices.jl +++ b/base/indices.jl @@ -38,9 +38,9 @@ Check two array shapes for compatibility, allowing trailing singleton dimensions whichever shape has more dimensions. ```jldoctest -julia> a = ones(3,4,1,1,1); +julia> a = fill(1, (3,4,1,1,1)); -julia> b = ones(3,4); +julia> b = fill(1, (3,4)); julia> promote_shape(a,b) (Base.OneTo(3), Base.OneTo(4), Base.OneTo(1), Base.OneTo(1), Base.OneTo(1)) diff --git a/base/linalg/blas.jl b/base/linalg/blas.jl index f35f871da5248..bf9d60a78931b 100644 --- a/base/linalg/blas.jl +++ b/base/linalg/blas.jl @@ -228,7 +228,7 @@ Dot product of two vectors consisting of `n` elements of array `X` with stride ` # Examples ```jldoctest -julia> dot(10, ones(10), 1, ones(20), 2) +julia> dot(10, fill(1.0, 10), 1, fill(1.0, 20), 2) 10.0 ``` """ @@ -243,7 +243,7 @@ conjugating the first vector. # Examples ```jldoctest -julia> Base.BLAS.dotc(10, im*ones(10), 1, complex.(ones(20), ones(20)), 2) +julia> Base.BLAS.dotc(10, fill(1.0im, 10), 1, fill(1.0+im, 20), 2) 10.0 - 10.0im ``` """ @@ -257,7 +257,7 @@ with stride `incx` and `n` elements of array `Y` with stride `incy`. # Examples ```jldoctest -julia> Base.BLAS.dotu(10, im*ones(10), 1, complex.(ones(20), ones(20)), 2) +julia> Base.BLAS.dotu(10, fill(1.0im, 10), 1, fill(1.0+im, 20), 2) -10.0 + 10.0im ``` """ @@ -349,10 +349,10 @@ stride1(x::Array) = 1 # Examples ```jldoctest -julia> Base.BLAS.nrm2(4, ones(8), 2) +julia> Base.BLAS.nrm2(4, fill(1.0, 8), 2) 2.0 -julia> Base.BLAS.nrm2(1, ones(8), 2) +julia> Base.BLAS.nrm2(1, fill(1.0, 8), 2) 1.0 ``` """ @@ -382,10 +382,10 @@ Sum of the absolute values of the first `n` elements of array `X` with stride `i # Examples ```jldoctest -julia> Base.BLAS.asum(5, im*ones(10), 2) +julia> Base.BLAS.asum(5, fill(1.0im, 10), 2) 5.0 -julia> Base.BLAS.asum(2, im*ones(10), 5) +julia> Base.BLAS.asum(2, fill(1.0im, 10), 5) 2.0 ``` """ diff --git a/base/linalg/dense.jl b/base/linalg/dense.jl index f27a901b02150..0e45718ea0b0c 100644 --- a/base/linalg/dense.jl +++ b/base/linalg/dense.jl @@ -753,7 +753,7 @@ compute the cosine. Otherwise, the cosine is determined by calling [`exp`](@ref) # Examples ```jldoctest -julia> cos(ones(2, 2)) +julia> cos(fill(1.0, (2,2))) 2×2 Array{Float64,2}: 0.291927 -0.708073 -0.708073 0.291927 @@ -786,7 +786,7 @@ compute the sine. Otherwise, the sine is determined by calling [`exp`](@ref). # Examples ```jldoctest -julia> sin(ones(2, 2)) +julia> sin(fill(1.0, (2,2))) 2×2 Array{Float64,2}: 0.454649 0.454649 0.454649 0.454649 @@ -820,7 +820,7 @@ Compute the matrix sine and cosine of a square matrix `A`. # Examples ```jldoctest -julia> S, C = sincos(ones(2, 2)); +julia> S, C = sincos(fill(1.0, (2,2))); julia> S 2×2 Array{Float64,2}: @@ -872,7 +872,7 @@ compute the tangent. Otherwise, the tangent is determined by calling [`exp`](@re # Examples ```jldoctest -julia> tan(ones(2, 2)) +julia> tan(fill(1.0, (2,2))) 2×2 Array{Float64,2}: -1.09252 -1.09252 -1.09252 -1.09252 @@ -1144,7 +1144,7 @@ will return a Cholesky factorization. # Examples ```jldoctest -julia> A = Array(Bidiagonal(ones(5, 5), :U)) +julia> A = Array(Bidiagonal(fill(1.0, (5, 5)), :U)) 5×5 Array{Float64,2}: 1.0 1.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0 0.0 diff --git a/base/linalg/generic.jl b/base/linalg/generic.jl index a14f2981146d8..805bc4202c066 100644 --- a/base/linalg/generic.jl +++ b/base/linalg/generic.jl @@ -121,7 +121,7 @@ Upper triangle of a matrix. # Examples ```jldoctest -julia> a = ones(4,4) +julia> a = fill(1.0, (4,4)) 4×4 Array{Float64,2}: 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 @@ -145,7 +145,7 @@ Lower triangle of a matrix. # Examples ```jldoctest -julia> a = ones(4,4) +julia> a = fill(1.0, (4,4)) 4×4 Array{Float64,2}: 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 @@ -169,7 +169,7 @@ Returns the upper triangle of `M` starting from the `k`th superdiagonal. # Examples ```jldoctest -julia> a = ones(4,4) +julia> a = fill(1.0, (4,4)) 4×4 Array{Float64,2}: 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 @@ -200,7 +200,7 @@ Returns the lower triangle of `M` starting from the `k`th superdiagonal. # Examples ```jldoctest -julia> a = ones(4,4) +julia> a = fill(1.0, (4,4)) 4×4 Array{Float64,2}: 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 @@ -1209,9 +1209,9 @@ running in parallel, only 1 BLAS thread is used. The argument `n` still refers t of the problem that is solved on each processor. """ function peakflops(n::Integer=2000; parallel::Bool=false) - a = ones(Float64,100,100) + a = fill(1.,100,100) t = @elapsed a2 = a*a - a = ones(Float64,n,n) + a = fill(1.,n,n) t = @elapsed a2 = a*a @assert a2[1,1] == n parallel ? sum(pmap(peakflops, [ n for i in 1:nworkers()])) : (2*Float64(n)^3/t) diff --git a/base/linalg/lapack.jl b/base/linalg/lapack.jl index dec2ed11b003f..c6a455aabaa81 100644 --- a/base/linalg/lapack.jl +++ b/base/linalg/lapack.jl @@ -3731,7 +3731,7 @@ for (stev, stebz, stegr, stein, elty) in isplit = similar(dv, BlasInt,n) w = similar(dv, $elty,n) if length(iblock_in) < m #Not enough block specifications - iblock[1:m] = ones(BlasInt, m) + iblock[1:m] = fill(BlasInt(1), m) w[1:m] = sort(w_in) else iblock[1:m] = iblock_in diff --git a/base/linalg/linalg.jl b/base/linalg/linalg.jl index 90a617ddfb24b..03b46b55a8233 100644 --- a/base/linalg/linalg.jl +++ b/base/linalg/linalg.jl @@ -173,7 +173,7 @@ For multiple arguments, return a vector. # Examples ```jldoctest -julia> A = ones(4,4); B = zeros(5,5); +julia> A = fill(1, (4,4)); B = fill(1, (5,5)); julia> LinAlg.checksquare(A, B) 2-element Array{Int64,1}: diff --git a/base/linalg/lu.jl b/base/linalg/lu.jl index b6c52465d6e47..8c391b8292607 100644 --- a/base/linalg/lu.jl +++ b/base/linalg/lu.jl @@ -466,7 +466,7 @@ factorize(A::Tridiagonal) = lufact(A) function getproperty(F::LU{T,Tridiagonal{T,V}}, d::Symbol) where {T,V} m, n = size(F) if d == :L - L = Array(Bidiagonal(ones(T, n), getfield(getfield(F, :factors), :dl), d)) + L = Array(Bidiagonal(fill(one(T), n), getfield(getfield(F, :factors), :dl), d)) for i = 2:n tmp = L[getfield(F, :ipiv)[i], 1:i - 1] L[getfield(F, :ipiv)[i], 1:i - 1] = L[i, 1:i - 1] diff --git a/base/linalg/qr.jl b/base/linalg/qr.jl index f31f50c8829b7..21e5263dc3c87 100644 --- a/base/linalg/qr.jl +++ b/base/linalg/qr.jl @@ -754,8 +754,8 @@ function ldiv!(A::QRPivoted{T}, B::StridedMatrix{T}, rcond::Real) where T<:BlasF return B, 0 end rnk = 1 - xmin = ones(T, 1) - xmax = ones(T, 1) + xmin = T[1] + xmax = T[1] tmin = tmax = ar while rnk < nr tmin, smin, cmin = LAPACK.laic1!(2, xmin, tmin, view(A.factors, 1:rnk, rnk + 1), A.factors[rnk + 1, rnk + 1]) diff --git a/base/linalg/triangular.jl b/base/linalg/triangular.jl index 7b421e10397fb..3685b38c88647 100644 --- a/base/linalg/triangular.jl +++ b/base/linalg/triangular.jl @@ -347,9 +347,9 @@ adjoint!(A::UpperTriangular) = LowerTriangular(copytri!(A.data, 'U' , true)) adjoint!(A::UnitUpperTriangular) = UnitLowerTriangular(copytri!(A.data, 'U' , true)) diag(A::LowerTriangular) = diag(A.data) -diag(A::UnitLowerTriangular) = ones(eltype(A), size(A,1)) +diag(A::UnitLowerTriangular) = fill(one(eltype(A)), size(A,1)) diag(A::UpperTriangular) = diag(A.data) -diag(A::UnitUpperTriangular) = ones(eltype(A), size(A,1)) +diag(A::UnitUpperTriangular) = fill(one(eltype(A)), size(A,1)) # Unary operations -(A::LowerTriangular) = LowerTriangular(-A.data) diff --git a/base/linalg/uniformscaling.jl b/base/linalg/uniformscaling.jl index 97773b9322c61..fec38e93aaa84 100644 --- a/base/linalg/uniformscaling.jl +++ b/base/linalg/uniformscaling.jl @@ -38,7 +38,7 @@ An object of type [`UniformScaling`](@ref), representing an identity matrix of a # Examples ```jldoctest -julia> ones(5, 6) * I == ones(5, 6) +julia> fill(1, (5,6)) * I == fill(1, (5,6)) true julia> [1 2im 3; 1im 2 3] * I diff --git a/base/multidimensional.jl b/base/multidimensional.jl index d42af46fe019c..c2e31b4db2d3c 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -189,7 +189,7 @@ module IteratorsMD CartesianIndex(1, 2, 2) CartesianIndex(2, 2, 2) - julia> CartesianIndices(ones(2,3)) + julia> CartesianIndices(fill(1, (2,3))) 2×3 CartesianIndices{2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}: CartesianIndex(1, 1) CartesianIndex(1, 2) CartesianIndex(1, 3) CartesianIndex(2, 1) CartesianIndex(2, 2) CartesianIndex(2, 3) diff --git a/base/sparse/linalg.jl b/base/sparse/linalg.jl index f68b58fc1419b..79d15212c89cc 100644 --- a/base/sparse/linalg.jl +++ b/base/sparse/linalg.jl @@ -406,7 +406,7 @@ end function sparse_diff1(S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} m,n = size(S) - m > 1 || return SparseMatrixCSC(0, n, ones(Ti,n+1), Ti[], Tv[]) + m > 1 || return SparseMatrixCSC(0, n, fill(one(Ti),n+1), Ti[], Tv[]) colptr = Vector{Ti}(uninitialized, n+1) numnz = 2 * nnz(S) # upper bound; will shrink later rowval = Vector{Ti}(uninitialized, numnz) diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index b864016b94f11..6d18d923b9185 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -323,7 +323,7 @@ function _sparsesimilar(S::SparseMatrixCSC, ::Type{TvNew}, ::Type{TiNew}) where end # parent methods for similar that preserves only storage space (for when new and old dims differ) _sparsesimilar(S::SparseMatrixCSC, ::Type{TvNew}, ::Type{TiNew}, dims::Dims{2}) where {TvNew,TiNew} = - SparseMatrixCSC(dims..., ones(TiNew, last(dims)+1), similar(S.rowval, TiNew), similar(S.nzval, TvNew)) + SparseMatrixCSC(dims..., fill(one(TiNew), last(dims)+1), similar(S.rowval, TiNew), similar(S.nzval, TvNew)) # parent method for similar that allocates an empty sparse vector (when new dims are single) _sparsesimilar(S::SparseMatrixCSC, ::Type{TvNew}, ::Type{TiNew}, dims::Dims{1}) where {TvNew,TiNew} = SparseVector(dims..., similar(S.rowval, TiNew, 0), similar(S.nzval, TvNew, 0)) @@ -506,7 +506,7 @@ function sparse(I::AbstractVector{Ti}, J::AbstractVector{Ti}, V::AbstractVector{ throw(ArgumentError("row indices I[k] must satisfy 1 <= I[k] <= m")) end end - SparseMatrixCSC(m, n, ones(Ti, n+1), Vector{Ti}(), Vector{Tv}()) + SparseMatrixCSC(m, n, fill(one(Ti), n+1), Vector{Ti}(), Vector{Tv}()) else # Allocate storage for CSR form csrrowptr = Vector{Ti}(uninitialized, m+1) @@ -1395,7 +1395,7 @@ function sprand(m::Integer, n::Integer, density::AbstractFloat, sparse_IJ_sorted!(I, J, rfn(length(I)), m, n, +) # it will never need to combine end -truebools(r::AbstractRNG, n::Integer) = ones(Bool, n) +truebools(r::AbstractRNG, n::Integer) = fill(true, n) sprand(m::Integer, n::Integer, density::AbstractFloat) = sprand(GLOBAL_RNG,m,n,density) @@ -1449,7 +1449,7 @@ spzeros(m::Integer, n::Integer) = spzeros(Float64, m, n) spzeros(::Type{Tv}, m::Integer, n::Integer) where {Tv} = spzeros(Tv, Int, m, n) function spzeros(::Type{Tv}, ::Type{Ti}, m::Integer, n::Integer) where {Tv, Ti} ((m < 0) || (n < 0)) && throw(ArgumentError("invalid Array dimensions")) - SparseMatrixCSC(m, n, ones(Ti, n+1), Vector{Ti}(), Vector{Tv}()) + SparseMatrixCSC(m, n, fill(one(Ti), n+1), Vector{Ti}(), Vector{Tv}()) end # de-splatting variant function spzeros(::Type{Tv}, ::Type{Ti}, sz::Tuple{Integer,Integer}) where {Tv, Ti} diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index 4ee09d93a8080..2d84dff2d7f2c 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -70,7 +70,7 @@ _sparsesimilar(S::SparseVector, ::Type{TvNew}, ::Type{TiNew}, dims::Dims{1}) whe SparseVector(dims..., similar(S.nzind, TiNew, 0), similar(S.nzval, TvNew, 0)) # parent method for similar that preserves storage space (for old and new dims differ, and new is 2d) _sparsesimilar(S::SparseVector, ::Type{TvNew}, ::Type{TiNew}, dims::Dims{2}) where {TvNew,TiNew} = - SparseMatrixCSC(dims..., ones(TiNew, last(dims)+1), similar(S.nzind, TiNew), similar(S.nzval, TvNew)) + SparseMatrixCSC(dims..., fill(one(TiNew), last(dims)+1), similar(S.nzind, TiNew), similar(S.nzval, TvNew)) # The following methods hook into the AbstractArray similar hierarchy. The first method # covers similar(A[, Tv]) calls, which preserve stored-entry structure, and the latter # methods cover similar(A[, Tv], shape...) calls, which preserve nothing if the dims diff --git a/doc/src/manual/noteworthy-differences.md b/doc/src/manual/noteworthy-differences.md index 707f883c972f7..5874e57dbde86 100644 --- a/doc/src/manual/noteworthy-differences.md +++ b/doc/src/manual/noteworthy-differences.md @@ -140,7 +140,7 @@ For users coming to Julia from R, these are some noteworthy differences: In Julia, they cannot be used interchangeably. * Julia's [`diag`](@ref) and [`diagm`](@ref) are not like R's. * Julia cannot assign to the results of function calls on the left hand side of an assignment operation: - you cannot write `diag(M) = ones(n)`. + you cannot write `diag(M) = fill(1, n)`. * Julia discourages populating the main namespace with functions. Most statistical functionality for Julia is found in [packages](https://pkg.julialang.org/) under the [JuliaStats organization](https://github.com/JuliaStats). For example: @@ -211,7 +211,7 @@ For users coming to Julia from R, these are some noteworthy differences: by default. To get optimal performance when looping over arrays, the order of the loops should be reversed in Julia relative to NumPy (see relevant section of [Performance Tips](@ref man-performance-tips)). * Julia's updating operators (e.g. `+=`, `-=`, ...) are *not in-place* whereas NumPy's are. This - means `A = ones(4); B = A; B += 3` doesn't change values in `A`, it rather rebinds the name `B` + means `A = [1, 1]; B = A; B += [3, 3]` doesn't change values in `A`, it rather rebinds the name `B` to the result of the right-hand side `B = B + 3`, which is a new array. For in-place operation, use `B .+= 3` (see also [dot operators](@ref man-dot-operators)), explicit loops, or `InplaceOps.jl`. * Julia evaluates default values of function arguments every time the method is invoked, unlike diff --git a/examples/ndgrid.jl b/examples/ndgrid.jl index e00a3c1fa6b79..dc736344f4a10 100644 --- a/examples/ndgrid.jl +++ b/examples/ndgrid.jl @@ -45,8 +45,8 @@ function meshgrid(vx::AbstractVector{T}, vy::AbstractVector{T}, vx = reshape(vx, 1, n, 1) vy = reshape(vy, m, 1, 1) vz = reshape(vz, 1, 1, o) - om = ones(Int, m) - on = ones(Int, n) - oo = ones(Int, o) + om = fill(1, m) + on = fill(1, n) + oo = fill(1, o) (vx[om, :, oo], vy[:, on, oo], vz[om, on, :]) end diff --git a/stdlib/Distributed/test/distributed_exec.jl b/stdlib/Distributed/test/distributed_exec.jl index 9f219085b0a54..14c1d5cc3ea71 100644 --- a/stdlib/Distributed/test/distributed_exec.jl +++ b/stdlib/Distributed/test/distributed_exec.jl @@ -289,7 +289,7 @@ workloads = Int[sum(ids .== i) for i in 2:nprocs()] # Testing buffered and unbuffered reads # This large array should write directly to the socket -a = ones(10^6) +a = fill(1, 10^6) @test a == remotecall_fetch((x)->x, id_other, a) # Not a bitstype, should be buffered @@ -1118,7 +1118,7 @@ for i in 1:5 end # Different global bindings to the same object -global v3 = ones(10) +global v3 = fill(1., 10) global v4 = v3 @test remotecall_fetch(()->v3, id_other) == remotecall_fetch(()->v4, id_other) @test remotecall_fetch(()->isdefined(Main, :v3), id_other) @@ -1188,8 +1188,8 @@ end @test testsercnt_d[object_id(tsc)] == n # Multiple references in a closure should be serialized only once. -global mrefs = TestSerCnt(ones(10)) -@test remotecall_fetch(()->(mrefs.v, 2*mrefs.v, 3*mrefs.v), id_other) == (ones(10), 2*ones(10), 3*ones(10)) +global mrefs = TestSerCnt(fill(1.,10)) +@test remotecall_fetch(()->(mrefs.v, 2*mrefs.v, 3*mrefs.v), id_other) == (fill(1.,10), fill(2.,10), fill(3.,10)) @test testsercnt_d[object_id(mrefs)] == 1 @@ -1201,7 +1201,7 @@ v = rand() @test remotecall_fetch(x->f2(x), id_other, v) == v # consts -const c1 = ones(10) +const c1 = fill(1., 10) @test remotecall_fetch(()->c1, id_other) == c1 @test remotecall_fetch(()->isconst(Main, :c1), id_other) @@ -1234,7 +1234,7 @@ end wrapped_var_ser_tests() # Test internal data structures being cleaned up upon gc. -global ids_cleanup = ones(6) +global ids_cleanup = fill(1., 6) global ids_func = ()->ids_cleanup clust_ser = (Distributed.worker_from_id(id_other)).w_serializer @@ -1449,8 +1449,8 @@ npids = addprocs_with_testenv(WorkerArgTester(`--worker=foobar`, false)) # Must be run on a new cluster, i.e., all workers must be in the same state. rmprocs(workers()) p1,p2 = addprocs_with_testenv(2) -@everywhere f22865(p) = remotecall_fetch(x->x.*2, p, ones(2)) -@test ones(2).*2 == remotecall_fetch(f22865, p1, p2) +@everywhere f22865(p) = remotecall_fetch(x->x.*2, p, fill(1.,2)) +@test fill(2.,2) == remotecall_fetch(f22865, p1, p2) function reuseport_tests() # Run the test on all processes. diff --git a/stdlib/IterativeEigensolvers/test/runtests.jl b/stdlib/IterativeEigensolvers/test/runtests.jl index 9012e128ba8f6..a8782939da55c 100644 --- a/stdlib/IterativeEigensolvers/test/runtests.jl +++ b/stdlib/IterativeEigensolvers/test/runtests.jl @@ -180,7 +180,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(sparse(1.0I, 50, 50), nev=10, tol = 5e-16)[1] ≈ ones(10) #Issue 4246 + @test eigs(sparse(1.0I, 50, 50), nev=10, tol = 5e-16)[1] ≈ fill(1., 10) #Issue 4246 end @testset "real svds" begin diff --git a/stdlib/SharedArrays/test/runtests.jl b/stdlib/SharedArrays/test/runtests.jl index d216e084c0fca..d69b3e0ce2f16 100644 --- a/stdlib/SharedArrays/test/runtests.jl +++ b/stdlib/SharedArrays/test/runtests.jl @@ -68,7 +68,7 @@ for p in procs(d) @test d[idxl] == rv end -@test ones(10, 10, 10) == SharedArrays.shmem_fill(1.0, (10,10,10)) +@test fill(1., 10, 10, 10) == SharedArrays.shmem_fill(1.0, (10,10,10)) @test zeros(Int32, 10, 10, 10) == SharedArrays.shmem_fill(0, (10,10,10)) d = SharedArrays.shmem_rand(dims) @@ -133,7 +133,7 @@ finalize(S) # Appending to a file fn3 = tempname() -write(fn3, ones(UInt8, 4)) +write(fn3, fill(0x1, 4)) S = SharedArray{UInt8}(fn3, sz, 4, mode="a+", init=D->D[localindices(D)]=0x02) len = prod(sz)+4 @test filesize(fn3) == len @@ -169,7 +169,7 @@ S = @inferred(SharedArray{Int}(1,2,3)) # reshape d = SharedArrays.shmem_fill(1.0, (10,10,10)) -@test ones(100, 10) == reshape(d,(100,10)) +@test fill(1., 100, 10) == reshape(d,(100,10)) d = SharedArrays.shmem_fill(1.0, (10,10,10)) @test_throws DimensionMismatch reshape(d,(50,)) @@ -223,7 +223,7 @@ d2 = map(x->1, d) map!(x->1, d, d) @test reduce(+, d) == 100 -@test fill!(d, 1) == ones(10, 10) +@test fill!(d, 1) == fill(1., 10, 10) @test fill!(d, 2.) == fill(2, 10, 10) @test d[:] == fill(2, 100) @test d[:,1] == fill(2, 10) diff --git a/stdlib/SuiteSparse/src/spqr.jl b/stdlib/SuiteSparse/src/spqr.jl index 846d6b7cd31e5..20716c2780561 100644 --- a/stdlib/SuiteSparse/src/spqr.jl +++ b/stdlib/SuiteSparse/src/spqr.jl @@ -163,7 +163,7 @@ solve least squares or underdetermined problems with [`\\`](@ref). The function # Examples ```jldoctest -julia> A = sparse([1,2,3,4], [1,1,2,2], ones(4)) +julia> A = sparse([1,2,3,4], [1,1,2,2], [1.0,1.0,1.0,1.0]) 4×2 SparseMatrixCSC{Float64,Int64} with 4 stored entries: [1, 1] = 1.0 [2, 1] = 1.0 @@ -397,13 +397,13 @@ when the problem is underdetermined. # Examples ```jldoctest -julia> A = sparse([1,2,4], [1,1,1], ones(3), 4, 2) +julia> A = sparse([1,2,4], [1,1,1], [1.0,1.0,1.0], 4, 2) 4×2 SparseMatrixCSC{Float64,Int64} with 3 stored entries: [1, 1] = 1.0 [2, 1] = 1.0 [4, 1] = 1.0 -julia> qrfact(A)\\ones(4) +julia> qrfact(A)\\fill(1.0, 4) 2-element Array{Float64,1}: 1.0 0.0 diff --git a/stdlib/SuiteSparse/test/cholmod.jl b/stdlib/SuiteSparse/test/cholmod.jl index fdf3425ac4483..db792b945ebde 100644 --- a/stdlib/SuiteSparse/test/cholmod.jl +++ b/stdlib/SuiteSparse/test/cholmod.jl @@ -38,7 +38,8 @@ srand(123) ## residual 1.3e-19 (|Ax-b|/(|A||x|+|b|)) after iterative refinement ## rcond 9.5e-06 - A = CHOLMOD.Sparse(48, 48, + n = 48 + A = CHOLMOD.Sparse(n, n, CHOLMOD.SuiteSparse_long[0,1,2,3,6,9,12,15,18,20,25,30,34,36,39,43,47,52,58, 62,67,71,77,84,90,93,95,98,103,106,110,115,119,123,130,136,142,146,150,155, 161,167,174,182,189,197,207,215,224], # zero-based column pointers @@ -102,12 +103,13 @@ srand(123) @test_throws ArgumentError CHOLMOD.norm_sparse(A, 2) @test CHOLMOD.isvalid(A) - B = A * ones(size(A,2)) + x = fill(1., n) + b = A*x + chma = ldltfact(A) # LDL' form @test CHOLMOD.isvalid(chma) @test unsafe_load(pointer(chma)).is_ll == 0 # check that it is in fact an LDLt - x = chma\B - @test x ≈ ones(size(x)) + @test chma\b ≈ x @test nnz(ldltfact(A, perm=1:size(A,1))) > nnz(chma) @test size(chma) == size(A) chmal = CHOLMOD.FactorComponent(chma, :L) @@ -117,8 +119,7 @@ srand(123) chma = cholfact(A) # LL' form @test CHOLMOD.isvalid(chma) @test unsafe_load(pointer(chma)).is_ll == 1 # check that it is in fact an LLt - x = chma\B - @test x ≈ ones(size(x)) + @test chma\b ≈ x @test nnz(chma) == 489 @test nnz(cholfact(A, perm=1:size(A,1))) > nnz(chma) @test size(chma) == size(A) @@ -127,7 +128,7 @@ srand(123) @test size(chmal, 1) == size(A, 1) @testset "eltype" begin - @test eltype(Dense(ones(3))) == Float64 + @test eltype(Dense(fill(1., 3))) == Float64 @test eltype(A) == Float64 @test eltype(chma) == Float64 end @@ -152,7 +153,7 @@ end afiro2 = CHOLMOD.aat(afiro, CHOLMOD.SuiteSparse_long[0:50;], CHOLMOD.SuiteSparse_long(1)) CHOLMOD.change_stype!(afiro2, -1) chmaf = cholfact(afiro2) - y = afiro'*ones(size(afiro,1)) + y = afiro'*fill(1., size(afiro,1)) sol = chmaf\(afiro*y) # least squares solution @test CHOLMOD.isvalid(sol) pred = afiro'*sol @@ -333,7 +334,7 @@ end A1pd.nzval) ## High level interface - @test isa(CHOLMOD.Sparse(3, 3, [0,1,3,4], [0,2,1,2], ones(4)), CHOLMOD.Sparse) # Sparse doesn't require columns to be sorted + @test isa(CHOLMOD.Sparse(3, 3, [0,1,3,4], [0,2,1,2], fill(1., 4)), CHOLMOD.Sparse) # Sparse doesn't require columns to be sorted @test_throws BoundsError A1Sparse[6, 1] @test_throws BoundsError A1Sparse[1, 6] @test sparse(A1Sparse) == A1 @@ -373,25 +374,30 @@ end @test_throws ArgumentError cholfact(A1, shift=1.0) @test_throws ArgumentError ldltfact(A1) @test_throws ArgumentError ldltfact(A1, shift=1.0) - @test_throws LinAlg.PosDefException cholfact(A1 + adjoint(A1) - 2eigmax(Array(A1 + adjoint(A1)))*I)\ones(size(A1, 1)) - @test_throws LinAlg.PosDefException cholfact(A1 + adjoint(A1), shift=-2eigmax(Array(A1 + adjoint(A1))))\ones(size(A1, 1)) - @test_throws ArgumentError ldltfact(A1 + adjoint(A1) - 2real(A1[1,1])*I)\ones(size(A1, 1)) - @test_throws ArgumentError ldltfact(A1 + adjoint(A1), shift=-2real(A1[1,1]))\ones(size(A1, 1)) - @test !isposdef(cholfact(A1 + adjoint(A1) - 2eigmax(Array(A1 + adjoint(A1)))*I)) - @test !isposdef(cholfact(A1 + adjoint(A1), shift=-2eigmax(Array(A1 + adjoint(A1))))) - @test !LinAlg.issuccess(ldltfact(A1 + adjoint(A1) - 2real(A1[1,1])*I)) - @test !LinAlg.issuccess(ldltfact(A1 + adjoint(A1), shift=-2real(A1[1,1]))) + C = A1 + adjoint(A1) + λmaxC = eigmax(Array(C)) + b = fill(1., size(A1, 1)) + @test_throws LinAlg.PosDefException cholfact(C - 2λmaxC*I)\b + @test_throws LinAlg.PosDefException cholfact(C, shift=-2λmaxC)\b + @test_throws ArgumentError ldltfact(C - C[1,1]*I)\b + @test_throws ArgumentError ldltfact(C, shift=-real(C[1,1]))\b + @test !isposdef(cholfact(C - 2λmaxC*I)) + @test !isposdef(cholfact(C, shift=-2λmaxC)) + @test !LinAlg.issuccess(ldltfact(C - C[1,1]*I)) + @test !LinAlg.issuccess(ldltfact(C, shift=-real(C[1,1]))) F = cholfact(A1pd) tmp = IOBuffer() show(tmp, F) @test tmp.size > 0 @test isa(CHOLMOD.Sparse(F), CHOLMOD.Sparse{elty}) - @test F\CHOLMOD.Sparse(sparse(ones(elty, 5))) ≈ A1pd\ones(5) - @test_throws DimensionMismatch F\CHOLMOD.Dense(ones(elty, 4)) - @test_throws DimensionMismatch F\CHOLMOD.Sparse(sparse(ones(elty, 4))) - @test F'\ones(elty, 5) ≈ Array(A1pd)'\ones(5) - @test F'\sparse(ones(elty, 5)) ≈ Array(A1pd)'\ones(5) - @test Transpose(F)\ones(elty, 5) ≈ conj(A1pd)'\ones(elty, 5) + @test_throws DimensionMismatch F\CHOLMOD.Dense(fill(elty(1), 4)) + @test_throws DimensionMismatch F\CHOLMOD.Sparse(sparse(fill(elty(1), 4))) + b = fill(1., 5) + bT = fill(elty(1), 5) + @test F'\bT ≈ Array(A1pd)'\b + @test F'\sparse(bT) ≈ Array(A1pd)'\b + @test Transpose(F)\bT ≈ conj(A1pd)'\bT + @test F\CHOLMOD.Sparse(sparse(bT)) ≈ A1pd\b @test logdet(F) ≈ logdet(Array(A1pd)) @test det(F) == exp(logdet(F)) let # to test supernodal, we must use a larger matrix @@ -460,9 +466,9 @@ end @test CHOLMOD.copy(A1Sparse, 0, 1) == A1Sparse @test CHOLMOD.horzcat(A1Sparse, A2Sparse, true) == [A1 A2] @test CHOLMOD.vertcat(A1Sparse, A2Sparse, true) == [A1; A2] - svec = ones(elty, 1) + svec = fill(elty(1), 1) @test CHOLMOD.scale!(CHOLMOD.Dense(svec), CHOLMOD.SCALAR, A1Sparse) == A1Sparse - svec = ones(elty, 5) + svec = fill(elty(1), 5) @test_throws DimensionMismatch CHOLMOD.scale!(CHOLMOD.Dense(svec), CHOLMOD.SCALAR, A1Sparse) @test CHOLMOD.scale!(CHOLMOD.Dense(svec), CHOLMOD.ROW, A1Sparse) == A1Sparse @test_throws DimensionMismatch CHOLMOD.scale!(CHOLMOD.Dense([svec; 1]), CHOLMOD.ROW, A1Sparse) @@ -610,8 +616,8 @@ end end @testset "Element promotion and type inference" begin - @inferred cholfact(As)\ones(Int, size(As, 1)) - @inferred ldltfact(As)\ones(Int, size(As, 1)) + @inferred cholfact(As)\fill(1, size(As, 1)) + @inferred ldltfact(As)\fill(1, size(As, 1)) end end @@ -630,7 +636,7 @@ gc() end @testset "Issue 14076" begin - @test cholfact(sparse([1,2,3,4], [1,2,3,4], Float32[1,4,16,64]))\[1,4,16,64] == ones(4) + @test cholfact(sparse([1,2,3,4], [1,2,3,4], Float32[1,4,16,64]))\[1,4,16,64] == fill(1, 4) end @testset "Issue 14134" begin @@ -647,7 +653,7 @@ end serialize(b, F) seekstart(b) Fnew = deserialize(b) - @test_throws ArgumentError Fnew\ones(5) + @test_throws ArgumentError Fnew\fill(1., 5) @test_throws ArgumentError show(Fnew) @test_throws ArgumentError size(Fnew) @test_throws ArgumentError diag(Fnew) @@ -655,15 +661,16 @@ end end @testset "Issue with promotion during conversion to CHOLMOD.Dense" begin - @test CHOLMOD.Dense(ones(Float32, 5)) == ones(5, 1) - @test CHOLMOD.Dense(ones(Int, 5)) == ones(5, 1) - @test CHOLMOD.Dense(ones(Complex{Float32}, 5, 2)) == ones(5, 2) + @test CHOLMOD.Dense(fill(1, 5)) == fill(1, 5, 1) + @test CHOLMOD.Dense(fill(1f0, 5)) == fill(1, 5, 1) + @test CHOLMOD.Dense(fill(1f0 + 0im, 5, 2)) == fill(1, 5, 2) end @testset "Further issue with promotion #14894" begin - @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) + x = fill(1., 5) + @test cholfact(sparse(Float16(1)I, 5, 5))\x == x + @test cholfact(Symmetric(sparse(Float16(1)I, 5, 5)))\x == x + @test cholfact(Hermitian(sparse(Complex{Float16}(1)I, 5, 5)))\x == x @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))) @@ -672,7 +679,7 @@ end @testset "test \\ for Factor and StridedVecOrMat" begin x = rand(5) A = cholfact(sparse(Diagonal(x.\1))) - @test A\view(ones(10),1:2:10) ≈ x + @test A\view(fill(1.,10),1:2:10) ≈ x @test A\view(Matrix(1.0I, 5, 5), :, :) ≈ Matrix(Diagonal(x)) end @@ -686,16 +693,16 @@ end m, n = 400, 500 A = sprandn(m, n, .2) M = [I adjoint(A); A -I] - b = M * ones(m + n) + b = M * fill(1., m+n) F = ldltfact(M) s = unsafe_load(pointer(F)) @test s.is_super == 0 - @test F\b ≈ ones(m + n) + @test F\b ≈ fill(1., m+n) F2 = cholfact(M) @test !LinAlg.issuccess(F2) ldltfact!(F2, M) @test LinAlg.issuccess(F2) - @test F2\b ≈ ones(m + n) + @test F2\b ≈ fill(1., m+n) end @testset "Test that imaginary parts in Hermitian{T,SparseMatrixCSC{T}} are ignored" begin @@ -703,7 +710,7 @@ end Fs = cholfact(Hermitian(A)) Fd = cholfact(Hermitian(Array(A))) @test sparse(Fs) ≈ Hermitian(A) - @test Fs\ones(4) ≈ Fd\ones(4) + @test Fs\fill(1., 4) ≈ Fd\fill(1., 4) end @testset "\\ '\\ and Transpose(...)\\" begin @@ -715,7 +722,7 @@ end Symmetric(Apre + 10I), Hermitian(Apre + 10I), Hermitian(complex(Apre)), Hermitian(complex(Apre) + 10I)) local A, x, b - x = ones(10) + x = fill(1., 10) b = A*x @test x ≈ A\b @test Transpose(A)\b ≈ A'\b @@ -763,31 +770,31 @@ end # Test both cholfact and LDLt with and without automatic permutations for F in (cholfact(AtA), cholfact(AtA, perm=1:5), ldltfact(AtA), ldltfact(AtA, perm=1:5)) local F - B0 = F\ones(5) + x0 = F\(b = fill(1., 5)) #Test both sparse/dense and vectors/matrices for Ctest in (C0, sparse(C0), [C0 2*C0], sparse([C0 2*C0])) - local B, C, F1 + local x, C, F1 C = copy(Ctest) F1 = copy(F) - B = (AtA+C*C')\ones(5) + x = (AtA+C*C')\b #Test update F11 = CHOLMOD.lowrankupdate(F1, C) @test Array(sparse(F11)) ≈ AtA+C*C' - @test F11\ones(5) ≈ B + @test F11\b ≈ x #Make sure we get back the same factor again F10 = CHOLMOD.lowrankdowndate(F11, C) @test Array(sparse(F10)) ≈ AtA - @test F10\ones(5) ≈ B0 + @test F10\b ≈ x0 #Test in-place update CHOLMOD.lowrankupdate!(F1, C) @test Array(sparse(F1)) ≈ AtA+C*C' - @test F1\ones(5) ≈ B + @test F1\b ≈ x #Test in-place downdate CHOLMOD.lowrankdowndate!(F1, C) @test Array(sparse(F1)) ≈ AtA - @test F1\ones(5) ≈ B0 + @test F1\b ≈ x0 @test C == Ctest #Make sure C didn't change end @@ -803,5 +810,5 @@ end @test !LinAlg.issuccess(F) @test LinAlg.issuccess(ldltfact!(F, A)) A[3, 3] = 1 - @test A[:, 3:-1:1]\ones(3) == [1, 1, 1] + @test A[:, 3:-1:1]\fill(1., 3) == [1, 1, 1] end diff --git a/stdlib/SuiteSparse/test/spqr.jl b/stdlib/SuiteSparse/test/spqr.jl index a918610787b34..704be55becac1 100644 --- a/stdlib/SuiteSparse/test/spqr.jl +++ b/stdlib/SuiteSparse/test/spqr.jl @@ -62,11 +62,12 @@ nn = 100 @test A\B[:,1] ≈ Array(A)\B[:,1] @test A\B ≈ Array(A)\B @test_throws DimensionMismatch A\B[1:m-1,:] - @test A[1:9,:]*(A[1:9,:]\ones(eltyB, 9)) ≈ ones(9) # Underdetermined system + C, x = A[1:9, :], fill(eltyB(1), 9) + @test C*(C\x) ≈ x # Underdetermined system end # Make sure that conversion to Sparse doesn't use SuiteSparse's symmetric flag - @test qrfact(SparseMatrixCSC{eltyA}(I, 5, 5)) \ ones(eltyA, 5) == ones(5) + @test qrfact(SparseMatrixCSC{eltyA}(I, 5, 5)) \ fill(eltyA(1), 5) == fill(1, 5) end @testset "basic solution of rank deficient ls" begin diff --git a/stdlib/SuiteSparse/test/umfpack.jl b/stdlib/SuiteSparse/test/umfpack.jl index 2fd053409f8a3..0f8b9fcfd59a1 100644 --- a/stdlib/SuiteSparse/test/umfpack.jl +++ b/stdlib/SuiteSparse/test/umfpack.jl @@ -2,7 +2,7 @@ @testset "UMFPACK wrappers" begin se33 = sparse(1.0I, 3, 3) - do33 = ones(3) + do33 = fill(1., 3) @test isequal(se33 \ do33, do33) # based on deps/Suitesparse-4.0.2/UMFPACK/Demo/umfpack_di_demo.c @@ -68,7 +68,7 @@ @test Transpose(A) * x ≈ b # Element promotion and type inference - @inferred lua\ones(Int, size(A, 2)) + @inferred lua\fill(1, size(A, 2)) end end @@ -76,7 +76,7 @@ Ac0 = complex.(A0,A0) for Ti in Base.uniontypes(SuiteSparse.UMFPACK.UMFITypes) Ac = convert(SparseMatrixCSC{ComplexF64,Ti}, Ac0) - x = complex.(ones(size(Ac, 1)), ones(size(Ac,1))) + x = fill(1.0 + im, size(Ac,1)) lua = lufact(Ac) L,U,p,q,Rs = lua.:(:) @test (Diagonal(Rs) * Ac)[p,q] ≈ L * U @@ -99,8 +99,8 @@ end @testset "Issue #4523 - complex sparse \\" begin - x = sparse((1.0 + 1.0im)I, 2, 2) - @test (x*(lufact(x) \ ones(2))) ≈ ones(2) + A, b = sparse((1.0 + im)I, 2, 2), fill(1., 2) + @test A * (lufact(A)\b) ≈ b @test det(sparse([1,3,3,1], [1,1,3,3], [1,1,1,1])) == 0 end @@ -119,8 +119,8 @@ (Int, Float64), ) - F = lufact(sparse(ones(Tin, 1, 1))) - L = sparse(ones(Tout, 1, 1)) + F = lufact(sparse(fill(Tin(1), 1, 1))) + L = sparse(fill(Tout(1), 1, 1)) @test F.p == F.q == [1] @test F.Rs == [1.0] @test F.L == F.U == L @@ -128,12 +128,12 @@ end @testset "BigFloat not supported" for T in (BigFloat, Complex{BigFloat}) - @test_throws ArgumentError lufact(sparse(ones(T, 1, 1))) + @test_throws ArgumentError lufact(sparse(fill(T(1), 1, 1))) end @testset "size(::UmfpackLU)" begin m = n = 1 - F = lufact(sparse(ones(m, n))) + F = lufact(sparse(fill(1., m, n))) @test size(F) == (m, n) @test size(F, 1) == m @test size(F, 2) == n diff --git a/stdlib/Test/docs/src/index.md b/stdlib/Test/docs/src/index.md index 317fb39eeceb1..32ec6041811aa 100644 --- a/stdlib/Test/docs/src/index.md +++ b/stdlib/Test/docs/src/index.md @@ -125,7 +125,7 @@ julia> @testset "Foo Tests" begin end @testset "Arrays $i" for i in 1:3 @test foo(zeros(i)) == i^2 - @test foo(ones(i)) == i^2 + @test foo(fill(1.0, i)) == i^2 end end; Test Summary: | Pass Total @@ -147,12 +147,12 @@ julia> @testset "Foo Tests" begin end @testset "Arrays" begin @test foo(zeros(2)) == 4 - @test foo(ones(4)) == 15 + @test foo(fill(1.0, 4)) == 15 end end Arrays: Test Failed - Expression: foo(ones(4)) == 15 + Expression: foo(fill(1.0, 4)) == 15 Evaluated: 16 == 15 [...] Test Summary: | Pass Fail Total diff --git a/stdlib/Test/test/runtests.jl b/stdlib/Test/test/runtests.jl index 4a5d8fb76e7c9..40cc539dd6a43 100644 --- a/stdlib/Test/test/runtests.jl +++ b/stdlib/Test/test/runtests.jl @@ -592,7 +592,7 @@ let msg = read(pipeline(ignorestatus(`$(Base.julia_cmd()) --startup-file=no --co end @testset "Arrays" begin @test foo(zeros(2)) == 4 - @test foo(ones(4)) == 15 + @test foo(fill(1., 4)) == 15 end end'`), stderr=DevNull), String) @test contains(msg, diff --git a/test/abstractarray.jl b/test/abstractarray.jl index a3149a9bb0223..8037cc552ddeb 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -784,7 +784,8 @@ end end @testset "dispatch loop introduced in #19305" begin - @test [(1:2) zeros(2,2); ones(3,3)] == [[1,2] zeros(2,2); ones(3,3)] == [reshape([1,2],2,1) zeros(2,2); ones(3,3)] + Z22, O33 = fill(0, 2, 2), fill(1, 3, 3) + @test [(1:2) Z22; O33] == [[1,2] Z22; O33] == [[1 2]' Z22; O33] end @testset "checkbounds_indices method ambiguities #20989" begin @@ -845,6 +846,6 @@ end @test CR[i_lin] == CartesianIndex(xrng[i],yrng[j]) end - @test CartesianIndices(ones(2,3)) == CartesianIndices((2,3)) + @test CartesianIndices(fill(1., 2, 3)) == CartesianIndices((2,3)) @test LinearIndices((2,3)) == [1 3 5; 2 4 6] end diff --git a/test/arrayops.jl b/test/arrayops.jl index dae808c5325fa..5e8732fa56214 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -8,7 +8,7 @@ using Main.TestHelpers.OAs @test length([1, 2, 3]) == 3 @test count(!iszero, [1, 2, 3]) == 3 - let a = ones(4), b = a+a, c = a-a, d = a+a+a + let a = fill(1., 4), b = a+a, c = a-a, d = a+a+a @test b[1] === 2. && b[2] === 2. && b[3] === 2. && b[4] === 2. @test c[1] === 0. && c[2] === 0. && c[3] === 0. && c[4] === 0. @test d[1] === 3. && d[2] === 3. && d[3] === 3. && d[4] === 3. @@ -43,7 +43,7 @@ using Main.TestHelpers.OAs @test isequal([1,2,5] .<< [1,2,5], [2,8,160]) @test isequal([10,20,50] .>> [1,2,5], [5,5,1]) - a = ones(2,2) + a = fill(1.,2,2) a[1,1] = 1 a[1,2] = 2 a[2,1] = 3 @@ -54,11 +54,11 @@ using Main.TestHelpers.OAs a[[1 2 3 4]] = 0 @test a == zeros(2,2) a[[1 2], [1 2]] = 1 - @test a == ones(2,2) + @test a == fill(1.,2,2) a[[1 2], 1] = 0 @test a[1,1] == 0. && a[1,2] == 1. && a[2,1] == 0. && a[2,2] == 1. a[:, [1 2]] = 2 - @test a == 2ones(2,2) + @test a == fill(2.,2,2) a = Array{Float64}(uninitialized, 2, 2, 2, 2, 2) a[1,1,1,1,1] = 10 @@ -78,8 +78,7 @@ using Main.TestHelpers.OAs @test_throws ArgumentError reinterpret(Any, b) c = ["hello", "world"] @test_throws ArgumentError reinterpret(Float32, c) - a = Vector(ones(5)) - @test_throws ArgumentError resize!(a, -2) + @test_throws ArgumentError resize!(Float64[], -2) b = rand(32) a = reshape(b, (2, 2, 2, 2, 2)) @@ -134,7 +133,7 @@ end end end @testset "reshape(a, Val(N))" begin - a = ones(Int,3,3) + a = fill(1,3,3) s = view(a, 1:2, 1:2) for N in (1,3) @test isa(reshape(a, Val(N)), Array{Int,N}) @@ -372,8 +371,8 @@ end @test_throws BoundsError insert!(v, 5, 5) end @testset "concatenation" begin - @test isequal([ones(2,2) 2*ones(2,1)], [1. 1 2; 1 1 2]) - @test isequal([ones(2,2); 2*ones(1,2)], [1. 1; 1 1; 2 2]) + @test isequal([fill(1.,2,2) fill(2.,2,1)], [1. 1 2; 1 1 2]) + @test isequal([fill(1.,2,2); fill(2.,1,2)], [1. 1; 1 1; 2 2]) end @testset "typed array literals" begin @@ -468,7 +467,7 @@ end @test find(!iszero, (i % 2 for i in 1:10)) == collect(1:2:9) end @testset "findn" begin - b = findn(ones(2,2,2,2)) + b = findn(fill(1,2,2,2,2)) @test (length(b[1]) == 16) @test (length(b[2]) == 16) @test (length(b[3]) == 16) @@ -600,7 +599,7 @@ Base.hash(::HashCollision, h::UInt) = h # All rows and columns unique let A, B, C, D - A = ones(10, 10) + A = fill(1., 10, 10) A[diagind(A)] = shuffle!([1:10;]) @test unique(A, 1) == A @test unique(A, 2) == A @@ -630,7 +629,7 @@ end @testset "repmat and repeat" begin local A, A1, A2, A3, v, v2, cv, cv2, c, R, T - A = ones(Int,2,3,4) + A = fill(1,2,3,4) A1 = reshape(repmat([1,2],1,12),2,3,4) A2 = reshape(repmat([1 2 3],2,4),2,3,4) A3 = reshape(repmat([1 2 3 4],6,1),2,3,4) @@ -895,7 +894,7 @@ end @test_throws BoundsError A[[true, false, true], [false, true, true, false]] @test_throws BoundsError A[[true, false, true, true], [false, false, true, true, false]] @test_throws BoundsError A[[true, false, true], [false, false, true, true, false, true]] - A = ones(Int, 3, 5) + A = fill(1, 3, 5) @test_throws DimensionMismatch A[2,[true, false, true, true, false]] = 2:5 A[2,[true, false, true, true, false]] = 2:4 @test A == [1 1 1 1 1; 2 1 3 4 1; 1 1 1 1 1] @@ -963,7 +962,7 @@ end end # issue #3613 - b = mapslices(sum, ones(2,3,4), [1,2]) + b = mapslices(sum, fill(1.,2,3,4), [1,2]) @test size(b) === (1,1,4) @test all(b.==6) @@ -978,26 +977,26 @@ end # issue #5177 - c = ones(2,3,4) - m1 = mapslices(x-> ones(2,3), c, [1,2]) - m2 = mapslices(x-> ones(2,4), c, [1,3]) - m3 = mapslices(x-> ones(3,4), c, [2,3]) + c = fill(1,2,3,4) + m1 = mapslices(x-> fill(1,2,3), c, [1,2]) + m2 = mapslices(x-> fill(1,2,4), c, [1,3]) + m3 = mapslices(x-> fill(1,3,4), c, [2,3]) @test size(m1) == size(m2) == size(m3) == size(c) - n1 = mapslices(x-> ones(6), c, [1,2]) - n2 = mapslices(x-> ones(6), c, [1,3]) - n3 = mapslices(x-> ones(6), c, [2,3]) - n1a = mapslices(x-> ones(1,6), c, [1,2]) - n2a = mapslices(x-> ones(1,6), c, [1,3]) - n3a = mapslices(x-> ones(1,6), c, [2,3]) + n1 = mapslices(x-> fill(1,6), c, [1,2]) + n2 = mapslices(x-> fill(1,6), c, [1,3]) + n3 = mapslices(x-> fill(1,6), c, [2,3]) + n1a = mapslices(x-> fill(1,1,6), c, [1,2]) + n2a = mapslices(x-> fill(1,1,6), c, [1,3]) + n3a = mapslices(x-> fill(1,1,6), c, [2,3]) @test size(n1a) == (1,6,4) && size(n2a) == (1,3,6) && size(n3a) == (2,1,6) @test size(n1) == (6,1,4) && size(n2) == (6,3,1) && size(n3) == (2,6,1) # mutating functions - o = ones(3, 4) + o = fill(1, 3, 4) m = mapslices(x->fill!(x, 0), o, 2) @test m == zeros(3, 4) - @test o == ones(3, 4) + @test o == fill(1, 3, 4) # issue #18524 m = mapslices(x->tuple(x), [1 2; 3 4], 1) @@ -1056,10 +1055,10 @@ end @test lexless(asc[:,2],asc[:,3]) # mutating functions - o = ones(3, 4) + o = fill(1, 3, 4) m = mapslices(x->fill!(x, 0), o, 2) @test m == zeros(3, 4) - @test o == ones(3, 4) + @test o == fill(1, 3, 4) asr = sortrows(a, rev=true) @test lexless(asr[2,:],asr[1,:]) @@ -1099,7 +1098,7 @@ end @testset "fill" begin @test fill!(Float64[1.0], -0.0)[1] === -0.0 - A = ones(3,3) + A = fill(1.,3,3) S = view(A, 2, 1:3) fill!(S, 2) S = view(A, 1:2, 3) @@ -1189,7 +1188,7 @@ end X = [ i+2j for i=1:5, j=1:5 ] @test X[2,3] == 8 @test X[4,5] == 14 - @test isequal(ones(2,3) * ones(2,3)', [3. 3.; 3. 3.]) + @test isequal(fill(3.,2,2), [3. 3.; 3. 3.]) # @test isequal([ [1,2] for i=1:2, : ], [1 2; 1 2]) # where element type is a Union. try to confuse type inference. foo32_64(x) = (x<2) ? Int32(x) : Int64(x) @@ -1478,10 +1477,10 @@ end @test mdsum(a) == 2 @test mdsum2(a) == 2 - a = ones(0,5) + a = Matrix{Float64}(uninitialized,0,5) b = view(a, :, :) @test mdsum(b) == 0 - a = ones(5,0) + a = Matrix{Float64}(uninitialized,5,0) b = view(a, :, :) @test mdsum(b) == 0 end @@ -1703,30 +1702,28 @@ end end @testset "simple transposes" begin - a = ones(Complex,1,5) - b = zeros(Complex,5) - c = ones(Complex,2,5) - d = ones(Complex,6) - @test_throws DimensionMismatch transpose!(a,d) - @test_throws DimensionMismatch transpose!(d,a) - @test_throws DimensionMismatch adjoint!(a,d) - @test_throws DimensionMismatch adjoint!(d,a) - @test_throws DimensionMismatch transpose!(b,c) - @test_throws DimensionMismatch adjoint!(b,c) - @test_throws DimensionMismatch transpose!(c,b) - @test_throws DimensionMismatch adjoint!(c,b) - transpose!(b,a) - @test b == ones(Complex,5) - b = ones(Complex,5) - a = zeros(Complex,1,5) - transpose!(a,b) - @test a == ones(Complex,1,5) - b = zeros(Complex,5) - adjoint!(b,a) - @test b == ones(Complex,5) - a = zeros(Complex,1,5) - adjoint!(a,b) - @test a == ones(Complex,1,5) + o5 = fill(1.0+0im,5); o1x5 = fill(1.0+0im,1,5); + z5 = fill(0.0+0im,5); z1x5 = fill(0.0+0im,1,5); + o2x5 = fill(1.0+0im,2,5) + o6 = fill(1.0+0im,6) + @test_throws DimensionMismatch transpose!(o1x5,o6) + @test_throws DimensionMismatch transpose!(o6,o1x5) + @test_throws DimensionMismatch adjoint!(o1x5,o6) + @test_throws DimensionMismatch adjoint!(o6,o1x5) + @test_throws DimensionMismatch transpose!(o5,o2x5) + @test_throws DimensionMismatch adjoint!(o5,o2x5) + @test_throws DimensionMismatch transpose!(o2x5,o5) + @test_throws DimensionMismatch adjoint!(o2x5,o5) + + transpose!(z5,o1x5) + @test z5 == o5 + transpose!(z1x5,o5) + @test z1x5 == o1x5 + fill!(z5, 0);fill!(z1x5, 0) + adjoint!(z5,o1x5) + @test z5 == o5 + adjoint!(z1x5,o5) + @test z1x5 == o1x5 end @testset "bounds checking for copyto!" begin @@ -1848,8 +1845,8 @@ end fill!(B, 2) @test all(x->x==2, B) -iall = (1:size(A,1)).*ones(Int,size(A,2))' -jall = ones(Int,size(A,1)).*(1:size(A,2))' +iall = repmat(1:size(A,1), 1, size(A,2)) +jall = repmat((1:size(A,2))', size(A,1), 1) i,j = findn(B) @test vec(i) == vec(iall) @test vec(j) == vec(jall) @@ -1992,7 +1989,7 @@ function f15894(d) end s end -@test f15894(ones(Int, 100)) == 100 +@test f15894(fill(1, 100)) == 100 end @testset "sign, conj, ~" begin @@ -2058,8 +2055,8 @@ end # module AutoRetType @testset "concatenations of dense matrices/vectors yield dense matrices/vectors" begin N = 4 - densevec = ones(N) - densemat = diagm(0 => ones(N)) + densevec = fill(1., N) + densemat = Matrix(1.0I, N, N) # Test that concatenations of homogeneous pairs of either dense matrices or dense vectors # (i.e., Matrix-Matrix concatenations, and Vector-Vector concatenations) yield dense arrays for densearray in (densevec, densemat) diff --git a/test/bitarray.jl b/test/bitarray.jl index 7fa0d42f7ad56..549891e1966f6 100644 --- a/test/bitarray.jl +++ b/test/bitarray.jl @@ -93,8 +93,8 @@ timesofar("conversions") @test isequal(fill!(b1, false), falses(size(b1))) for (sz,T) in allsizes - @test isequal(Array(trues(sz...)), ones(Bool, sz...)) - @test isequal(Array(falses(sz...)), zeros(Bool, sz...)) + @test isequal(Array(trues(sz...)), fill(true, sz...)) + @test isequal(Array(falses(sz...)), fill(false, sz...)) b1 = rand!(falses(sz...)) @test isa(b1, T) @@ -113,7 +113,7 @@ timesofar("conversions") for n in [1; 1023:1025] b1 = falses(n) for m in [1; 10; 1023:1025] - u1 = ones(Bool, m) + u1 = fill(true, m) for fu! in [u->fill!(u, true), u->rand!(u)] fu!(u1) c1 = convert(Vector{Int}, u1) diff --git a/test/broadcast.jl b/test/broadcast.jl index 1aef1408031e5..e8c17fda643b7 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -553,16 +553,16 @@ end # Test that broadcasting identity where the input and output Array shapes do not match # yields the correct result, not merely a partial copy. See pull request #19895 for discussion. let N = 5 - @test iszero(ones(N, N) .= zeros(N, N)) - @test iszero(ones(N, N) .= zeros(N, 1)) - @test iszero(ones(N, N) .= zeros(1, N)) - @test iszero(ones(N, N) .= zeros(1, 1)) + @test iszero(fill(1, N, N) .= zeros(N, N)) + @test iszero(fill(1, N, N) .= zeros(N, 1)) + @test iszero(fill(1, N, N) .= zeros(1, N)) + @test iszero(fill(1, N, N) .= zeros(1, 1)) end @testset "test broadcast for matrix of matrices" begin - A = fill(zeros(2,2), 4, 4) - A[1:3,1:3] .= [ones(2,2)] - @test all(A[1:3,1:3] .== [ones(2,2)]) + A = fill([0 0; 0 0], 4, 4) + A[1:3,1:3] .= [[1 1; 1 1]] + @test all(A[1:3,1:3] .== [[1 1; 1 1]]) end # Test that broadcast does not confuse eltypes. See also diff --git a/test/core.jl b/test/core.jl index 407d6c137257f..2eca75fcd9430 100644 --- a/test/core.jl +++ b/test/core.jl @@ -2217,7 +2217,7 @@ function issue7897!(data, arr) end let - a = ones(UInt8, 10) + a = fill(0x01, 10) sa = view(a, 4:6) # This can throw an error, but shouldn't segfault try @@ -4050,7 +4050,7 @@ end # `TypeVar`) without crashing let function arrayset_unknown_dim(::Type{T}, n) where T - Base.arrayset(true, reshape(Vector{T}(uninitialized, 1), ones(Int, n)...), 2, 1) + Base.arrayset(true, reshape(Vector{T}(uninitialized, 1), fill(1, n)...), 2, 1) end arrayset_unknown_dim(Any, 1) arrayset_unknown_dim(Any, 2) diff --git a/test/fastmath.jl b/test/fastmath.jl index b4b78a2c46a66..28af23e81932f 100644 --- a/test/fastmath.jl +++ b/test/fastmath.jl @@ -188,8 +188,8 @@ end end end @testset "issue #10544" begin - a = ones(2,2) - b = ones(2,2) + a = fill(1.,2,2) + b = fill(1.,2,2) @test @fastmath(a[1] += 2.0) ≈ (b[1] += 2.0) @test @fastmath(a[2] -= 2.0) ≈ (b[2] -= 2.0) @test @fastmath(a[1,1] *= 2.0) ≈ (b[1,1] *= 2.0) @@ -203,7 +203,7 @@ end @testset "issue #23218" begin a = zeros(1) - b = ones(1) + b = [1.0] idx = (1,) @fastmath a[idx...] += b[idx...] @test a == b diff --git a/test/generic_map_tests.jl b/test/generic_map_tests.jl index 53d816a3b95f9..fb2444134f072 100644 --- a/test/generic_map_tests.jl +++ b/test/generic_map_tests.jl @@ -80,5 +80,5 @@ function run_map_equivalence_tests(mapf) testmap_equivalence(mapf, (x,y,z)->x+y+z, 1,2,3) testmap_equivalence(mapf, x->x ? false : true, BitMatrix(uninitialized, 10,10)) testmap_equivalence(mapf, x->"foobar", BitMatrix(uninitialized, 10,10)) - testmap_equivalence(mapf, (x,y,z)->string(x,y,z), BitVector(uninitialized, 10), ones(10), "1234567890") + testmap_equivalence(mapf, (x,y,z)->string(x,y,z), BitVector(uninitialized, 10), fill(1.0, 10), "1234567890") end diff --git a/test/hashing.jl b/test/hashing.jl index 0da3f859a3a85..afd6ddecf87b8 100644 --- a/test/hashing.jl +++ b/test/hashing.jl @@ -81,7 +81,7 @@ vals = Any[ 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), Matrix(1.0I, 2, 2), sparse(1.0I, 2, 2), - sparse(ones(2, 2)), ones(2, 2), sparse([0 0; 1 0]), [0 0; 1 0], + sparse(fill(1., 2, 2)), fill(1., 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.]), # issue #16364 1:4, 1:1:4, 1:-1:0, 1.0:4.0, 1.0:1.0:4.0, linspace(1, 4, 4), diff --git a/test/iobuffer.jl b/test/iobuffer.jl index 00de2463ad727..a46b9f44f0c81 100644 --- a/test/iobuffer.jl +++ b/test/iobuffer.jl @@ -223,7 +223,7 @@ end # issue #11917 # (previous tests triggered this sometimes, but this should trigger nearly all the time) let io = IOBuffer(0) - write(io, ones(UInt8, 1048577)) + write(io, fill(0x01, 1048577)) end let bstream = BufferStream() diff --git a/test/linalg/bidiag.jl b/test/linalg/bidiag.jl index 86a2dacc61a55..d48d55cb10554 100644 --- a/test/linalg/bidiag.jl +++ b/test/linalg/bidiag.jl @@ -176,9 +176,10 @@ srand(1) @test norm(x-tx,Inf) <= 4*condT*max(eps()*norm(tx,Inf), eps(promty)*norm(x,Inf)) @test_throws DimensionMismatch T\Transpose(b) end - @test_throws DimensionMismatch T \ ones(elty,n+1,2) - @test_throws DimensionMismatch Transpose(T) \ ones(elty,n+1,2) - @test_throws DimensionMismatch T' \ ones(elty,n+1,2) + offsizemat = Matrix{elty}(uninitialized, n+1, 2) + @test_throws DimensionMismatch T \ offsizemat + @test_throws DimensionMismatch Transpose(T) \ offsizemat + @test_throws DimensionMismatch T' \ offsizemat let bb = b, cc = c for atype in ("Array", "SubArray") @@ -192,7 +193,7 @@ srand(1) end x = T \ b tx = Tfull \ b - @test_throws DimensionMismatch Base.LinAlg.naivesub!(T,ones(elty,n+1)) + @test_throws DimensionMismatch Base.LinAlg.naivesub!(T,Vector{elty}(uninitialized,n+1)) @test norm(x-tx,Inf) <= 4*condT*max(eps()*norm(tx,Inf), eps(promty)*norm(x,Inf)) @testset "Generic Mat-vec ops" begin @test T*b ≈ Tfull*b @@ -291,7 +292,7 @@ end @test Bidiagonal(1:3, 1:2, :U) == [1 1 0; 0 2 2; 0 0 3] @testset "promote_rule" begin - A = Bidiagonal(ones(Float32,10),ones(Float32,9),:U) + A = Bidiagonal(fill(1f0,10),fill(1f0,9),:U) B = rand(Float64,10,10) C = Tridiagonal(rand(Float64,9),rand(Float64,10),rand(Float64,9)) @test promote_rule(Matrix{Float64}, Bidiagonal{Float64}) == Matrix{Float64} diff --git a/test/linalg/blas.jl b/test/linalg/blas.jl index 1c877e6464bcc..e8abb22888a50 100644 --- a/test/linalg/blas.jl +++ b/test/linalg/blas.jl @@ -35,13 +35,13 @@ srand(100) end end - o4 = ones(elty, 4) + o4 = fill(elty(1), 4) z4 = zeros(elty, 4) I4 = Matrix{elty}(I, 4, 4) I43 = Matrix{elty}(I, 4, 3) - L4 = tril(ones(elty, (4,4))) - U4 = triu(ones(elty, (4,4))) + L4 = tril(fill(elty(1), 4,4)) + U4 = triu(fill(elty(1), 4,4)) Z4 = zeros(elty, (4,4)) elm1 = convert(elty, -1) @@ -121,7 +121,7 @@ srand(100) A = triu(rand(elty,n,n)) @testset "Vector and SubVector" for x in (rand(elty, n), view(rand(elty,2n),1:2:2n)) @test A\x ≈ BLAS.trsv('U','N','N',A,x) - @test_throws DimensionMismatch BLAS.trsv('U','N','N',A,ones(elty,n+1)) + @test_throws DimensionMismatch BLAS.trsv('U','N','N',A,Vector{elty}(uninitialized,n+1)) end end @testset "ger, her, syr" for x in (rand(elty, n), view(rand(elty,2n), 1:2:2n)), @@ -131,20 +131,20 @@ srand(100) α = rand(elty) @test BLAS.ger!(α,x,y,copy(A)) ≈ A + α*x*y' - @test_throws DimensionMismatch BLAS.ger!(α,ones(elty,n+1),y,copy(A)) + @test_throws DimensionMismatch BLAS.ger!(α,Vector{elty}(uninitialized,n+1),y,copy(A)) A = rand(elty,n,n) A = A + transpose(A) @test issymmetric(A) @test triu(BLAS.syr!('U',α,x,copy(A))) ≈ triu(A + α*x*Transpose(x)) - @test_throws DimensionMismatch BLAS.syr!('U',α,ones(elty,n+1),copy(A)) + @test_throws DimensionMismatch BLAS.syr!('U',α,Vector{elty}(uninitialized,n+1),copy(A)) if elty <: Complex A = rand(elty,n,n) A = A + adjoint(A) α = real(α) @test triu(BLAS.her!('U',α,x,copy(A))) ≈ triu(A + α*x*x') - @test_throws DimensionMismatch BLAS.her!('U',α,ones(elty,n+1),copy(A)) + @test_throws DimensionMismatch BLAS.her!('U',α,Vector{elty}(uninitialized,n+1),copy(A)) end end @testset "copy" begin @@ -167,31 +167,34 @@ srand(100) Asymm = A + transpose(A) @testset "symv and hemv" begin @test BLAS.symv('U',Asymm,x) ≈ Asymm*x - @test_throws DimensionMismatch BLAS.symv!('U',one(elty),Asymm,x,one(elty),ones(elty,n+1)) - @test_throws DimensionMismatch BLAS.symv('U',ones(elty,n,n+1),x) + offsizevec, offsizemat = Array{elty}.(uninitialized,(n+1, (n,n+1))) + @test_throws DimensionMismatch BLAS.symv!('U',one(elty),Asymm,x,one(elty),offsizevec) + @test_throws DimensionMismatch BLAS.symv('U',offsizemat,x) if elty <: BlasComplex @test BLAS.hemv('U',Aherm,x) ≈ Aherm*x - @test_throws DimensionMismatch BLAS.hemv('U',ones(elty,n,n+1),x) - @test_throws DimensionMismatch BLAS.hemv!('U',one(elty),Aherm,x,one(elty),ones(elty,n+1)) + @test_throws DimensionMismatch BLAS.hemv('U',offsizemat,x) + @test_throws DimensionMismatch BLAS.hemv!('U',one(elty),Aherm,x,one(elty),offsizevec) end end @testset "symm error throwing" begin - @test_throws DimensionMismatch BLAS.symm('L','U',ones(elty,n,n-1),rand(elty,n,n)) - @test_throws DimensionMismatch BLAS.symm('R','U',ones(elty,n-1,n),rand(elty,n,n)) - @test_throws DimensionMismatch BLAS.symm!('L','U',one(elty),Asymm,ones(elty,n,n),one(elty),rand(elty,n-1,n)) - @test_throws DimensionMismatch BLAS.symm!('L','U',one(elty),Asymm,ones(elty,n,n),one(elty),rand(elty,n,n-1)) + Cnn, Cnm, Cmn = Matrix{elty}.(uninitialized,((n,n), (n,n-1), (n-1,n))) + @test_throws DimensionMismatch BLAS.symm('L','U',Cnm,Cnn) + @test_throws DimensionMismatch BLAS.symm('R','U',Cmn,Cnn) + @test_throws DimensionMismatch BLAS.symm!('L','U',one(elty),Asymm,Cnn,one(elty),Cmn) + @test_throws DimensionMismatch BLAS.symm!('L','U',one(elty),Asymm,Cnn,one(elty),Cnm) if elty <: BlasComplex - @test_throws DimensionMismatch BLAS.hemm('L','U',ones(elty,n,n-1),rand(elty,n,n)) - @test_throws DimensionMismatch BLAS.hemm('R','U',ones(elty,n-1,n),rand(elty,n,n)) - @test_throws DimensionMismatch BLAS.hemm!('L','U',one(elty),Aherm,ones(elty,n,n),one(elty),rand(elty,n-1,n)) - @test_throws DimensionMismatch BLAS.hemm!('L','U',one(elty),Aherm,ones(elty,n,n),one(elty),rand(elty,n,n-1)) + @test_throws DimensionMismatch BLAS.hemm('L','U',Cnm,Cnn) + @test_throws DimensionMismatch BLAS.hemm('R','U',Cmn,Cnn) + @test_throws DimensionMismatch BLAS.hemm!('L','U',one(elty),Aherm,Cnn,one(elty),Cmn) + @test_throws DimensionMismatch BLAS.hemm!('L','U',one(elty),Aherm,Cnn,one(elty),Cnm) end end end @testset "trmm error throwing" begin - @test_throws DimensionMismatch BLAS.trmm('L','U','N','N',one(elty),triu(rand(elty,n,n)),ones(elty,n+1,n)) - @test_throws DimensionMismatch BLAS.trmm('R','U','N','N',one(elty),triu(rand(elty,n,n)),ones(elty,n,n+1)) + Cnn, Cmn, Cnm = Matrix{elty}.(uninitialized,((n,n), (n+1,n), (n,n+1))) + @test_throws DimensionMismatch BLAS.trmm('L','U','N','N',one(elty),triu(Cnn),Cmn) + @test_throws DimensionMismatch BLAS.trmm('R','U','N','N',one(elty),triu(Cnn),Cnm) end #trsm diff --git a/test/linalg/bunchkaufman.jl b/test/linalg/bunchkaufman.jl index 2b264d502e311..41395fcaaf4c1 100644 --- a/test/linalg/bunchkaufman.jl +++ b/test/linalg/bunchkaufman.jl @@ -104,9 +104,9 @@ bimg = randn(n,2)/2 end end if eltya <: BlasReal - As1 = ones(eltya, n, n) - As2 = complex(ones(eltya, n, n)) - As3 = complex(ones(eltya, n, n)) + As1 = fill(eltya(1), n, n) + As2 = fill(complex(eltya(1)), n, n) + As3 = fill(complex(eltya(1)), n, n) As3[end, 1] += im/2 As3[1, end] -= im/2 for As = (As1, As2, As3) @@ -120,7 +120,7 @@ bimg = randn(n,2)/2 @test bks == "Failed factorization of type $(typeof(F))" @test det(F) == 0 @test_throws LinAlg.SingularException inv(F) - @test_throws LinAlg.SingularException F \ ones(size(As, 1)) + @test_throws LinAlg.SingularException F \ fill(1., size(As,1)) end end end diff --git a/test/linalg/cholesky.jl b/test/linalg/cholesky.jl index fb7dc8065ea5a..b95c54ea85a1a 100644 --- a/test/linalg/cholesky.jl +++ b/test/linalg/cholesky.jl @@ -221,9 +221,9 @@ end F = cholfact(Hermitian(AcA, uplo)) G = cholfact(Hermitian(BcB, uplo)) @test Base.getproperty(LinAlg.lowrankupdate(F, v), uplo) ≈ Base.getproperty(G, uplo) - @test_throws DimensionMismatch LinAlg.lowrankupdate(F, ones(eltype(v), length(v)+1)) + @test_throws DimensionMismatch LinAlg.lowrankupdate(F, Vector{eltype(v)}(uninitialized,length(v)+1)) @test Base.getproperty(LinAlg.lowrankdowndate(G, v), uplo) ≈ Base.getproperty(F, uplo) - @test_throws DimensionMismatch LinAlg.lowrankdowndate(G, ones(eltype(v), length(v)+1)) + @test_throws DimensionMismatch LinAlg.lowrankdowndate(G, Vector{eltype(v)}(uninitialized,length(v)+1)) end end diff --git a/test/linalg/dense.jl b/test/linalg/dense.jl index 55baf6ee2fae9..9bc2a5e3fce29 100644 --- a/test/linalg/dense.jl +++ b/test/linalg/dense.jl @@ -62,7 +62,7 @@ bimg = randn(n,2)/2 @test_throws DimensionMismatch b'\b @test_throws DimensionMismatch b\b' @test norm(a*x - b, 1)/norm(b) < ε*κ*n*2 # Ad hoc, revisit! - @test zeros(eltya,n)\ones(eltya,n) ≈ (zeros(eltya,n,1)\ones(eltya,n,1))[1,1] + @test zeros(eltya,n)\fill(eltya(1),n) ≈ (zeros(eltya,n,1)\fill(eltya(1),n,1))[1,1] end @testset "Test nullspace" begin @@ -82,7 +82,7 @@ bimg = randn(n,2)/2 @test a[:,1:n1]*pinva15*a[:,1:n1] ≈ a[:,1:n1] @test pinva15*a[:,1:n1]*pinva15 ≈ pinva15 - @test size(pinv(ones(eltya,0,0))) == (0,0) + @test size(pinv(Matrix{eltya}(uninitialized,0,0))) == (0,0) end @testset "Lyapunov/Sylvester" begin @@ -154,7 +154,7 @@ end mmat = 10 nmat = 8 @testset "For $elty" for elty in (Float32, Float64, BigFloat, Complex{Float32}, Complex{Float64}, Complex{BigFloat}, Int32, Int64, BigInt) - x = ones(elty,10) + x = fill(elty(1),10) @testset "Vector" begin xs = view(x,1:2:10) @test norm(x, -Inf) ≈ 1 @@ -234,7 +234,7 @@ end end @testset "Matrix (Operator)" begin - A = ones(elty,10,10) + A = fill(elty(1),10,10) As = view(A,1:5,1:5) @test norm(A, 1) ≈ 10 elty <: Union{BigFloat,Complex{BigFloat},BigInt} || @test norm(A, 2) ≈ 10 @@ -744,7 +744,7 @@ end end @testset "Least squares solutions" begin - a = [ones(20) 1:20 1:20] + a = [fill(1, 20) 1:20 1:20] b = reshape(Matrix(1.0I, 8, 5), 20, 2) @testset "Tests for type $elty" for elty in (Float32, Float64, ComplexF32, ComplexF64) a = convert(Matrix{elty}, a) diff --git a/test/linalg/diagonal.jl b/test/linalg/diagonal.jl index f1680f903ae63..2c8fb4cab6539 100644 --- a/test/linalg/diagonal.jl +++ b/test/linalg/diagonal.jl @@ -111,7 +111,7 @@ srand(1) @test rdiv!(Uc, Transpose(D)) ≈ target atol=atol_three @test rdiv!(Uc, Adjoint(conj(D))) ≈ target atol=atol_three @test ldiv!(D, Matrix{eltype(D)}(I, size(D))) ≈ D \ Matrix{eltype(D)}(I, size(D)) atol=atol_three - @test_throws DimensionMismatch ldiv!(D, ones(elty, n + 1)) + @test_throws DimensionMismatch ldiv!(D, fill(elty(1), n + 1)) @test_throws SingularException ldiv!(Diagonal(zeros(relty, n)), copy(v)) b = rand(elty, n, n) b = sparse(b) @@ -251,7 +251,7 @@ srand(1) @test issymmetric(D2) @test ishermitian(D2) if elty <: Complex - dc = d + im*convert(Vector{elty}, ones(n)) + dc = d .+ elty(1im) D3 = Diagonal(dc) @test issymmetric(D3) @test !ishermitian(D3) @@ -328,11 +328,11 @@ end # Issue 12803 for t in (Float32, Float64, Int, Complex{Float64}, Rational{Int}) - @test Diagonal(Matrix{t}[ones(t, 2, 2), ones(t, 3, 3)])[2,1] == zeros(t, 3, 2) + @test Diagonal(Matrix{t}[fill(t(1), 2, 2), fill(t(1), 3, 3)])[2,1] == zeros(t, 3, 2) end # Issue 15401 -@test Matrix(1.0I, 5, 5) \ Diagonal(ones(5)) == Matrix(I, 5, 5) +@test Matrix(1.0I, 5, 5) \ Diagonal(fill(1.,5)) == Matrix(I, 5, 5) @testset "Triangular and Diagonal" begin for T in (LowerTriangular(randn(5,5)), LinAlg.UnitLowerTriangular(randn(5,5))) diff --git a/test/linalg/generic.jl b/test/linalg/generic.jl index aea5dc2bf7a6d..6e3ed0f8a6a57 100644 --- a/test/linalg/generic.jl +++ b/test/linalg/generic.jl @@ -151,9 +151,9 @@ end @testset "diag" begin A = Matrix(1.0I, 4, 4) - @test diag(A) == ones(4) - @test diag(view(A, 1:3, 1:3)) == ones(3) - @test diag(view(A, 1:2, 1:2)) == ones(2) + @test diag(A) == fill(1, 4) + @test diag(view(A, 1:3, 1:3)) == fill(1, 3) + @test diag(view(A, 1:2, 1:2)) == fill(1, 2) @test_throws ArgumentError diag(rand(10)) end @@ -169,16 +169,17 @@ end @test_throws DimensionMismatch Base.LinAlg.axpy!(α,x,collect(1:3),y,collect(1:5)) end -@test !issymmetric(ones(5,3)) -@test !ishermitian(ones(5,3)) -@test cross(ones(3),ones(3)) == zeros(3) +@test !issymmetric(fill(1,5,3)) +@test !ishermitian(fill(1,5,3)) +@test (x = fill(1,3); cross(x,x) == zeros(3)) -@test trace(Bidiagonal(ones(5),zeros(4),:U)) == 5 +@test trace(Bidiagonal(fill(1,5),fill(0,4),:U)) == 5 @testset "array and subarray" begin aa = reshape([1.:6;], (2,3)) for a in (aa, view(aa, 1:2, 1:2)) + am, an = size(a) @testset "2-argument version of scale!" begin @test scale!(copy(a), 5.) == a*5 @test scale!(5., copy(a)) == a*5 @@ -188,16 +189,10 @@ end @test scale!(copy(subB), 5.) == subB*5 @test scale!([1.; 2.], copy(a)) == a.*[1; 2] @test scale!([1; 2], copy(a)) == a.*[1; 2] - @test_throws DimensionMismatch scale!(ones(3), a) - if isa(a, Array) - @test scale!(copy(a), [1.; 2.; 3.]) == a.*[1 2 3] - @test scale!(copy(a), [1; 2; 3]) == a.*[1 2 3] - @test_throws DimensionMismatch scale!(a, ones(2)) - else - @test scale!(copy(a), [1.; 2.]) == a.*[1 2] - @test scale!(copy(a), [1; 2]) == a.*[1 2] - @test_throws DimensionMismatch scale!(a, ones(3)) - end + @test scale!(copy(a), Vector(1.:an)) == a.*Vector(1:an)' + @test scale!(copy(a), Vector(1:an)) == a.*Vector(1:an)' + @test_throws DimensionMismatch scale!(Vector{Float64}(uninitialized,am+1), a) + @test_throws DimensionMismatch scale!(a, Vector{Float64}(uninitialized,an+1)) end @testset "3-argument version of scale!" begin @@ -205,18 +200,11 @@ end @test scale!(similar(a), a, 5.) == a*5 @test scale!(similar(a), [1.; 2.], a) == a.*[1; 2] @test scale!(similar(a), [1; 2], a) == a.*[1; 2] - @test_throws DimensionMismatch scale!(similar(a), ones(3), a) - @test_throws DimensionMismatch scale!(Matrix{Float64}(uninitialized, 3, 2), a, ones(3)) - - if isa(a, Array) - @test scale!(similar(a), a, [1.; 2.; 3.]) == a.*[1 2 3] - @test scale!(similar(a), a, [1; 2; 3]) == a.*[1 2 3] - @test_throws DimensionMismatch scale!(similar(a), a, ones(2)) - else - @test scale!(similar(a), a, [1.; 2.]) == a.*[1 2] - @test scale!(similar(a), a, [1; 2]) == a.*[1 2] - @test_throws DimensionMismatch scale!(similar(a), a, ones(3)) - end + @test_throws DimensionMismatch scale!(similar(a), Vector{Float64}(uninitialized, am+1), a) + @test_throws DimensionMismatch scale!(Matrix{Float64}(uninitialized, 3, 2), a, Vector{Float64}(uninitialized, an+1)) + @test_throws DimensionMismatch scale!(similar(a), a, Vector{Float64}(uninitialized, an+1)) + @test scale!(similar(a), a, Vector(1.:an)) == a.*Vector(1:an)' + @test scale!(similar(a), a, Vector(1:an)) == a.*Vector(1:an)' end end end @@ -267,7 +255,7 @@ end @test norm([2.4e-322, 4.4e-323]) ≈ 2.47e-322 @test norm([2.4e-322, 4.4e-323], 3) ≈ 2.4e-322 -@test_throws ArgumentError norm(ones(5,5),5) +@test_throws ArgumentError norm(Matrix{Float64}(uninitialized,5,5),5) @testset "generic vecnorm for arrays of arrays" begin x = Vector{Int}[[1,2], [3,4]] @@ -293,7 +281,7 @@ end @testset "LinAlg.axpy! for x and y of different dimensions" begin α = 5 x = 2:5 - y = ones(Int, 2, 4) + y = fill(1, 2, 4) rx = [1 4] ry = [2 8] @test LinAlg.axpy!(α, x, rx, y, ry) == [1 1 1 1; 11 1 1 26] diff --git a/test/linalg/lapack.jl b/test/linalg/lapack.jl index de22763d54d2d..5c9185b744f89 100644 --- a/test/linalg/lapack.jl +++ b/test/linalg/lapack.jl @@ -26,8 +26,7 @@ import Base.LinAlg.BlasInt @test LAPACK.syevr!('N','V','U',copy(Asym),0.0,1.0,4,5,-1.0)[1] ≈ vals[vals .< 1.0] @test LAPACK.syevr!('N','I','U',copy(Asym),0.0,1.0,4,5,-1.0)[1] ≈ vals[4:5] @test vals ≈ LAPACK.syev!('N','U',copy(Asym)) - - @test_throws DimensionMismatch LAPACK.sygvd!(1,'V','U',copy(Asym),ones(elty,6,6)) + @test_throws DimensionMismatch LAPACK.sygvd!(1,'V','U',copy(Asym),Matrix{elty}(uninitialized,6,6)) end end @@ -105,7 +104,7 @@ end D = LAPACK.gbtrs!('N',2,1,6,AB,ipiv,D) A = diagm(-2 => dl2, -1 => dl, 0 => d, 1 => du) @test A\C ≈ D - @test_throws DimensionMismatch LAPACK.gbtrs!('N',2,1,6,AB,ipiv,ones(elty,7,6)) + @test_throws DimensionMismatch LAPACK.gbtrs!('N',2,1,6,AB,ipiv,Matrix{elty}(uninitialized,7,6)) @test_throws Base.LinAlg.LAPACKException LAPACK.gbtrf!(2,1,6,zeros(elty,6,6)) end end @@ -113,29 +112,31 @@ end @testset "geqp3, geqrt error handling" begin @testset for elty in (Float32, Float64, ComplexF32, ComplexF64) - A = rand(elty,10,10) - @test_throws DimensionMismatch LAPACK.geqlf!(A,zeros(elty,11)) - @test_throws DimensionMismatch LAPACK.gelqf!(A,zeros(elty,11)) - @test_throws DimensionMismatch LAPACK.geqp3!(A,ones(Base.LinAlg.BlasInt,11),zeros(elty,10)) - @test_throws DimensionMismatch LAPACK.geqp3!(A,ones(Base.LinAlg.BlasInt,10),zeros(elty,11)) - @test_throws ArgumentError LAPACK.geqrt!(A,zeros(elty,11,10)) - @test_throws DimensionMismatch LAPACK.geqrt3!(A,zeros(elty,11,10)) - @test_throws DimensionMismatch LAPACK.geqrt3!(ones(elty,10,11),zeros(elty,11,11)) - @test_throws DimensionMismatch LAPACK.geqrf!(A,zeros(elty,11)) - @test_throws DimensionMismatch LAPACK.gerqf!(A,zeros(elty,11)) + x10, x11 = Vector{elty}.(uninitialized, (10, 11)) + y10, y11 = Vector{Base.LinAlg.BlasInt}.(uninitialized, (10, 11)) + A10x10, A11x10, A10x11, A11x11 = Matrix{elty}.(uninitialized, ((10,10), (11,10), (10,11), (11,11))) + @test_throws DimensionMismatch LAPACK.geqlf!(A10x10, x11) + @test_throws DimensionMismatch LAPACK.gelqf!(A10x10, x11) + @test_throws DimensionMismatch LAPACK.geqp3!(A10x10, y11, x10) + @test_throws DimensionMismatch LAPACK.geqp3!(A10x10, y10, x11) + @test_throws ArgumentError LAPACK.geqrt!(A10x10, A11x10) + @test_throws DimensionMismatch LAPACK.geqrt3!(A10x10, A11x10) + @test_throws DimensionMismatch LAPACK.geqrt3!(A10x11, A11x11) + @test_throws DimensionMismatch LAPACK.geqrf!(A10x10, x11) + @test_throws DimensionMismatch LAPACK.gerqf!(A10x10, x11) end end @testset "gels, gesv, getrs, getri error handling" begin @testset for elty in (Float32, Float64, ComplexF32, ComplexF64) - A = rand(elty,10,10) - B = rand(elty,11,11) - @test_throws DimensionMismatch LAPACK.gels!('N',A,B) - @test_throws DimensionMismatch LAPACK.gels!('T',A,B) - @test_throws DimensionMismatch LAPACK.gesv!(A,B) - @test_throws DimensionMismatch LAPACK.getrs!('N',A,ones(Base.LinAlg.BlasInt,10),B) - @test_throws DimensionMismatch LAPACK.getrs!('T',A,ones(Base.LinAlg.BlasInt,10),B) - @test_throws DimensionMismatch LAPACK.getri!(A,ones(Base.LinAlg.BlasInt,11)) + A10x10, B11x11 = Matrix{elty}.(uninitialized, ((10,10), (11,11))) + x10, x11 = Vector{Base.LinAlg.BlasInt}.(uninitialized, (10, 11)) + @test_throws DimensionMismatch LAPACK.gels!('N',A10x10,B11x11) + @test_throws DimensionMismatch LAPACK.gels!('T',A10x10,B11x11) + @test_throws DimensionMismatch LAPACK.gesv!(A10x10,B11x11) + @test_throws DimensionMismatch LAPACK.getrs!('N',A10x10,x10,B11x11) + @test_throws DimensionMismatch LAPACK.getrs!('T',A10x10,x10,B11x11) + @test_throws DimensionMismatch LAPACK.getri!(A10x10,x11) end end @@ -257,11 +258,13 @@ end d = rand(elty,10) dl = rand(elty,9) b = rand(elty,10) - @test_throws DimensionMismatch LAPACK.gttrf!(zeros(elty,11),d,du) - @test_throws DimensionMismatch LAPACK.gttrf!(dl,d,zeros(elty,11)) - @test_throws DimensionMismatch LAPACK.gttrs!('N',zeros(elty,11),d,du,ones(elty,9),zeros(Base.LinAlg.BlasInt,10),b) - @test_throws DimensionMismatch LAPACK.gttrs!('N',dl,d,zeros(elty,11),ones(elty,9),zeros(Base.LinAlg.BlasInt,10),b) - @test_throws DimensionMismatch LAPACK.gttrs!('N',dl,d,du,ones(elty,9),zeros(Base.LinAlg.BlasInt,10),zeros(elty,11)) + y10 = Vector{BlasInt}(uninitialized, 10) + x9, x11 = Vector{elty}.(uninitialized, (9, 11)) + @test_throws DimensionMismatch LAPACK.gttrf!(x11, d, du) + @test_throws DimensionMismatch LAPACK.gttrf!(dl, d, x11) + @test_throws DimensionMismatch LAPACK.gttrs!('N', x11, d, du, x9, y10, b) + @test_throws DimensionMismatch LAPACK.gttrs!('N', dl, d, x11, x9, y10, b) + @test_throws DimensionMismatch LAPACK.gttrs!('N', dl, d, du, x9, y10, x11) A = lufact(Tridiagonal(dl,d,du)) b = rand(elty,10,5) c = copy(b) @@ -467,7 +470,7 @@ end @testset "ptsv" begin @testset for elty in (Float32, Float64, ComplexF32, ComplexF64) - dv = ones(elty,10) + dv = fill(elty(1),10) ev = zeros(elty,9) rdv = real(dv) A = SymTridiagonal(dv,ev) @@ -477,14 +480,14 @@ end B = rand(elty,10,10) C = copy(B) @test A\B ≈ LAPACK.ptsv!(rdv,ev,C) - @test_throws DimensionMismatch LAPACK.ptsv!(rdv,ones(elty,10),C) - @test_throws DimensionMismatch LAPACK.ptsv!(rdv,ev,ones(elty,11,11)) + @test_throws DimensionMismatch LAPACK.ptsv!(rdv,Vector{elty}(uninitialized,10),C) + @test_throws DimensionMismatch LAPACK.ptsv!(rdv,ev,Matrix{elty}(uninitialized,11,11)) end end @testset "pttrf and pttrs" begin @testset for elty in (Float32, Float64, ComplexF32, ComplexF64) - dv = ones(elty,10) + dv = fill(elty(1),10) ev = zeros(elty,9) rdv = real(dv) A = SymTridiagonal(dv,ev) @@ -497,34 +500,36 @@ end C = copy(B) if elty <: Complex @test A\B ≈ LAPACK.pttrs!('U',rdv,ev,C) - @test_throws DimensionMismatch LAPACK.pttrs!('U',rdv,ones(elty,10),C) - @test_throws DimensionMismatch LAPACK.pttrs!('U',rdv,ev,rand(elty,11,11)) + @test_throws DimensionMismatch LAPACK.pttrs!('U',rdv,Vector{elty}(uninitialized,10),C) + @test_throws DimensionMismatch LAPACK.pttrs!('U',rdv,ev,Matrix{elty}(uninitialized,11,11)) else @test A\B ≈ LAPACK.pttrs!(rdv,ev,C) - @test_throws DimensionMismatch LAPACK.pttrs!(rdv,ones(elty,10),C) - @test_throws DimensionMismatch LAPACK.pttrs!(rdv,ev,rand(elty,11,11)) + @test_throws DimensionMismatch LAPACK.pttrs!(rdv,Vector{elty}(uninitialized,10),C) + @test_throws DimensionMismatch LAPACK.pttrs!(rdv,ev,Matrix{elty}(uninitialized,11,11)) end end end @testset "posv and some errors for friends" begin @testset for elty in (Float32, Float64, ComplexF32, ComplexF64) - A = rand(elty,10,10)/100 - A += real(diagm(0 => 10*real(rand(elty,10)))) + local n = 10 + A = rand(elty,n,n)/100 + A += real(diagm(0 => n*real(rand(elty,n)))) if elty <: Complex A = A + adjoint(A) else A = A + transpose(A) end - B = rand(elty,10,10) + B = rand(elty,n,n) D = copy(A) C = copy(B) D,C = LAPACK.posv!('U',D,C) @test A\B ≈ C - @test_throws DimensionMismatch LAPACK.posv!('U',D,ones(elty,12,12)) - @test_throws DimensionMismatch LAPACK.potrs!('U',D,ones(elty,12,12)) + offsizemat = Matrix{elty}(uninitialized, n+1, n+1) + @test_throws DimensionMismatch LAPACK.posv!('U', D, offsizemat) + @test_throws DimensionMismatch LAPACK.potrs!('U', D, offsizemat) - @test LAPACK.potrs!('U',zeros(elty,0,0),ones(elty,0)) == ones(elty,0) + @test LAPACK.potrs!('U',Matrix{elty}(uninitialized,0,0),elty[]) == elty[] end end diff --git a/test/linalg/lq.jl b/test/linalg/lq.jl index 7d550a323a224..8bb610911812a 100644 --- a/test/linalg/lq.jl +++ b/test/linalg/lq.jl @@ -80,8 +80,8 @@ rectangularQ(Q::LinAlg.LQPackedQ) = convert(Array, Q) @test a'*q ≈ a'*squareQ(q) atol=100ε @test a'*q' ≈ a'*squareQ(q)' atol=100ε @test_throws DimensionMismatch q*b[1:n1 + 1] - @test_throws DimensionMismatch Adjoint(q) * ones(eltya,n+2,n+2) - @test_throws DimensionMismatch ones(eltyb,n+2,n+2)*q + @test_throws DimensionMismatch Adjoint(q) * Matrix{eltya}(uninitialized,n+2,n+2) + @test_throws DimensionMismatch Matrix{eltyb}(uninitialized,n+2,n+2)*q if isa(a, DenseArray) && isa(b, DenseArray) # use this to test 2nd branch in mult code pad_a = vcat(I, a) diff --git a/test/linalg/matmul.jl b/test/linalg/matmul.jl index 9afe483dabc14..d47543da2072e 100644 --- a/test/linalg/matmul.jl +++ b/test/linalg/matmul.jl @@ -7,13 +7,16 @@ using Base.LinAlg: mul!, Adjoint, Transpose ## Test Julia fallbacks to BLAS routines @testset "matrices with zero dimensions" begin - @test ones(0,5)*ones(5,3) == zeros(0,3) - @test ones(3,5)*ones(5,0) == zeros(3,0) - @test ones(3,0)*ones(0,4) == zeros(3,4) - @test ones(0,5)*ones(5,0) == zeros(0,0) - @test ones(0,0)*ones(0,4) == zeros(0,4) - @test ones(3,0)*ones(0,0) == zeros(3,0) - @test ones(0,0)*ones(0,0) == zeros(0,0) + for (dimsA, dimsB, dimsC) in ( + ((0,5), (5,3), (0,3)), + ((3,5), (5,0), (3,0)), + ((3,0), (0,4), (3,4)), + ((0,5), (5,0), (0,0)), + ((0,0), (0,4), (0,4)), + ((3,0), (0,0), (3,0)), + ((0,0), (0,0), (0,0)) ) + @test Matrix{Float64}(uninitialized, dimsA) * Matrix{Float64}(uninitialized, dimsB) == zeros(dimsC) + end @test Matrix{Float64}(uninitialized, 5, 0) |> t -> t't == zeros(0,0) @test Matrix{Float64}(uninitialized, 5, 0) |> t -> t*t' == zeros(5,5) @test Matrix{ComplexF64}(uninitialized, 5, 0) |> t -> t't == zeros(0,0) @@ -37,8 +40,7 @@ end @test *(Adjoint(Ai), Adjoint(Bi)) == [-28.25-66im 9.75-58im; -26-89im 21-73im] @test_throws DimensionMismatch [1 2; 0 0; 0 0] * [1 2] end - CC = ones(3, 3) - @test_throws DimensionMismatch mul!(CC, AA, BB) + @test_throws DimensionMismatch mul!(Matrix{Float64}(uninitialized,3,3), AA, BB) end @testset "3x3 matmul" begin AA = [1 2 3; 4 5 6; 7 8 9].-5 @@ -58,8 +60,7 @@ end @test *(Adjoint(Ai), Adjoint(Bi)) == [1+2im 20.75+9im -44.75+42im; 19.5+17.5im -54-36.5im 51-14.5im; 13+7.5im 11.25+31.5im -43.25-14.5im] @test_throws DimensionMismatch [1 2 3; 0 0 0; 0 0 0] * [1 2 3] end - CC = ones(4, 4) - @test_throws DimensionMismatch mul!(CC, AA, BB) + @test_throws DimensionMismatch mul!(Matrix{Float64}(uninitialized,4,4), AA, BB) end # Generic AbstractArrays @@ -84,8 +85,8 @@ end @test A*B == [-7 9; -4 9] @test *(Transpose(A), Transpose(B)) == [-6 -11 15; -6 -13 18; -6 -15 21] end - AA = ones(Int, 2, 100) - BB = ones(Int, 100, 3) + AA = fill(1, 2, 100) + BB = fill(1, 100, 3) for A in (copy(AA), view(AA, 1:2, 1:100)), B in (copy(BB), view(BB, 1:100, 1:3)) @test A*B == [100 100 100; 100 100 100] end @@ -103,8 +104,8 @@ end @test Base.LinAlg.mul!(C, Adjoint(A), Transpose(B)) == A'*Transpose(B) #test DimensionMismatch for generic_matmatmul - @test_throws DimensionMismatch Base.LinAlg.mul!(C, Adjoint(A), Transpose(ones(Int,4,4))) - @test_throws DimensionMismatch Base.LinAlg.mul!(C, Adjoint(ones(Int,4,4)), Transpose(B)) + @test_throws DimensionMismatch Base.LinAlg.mul!(C, Adjoint(A), Transpose(fill(1,4,4))) + @test_throws DimensionMismatch Base.LinAlg.mul!(C, Adjoint(fill(1,4,4)), Transpose(B)) end vv = [1,2] CC = Matrix{Int}(uninitialized, 2, 2) @@ -191,8 +192,9 @@ end Aref = Ai[1:2:2*cutoff, 1:3] @test *(Adjoint(Asub), Asub) == *(Adjoint(Aref), Aref) - @test_throws DimensionMismatch Base.LinAlg.syrk_wrapper!(zeros(5,5),'N',ones(6,5)) - @test_throws DimensionMismatch Base.LinAlg.herk_wrapper!(zeros(5,5),'N',ones(6,5)) + A5x5, A6x5 = Matrix{Float64}.(uninitialized, ((5, 5), (6, 5))) + @test_throws DimensionMismatch Base.LinAlg.syrk_wrapper!(A5x5,'N',A6x5) + @test_throws DimensionMismatch Base.LinAlg.herk_wrapper!(A5x5,'N',A6x5) end @testset "matmul for types w/o sizeof (issue #1282)" begin @@ -204,11 +206,9 @@ end end @testset "scale!" begin - AA = zeros(5, 5) - BB = ones(5) - CC = rand(5, 6) - for A in (copy(AA), view(AA, 1:5, 1:5)), B in (copy(BB), view(BB, 1:5)), C in (copy(CC), view(CC, 1:5, 1:6)) - @test_throws DimensionMismatch scale!(A, B, C) + A5x5, b5, C5x6 = Array{Float64}.(uninitialized,((5,5), 5, (5,6))) + for A in (A5x5, view(A5x5, :, :)), b in (b5, view(b5, :)), C in (C5x6, view(C5x6, :, :)) + @test_throws DimensionMismatch scale!(A, b, C) end end @@ -224,7 +224,7 @@ end @test dot(x, 1:2,y, 1:2) == convert(elty, 12.5) @test Transpose(x)*y == convert(elty, 29.0) @test_throws MethodError dot(rand(elty, 2, 2), randn(elty, 2, 2)) - X = convert(Vector{Matrix{elty}},[reshape(1:4, 2, 2), ones(2, 2)]) + X = convert(Vector{Matrix{elty}},[reshape(1:4, 2, 2), fill(1, 2, 2)]) res = convert(Matrix{elty}, [7.0 13.0; 13.0 27.0]) @test dot(X, X) == res end @@ -255,18 +255,19 @@ end A[1,2] = Matrix(1.0I, 3, 2) A[2,1] = Matrix(1.0I, 2, 3) b = Vector{Vector{Float64}}(uninitialized, 2) - b[1] = ones(3) - b[2] = ones(2) + b[1] = fill(1., 3) + b[2] = fill(1., 2) @test A*b == Vector{Float64}[[2,2,1], [2,2]] end -@test_throws ArgumentError Base.LinAlg.copytri!(ones(10,10),'Z') +@test_throws ArgumentError Base.LinAlg.copytri!(Matrix{Float64}(uninitialized,10,10),'Z') @testset "gemv! and gemm_wrapper for $elty" for elty in [Float32,Float64,ComplexF64,ComplexF32] - @test_throws DimensionMismatch Base.LinAlg.gemv!(ones(elty,10),'N',rand(elty,10,10),ones(elty,11)) - @test_throws DimensionMismatch Base.LinAlg.gemv!(ones(elty,11),'N',rand(elty,10,10),ones(elty,10)) - @test Base.LinAlg.gemv!(ones(elty,0),'N',rand(elty,0,0),rand(elty,0)) == ones(elty,0) - @test Base.LinAlg.gemv!(ones(elty,10), 'N',ones(elty,10,0),ones(elty,0)) == zeros(elty,10) + A10x10, x10, x11 = Array{elty}.(uninitialized, ((10,10), 10, 11)) + @test_throws DimensionMismatch Base.LinAlg.gemv!(x10,'N',A10x10,x11) + @test_throws DimensionMismatch Base.LinAlg.gemv!(x11,'N',A10x10,x10) + @test Base.LinAlg.gemv!(elty[], 'N', Matrix{elty}(uninitialized,0,0), elty[]) == elty[] + @test Base.LinAlg.gemv!(x10, 'N', Matrix{elty}(uninitialized,10,0), elty[]) == zeros(elty,10) I0x0 = Matrix{elty}(I, 0, 0) I10x10 = Matrix{elty}(I, 10, 10) diff --git a/test/linalg/qr.jl b/test/linalg/qr.jl index 04e3373adda08..5757c6f44b4eb 100644 --- a/test/linalg/qr.jl +++ b/test/linalg/qr.jl @@ -192,7 +192,7 @@ end end @testset "Issue 16520" begin - @test_throws DimensionMismatch ones(3,2)\(1:5) + @test_throws DimensionMismatch Matrix{Float64}(uninitialized,3,2)\(1:5) end @testset "Issue 22810" begin diff --git a/test/linalg/special.jl b/test/linalg/special.jl index bfe2ab8ea7b19..751b63c6e1de1 100644 --- a/test/linalg/special.jl +++ b/test/linalg/special.jl @@ -47,20 +47,20 @@ srand(1) for newtype in [Diagonal, Bidiagonal, SymTridiagonal, Matrix] @test Matrix(convert(newtype, A)) == Matrix(A) end - A = Tridiagonal(ones(n-1), [1.0:n;], ones(n-1)) #not morally Diagonal + A = Tridiagonal(fill(1., n-1), [1.0:n;], fill(1., n-1)) #not morally Diagonal for newtype in [SymTridiagonal, Matrix] @test Matrix(convert(newtype, A)) == Matrix(A) end for newtype in [Diagonal, Bidiagonal] @test_throws ArgumentError convert(newtype,A) end - A = Tridiagonal(zeros(n-1), [1.0:n;], ones(n-1)) #not morally Diagonal + A = Tridiagonal(zeros(n-1), [1.0:n;], fill(1., n-1)) #not morally Diagonal @test Matrix(convert(Bidiagonal, A)) == Matrix(A) - A = UpperTriangular(Tridiagonal(zeros(n-1), [1.0:n;], ones(n-1))) + A = UpperTriangular(Tridiagonal(zeros(n-1), [1.0:n;], fill(1., n-1))) @test Matrix(convert(Bidiagonal, A)) == Matrix(A) - A = Tridiagonal(ones(n-1), [1.0:n;], zeros(n-1)) #not morally Diagonal + A = Tridiagonal(fill(1., n-1), [1.0:n;], zeros(n-1)) #not morally Diagonal @test Matrix(convert(Bidiagonal, A)) == Matrix(A) - A = LowerTriangular(Tridiagonal(ones(n-1), [1.0:n;], zeros(n-1))) + A = LowerTriangular(Tridiagonal(fill(1., n-1), [1.0:n;], zeros(n-1))) @test Matrix(convert(Bidiagonal, A)) == Matrix(A) @test_throws ArgumentError convert(SymTridiagonal,A) @@ -90,7 +90,7 @@ end @test Matrix(B - C) ≈ Matrix(A - A) end end - B = SymTridiagonal(a, ones(n-1)) + B = SymTridiagonal(a, fill(1., n-1)) for Spectype in [Diagonal, Bidiagonal, Tridiagonal, Matrix] @test Matrix(B + convert(Spectype,A)) ≈ Matrix(B + A) @test Matrix(convert(Spectype,A) + B) ≈ Matrix(B + A) @@ -128,10 +128,10 @@ end @testset "concatenations of combinations of special and other matrix types" begin N = 4 # Test concatenating pairwise combinations of special matrices - diagmat = Diagonal(ones(N)) - bidiagmat = Bidiagonal(ones(N), ones(N-1), :U) - tridiagmat = Tridiagonal(ones(N-1), ones(N), ones(N-1)) - symtridiagmat = SymTridiagonal(ones(N), ones(N-1)) + diagmat = Diagonal(1:N) + bidiagmat = Bidiagonal(1:N, 1:(N-1), :U) + tridiagmat = Tridiagonal(1:(N-1), 1:N, 1:(N-1)) + symtridiagmat = SymTridiagonal(1:N, 1:(N-1)) specialmats = (diagmat, bidiagmat, tridiagmat, symtridiagmat) for specialmata in specialmats, specialmatb in specialmats @test issparse(hcat(specialmata, specialmatb)) @@ -141,7 +141,7 @@ end end # Test concatenating pairwise combinations of special matrices with sparse matrices, # dense matrices, or dense vectors - densevec = ones(N) + densevec = fill(1., N) densemat = diagm(0 => densevec) spmat = spdiagm(0 => densevec) for specialmat in specialmats @@ -181,14 +181,14 @@ end # Concatenations involving these types, un/annotated, should yield sparse arrays spvec = spzeros(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)) - symtridiagmat = SymTridiagonal(ones(N), ones(N-1)) + diagmat = Diagonal(1:N) + bidiagmat = Bidiagonal(1:N, 1:(N-1), :U) + tridiagmat = Tridiagonal(1:(N-1), 1:N, 1:(N-1)) + symtridiagmat = SymTridiagonal(1:N, 1:(N-1)) sparseconcatmats = testfull ? (spmat, diagmat, bidiagmat, tridiagmat, symtridiagmat) : (spmat, diagmat) # Concatenations involving strictly these types, un/annotated, should yield dense arrays - densevec = ones(N) - densemat = ones(N, N) + densevec = fill(1., N) + densemat = fill(1., N, N) # Annotated collections annodmats = [annot(densemat) for annot in annotations] annospcmats = [annot(spcmat) for annot in annotations, spcmat in sparseconcatmats] diff --git a/test/linalg/svd.jl b/test/linalg/svd.jl index 9d5af015d5964..ff56a00f939c3 100644 --- a/test/linalg/svd.jl +++ b/test/linalg/svd.jl @@ -62,7 +62,7 @@ a2img = randn(n,n)/2 @test usv\b ≈ a\b if eltya <: BlasFloat - svdz = svdfact!(ones(eltya,0,0)) + svdz = svdfact!(Matrix{eltya}(uninitialized,0,0)) @test svdz.U ≈ Matrix{eltya}(I, 0, 0) @test svdz.S ≈ real(zeros(eltya,0)) @test svdz.Vt ≈ Matrix{eltya}(I, 0, 0) @@ -87,7 +87,7 @@ a2img = randn(n,n)/2 @test d1 ≈ gsvd.D1 @test d2 ≈ gsvd.D2 @test q ≈ gsvd.Q - @test gsvd.a.^2 + gsvd.b.^2 ≈ ones(eltya,length(gsvd.a)) + @test gsvd.a.^2 + gsvd.b.^2 ≈ fill(1, length(gsvd.a)) #testing the other layout for D1 & D2 b = rand(eltya,n,2*n) diff --git a/test/linalg/symmetric.jl b/test/linalg/symmetric.jl index 0cc47d0b9c1bc..d05e292591ca5 100644 --- a/test/linalg/symmetric.jl +++ b/test/linalg/symmetric.jl @@ -306,14 +306,14 @@ end @test Hermitian(aherm) * a ≈ aherm * a @test a * Hermitian(aherm) ≈ a * aherm @test Hermitian(aherm) * Hermitian(aherm) ≈ aherm*aherm - @test_throws DimensionMismatch Hermitian(aherm) * ones(eltya,n+1) + @test_throws DimensionMismatch Hermitian(aherm) * Vector{eltya}(uninitialized, n+1) Base.LinAlg.mul!(C,a,Hermitian(aherm)) @test C ≈ a*aherm @test Symmetric(asym) * Symmetric(asym) ≈ asym*asym @test Symmetric(asym) * a ≈ asym * a @test a * Symmetric(asym) ≈ a * asym - @test_throws DimensionMismatch Symmetric(asym) * ones(eltya,n+1) + @test_throws DimensionMismatch Symmetric(asym) * Vector{eltya}(uninitialized, n+1) Base.LinAlg.mul!(C,a,Symmetric(asym)) @test C ≈ a*asym @@ -445,8 +445,8 @@ end @testset "inversion of Hilbert matrix" begin for T in (Float64, ComplexF64) H = T[1/(i + j - 1) for i in 1:8, j in 1:8] - @test norm(inv(Symmetric(H))*(H*ones(8)) .- 1) ≈ 0 atol = 1e-5 - @test norm(inv(Hermitian(H))*(H*ones(8)) .- 1) ≈ 0 atol = 1e-5 + @test norm(inv(Symmetric(H))*(H*fill(1., 8)) .- 1) ≈ 0 atol = 1e-5 + @test norm(inv(Hermitian(H))*(H*fill(1., 8)) .- 1) ≈ 0 atol = 1e-5 end end diff --git a/test/linalg/triangular.jl b/test/linalg/triangular.jl index 7507d129f6eaf..8a6dfcba2f0a7 100644 --- a/test/linalg/triangular.jl +++ b/test/linalg/triangular.jl @@ -243,7 +243,7 @@ for elty1 in (Float32, Float64, BigFloat, ComplexF32, ComplexF64, Complex{BigFlo @test sqrt(A1) |> t -> t*t ≈ A1 # naivesub errors - @test_throws DimensionMismatch naivesub!(A1,ones(elty1,n+1)) + @test_throws DimensionMismatch naivesub!(A1,Vector{elty1}(uninitialized,n+1)) # eigenproblems if !(elty1 in (BigFloat, Complex{BigFloat})) # Not handled yet @@ -314,7 +314,7 @@ for elty1 in (Float32, Float64, BigFloat, ComplexF32, ComplexF64, Complex{BigFlo end for eltyB in (Float32, Float64, BigFloat, ComplexF32, ComplexF64, Complex{BigFloat}) - B = convert(Matrix{eltyB}, elty1 <: Complex ? real(A1)*ones(n, n) : A1*ones(n, n)) + B = convert(Matrix{eltyB}, (elty1 <: Complex ? real(A1) : A1)*fill(1., n, n)) debug && println("elty1: $elty1, A1: $t1, B: $eltyB") @@ -357,12 +357,13 @@ for elty1 in (Float32, Float64, BigFloat, ComplexF32, ComplexF64, Complex{BigFlo @test mul!(similar(B1), Transpose(A1), B1) ≈ Transpose(A1)*B1 end #error handling - @test_throws DimensionMismatch mul!(A1, ones(eltyB,n+1)) - @test_throws DimensionMismatch mul!(ones(eltyB,n+1,n+1), A1) - @test_throws DimensionMismatch mul!(Transpose(A1), ones(eltyB,n+1)) - @test_throws DimensionMismatch mul!(Adjoint(A1), ones(eltyB,n+1)) - @test_throws DimensionMismatch mul!(ones(eltyB,n+1,n+1), Adjoint(A1)) - @test_throws DimensionMismatch mul!(ones(eltyB,n+1,n+1), Transpose(A1)) + Ann, Bmm, bm = A1, Matrix{eltyB}(uninitialized, n+1, n+1), Vector{eltyB}(uninitialized, n+1) + @test_throws DimensionMismatch mul!(Ann, bm) + @test_throws DimensionMismatch mul!(Bmm, Ann) + @test_throws DimensionMismatch mul!(Transpose(Ann), bm) + @test_throws DimensionMismatch mul!(Adjoint(Ann), bm) + @test_throws DimensionMismatch mul!(Bmm, Adjoint(Ann)) + @test_throws DimensionMismatch mul!(Bmm, Transpose(Ann)) # ... and division @test A1\B[:,1] ≈ Matrix(A1)\B[:,1] @@ -375,11 +376,12 @@ for elty1 in (Float32, Float64, BigFloat, ComplexF32, ComplexF64, Complex{BigFlo @test A1\B' ≈ Matrix(A1)\B' @test Transpose(A1)\Transpose(B) ≈ Transpose(Matrix(A1))\Transpose(B) @test A1'\B' ≈ Matrix(A1)'\B' - @test_throws DimensionMismatch A1\ones(elty1,n+2) - @test_throws DimensionMismatch A1'\ones(elty1,n+2) - @test_throws DimensionMismatch Transpose(A1)\ones(elty1,n+2) + Ann, bm = A1, Vector{elty1}(uninitialized,n+1) + @test_throws DimensionMismatch Ann\bm + @test_throws DimensionMismatch Ann'\bm + @test_throws DimensionMismatch Transpose(Ann)\bm if t1 == UpperTriangular || t1 == LowerTriangular - @test_throws Base.LinAlg.SingularException naivesub!(t1(zeros(elty1,n,n)),ones(eltyB,n)) + @test_throws Base.LinAlg.SingularException naivesub!(t1(zeros(elty1,n,n)),fill(eltyB(1),n)) end @test B/A1 ≈ B/Matrix(A1) @test B/Transpose(A1) ≈ B/Transpose(Matrix(A1)) @@ -422,7 +424,7 @@ for eltya in (Float32, Float64, ComplexF32, ComplexF64, BigFloat, Int) debug && println("Solve upper triangular system") Atri = UpperTriangular(lufact(A).U) |> t -> eltya <: Complex && eltyb <: Real ? real(t) : t # Here the triangular matrix can't be too badly conditioned - b = convert(Matrix{eltyb}, eltya <: Complex ? Matrix(Atri)*ones(n, 2) : Matrix(Atri)*ones(n, 2)) + b = convert(Matrix{eltyb}, Matrix(Atri)*fill(1., n, 2)) x = Matrix(Atri) \ b debug && println("Test error estimates") @@ -437,7 +439,7 @@ for eltya in (Float32, Float64, ComplexF32, ComplexF64, BigFloat, Int) γ = n*ε/(1 - n*ε) if eltya != BigFloat bigA = big.(Atri) - x̂ = ones(n, 2) + x̂ = fill(1., n, 2) for i = 1:size(b, 2) @test norm(x̂[:,i] - x[:,i], Inf)/norm(x̂[:,i], Inf) <= condskeel(bigA, x̂[:,i])*γ/(1 - condskeel(bigA)*γ) end @@ -450,7 +452,7 @@ for eltya in (Float32, Float64, ComplexF32, ComplexF64, BigFloat, Int) debug && println("Solve lower triangular system") Atri = UpperTriangular(lufact(A).U) |> t -> eltya <: Complex && eltyb <: Real ? real(t) : t # Here the triangular matrix can't be too badly conditioned - b = convert(Matrix{eltyb}, eltya <: Complex ? Matrix(Atri)*ones(n, 2) : Matrix(Atri)*ones(n, 2)) + b = convert(Matrix{eltyb}, Matrix(Atri)*fill(1., n, 2)) x = Matrix(Atri)\b debug && println("Test error estimates") @@ -461,12 +463,12 @@ for eltya in (Float32, Float64, ComplexF32, ComplexF64, BigFloat, Int) end debug && println("Test forward error [JIN 5705] if this is not a BigFloat") - b = eltyb == Int ? trunc.(Int,Atri*ones(n, 2)) : convert(Matrix{eltyb}, Atri*ones(eltya, n, 2)) + b = (b0 = Atri*fill(1, n, 2); convert(Matrix{eltyb}, eltyb == Int ? trunc.(b0) : b0)) x = Atri \ b γ = n*ε/(1 - n*ε) if eltya != BigFloat bigA = big.(Atri) - x̂ = ones(n, 2) + x̂ = fill(1., n, 2) for i = 1:size(b, 2) @test norm(x̂[:,i] - x[:,i], Inf)/norm(x̂[:,i], Inf) <= condskeel(bigA, x̂[:,i])*γ/(1 - condskeel(bigA)*γ) end @@ -512,7 +514,7 @@ end @test_throws ArgumentError UpperTriangular(LowerTriangular(randn(3,3))) # Issue 16196 -@test UpperTriangular(Matrix(1.0I, 3, 3)) \ view(ones(3), [1,2,3]) == ones(3) +@test UpperTriangular(Matrix(1.0I, 3, 3)) \ view(fill(1., 3), [1,2,3]) == fill(1., 3) # dimensional correctness: isdefined(Main, :TestHelpers) || @eval Main include("../TestHelpers.jl") diff --git a/test/linalg/tridiag.jl b/test/linalg/tridiag.jl index 04cadfbf4155f..2fd6b4a4ca5a0 100644 --- a/test/linalg/tridiag.jl +++ b/test/linalg/tridiag.jl @@ -42,14 +42,14 @@ end c += im*convert(Vector{elty}, randn(n - 1)) end end - @test_throws DimensionMismatch SymTridiagonal(dl, ones(elty, n + 1)) + @test_throws DimensionMismatch SymTridiagonal(dl, fill(elty(1), n+1)) @test_throws ArgumentError SymTridiagonal(rand(n, n)) @test_throws ArgumentError Tridiagonal(dl, dl, dl) @test_throws ArgumentError convert(SymTridiagonal{elty}, Tridiagonal(dl, d, du)) if elty != Int @testset "issue #1490" begin - @test det(ones(elty,3,3)) ≈ zero(elty) atol=3*eps(real(one(elty))) + @test det(fill(elty(1),3,3)) ≈ zero(elty) atol=3*eps(real(one(elty))) @test det(SymTridiagonal(elty[],elty[])) == one(elty) end end @@ -194,8 +194,8 @@ end fds = [abs.(d) for d in ds] @test abs.(A)::mat_type == mat_type(fds...) @testset "Multiplication with strided matrix/vector" begin - @test A*ones(n) ≈ Array(A)*ones(n) - @test A*ones(n, 2) ≈ Array(A)*ones(n, 2) + @test (x = fill(1.,n); A*x ≈ Array(A)*x) + @test (X = fill(1.,n,2); A*X ≈ Array(A)*X) end @testset "Binary operations" begin B = mat_type == Tridiagonal ? mat_type(a, b, c) : mat_type(b, a) @@ -215,11 +215,12 @@ end @test A*LowerTriangular(Matrix(1.0I, n, n)) ≈ fA end @testset "mul! errors" begin - @test_throws DimensionMismatch Base.LinAlg.mul!(similar(fA),A,ones(elty,n,n+1)) - @test_throws DimensionMismatch Base.LinAlg.mul!(similar(fA),A,ones(elty,n+1,n)) - @test_throws DimensionMismatch Base.LinAlg.mul!(zeros(elty,n,n),B,ones(elty,n+1,n)) - @test_throws DimensionMismatch Base.LinAlg.mul!(zeros(elty,n+1,n),B,ones(elty,n,n)) - @test_throws DimensionMismatch Base.LinAlg.mul!(zeros(elty,n,n+1),B,ones(elty,n,n)) + Cnn, Cnm, Cmn = Matrix{elty}.(uninitialized, ((n,n), (n,n+1), (n+1,n))) + @test_throws DimensionMismatch Base.LinAlg.mul!(Cnn,A,Cnm) + @test_throws DimensionMismatch Base.LinAlg.mul!(Cnn,A,Cmn) + @test_throws DimensionMismatch Base.LinAlg.mul!(Cnn,B,Cmn) + @test_throws DimensionMismatch Base.LinAlg.mul!(Cmn,B,Cnn) + @test_throws DimensionMismatch Base.LinAlg.mul!(Cnm,B,Cnn) end end if mat_type == SymTridiagonal @@ -331,8 +332,9 @@ end end @testset "convert for SymTridiagonal" begin - @test convert(SymTridiagonal{Float64},SymTridiagonal(ones(Float32,5),ones(Float32,4))) == SymTridiagonal(ones(Float64,5),ones(Float64,4)) - @test convert(AbstractMatrix{Float64},SymTridiagonal(ones(Float32,5),ones(Float32,4))) == SymTridiagonal(ones(Float64,5),ones(Float64,4)) + STF32 = SymTridiagonal{Float32}(fill(1f0, 5), fill(1f0, 4)) + @test convert(SymTridiagonal{Float64}, STF32)::SymTridiagonal{Float64} == STF32 + @test convert(AbstractMatrix{Float64}, STF32)::SymTridiagonal{Float64} == STF32 end @testset "constructors from matrix" begin diff --git a/test/linalg/uniformscaling.jl b/test/linalg/uniformscaling.jl index 74e3bd45edea7..7a9f3a1236c16 100644 --- a/test/linalg/uniformscaling.jl +++ b/test/linalg/uniformscaling.jl @@ -41,7 +41,7 @@ end @test UniformScaling(4.32) ≈ 4.3 * [1 0; 0 1] rtol=0.1 atol=0.01 norm=norm @test 4.3 * [1 0; 0 1] ≈ UniformScaling(4.32) rtol=0.1 atol=0.01 @test [4.3201 0.002;0.001 4.32009] ≈ UniformScaling(4.32) rtol=0.1 atol=0. - @test UniformScaling(4.32) ≉ 4.3*ones(2,2) rtol=0.1 atol=0.01 + @test UniformScaling(4.32) ≉ fill(4.3,2,2) rtol=0.1 atol=0.01 @test UniformScaling(4.32) ≈ 4.32 * [1 0; 0 1] end @@ -111,7 +111,7 @@ let @test @inferred(J*S) == S*λ @test @inferred(A*J) == A*λ @test @inferred(J*A) == A*λ - @test @inferred(J*ones(3)) == ones(3)*λ + @test @inferred(J*fill(1, 3)) == fill(λ, 3) @test @inferred(λ*J) === UniformScaling(λ*J.λ) @test @inferred(J*λ) === UniformScaling(λ*J.λ) @test @inferred(J/I) === J diff --git a/test/logging.jl b/test/logging.jl index 39bcc9ec73b07..658a1081d22fb 100644 --- a/test/logging.jl +++ b/test/logging.jl @@ -22,7 +22,7 @@ end @testset "Log message formatting" begin @test_logs (Info, "sum(A) = 16.0") @info begin - A = ones(4,4) + A = fill(1.0, 4, 4) "sum(A) = $(sum(A))" end x = 10.50 diff --git a/test/math.jl b/test/math.jl index 97d266253f8fa..ffe8cae351390 100644 --- a/test/math.jl +++ b/test/math.jl @@ -301,9 +301,10 @@ end STAA = Symmetric(TAA) @test Array(atanh.(STAA)) == atanh.(TAA) @test Array(asinh.(STAA)) == asinh.(TAA) - @test Array(acosh.(STAA+Symmetric(ones(2,2)))) == acosh.(TAA+ones(2,2)) - @test Array(acsch.(STAA+Symmetric(ones(2,2)))) == acsch.(TAA+ones(2,2)) - @test Array(acoth.(STAA+Symmetric(ones(2,2)))) == acoth.(TAA+ones(2,2)) + TAA .+= 1 + @test Array(acosh.(STAA)) == acosh.(TAA) + @test Array(acsch.(STAA)) == acsch.(TAA) + @test Array(acoth.(STAA)) == acoth.(TAA) end @testset "check exp2(::Integer) matches exp2(::Float)" begin diff --git a/test/mpfr.jl b/test/mpfr.jl index af3a2807dc960..ccfa446d160cf 100644 --- a/test/mpfr.jl +++ b/test/mpfr.jl @@ -543,7 +543,7 @@ end z = BigFloat(3) w = BigFloat(4) @test sum([x,y,z,w]) == BigFloat(10) - big_array = ones(BigFloat, 100) + big_array = fill(BigFloat(1), 100) @test sum(big_array) == BigFloat(100) @test sum(BigFloat[]) == BigFloat(0) end diff --git a/test/numbers.jl b/test/numbers.jl index 831398f443d31..362c474ee61e3 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -3021,8 +3021,8 @@ end # Array reduction @test !iszero([0, 1, 2, 3]) @test iszero(zeros(Int, 5)) - @test !isone(tril(ones(Int, 5, 5))) - @test !isone(triu(ones(Int, 5, 5))) + @test !isone(tril(fill(1, 5, 5))) + @test !isone(triu(fill(1, 5, 5))) @test !isone(zeros(Int, 5, 5)) @test isone(Matrix(1I, 5, 5)) @test isone(Matrix(1I, 1000, 1000)) # sizeof(X) > 2M == ISONE_CUTOFF diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 9b5168c62a6af..f27a59983eb3b 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -188,7 +188,7 @@ targets2 = ["(1.0, 1.0)", "([1.0], [1.0])", "([1.0], [1.0])"] for n = 0:4 - a = OffsetArray(ones(Float64,ntuple(d->1,n)), ntuple(identity,n)) + a = OffsetArray(fill(1.,ntuple(d->1,n)), ntuple(identity,n)) show(IOContext(io, :limit => true), MIME("text/plain"), a) @test String(take!(io)) == targets1[n+1] show(IOContext(io, :limit => true), MIME("text/plain"), (a,a)) @@ -377,7 +377,7 @@ I,J,N = findnz(z) @test var(A_3_3) == 7.5 @test std(A_3_3, 1) == OffsetArray([1 1 1], (0,A_3_3.offsets[2])) @test std(A_3_3, 2) == OffsetArray(reshape([3,3,3], (3,1)), (A_3_3.offsets[1],0)) -@test sum(OffsetArray(ones(Int,3000), -1000)) == 3000 +@test sum(OffsetArray(fill(1,3000), -1000)) == 3000 @test vecnorm(v) ≈ vecnorm(parent(v)) @test vecnorm(A) ≈ vecnorm(parent(A)) diff --git a/test/perf/kernel/bench_eu.jl b/test/perf/kernel/bench_eu.jl index e0280db040a56..6aa9bdce1b7c2 100644 --- a/test/perf/kernel/bench_eu.jl +++ b/test/perf/kernel/bench_eu.jl @@ -11,7 +11,7 @@ function bench_eu_devec(numPaths) dt = T/(steps) K = 100 - S = 100 * ones(numPaths,1) + S = fill(100.,numPaths,1) t1 = (r-0.5*sigma.^2)*dt t2 = sigma*sqrt(dt) @@ -32,7 +32,7 @@ function bench_eu_vec(numPaths) dt = T/(steps) K = 100 - S = 100 * ones(numPaths,1) + S = fill(100.,numPaths,1) t1 = (r-0.5*sigma.^2)*dt t2 = sigma*sqrt(dt) diff --git a/test/perf/kernel/getdivgrad.jl b/test/perf/kernel/getdivgrad.jl index d0db3b0eca6a9..407352be8e653 100644 --- a/test/perf/kernel/getdivgrad.jl +++ b/test/perf/kernel/getdivgrad.jl @@ -17,7 +17,7 @@ end #----------------- 1D finite difference on staggered grid function ddx(n) # generate 1D derivatives - return d = spdiags(ones(n)*[-1, 1]',[0,1],n,n+1) + return d = spdiags(repmat([-1. 1.], n),[0,1],n,n+1) end #------------- Build a diagonal matrix diff --git a/test/perf/kernel/gk.jl b/test/perf/kernel/gk.jl index 2e54e15dabe11..11b59cfbb4c0f 100644 --- a/test/perf/kernel/gk.jl +++ b/test/perf/kernel/gk.jl @@ -52,7 +52,7 @@ function gk(n, myeps) eps = myeps[KK] - e = ones(n) + e = fill(1., n) X = zeros(n) U = zeros(n) p = e./n diff --git a/test/perf/kernel/perf.jl b/test/perf/kernel/perf.jl index eabc784c32f53..4ccb0e2d590bd 100644 --- a/test/perf/kernel/perf.jl +++ b/test/perf/kernel/perf.jl @@ -33,9 +33,9 @@ include("gk.jl") @timeit gk(350,[0.1]) "gk" "Grigoriadis Khachiyan matrix games" # issue #942 -s = sparse(ones(280,280)) +s = sparse(fill(1.,280,280)) @timeit s*s "sparsemul" "Sparse matrix - sparse matrix multiplication" -s2 = sparse(rand(1:2000,10^5), kron([1:10^4;],ones(Int,10)), ones(Int,10^5), 2000, 10^4) +s2 = sparse(rand(1:2000,10^5), kron([1:10^4;],fill(1,10)), fill(1,10^5), 2000, 10^4) @timeit s2*s2' "sparsemul2" "Sparse matrix - matrix multiplication with fill-in" # issue #938 diff --git a/test/perf/micro/perf.jl b/test/perf/micro/perf.jl index 129e1de25b934..125667ed646ef 100644 --- a/test/perf/micro/perf.jl +++ b/test/perf/micro/perf.jl @@ -28,12 +28,11 @@ end ## array constructors ## -@test all(ones(200,200) .== 1) -# @timeit ones(200,200) "ones" "description" +@test all(fill(1.,200,200) .== 1) ## matmul and transpose ## -A = ones(200,200) +A = fill(1.,200,200) @test all(A*A' .== 200) # @timeit A*A' "AtA" "description" diff --git a/test/perf/shootout/spectralnorm.jl b/test/perf/shootout/spectralnorm.jl index 52b05c63381dd..3b812141650d1 100644 --- a/test/perf/shootout/spectralnorm.jl +++ b/test/perf/shootout/spectralnorm.jl @@ -27,7 +27,7 @@ function Atu(w,v) end function approximate(n) - u = ones(Float64,n) + u = fill(1., n) v = zeros(Float64,n) w = zeros(Float64,n) vv = vBv = 0 diff --git a/test/perf/sort/perf.jl b/test/perf/sort/perf.jl index bdc0dc34eaaf6..0ec5373ecc429 100644 --- a/test/perf/sort/perf.jl +++ b/test/perf/sort/perf.jl @@ -81,7 +81,7 @@ else ## All values equal name = "$(typename)_$(logsize)_$(string(s)[1:end-5])_allequal" - data1 = data[ones(Int, size)] + data1 = data[fill(1, size)] @timeit(sort!(data1, alg=s), name, "") ## QuickSort median killer diff --git a/test/perf/sparse/fem.jl b/test/perf/sparse/fem.jl index 63940cdf1cf9d..1adef267fd116 100644 --- a/test/perf/sparse/fem.jl +++ b/test/perf/sparse/fem.jl @@ -5,7 +5,7 @@ # assemble the finite-difference laplacian 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)) + fdl1 = sparse(SymTridiagonal(fill(-2., N), fill(1., N-1))) # laplace operator on the full grid return kron(sparse(1.0I, N, N), fdl1) + kron(fdl1, sparse(1.0I, N, N)) end diff --git a/test/reducedim.jl b/test/reducedim.jl index 6bbd491db2b87..36f7d23e5b1d2 100644 --- a/test/reducedim.jl +++ b/test/reducedim.jl @@ -79,9 +79,9 @@ fill!(r, -1.1) # Small arrays with init=false let A = reshape(1:15, 3, 5) - R = ones(Int, 3) + R = fill(1, 3) @test sum!(R, A, init=false) == [36,41,46] - R = ones(Int, 1, 5) + R = fill(1, 1, 5) @test sum!(R, A, init=false) == [7 16 25 34 43] end let R = [2] @@ -133,10 +133,10 @@ end @test isequal(sum(A, 2), zeros(Int, 0, 1)) @test isequal(sum(A, (1, 2)), zeros(Int, 1, 1)) @test isequal(sum(A, 3), zeros(Int, 0, 1)) - @test isequal(prod(A, 1), ones(Int, 1, 1)) - @test isequal(prod(A, 2), ones(Int, 0, 1)) - @test isequal(prod(A, (1, 2)), ones(Int, 1, 1)) - @test isequal(prod(A, 3), ones(Int, 0, 1)) + @test isequal(prod(A, 1), fill(1, 1, 1)) + @test isequal(prod(A, 2), fill(1, 0, 1)) + @test isequal(prod(A, (1, 2)), fill(1, 1, 1)) + @test isequal(prod(A, 3), fill(1, 0, 1)) @test isequal(var(A, 1), fill(NaN, 1, 1)) @test isequal(var(A, 2), fill(NaN, 0, 1)) @test isequal(var(A, (1, 2)), fill(NaN, 1, 1)) diff --git a/test/serialize.jl b/test/serialize.jl index 2f68f74fd9dbb..56420834212ec 100644 --- a/test/serialize.jl +++ b/test/serialize.jl @@ -253,9 +253,9 @@ end # SubArray create_serialization_stream() do s # slices - slc1 = view(ones(UInt8, 4), 2:3) + slc1 = view(UInt8[1,1,1,1], 2:3) serialize(s, slc1) - slc2 = view(ones(UInt8, 4, 4) .+ [0x00, 0x01, 0x02, 0x03], 1, 2:4) + slc2 = view(repmat(UInt8[1,2,3,4], 1, 4), 1, 2:4) serialize(s, slc2) seek(s, 0) diff --git a/test/show.jl b/test/show.jl index 0992c40f291ca..3d869d83d4019 100644 --- a/test/show.jl +++ b/test/show.jl @@ -713,8 +713,8 @@ end @test string(Tuple{Array}) == "Tuple{Array}" # PR #16651 -@test !contains(repr(ones(10,10)), "\u2026") -@test contains(sprint((io, x) -> show(IOContext(io, :limit => true), x), ones(30, 30)), "\u2026") +@test !contains(repr(fill(1.,10,10)), "\u2026") +@test contains(sprint((io, x) -> show(IOContext(io, :limit => true), x), fill(1.,30,30)), "\u2026") # showcompact() also sets :multiline=>false (#16817) let io = IOBuffer(), diff --git a/test/sorting.jl b/test/sorting.jl index 7384459a0c8e3..7d969627b0ca9 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -39,7 +39,7 @@ end @test searchsorted([1:10;], 1, by=(x -> x >= 5)) == 1:4 @test searchsorted([1:10;], 10, by=(x -> x >= 5)) == 5:10 @test searchsorted([1:5; 1:5; 1:5], 1, 6, 10, Forward) == 6:6 - @test searchsorted(ones(15), 1, 6, 10, Forward) == 6:10 + @test searchsorted(fill(1, 15), 1, 6, 10, Forward) == 6:10 for R in numTypes, T in numTypes @test searchsorted(R[1, 1, 2, 2, 3, 3], T(0)) == 1:0 @@ -56,7 +56,7 @@ end @test searchsorted(R[1:10;], T(1), by=(x -> x >= 5)) == 1:4 @test searchsorted(R[1:10;], T(10), by=(x -> x >= 5)) == 5:10 @test searchsorted(R[1:5; 1:5; 1:5], T(1), 6, 10, Forward) == 6:6 - @test searchsorted(ones(R, 15), T(1), 6, 10, Forward) == 6:10 + @test searchsorted(fill(R(1), 15), T(1), 6, 10, Forward) == 6:10 end for (rg,I) in [(49:57,47:59), (1:2:17,-1:19), (-3:0.5:2,-5:.5:4)] diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index de44d59d8dbd1..4727949818754 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -4,8 +4,8 @@ using Base.LinAlg: mul!, ldiv!, rdiv!, Adjoint, Transpose using Base.Printf: @printf @testset "issparse" begin - @test issparse(sparse(ones(5,5))) - @test !issparse(ones(5,5)) + @test issparse(sparse(fill(1,5,5))) + @test !issparse(fill(1,5,5)) end @testset "iszero specialization for SparseMatrixCSC" begin @@ -23,11 +23,11 @@ end end @testset "indtype" begin - @test Base.SparseArrays.indtype(sparse(ones(Int8,2),ones(Int8,2),rand(2))) == Int8 + @test Base.SparseArrays.indtype(sparse(Int8[1,1],Int8[1,1],[1,1])) == Int8 end @testset "sparse matrix construction" begin - @test isequal(Array(sparse(complex.(ones(5,5), ones(5,5)))), complex.(ones(5,5), ones(5,5))) + @test (A = fill(1.0+im,5,5); isequal(Array(sparse(A)), A)) @test_throws ArgumentError sparse([1,2,3], [1,2], [1,2,3], 3, 3) @test_throws ArgumentError sparse([1,2,3], [1,2,3], [1,2], 3, 3) @test_throws ArgumentError sparse([1,2,3], [1,2,3], [1,2,3], 0, 1) @@ -59,7 +59,7 @@ end end se33 = SparseMatrixCSC{Float64}(I, 3, 3) -do33 = ones(3) +do33 = fill(1.,3) @testset "sparse binary operations" begin @test isequal(se33 * se33, se33) @@ -84,14 +84,14 @@ end 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))) + @test [se33 se33] == [Array(se33) Array(se33)] @test length(([sp33 0I]).nzval) == 3 end @testset "vertical concatenation" begin - @test all([se33; se33] == sparse([1, 4, 2, 5, 3, 6], [1, 1, 2, 2, 3, 3], ones(6))) + @test [se33; se33] == [Array(se33); Array(se33)] se33_32bit = convert(SparseMatrixCSC{Float32,Int32}, se33) - @test all([se33; se33_32bit] == sparse([1, 4, 2, 5, 3, 6], [1, 1, 2, 2, 3, 3], ones(6))) + @test [se33; se33_32bit] == [Array(se33); Array(se33_32bit)] @test length(([sp33; 0I]).nzval) == 3 end @@ -106,7 +106,7 @@ end end @testset "blkdiag concatenation" begin - @test all(blkdiag(se33, se33) == sparse([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], ones(6))) + @test blkdiag(se33, se33) == sparse(1:6,1:6,fill(1.,6)) end @testset "concatenation promotion" begin @@ -208,7 +208,7 @@ end @test (maximum(abs.((Transpose(a)*c + d) - (Transpose(Array(a))*c + d))) < 1000*eps()) c = randn(6) + im*randn(6) @test_throws DimensionMismatch α*Transpose(a)*c + β*c - @test_throws DimensionMismatch α*Transpose(a)*ones(5) + β*c + @test_throws DimensionMismatch α*Transpose(a)*fill(1.,5) + β*c a = I + 0.1*sprandn(5, 5, 0.2) + 0.1*im*sprandn(5, 5, 0.2) b = randn(5,3) @@ -346,7 +346,7 @@ dA = Array(sA) @test scale!(copy(dAt), bi) ≈ rdiv!(copy(sAt), Diagonal(b)) @test scale!(copy(dAt), bi) ≈ rdiv!(copy(sAt), Transpose(Diagonal(b))) @test scale!(copy(dAt), conj(bi)) ≈ rdiv!(copy(sAt), Adjoint(Diagonal(b))) - @test_throws DimensionMismatch rdiv!(copy(sAt), Diagonal(ones(length(b) + 1))) + @test_throws DimensionMismatch rdiv!(copy(sAt), Diagonal(fill(1., length(b)+1))) @test_throws LinAlg.SingularException rdiv!(copy(sAt), Diagonal(zeros(length(b)))) end end @@ -540,12 +540,12 @@ end end @testset "issue #5824" begin - @test sprand(4,5,0.5).^0 == sparse(ones(4,5)) + @test sprand(4,5,0.5).^0 == sparse(fill(1,4,5)) end @testset "issue #5985" begin @test sprand(Bool, 4, 5, 0.0) == sparse(zeros(Bool, 4, 5)) - @test sprand(Bool, 4, 5, 1.00) == sparse(ones(Bool, 4, 5)) + @test sprand(Bool, 4, 5, 1.00) == sparse(fill(true, 4, 5)) sprb45nnzs = zeros(5) for i=1:5 sprb45 = sprand(Bool, 4, 5, 0.5) @@ -581,8 +581,8 @@ end @test maximum(P, (2,)) == reshape([1.0,2.0,3.0],3,1) @test maximum(P, (1,2)) == reshape([3.0],1,1) - @test maximum(sparse(-ones(3,3))) == -1 - @test minimum(sparse(ones(3,3))) == 1 + @test maximum(sparse(fill(-1,3,3))) == -1 + @test minimum(sparse(fill(1,3,3))) == 1 end @testset "unary functions" begin @@ -737,10 +737,10 @@ end @test count(!iszero, a) == 0 a[1,:] = 1 @test count(!iszero, a) == 10 - @test a[1,:] == sparse(ones(Int,10)) + @test a[1,:] == sparse(fill(1,10)) a[:,2] = 2 @test count(!iszero, a) == 19 - @test a[:,2] == 2*sparse(ones(Int,10)) + @test a[:,2] == sparse(fill(2,10)) b = copy(a) # Zero-assignment behavior of setindex!(A, v, i, j) @@ -778,10 +778,10 @@ end @test nnz(a) == 20 @test count(!iszero, a) == 11 a = copy(b) - a[1:2,:] = let c = sparse(ones(2,10)); fill!(c.nzval, 0); c; end + a[1:2,:] = let c = sparse(fill(1,2,10)); fill!(c.nzval, 0); c; end @test nnz(a) == 19 @test count(!iszero, a) == 8 - a[1:2,1:3] = let c = sparse(ones(2,3)); c[1,2] = c[2,1] = c[2,2] = 0; c; end + a[1:2,1:3] = let c = sparse(fill(1,2,3)); c[1,2] = c[2,1] = c[2,2] = 0; c; end @test nnz(a) == 20 @test count(!iszero, a) == 11 @@ -828,15 +828,15 @@ end A[1:5,1:10] = 10 A[1:5,1:10] = 10 @test count(!iszero, A) == 50 - @test A[1:5,1:10] == 10 * ones(Int, 5, 10) + @test A[1:5,1:10] == fill(10, 5, 10) A[6:10,11:20] = 0 @test count(!iszero, A) == 50 A[6:10,11:20] = 20 @test count(!iszero, A) == 100 - @test A[6:10,11:20] == 20 * ones(Int, 5, 10) + @test A[6:10,11:20] == fill(20, 5, 10) A[4:8,8:16] = 15 @test count(!iszero, A) == 121 - @test A[4:8,8:16] == 15 * ones(Int, 5, 9) + @test A[4:8,8:16] == fill(15, 5, 9) ASZ = 1000 TSZ = 800 @@ -1293,7 +1293,7 @@ end end @testset "test that sparse / sparsevec constructors work for AbstractMatrix subtypes" begin - D = Diagonal(ones(10,10)) + D = Diagonal(fill(1,10)) sm = sparse(D) sv = sparsevec(D) @@ -1367,14 +1367,14 @@ end end @testset "sparsevec" begin - local A = sparse(ones(5, 5)) - @test all(Array(sparsevec(A)) .== ones(25)) - @test all(Array(sparsevec([1:5;], 1)) .== ones(5)) + local A = sparse(fill(1, 5, 5)) + @test sparsevec(A) == fill(1, 25) + @test sparsevec([1:5;], 1) == fill(1, 5) @test_throws ArgumentError sparsevec([1:5;], [1:4;]) end @testset "sparse" begin - local A = sparse(ones(5, 5)) + local A = sparse(fill(1, 5, 5)) @test sparse(A) == A @test sparse([1:5;], [1:5;], 1) == sparse(1.0I, 5, 5) end @@ -1385,13 +1385,11 @@ end end @testset "istriu/istril" begin - local A - A = sparse(triu(rand(5, 5))) - @test istriu(A) - @test !istriu(sparse(ones(5, 5))) - A = sparse(tril(rand(5, 5))) - @test istril(A) - @test !istril(sparse(ones(5, 5))) + local A = fill(1, 5, 5) + @test istriu(sparse(triu(A))) + @test !istriu(sparse(A)) + @test istril(sparse(tril(A))) + @test !istril(sparse(A)) end @testset "droptol" begin @@ -1446,13 +1444,14 @@ end end @testset "trace" begin - @test_throws DimensionMismatch trace(sparse(ones(5,6))) + @test_throws DimensionMismatch trace(spzeros(5,6)) @test trace(sparse(1.0I, 5, 5)) == 5 end @testset "spdiagm" begin - @test spdiagm(0 => ones(2), -1 => ones(2)) == [1.0 0.0 0.0; 1.0 1.0 0.0; 0.0 1.0 0.0] - @test spdiagm(0 => ones(2), 1 => ones(2)) == [1.0 1.0 0.0; 0.0 1.0 1.0; 0.0 0.0 0.0] + x = fill(1, 2) + @test spdiagm(0 => x, -1 => x) == [1 0 0; 1 1 0; 0 1 0] + @test spdiagm(0 => x, 1 => x) == [1 1 0; 0 1 1; 0 0 0] for (x, y) in ((rand(5), rand(4)),(sparse(rand(5)), sparse(rand(4)))) @test spdiagm(-1 => x)::SparseMatrixCSC == diagm(-1 => x) @@ -1762,7 +1761,8 @@ end @test isa(factorize(triu(A)), UpperTriangular{Float64, SparseMatrixCSC{Float64, Int}}) @test factorize(tril(A)) == tril(A) @test isa(factorize(tril(A)), LowerTriangular{Float64, SparseMatrixCSC{Float64, Int}}) - @test !Base.USE_GPL_LIBS || factorize(A[:, 1:4])\ones(size(A, 1)) ≈ Array(A[:, 1:4])\ones(size(A, 1)) + C, b = A[:, 1:4], fill(1., size(A, 1)) + @test !Base.USE_GPL_LIBS || factorize(C)\b ≈ Array(C)\b @test_throws ErrorException chol(A) @test_throws ErrorException lu(A) @test_throws ErrorException eig(A) @@ -1772,15 +1772,13 @@ end @testset "issue #13792, use sparse triangular solvers for sparse triangular solves" begin local A, n, x n = 100 - A = sprandn(n, n, 0.5) + sqrt(n)*I - x = LowerTriangular(A)*ones(n) - @test LowerTriangular(A)\x ≈ ones(n) - x = UpperTriangular(A)*ones(n) - @test UpperTriangular(A)\x ≈ ones(n) + A, b = sprandn(n, n, 0.5) + sqrt(n)*I, fill(1., n) + @test LowerTriangular(A)\(LowerTriangular(A)*b) ≈ b + @test UpperTriangular(A)\(UpperTriangular(A)*b) ≈ b A[2,2] = 0 dropzeros!(A) - @test_throws LinAlg.SingularException LowerTriangular(A)\ones(n) - @test_throws LinAlg.SingularException UpperTriangular(A)\ones(n) + @test_throws LinAlg.SingularException LowerTriangular(A)\b + @test_throws LinAlg.SingularException UpperTriangular(A)\b end @testset "issue described in https://groups.google.com/forum/#!topic/julia-dev/QT7qpIpgOaA" begin @@ -1823,7 +1821,7 @@ end # matrices/vectors yield sparse arrays @testset "sparse and dense concatenations" begin N = 4 - densevec = ones(N) + densevec = fill(1., N) densemat = diagm(0 => densevec) spmat = spdiagm(0 => densevec) # Test that concatenations of pairs of sparse matrices yield sparse arrays @@ -1971,7 +1969,7 @@ end @test String(take!(io)) == string("5×3 SparseMatrixCSC{Float64,Int64} with 5 stored entries:\n [1, 1]", " = 1.0\n ⋮\n [5, 3] = 5.0") - show(ioc, MIME"text/plain"(), sparse(ones(5,3))) + show(ioc, MIME"text/plain"(), sparse(fill(1.,5,3))) @test String(take!(io)) == string("5×3 SparseMatrixCSC{Float64,$Int} with 15 stored entries:\n [1, 1]", " = 1.0\n ⋮\n [5, 3] = 1.0") @@ -1985,7 +1983,7 @@ end @test String(take!(io)) == string("6×3 SparseMatrixCSC{Float64,Int64} with 6 stored entries:\n [1, 1]", " = 1.0\n [2, 1] = 2.0\n ⋮\n [5, 3] = 5.0\n [6, 3] = 6.0") - show(ioc, MIME"text/plain"(), sparse(ones(6,3))) + show(ioc, MIME"text/plain"(), sparse(fill(1.,6,3))) @test String(take!(io)) == string("6×3 SparseMatrixCSC{Float64,$Int} with 18 stored entries:\n [1, 1]", " = 1.0\n [2, 1] = 1.0\n ⋮\n [5, 3] = 1.0\n [6, 3] = 1.0") @@ -1999,8 +1997,8 @@ end local A colptr = [1,2,3,4] rowval = [1,2,3] - nzval1 = ones(0) - nzval2 = ones(3) + nzval1 = Int[] + nzval2 = [1,1,1] A = SparseMatrixCSC(n, n, colptr, rowval, nzval1) @test nnz(A) == n @test_throws BoundsError A[n,n] @@ -2123,21 +2121,21 @@ end simA = similar(A, (6,6)) @test typeof(simA) == typeof(A) @test size(simA) == (6,6) - @test simA.colptr == ones(eltype(A.colptr), 6+1) + @test simA.colptr == fill(1, 6+1) @test length(simA.rowval) == length(A.rowval) @test length(simA.nzval) == length(A.nzval) # test similar with entry type and Dims{2} specification (preserves storage space only) simA = similar(A, Float32, (6,6)) @test typeof(simA) == SparseMatrixCSC{Float32,eltype(A.colptr)} @test size(simA) == (6,6) - @test simA.colptr == ones(eltype(A.colptr), 6+1) + @test simA.colptr == fill(1, 6+1) @test length(simA.rowval) == length(A.rowval) @test length(simA.nzval) == length(A.nzval) # test similar with entry type, index type, and Dims{2} specification (preserves storage space only) simA = similar(A, Float32, Int8, (6,6)) @test typeof(simA) == SparseMatrixCSC{Float32, Int8} @test size(simA) == (6,6) - @test simA.colptr == ones(eltype(A.colptr), 6+1) + @test simA.colptr == fill(1, 6+1) @test length(simA.rowval) == length(A.rowval) @test length(simA.nzval) == length(A.nzval) # test similar with Dims{1} specification (preserves nothing) diff --git a/test/sparse/sparsevector.jl b/test/sparse/sparsevector.jl index 98061a89f22c5..d2a822cd03226 100644 --- a/test/sparse/sparsevector.jl +++ b/test/sparse/sparsevector.jl @@ -457,9 +457,9 @@ end let N = 4 spvec = spzeros(N) spmat = spzeros(N, 1) - densevec = ones(N) - densemat = ones(N, 1) - diagmat = Diagonal(ones(4)) + densevec = fill(1., N) + densemat = fill(1., N, 1) + diagmat = Diagonal(densevec) # Test that concatenations of pairwise combinations of sparse vectors with dense # vectors/matrices, sparse matrices, or special matrices yield sparse arrays for othervecormat in (densevec, densemat, spmat) @@ -1135,11 +1135,9 @@ end for (siz, Sp) in zip(sizes, sptypes) arr = rand(Tv, siz...) sparr = Sp(arr) - fillval = rand(Tv) - fill!(sparr, fillval) - @test Array(sparr) == fillval * ones(Tv, siz...) - fill!(sparr, 0) - @test Array(sparr) == zeros(Tv, siz...) + x = rand(Tv) + @test fill!(sparr, x) == fill(x, siz) + @test fill!(sparr, 0) == fill(0, siz) end end end @@ -1229,21 +1227,21 @@ end simA = similar(A, (6,6)) @test typeof(simA) == SparseMatrixCSC{eltype(A.nzval),eltype(A.nzind)} @test size(simA) == (6,6) - @test simA.colptr == ones(eltype(A.nzind), 6+1) + @test simA.colptr == fill(1, 6+1) @test length(simA.rowval) == length(A.nzind) @test length(simA.nzval) == length(A.nzval) # test similar with entry type and Dims{2} specification (preserves storage space only) simA = similar(A, Float32, (6,6)) @test typeof(simA) == SparseMatrixCSC{Float32,eltype(A.nzind)} @test size(simA) == (6,6) - @test simA.colptr == ones(eltype(A.nzind), 6+1) + @test simA.colptr == fill(1, 6+1) @test length(simA.rowval) == length(A.nzind) @test length(simA.nzval) == length(A.nzval) # test similar with entry type, index type, and Dims{2} specification (preserves storage space only) simA = similar(A, Float32, Int8, (6,6)) @test typeof(simA) == SparseMatrixCSC{Float32, Int8} @test size(simA) == (6,6) - @test simA.colptr == ones(eltype(A.nzind), 6+1) + @test simA.colptr == fill(1, 6+1) @test length(simA.rowval) == length(A.nzind) @test length(simA.nzval) == length(A.nzval) end @@ -1259,8 +1257,8 @@ end @test Aj*0.1 == Ajview*0.1 @test 0.1*Aj == 0.1*Ajview @test Aj/0.1 == Ajview/0.1 - @test LinAlg.axpy!(1.0, Aj, sparse(ones(n))) == - LinAlg.axpy!(1.0, Ajview, sparse(ones(n))) + @test LinAlg.axpy!(1.0, Aj, sparse(fill(1., n))) == + LinAlg.axpy!(1.0, Ajview, sparse(fill(1., n))) @test LinAlg.lowrankupdate!(Matrix(1.0*I, n, n), fill(1.0, n), Aj) == LinAlg.lowrankupdate!(Matrix(1.0*I, n, n), fill(1.0, n), Ajview) end diff --git a/test/subarray.jl b/test/subarray.jl index ca84ae9347654..fb5fc30a3e7d6 100644 --- a/test/subarray.jl +++ b/test/subarray.jl @@ -438,11 +438,11 @@ end # issue #6218 - logical indexing A = rand(2, 2, 3) -msk = ones(Bool, 2, 2) +msk = fill(true, 2, 2) msk[2,1] = false sA = view(A, :, :, 1) sA[msk] = 1.0 -@test sA[msk] == ones(count(msk)) +@test sA[msk] == fill(1, count(msk)) # bounds checking upon construction; see #4044, #10296 @test_throws BoundsError view(1:10, 8:11) @@ -459,13 +459,13 @@ A = reshape(1:120, 3, 5, 8) sA = view(A, :, :, :) @test sA[[72 17; 107 117]] == [72 17; 107 117] @test sA[[99 38 119 14 76 81]] == [99 38 119 14 76 81] -@test sA[[ones(Int, 2, 2, 2); 2ones(Int, 2, 2, 2)]] == [ones(Int, 2, 2, 2); 2ones(Int, 2, 2, 2)] +@test sA[[fill(1, (2, 2, 2)); fill(2, (2, 2, 2))]] == [fill(1, (2, 2, 2)); fill(2, (2, 2, 2))] sA = view(A, 1:2, 2:3, 3:4) @test sA[(1:8)'] == [34 35 37 38 49 50 52 53] @test sA[[1 2 4 4; 6 1 1 4]] == [34 35 38 38; 50 34 34 38] # issue #11871 -let a = ones(Float64, (2,2)), +let a = fill(1., (2,2)), b = view(a, 1:2, 1:2) b[2] = 2 @test b[2] === 2.0 diff --git a/test/subtype.jl b/test/subtype.jl index ba692e341f155..3fd15f5f3f60c 100644 --- a/test/subtype.jl +++ b/test/subtype.jl @@ -1246,7 +1246,7 @@ f3_24305(x,y,z) = exp(x)+z-exp(y)-3 Fun_24305(x) = [ f1_24305(x[1],x[2],x[3]); f2_24305(x[1],x[2],x[3]); f3_24305(x[1],x[2],x[3]) ] Jac_24305(x) = [ x[2] x[1] -2*x[3] ; x[2]*x[3]-2x[1] x[1]*x[3]+2x[2] x[1]*x[2] ; exp(x[1]) -exp(x[2]) 1 ] -x_24305 = ones(3) +x_24305 = fill(1.,3) for it = 1:5 h = - \(Jac_24305(x_24305), Fun_24305(x_24305)) diff --git a/test/tuple.jl b/test/tuple.jl index b3020d5700045..74becec9e6401 100644 --- a/test/tuple.jl +++ b/test/tuple.jl @@ -64,8 +64,8 @@ end @test Tuple{Vararg{Float32}}(Float64[1,2,3]) === (1.0f0, 2.0f0, 3.0f0) @test Tuple{Int,Vararg{Float32}}(Float64[1,2,3]) === (1, 2.0f0, 3.0f0) @test Tuple{Int,Vararg{Any}}(Float64[1,2,3]) === (1, 2.0, 3.0) - @test Tuple(ones(5)) === (1.0,1.0,1.0,1.0,1.0) - @test_throws MethodError convert(Tuple, ones(5)) + @test Tuple(fill(1.,5)) === (1.0,1.0,1.0,1.0,1.0) + @test_throws MethodError convert(Tuple, fill(1.,5)) @testset "ambiguity between tuple constructors #20990" begin Tuple16Int = Tuple{Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int}