Skip to content

Fix _solve with nonisbits type #1071

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

Merged
merged 4 commits into from
Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 = "StaticArrays"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.5.2"
version = "1.5.3"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
31 changes: 19 additions & 12 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,40 @@ end
T = typeof((one(Ta)*zero(Tb) + one(Ta)*zero(Tb))/d)
@inbounds return similar_type(b, T)(
((a[2,2]*a[3,3] - a[2,3]*a[3,2])*b[1] +
(a[1,3]*a[3,2] - a[1,2]*a[3,3])*b[2] +
(a[1,2]*a[2,3] - a[1,3]*a[2,2])*b[3]) / d,
(a[1,3]*a[3,2] - a[1,2]*a[3,3])*b[2] +
(a[1,2]*a[2,3] - a[1,3]*a[2,2])*b[3]) / d,
((a[2,3]*a[3,1] - a[2,1]*a[3,3])*b[1] +
(a[1,1]*a[3,3] - a[1,3]*a[3,1])*b[2] +
(a[1,3]*a[2,1] - a[1,1]*a[2,3])*b[3]) / d,
(a[1,1]*a[3,3] - a[1,3]*a[3,1])*b[2] +
(a[1,3]*a[2,1] - a[1,1]*a[2,3])*b[3]) / d,
((a[2,1]*a[3,2] - a[2,2]*a[3,1])*b[1] +
(a[1,2]*a[3,1] - a[1,1]*a[3,2])*b[2] +
(a[1,1]*a[2,2] - a[1,2]*a[2,1])*b[3]) / d )
(a[1,2]*a[3,1] - a[1,1]*a[3,2])*b[2] +
(a[1,1]*a[2,2] - a[1,2]*a[2,1])*b[3]) / d )
end

for Sa in [(2,2), (3,3)] # not needed for Sa = (1, 1);
@eval begin
@inline function _solve(::Size{$Sa}, ::Size{Sb}, a::StaticMatrix{<:Any, <:Any, Ta}, b::StaticMatrix{<:Any, <:Any, Tb}) where {Sb, Ta, Tb}
d = det(a)
T = typeof((one(Ta)*zero(Tb) + one(Ta)*zero(Tb))/d)
c = similar(b, T)
for col = 1:Sb[2]
@inbounds c[:, col] = _solve(Size($Sa), Size($Sa[1],), a, b[:, col])
if isbitstype(T)
# This if block can be removed when https://github.com/JuliaArrays/StaticArrays.jl/pull/749 is merged.
Comment on lines +34 to +35
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

x-ref :#749
If the PR is resolved, we might not need this PR #1071.

c = similar(b, T)
for col in 1:Sb[2]
@inbounds c[:, col] = _solve(Size($Sa), Size($Sa[1],), a, b[:, col])
end
return similar_type(b, T)(c)
else
return _solve_general($(Size(Sa)), Size(Sb), a, b)
end
return similar_type(b, T)(c)
end
end # @eval
end

@inline function _solve(sa::Size, sb::Size, a::StaticMatrix, b::StaticVecOrMat)
_solve_general(sa, sb, a, b)
end


@generated function _solve(::Size{Sa}, ::Size{Sb}, a::StaticMatrix{<:Any, <:Any, Ta}, b::StaticVecOrMat{Tb}) where {Sa, Sb, Ta, Tb}
@generated function _solve_general(::Size{Sa}, ::Size{Sb}, a::StaticMatrix{<:Any, <:Any, Ta}, b::StaticVecOrMat{Tb}) where {Sa, Sb, Ta, Tb}
if Sa[1] != Sb[1]
return quote
throw(DimensionMismatch("Left and right hand side first dimensions do not match in backdivide (got sizes $Sa and $Sb)"))
Expand Down
4 changes: 2 additions & 2 deletions test/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using StaticArrays, Test, LinearAlgebra
@testset "Solving linear system" begin
@testset "Problem size: $n x $n. Matrix type: $m. Element type: $elty, Wrapper: $wrapper" for n in (1,2,3,4,5,8,15),
(m, v) in ((SMatrix{n,n}, SVector{n}), (MMatrix{n,n}, MVector{n})),
elty in (Float64, Int), wrapper in (identity, Symmetric, Hermitian)
elty in (Float64, Int, BigFloat), wrapper in (identity, Symmetric, Hermitian)

A = wrapper(elty.(rand(-99:2:99, n, n)))
b = A * elty.(rand(2:5, n))
Expand Down Expand Up @@ -33,7 +33,7 @@ end
@testset "Solving linear system (multiple RHS)" begin
@testset "Problem size: $n x $n. Matrix type: $m1. Element type: $elty" for n in (1,2,3,4,5,8,15),
(m1, m2) in ((SMatrix{n,n}, SMatrix{n,2}), (MMatrix{n,n}, MMatrix{n,2})),
elty in (Float64, Int)
elty in (Float64, Int, BigFloat)

A = elty.(rand(-99:2:99, n, n))
b = A * elty.(rand(2:5, n, 2))
Expand Down