Skip to content

Commit b3a7cb1

Browse files
lbenetLuEdRaMo
andauthored
Inplace multiplicacion for Matrix{Taylor1} and Matrix{TaylorN} (#403)
* Add taylormatmul! for allocation-free matrix multiplication with Taylor1 or TaylorN elements Co-authored-by: LuEdRaMo <73906617+LuEdRaMo@users.noreply.github.com> * Add some tests * Rename to matmul! * Bump patch version * Reduce tolerance of tests, and add new --------- Co-authored-by: LuEdRaMo <73906617+LuEdRaMo@users.noreply.github.com>
1 parent da2908a commit b3a7cb1

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "TaylorSeries"
22
uuid = "6aa5eb33-94cf-58f4-a9d0-e4b2c4fc25ea"
3-
version = "0.21.3"
3+
version = "0.21.4"
44
repo = "https://github.com/JuliaDiff/TaylorSeries.jl.git"
55

66
[deps]

src/arithmetic.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,3 +1461,24 @@ function lu(A::AbstractMatrix{Taylor1{T}}; check::Bool = true) where {T<:Number}
14611461
return lu!(copy_oftype(A, S), LU_RowMaximum; check = check)
14621462
end
14631463
end
1464+
1465+
1466+
# Fast allocation-free matrix multiplication
1467+
for T in (:Taylor1, :TaylorN)
1468+
@eval function matmul!(C::Matrix{$T{T}},
1469+
A::Matrix{$T{T}}, B::Matrix{$T{T}}) where {T}
1470+
mc, nc = size(C)
1471+
ma, na = size(A)
1472+
mb, nb = size(B)
1473+
@assert (na == mb && mc == ma && nc == nb)
1474+
for j in axes(C,2)
1475+
for i in axes(C,1)
1476+
TS.zero!(C[i,j])
1477+
for k in 1:na
1478+
TS.muladd!(C[i,j], A[i,k], B[k,j])
1479+
end
1480+
end
1481+
end
1482+
return nothing
1483+
end
1484+
end

test/onevariable.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,21 @@ end
691691
end
692692
end
693693

694+
@testset "Matrix multiplication with Taylor1" begin
695+
A = [Taylor1.(randn.(16)) for i=1:3, j=1:3]
696+
B = [Taylor1.(randn.(16)) for i=1:3, j=1:3]
697+
C = [Taylor1.(randn.(16)) for i=1:3, j=1:3]
698+
D = [Taylor1.(randn.(16)) for i=1:3, j=1:3]
699+
for _ = 1:4
700+
A .= [Taylor1.(randn.(16)) for i=1:3, j=1:3]
701+
B .= [Taylor1.(randn.(16)) for i=1:3, j=1:3]
702+
TS.matmul!(C, A, B)
703+
mul!(D, A, B) # usual matrix product
704+
@test all(D .≈ C)
705+
@test norm(D - C, Inf) < 1.0e-13
706+
end
707+
end
708+
694709
# @testset "Matrix multiplication for Taylor1" begin
695710
# order = 30
696711
# n1 = 100

0 commit comments

Comments
 (0)