Skip to content

Commit

Permalink
specialize one for FillArrays (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored Sep 5, 2022
1 parent 255f641 commit 404fbb5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Base: size, getindex, setindex!, IndexStyle, checkbounds, convert,
+, -, *, /, \, diff, sum, cumsum, maximum, minimum, sort, sort!,
any, all, axes, isone, iterate, unique, allunique, permutedims, inv,
copy, vec, setindex!, count, ==, reshape, _throw_dmrs, map, zero,
show, view, in, mapreduce
show, view, in, mapreduce, one

import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag, transpose, adjoint, fill!,
dot, norm2, norm1, normInf, normMinusInf, normp, lmul!, rmul!, diagzero, AbstractTriangular, AdjointAbsVec, TransposeAbsVec,
Expand Down Expand Up @@ -336,7 +336,7 @@ axes(rd::Diagonal{<:Any,<:AbstractFill}) = (axes(rd.diag,1),axes(rd.diag,1))
axes(T::AbstractTriangular{<:Any,<:AbstractFill}) = axes(parent(T))

axes(rd::RectDiagonal) = rd.axes
size(rd::RectDiagonal) = length.(rd.axes)
size(rd::RectDiagonal) = map(length, rd.axes)

@inline function getindex(rd::RectDiagonal{T}, i::Integer, j::Integer) where T
@boundscheck checkbounds(rd, i, j)
Expand Down Expand Up @@ -551,6 +551,17 @@ zero(r::Zeros{T,N}) where {T,N} = r
zero(r::Ones{T,N}) where {T,N} = Zeros{T,N}(r.axes)
zero(r::Fill{T,N}) where {T,N} = Zeros{T,N}(r.axes)

#########
# oneunit
#########

function one(A::AbstractFill{T,2}) where {T}
Base.require_one_based_indexing(A)
m, n = size(A)
m == n || throw(ArgumentError("multiplicative identity defined only for square matrices"))
SquareEye{T}(m)
end

#########
# any/all/isone/iszero
#########
Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@ end
@test convert(Diagonal{Int}, Eye(5)) == Diagonal(ones(Int,5))
end

@testset "one" begin
@testset for A in Any[Eye(4), Zeros(4,4), Ones(4,4), Fill(3,4,4)]
B = one(A)
@test B * A == A * B == A
end
@test_throws ArgumentError one(Ones(3,4))
@test_throws ArgumentError one(Ones((3:5,4:5)))
end

@testset "Sparse vectors and matrices" begin
@test SparseVector(Zeros(5)) ==
SparseVector{Float64}(Zeros(5)) ==
Expand Down

2 comments on commit 404fbb5

@dlfivefifty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/67737

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.13.4 -m "<description of version>" 404fbb597034c2d6e6e7d62645a688789fcae259
git push origin v0.13.4

Please sign in to comment.