Skip to content

Commit

Permalink
Fix _solve with nonisbits type (#1071)
Browse files Browse the repository at this point in the history
* fix indents

* add BigFloat in solve test

* add support for non-isbits type in _solve

* bump version to v1.5.3
  • Loading branch information
hyrodium authored Aug 14, 2022
1 parent 8d88f0c commit f9c10d2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 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.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.
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

2 comments on commit f9c10d2

@hyrodium
Copy link
Collaborator Author

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 created: JuliaRegistries/General/66218

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 v1.5.3 -m "<description of version>" f9c10d2f25213d1376678a11230ef71274cd0281
git push origin v1.5.3

Please sign in to comment.