Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialize sqrt and cbrt #379

Merged
merged 6 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FillArrays"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "1.12.0"
version = "1.13.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
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::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)

Check warning on line 381 in src/FillArrays.jl

View check run for this annotation

Codecov / codecov/patch

src/FillArrays.jl#L378-L381

Added lines #L378 - L381 were not covered by tests
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))

Check warning on line 388 in src/FillArrays.jl

View check run for this annotation

Codecov / codecov/patch

src/FillArrays.jl#L383-L388

Added lines #L383 - L388 were not covered by tests
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)

Check warning on line 393 in src/FillArrays.jl

View check run for this annotation

Codecov / codecov/patch

src/FillArrays.jl#L390-L393

Added lines #L390 - L393 were not covered by tests
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))

Check warning on line 400 in src/FillArrays.jl

View check run for this annotation

Codecov / codecov/patch

src/FillArrays.jl#L395-L400

Added lines #L395 - L400 were not covered by tests
end

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

@testset "sqrt/cbrt" begin
F = Fill(4, 4, 4)
@test sqrt(F)^2 ≈ F
dlfivefifty marked this conversation as resolved.
Show resolved Hide resolved
F = Fill(4+4im, 4, 4)
@test sqrt(F)^2 ≈ F
F = Fill(-4, 4, 4)
@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)
@test sqrt(F)^2 == F
@test cbrt(F)^3 == F
end
Loading