Skip to content

Commit 0bc4833

Browse files
committed
Specialize lu instead of inv
1 parent a620055 commit 0bc4833

File tree

3 files changed

+54
-28
lines changed

3 files changed

+54
-28
lines changed

src/TaylorSeries.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ using Markdown
2323
using Requires
2424

2525
using LinearAlgebra: norm, mul!,
26-
lu, istriu, triu!, istril, tril!, UpperTriangular, LowerTriangular,
27-
LinearAlgebra.inv!, LinearAlgebra.checksquare
26+
lu, lu!, LinearAlgebra.lutype, LinearAlgebra.copy_oftype,
27+
LinearAlgebra.issuccess
28+
# istriu, triu!, istril, tril!, UpperTriangular, LowerTriangular,
29+
# LinearAlgebra.inv!, LinearAlgebra.checksquare
2830

29-
import LinearAlgebra: norm, mul!
31+
import LinearAlgebra: norm, mul!, lu
3032

3133
import Base: ==, +, -, *, /, ^
3234

src/arithmetic.jl

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -566,18 +566,34 @@ end
566566
# licensed under MIT "Expat".
567567
# Specialize a method of `inv` for Matrix{Taylor1{T}}. Simply, avoid pivoting,
568568
# 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))))
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
577596
else
578-
# Do not use pivoting !!
579-
Ai = inv!(lu(AA, Val(false)))
580-
Ai = convert(typeof(parent(Ai)), Ai)
597+
return lu!(copy_oftype(A, S), Val(true); check = check)
581598
end
582-
return Ai
583599
end

test/onevariable.jl

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -506,22 +506,30 @@ end
506506

507507
@testset "Test `inv` for `Matrix{Taylor1{Float64}}``" begin
508508
t = Taylor1(5)
509-
510509
a = Diagonal(rand(0:10,3)) + rand(3, 3)
510+
ainv = inv(a)
511511
b = Taylor1.(a, 5)
512512
binv = inv(b)
513-
@test norm(binv - inv(a), Inf) 1e-11
514-
@test norm(b*binv - I, Inf) 1e-11
515-
@test norm(binv*b - I, Inf) 1e-11
516-
@test norm(triu(b)*inv(UpperTriangular(b)) - I, Inf) 1e-11
517-
@test norm(inv(LowerTriangular(b))*tril(b) - I, Inf) 1e-11
518-
519-
b .= b .+ t
520-
binv = inv(b)
521-
@test norm(b*binv - I, Inf) 1e-11
522-
@test norm(binv*b - I, Inf) 1e-11
523-
@test norm(triu(b)*inv(triu(b)) - I, Inf) 1e-11
524-
@test norm(inv(tril(b))*tril(b) - I, Inf) 1e-11
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
525533
end
526534

527535
@testset "Matrix multiplication for Taylor1" begin

0 commit comments

Comments
 (0)