@@ -119,6 +119,13 @@ default_alias_b(::Any, ::Any, ::Any) = false
119119default_alias_A (:: AbstractKrylovSubspaceMethod , :: Any , :: Any ) = true
120120default_alias_b (:: AbstractKrylovSubspaceMethod , :: Any , :: Any ) = true
121121
122+ function __init_u0_from_Ab (A, b)
123+ u0 = similar (b, size (A, 2 ))
124+ fill! (u0, false )
125+ return u0
126+ end
127+ __init_u0_from_Ab (:: SMatrix{S1, S2} , b) where {S1, S2} = zeros (SVector{S2, eltype (b)})
128+
122129function SciMLBase. init (prob:: LinearProblem , alg:: SciMLLinearSolveAlgorithm ,
123130 args... ;
124131 alias_A = default_alias_A (alg, prob. A, prob. b),
@@ -133,7 +140,7 @@ function SciMLBase.init(prob::LinearProblem, alg::SciMLLinearSolveAlgorithm,
133140 kwargs... )
134141 @unpack A, b, u0, p = prob
135142
136- A = if alias_A
143+ A = if alias_A || A isa SMatrix
137144 A
138145 elseif A isa Array || A isa SparseMatrixCSC
139146 copy (A)
@@ -143,55 +150,28 @@ function SciMLBase.init(prob::LinearProblem, alg::SciMLLinearSolveAlgorithm,
143150
144151 b = if b isa SparseArrays. AbstractSparseArray && ! (A isa Diagonal)
145152 Array (b) # the solution to a linear solve will always be dense!
146- elseif alias_b
153+ elseif alias_b || b isa SVector
147154 b
148155 elseif b isa Array || b isa SparseMatrixCSC
149156 copy (b)
150157 else
151158 deepcopy (b)
152159 end
153160
154- u0 = if u0 != = nothing
155- u0
156- else
157- u0 = similar (b, size (A, 2 ))
158- fill! (u0, false )
159- end
161+ u0_ = u0 != = nothing ? u0 : __init_u0_from_Ab (A, b)
160162
161163 # Guard against type mismatch for user-specified reltol/abstol
162164 reltol = real (eltype (prob. b))(reltol)
163165 abstol = real (eltype (prob. b))(abstol)
164166
165- cacheval = init_cacheval (alg, A, b, u0 , Pl, Pr, maxiters, abstol, reltol, verbose,
167+ cacheval = init_cacheval (alg, A, b, u0_ , Pl, Pr, maxiters, abstol, reltol, verbose,
166168 assumptions)
167169 isfresh = true
168170 Tc = typeof (cacheval)
169171
170- cache = LinearCache{
171- typeof (A),
172- typeof (b),
173- typeof (u0),
174- typeof (p),
175- typeof (alg),
176- Tc,
177- typeof (Pl),
178- typeof (Pr),
179- typeof (reltol),
180- typeof (assumptions. issq),
181- }(A,
182- b,
183- u0,
184- p,
185- alg,
186- cacheval,
187- isfresh,
188- Pl,
189- Pr,
190- abstol,
191- reltol,
192- maxiters,
193- verbose,
194- assumptions)
172+ cache = LinearCache{typeof (A), typeof (b), typeof (u0_), typeof (p), typeof (alg), Tc,
173+ typeof (Pl), typeof (Pr), typeof (reltol), typeof (assumptions. issq)}(A, b, u0_,
174+ p, alg, cacheval, isfresh, Pl, Pr, abstol, reltol, maxiters, verbose, assumptions)
195175 return cache
196176end
197177
208188function SciMLBase. solve! (cache:: LinearCache , args... ; kwargs... )
209189 solve! (cache, cache. alg, args... ; kwargs... )
210190end
191+
192+ # Special Case for StaticArrays
193+ const StaticLinearProblem = LinearProblem{uType, iip, <: SMatrix ,
194+ <: Union{<:SMatrix, <:SVector} } where {uType, iip}
195+
196+ function SciMLBase. solve (prob:: StaticLinearProblem , args... ; kwargs... )
197+ return SciMLBase. solve (prob, nothing , args... ; kwargs... )
198+ end
199+
200+ function SciMLBase. solve (prob:: StaticLinearProblem ,
201+ alg:: Union{Nothing, SciMLLinearSolveAlgorithm} , args... ; kwargs... )
202+ if alg === nothing || alg isa DirectLdiv!
203+ u = prob. A \ prob. b
204+ elseif alg isa LUFactorization
205+ u = lu (prob. A) \ prob. b
206+ elseif alg isa QRFactorization
207+ u = qr (prob. A) \ prob. b
208+ elseif alg isa CholeskyFactorization
209+ u = cholesky (prob. A) \ prob. b
210+ elseif alg isa NormalCholeskyFactorization
211+ u = cholesky (Symmetric (prob. A' * prob. A)) \ (prob. A' * prob. b)
212+ elseif alg isa SVDFactorization
213+ u = svd (prob. A) \ prob. b
214+ else
215+ # Slower Path but handles all cases
216+ cache = init (prob, alg, args... ; kwargs... )
217+ return solve! (cache)
218+ end
219+ return SciMLBase. build_linear_solution (alg, u, nothing , prob)
220+ end
0 commit comments