Skip to content

Commit

Permalink
Merge pull request #570 from JuliaArrays/teh/mixed
Browse files Browse the repository at this point in the history
implement solve with mixed StaticArray/DynamicArray types
  • Loading branch information
timholy committed Dec 21, 2018
2 parents 4adc061 + 5445ae0 commit 241a4ae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/SDiagonal.jl
Expand Up @@ -28,6 +28,8 @@ size(::Type{<:SDiagonal{N}}, d::Int) where {N} = d > 2 ? 1 : N
\(Da::SDiagonal, Db::SDiagonal) = SDiagonal(Db.diag ./ Da.diag)
/(Da::SDiagonal, Db::SDiagonal) = SDiagonal(Da.diag ./ Db.diag )

\(D::Diagonal, B::StaticMatrix) = ldiv!(D, Matrix(B))

# override to avoid copying
diag(D::SDiagonal) = D.diag

Expand Down
3 changes: 3 additions & 0 deletions src/lu.jl
Expand Up @@ -138,3 +138,6 @@ end

# Base.lufact() interface is fairly inherently type unstable. Punt on
# implementing that, for now...

\(F::LU, v::AbstractVector) = F.U \ (F.L \ v[F.p])
\(F::LU, B::AbstractMatrix) = F.U \ (F.L \ B[F.p,:])
23 changes: 23 additions & 0 deletions test/solve.jl
Expand Up @@ -15,8 +15,21 @@ using StaticArrays, Test, LinearAlgebra
v = @SVector ones(4)
@test_throws DimensionMismatch m1\v
@test_throws DimensionMismatch m1\m2

# Mixed static/dynamic arrays
# \ specializes for Diagonal, LowerTriangular, UpperTriangular, square, and non-square
# So try all of these
@testset "Mixed static/dynamic" begin
v = @SVector([0.2,0.3])
for m in (@SMatrix([1.0 0; 0 1.0]), @SMatrix([1.0 0; 1.0 1.0]),
@SMatrix([1.0 1.0; 0 1.0]), @SMatrix([1.0 0.5; 0.25 1.0]))
# TODO: include @SMatrix([1.0 0.0 0.0; 1.0 2.0 0.5]), need qr methods
@test m \ v Array(m) \ v m \ Array(v) Array(m) \ Array(v)
end
end
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})),
Expand All @@ -25,5 +38,15 @@ end
A = elty.(rand(-99:2:99, n, n))
b = A * elty.(rand(2:5, n, 2))
@test m1(A)\m2(b) A\b

end

@testset "Mixed static/dynamic" begin
m2 = @SMatrix([0.2 0.3; 0.0 0.1])
for m1 in (@SMatrix([1.0 0; 0 1.0]), @SMatrix([1.0 0; 1.0 1.0]),
@SMatrix([1.0 1.0; 0 1.0]), @SMatrix([1.0 0.5; 0.25 1.0]))
# TODO: include @SMatrix([1.0 0.0 0.0; 1.0 2.0 0.5]), need qr methods
@test m1 \ m2 Array(m1) \ m2 m1 \ Array(m2) Array(m1) \ Array(m2)
end
end
end

0 comments on commit 241a4ae

Please sign in to comment.