Skip to content

Commit 7e3801f

Browse files
lbenetdpsanders
authored andcommitted
LU-factorize without pivoting for inv of Taylor1 matrices (#223)
* Add `inv` for `Matrix{Taylor1{T}}`, without pivoting in `lu` Fixes #221 * Fixes tests for inv by shifting the diagonal * Specialize `lu` instead of `inv`
1 parent b55f183 commit 7e3801f

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

src/TaylorSeries.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ using SparseArrays: SparseMatrixCSC
2222
using Markdown
2323
using Requires
2424

25-
import LinearAlgebra: norm, mul!
25+
using LinearAlgebra: norm, mul!,
26+
lu, lu!, LinearAlgebra.lutype, LinearAlgebra.copy_oftype,
27+
LinearAlgebra.issuccess
28+
# istriu, triu!, istril, tril!, UpperTriangular, LowerTriangular,
29+
# LinearAlgebra.inv!, LinearAlgebra.checksquare
30+
31+
import LinearAlgebra: norm, mul!, lu
2632

2733
import Base: ==, +, -, *, /, ^
2834

src/arithmetic.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,40 @@ function mul!(y::Vector{Taylor1{T}},
560560

561561
return y
562562
end
563+
564+
565+
# Adapted from (Julia v1.2) stdlib/v1.2/LinearAlgebra/src/dense.jl#721-734,
566+
# licensed under MIT "Expat".
567+
# Specialize a method of `inv` for Matrix{Taylor1{T}}. Simply, avoid pivoting,
568+
# since the polynomial field is not an ordered one.
569+
# function Base.inv(A::StridedMatrix{Taylor1{T}}) where T
570+
# checksquare(A)
571+
# S = Taylor1{typeof((one(T)*zero(T) + one(T)*zero(T))/one(T))}
572+
# AA = convert(AbstractArray{S}, A)
573+
# if istriu(AA)
574+
# Ai = triu!(parent(inv(UpperTriangular(AA))))
575+
# elseif istril(AA)
576+
# Ai = tril!(parent(inv(LowerTriangular(AA))))
577+
# else
578+
# # Do not use pivoting !!
579+
# Ai = inv!(lu(AA, Val(false)))
580+
# Ai = convert(typeof(parent(Ai)), Ai)
581+
# end
582+
# return Ai
583+
# end
584+
585+
# Adapted from (Julia v1.2) stdlib/v1.2/LinearAlgebra/src/lu.jl#240-253
586+
# and (Julia v1.4.0-dev) stdlib/LinearAlgebra/v1.4/src/lu.jl#270-274,
587+
# licensed under MIT "Expat".
588+
# Specialize a method of `lu` for Matrix{Taylor1{T}}, which avoids pivoting,
589+
# since the polynomial field is not an ordered one.
590+
# We can't assume an ordered field so we first try without pivoting
591+
function lu(A::AbstractMatrix{Taylor1{T}}; check::Bool = true) where T
592+
S = Taylor1{lutype(T)}
593+
F = lu!(copy_oftype(A, S), Val(false); check = false)
594+
if issuccess(F)
595+
return F
596+
else
597+
return lu!(copy_oftype(A, S), Val(true); check = check)
598+
end
599+
end

test/onevariable.jl

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,34 @@ eeuler = Base.MathConstants.e
502502
@test Taylor1{Float64}(false) == Taylor1([0.0])
503503
@test Taylor1{Int}(true) == Taylor1([1])
504504
@test Taylor1{Int}(false) == Taylor1([0])
505+
end
505506

506-
# Test use of `inv` for computation of matrix inverse of `Matrix{Taylor1{Float64}}`
507-
a = rand(3, 3)
507+
@testset "Test `inv` for `Matrix{Taylor1{Float64}}``" begin
508+
t = Taylor1(5)
509+
a = Diagonal(rand(0:10,3)) + rand(3, 3)
510+
ainv = inv(a)
508511
b = Taylor1.(a, 5)
509512
binv = inv(b)
510-
I_t1_5 = Taylor1.(Matrix{Float64}(I, size(b)), 5) # 5x5 Taylor1{Float64} identity matrix, order 5
511-
@test norm(b*binv - I_t1_5, Inf) 1e-12
512-
@test norm(binv*b - I_t1_5, Inf) 1e-12
513+
tol = 1.0e-11
514+
515+
for its = 1:10
516+
a .= Diagonal(rand(2:12,3)) + rand(3, 3)
517+
ainv .= inv(a)
518+
b .= Taylor1.(a, 5)
519+
binv .= inv(b)
520+
@test norm(binv - ainv, Inf) tol
521+
@test norm(b*binv - I, Inf) tol
522+
@test norm(binv*b - I, Inf) tol
523+
@test norm(triu(b)*inv(UpperTriangular(b)) - I, Inf) tol
524+
@test norm(inv(LowerTriangular(b))*tril(b) - I, Inf) tol
525+
526+
b .= b .+ t
527+
binv .= inv(b)
528+
@test norm(b*binv - I, Inf) tol
529+
@test norm(binv*b - I, Inf) tol
530+
@test norm(triu(b)*inv(triu(b)) - I, Inf) tol
531+
@test norm(inv(tril(b))*tril(b) - I, Inf) tol
532+
end
513533
end
514534

515535
@testset "Matrix multiplication for Taylor1" begin

0 commit comments

Comments
 (0)