Skip to content

Commit

Permalink
Complex multiplication with SHermitianCompact (#1264)
Browse files Browse the repository at this point in the history
* fix complex scalar times SHermitianCompact

* add tests

* implement suggestion

* bump v1.9.6
  • Loading branch information
lxvm authored Jun 27, 2024
1 parent 609aa34 commit cc1ed9c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StaticArrays"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.9.5"
version = "1.9.6"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
24 changes: 19 additions & 5 deletions src/SHermitianCompact.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,25 @@ LinearAlgebra.issymmetric(a::SHermitianCompact) = true
end
end

@inline Base.:*(a::Number, b::SHermitianCompact) = SHermitianCompact(a * b.lowertriangle)
@inline Base.:*(a::SHermitianCompact, b::Number) = SHermitianCompact(a.lowertriangle * b)

@inline Base.:/(a::SHermitianCompact, b::Number) = SHermitianCompact(a.lowertriangle / b)
@inline Base.:\(a::Number, b::SHermitianCompact) = SHermitianCompact(a \ b.lowertriangle)
@inline Base.:*(a::Real, b::SHermitianCompact) = SHermitianCompact(a * b.lowertriangle)
@inline Base.:*(a::SHermitianCompact, b::Real) = SHermitianCompact(a.lowertriangle * b)
@inline Base.:*(a::Number, b::SHermitianCompact) = a * SMatrix(b)
@inline Base.:*(a::SHermitianCompact, b::Number) = SMatrix(a) * b

@inline Base.:/(a::SHermitianCompact, b::Real) = SHermitianCompact(a.lowertriangle / b)
@inline Base.:\(a::Real, b::SHermitianCompact) = SHermitianCompact(a \ b.lowertriangle)
@inline Base.:/(a::SHermitianCompact, b::Number) = SMatrix(a) / b
@inline Base.:\(a::Number, b::SHermitianCompact) = a \ SMatrix(b)

@inline Base.muladd(scalar::Number, a::SHermitianCompact, b::StaticArray) = muladd(scalar, SMatrix(a), b)
@inline Base.muladd(a::SHermitianCompact, scalar::Number, b::StaticArray) = muladd(SMatrix(a), scalar, b)
@inline Base.muladd(scalar::Real, a::SHermitianCompact, b::StaticArray) = map((ai, bi) -> muladd(scalar, ai, bi), a, b)
@inline Base.muladd(a::SHermitianCompact, scalar::Real, b::StaticArray) = map((ai, bi) -> muladd(ai, scalar, bi), a, b)

@inline Base.FastMath.mul_fast(a::Number, b::SHermitianCompact) = Base.FastMath.mul_fast(a, SMatrix(b))
@inline Base.FastMath.mul_fast(a::SHermitianCompact, b::Number) = Base.FastMath.mul_fast(SMatrix(a), b)
@inline Base.FastMath.mul_fast(a::Real, b::SHermitianCompact) = map(c -> Base.FastMath.mul_fast(a, c), b)
@inline Base.FastMath.mul_fast(a::SHermitianCompact, b::Real) = map(c -> Base.FastMath.mul_fast(c, b), a)

@generated function _plus_uniform(::Size{S}, a::SHermitianCompact{N, T, L}, λ) where {S, N, T, L}
@assert S[1] == N
Expand Down
29 changes: 18 additions & 11 deletions test/SHermitianCompact.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,28 @@ fill3(x) = fill(3, x)

@testset "Scalar-array" begin
x = SHermitianCompact(SVector{6, Int}(1 : 6))
y = -5
for op in (:*, :/, :\)
if op != :\
@eval begin
@test $op($x, $y) == $op(SMatrix($x), $y)
@test_noalloc $op($x, $y)
for y = (-5, 1.1+4.3im)
for op in (:*, :/, :\, :(Base.FastMath.mul_fast))
if op != :\
@eval begin
@test $op($x, $y) == $op(SMatrix($x), $y)
@test_noalloc $op($x, $y)
end
end
end

if op != :/
@eval begin
@test $op($y, $x) == $op($y, SMatrix($x))
@test_noalloc $op($y, $x)
if op != :/
@eval begin
@test $op($y, $x) == $op($y, SMatrix($x))
@test_noalloc $op($y, $x)
end
end
end
@eval begin
@test muladd($y, $x, $x) == muladd($y, SMatrix($x), $x)
@test_noalloc muladd($y, $x, $x)
@test muladd($x, $y, $x) == muladd(SMatrix($x), $y, $x)
@test_noalloc muladd($x, $y, $x)
end
end
end

Expand Down

0 comments on commit cc1ed9c

Please sign in to comment.