We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5f7ef30 commit 9b6427fCopy full SHA for 9b6427f
src/solve.jl
@@ -1,7 +1,4 @@
1
-@inline (\)(a::StaticMatrix, b::StaticVector) = solve(Size(a), Size(b), a, b)
2
-
3
-# TODO: Ineffecient but requires some infrastructure (e.g. LU or QR) to make efficient so we fall back on inv for now
4
-@inline solve(::Size, ::Size, a, b) = inv(a) * b
+@inline (\)(a::StaticMatrix, b::StaticVecOrMat) = solve(Size(a), Size(b), a, b)
5
6
@inline function solve(::Size{(1,1)}, ::Size{(1,)}, a::StaticMatrix{<:Any, <:Any, Ta}, b::StaticVector{<:Any, Tb}) where {Ta, Tb}
7
@inbounds return similar_type(b, typeof(a[1] \ b[1]))(a[1] \ b[1])
@@ -28,3 +25,23 @@ end
28
25
(a[1,2]*a[3,1] - a[1,1]*a[3,2])*b[2] +
29
26
(a[1,1]*a[2,2] - a[1,2]*a[2,1])*b[3]) / d )
30
27
end
+
+@generated function solve(::Size{Sa}, ::Size{Sb}, a::StaticMatrix{<:Any, <:Any, Ta}, b::StaticVecOrMat{Tb}) where {Sa, Sb, Ta, Tb}
+ if Sa[end] != Sb[1]
31
+ throw(DimensionMismatch("right hand side B needs first dimension of size $(Sa[end]), has size $Sb"))
32
+ end
33
+ LinearAlgebra.checksquare(a)
34
+ if prod(Sa) ≤ 14*14
35
+ quote
36
+ @_inline_meta
37
+ L, U, p = lu(a)
38
+ U \ (L \ $(length(Sb) > 1 ? :(b[p,:]) : :(b[p])))
39
40
+ else
41
42
43
+ T = typeof((one(Ta)*zero(Tb) + one(Ta)*zero(Tb))/one(Ta))
44
+ similar_type(b, T)(Matrix(a) \ b)
45
46
47
+end
test/solve.jl
@@ -1,7 +1,7 @@
using StaticArrays, Compat.Test
@testset "Solving linear system" begin
- @testset "Problem size: $n x $n. Matrix type: $m. Element type: $elty" for n in (1,2,3,4),
+ @testset "Problem size: $n x $n. Matrix type: $m. Element type: $elty" for n in (1,2,3,4,5,14,15,50),
(m, v) in ((SMatrix{n,n}, SVector{n}), (MMatrix{n,n}, MVector{n})),
elty in (Float64, Int)
@@ -16,3 +16,14 @@ using StaticArrays, Compat.Test
16
@test_throws DimensionMismatch m1\v
17
@test_throws DimensionMismatch m1\m2
18
19
20
+@testset "Solving linear system (multiple RHS)" begin
21
+ @testset "Problem size: $n x $n. Matrix type: $m1. Element type: $elty" for n in (1,2,3,4,5,14,15,50),
22
+ (m1, m2) in ((SMatrix{n,n}, SMatrix{n,2}), (MMatrix{n,n}, MMatrix{n,2})),
23
+ elty in (Float64, Int)
24
+ A = elty.(rand(-99:2:99, n, n))
+ b = A * elty.(rand(2:5, n, 2))
+ @test m1(A)\m2(b) ≈ A\b
0 commit comments