Skip to content

Commit

Permalink
Merge branch 'master' into dl/diagconvertbug
Browse files Browse the repository at this point in the history
  • Loading branch information
dlfivefifty authored Aug 29, 2024
2 parents bac65de + 90ef3ce commit f6c723c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,32 @@ fillsimilar(a::Ones{T}, axes...) where T = Ones{T}(axes...)
fillsimilar(a::Zeros{T}, axes...) where T = Zeros{T}(axes...)
fillsimilar(a::AbstractFill, axes...) = Fill(getindex_value(a), axes...)

# functions
function Base.sqrt(a::AbstractFillMatrix{<:Union{Real, Complex}})
Base.require_one_based_indexing(a)
size(a,1) == size(a,2) || throw(DimensionMismatch("matrix is not square: dimensions are $(size(a))"))
_sqrt(a)
end
_sqrt(a::AbstractZerosMatrix) = float(a)
function _sqrt(a::AbstractFillMatrix)
n = size(a,1)
n == 0 && return float(a)
v = getindex_value(a)
Fill((v/n), axes(a))
end
function Base.cbrt(a::AbstractFillMatrix{<:Real})
Base.require_one_based_indexing(a)
size(a,1) == size(a,2) || throw(DimensionMismatch("matrix is not square: dimensions are $(size(a))"))
_cbrt(a)
end
_cbrt(a::AbstractZerosMatrix) = float(a)
function _cbrt(a::AbstractFillMatrix)
n = size(a,1)
n == 0 && return float(a)
v = getindex_value(a)
Fill(cbrt(v)/cbrt(n)^2, axes(a))
end

struct RectDiagonal{T,V<:AbstractVector{T},Axes<:Tuple{Vararg{AbstractUnitRange,2}}} <: AbstractMatrix{T}
diag::V
axes::Axes
Expand Down
32 changes: 30 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2960,8 +2960,36 @@ end
@test tril(Z, 2) === Z
end


@testset "Diagonal conversion (#389)" begin
@test convert(Diagonal{Int, Vector{Int}}, Zeros(5,5)) isa Diagonal{Int,Vector{Int}}
@test convert(Diagonal{Int, Vector{Int}}, Zeros(5,5)) == zeros(5,5)
end
end

@testset "sqrt/cbrt" begin
F = Fill(4, 4, 4)
A = Array(F)
@test sqrt(F) sqrt(A) rtol=3e-8
@test sqrt(F)^2 F
F = Fill(4+4im, 4, 4)
A = Array(F)
@test sqrt(F) sqrt(A) rtol=1e-8
@test sqrt(F)^2 F
F = Fill(-4, 4, 4)
A = Array(F)
if VERSION >= v"1.11.0-rc3"
@test cbrt(F) cbrt(A) rtol=1e-5
end
@test cbrt(F)^3 F

# avoid overflow
F = Fill(4, typemax(Int), typemax(Int))
@test sqrt(F)^2 F
@test cbrt(F)^3 F

# zeros
F = Zeros(4, 4)
A = Array(F)
@test sqrt(F) sqrt(A) atol=1e-14
@test sqrt(F)^2 == F
@test cbrt(F)^3 == F
end

0 comments on commit f6c723c

Please sign in to comment.