Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/TensorAlgebra/Operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -472,18 +472,34 @@ end


@inline @generated function ⊙₁₂₃³(H::TensorValue{D,D²}, V::VectorValue{D}) where {D, D²}
@assert D*D == D² "Fourth- and second-order tensors size mismatch"
@assert D*D == D² "Third- and first-order tensors size mismatch"
str = ""
for j in 1:D
for i in 1:D
for k in 1:D
a = _flat_idx(j,k,D)
str *= "+H[$i,$a]*V[$k]"
a = _flat_idx(i,j,k,D)
str *= "+H[$a]*V[$k]"
end
str *= ","
end
end
Meta.parse("TensorValue{$D}($str)")
Meta.parse("TensorValue{$D,$D}($str)")
end


@inline @generated function ⊙₁¹²³(V::VectorValue{D}, H::TensorValue{D,D²}) where {D, D²}
@assert D*D == D² "First- and third-order tensors size mismatch"
str = ""
for k in 1:D
for j in 1:D
for i in 1:D
a = _flat_idx(i,j,k,D)
str *= "+V[$i]*H[$a]"
end
str *= ","
end
end
Meta.parse("TensorValue{$D,$D}($str)")
end


Expand All @@ -493,6 +509,8 @@ Gridap.TensorValues.inner(H::TensorValue{2,4}, A::TensorValue{2,2}) = H ⊙₁
Gridap.TensorValues.inner(H::TensorValue{3,9}, A::TensorValue{3,3}) = H ⊙₁₂₃²³ A
Gridap.TensorValues.inner(H::TensorValue{2,4}, V::VectorValue{2}) = H ⊙₁₂₃³ V
Gridap.TensorValues.inner(H::TensorValue{3,9}, V::VectorValue{3}) = H ⊙₁₂₃³ V
Gridap.TensorValues.inner(V::VectorValue{2}, H::TensorValue{2,4}) = V ⊙₁¹²³ H
Gridap.TensorValues.inner(V::VectorValue{3}, H::TensorValue{3,9}) = V ⊙₁¹²³ H
Gridap.TensorValues.inner(V::VectorValue, H::TensorValue) = TensorValue(V.data) ⊙ H


Expand Down Expand Up @@ -607,6 +625,10 @@ Gridap.TensorValues.dot(A::TensorValue{2,2}, B::TensorValue{4,4}) = contraction_
Gridap.TensorValues.dot(A::TensorValue{3,3}, B::TensorValue{9,9}) = contraction_IP_PJKL(A,B)
Gridap.TensorValues.dot(A::TensorValue{2,4}, B::TensorValue{2,4}) = contraction_IJK_KLP(A,B)
Gridap.TensorValues.dot(A::TensorValue{3,9}, B::TensorValue{3,9}) = contraction_IJK_KLP(A,B)
Gridap.TensorValues.dot(H::TensorValue{2,4}, V::VectorValue{2}) = H ⊙₁₂₃³ V
Gridap.TensorValues.dot(H::TensorValue{3,9}, V::VectorValue{3}) = H ⊙₁₂₃³ V
Gridap.TensorValues.dot(V::VectorValue{2}, H::TensorValue{2,4}) = V ⊙₁¹²³ H
Gridap.TensorValues.dot(V::VectorValue{3}, H::TensorValue{3,9}) = V ⊙₁¹²³ H


"""
Expand Down
41 changes: 37 additions & 4 deletions test/TestTensorAlgebra/TensorAlgebraTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ end
@test contraction_IP_PJKL(A,H) == TensorValue(7.0, 10.0, 15.0, 22.0, 23.0, 34.0, 31.0, 46.0, 39.0, 58.0, 47.0, 70.0, 55.0, 82.0, 63.0, 94.0)
@test contraction_IP_JPKL(A,H) == TensorValue(10.0, 14.0, 14.0, 20.0, 26.0, 38.0, 30.0, 44.0, 42.0, 62.0, 46.0, 68.0, 58.0, 86.0, 62.0, 92.0)

digits2(D) = [Float64(10i + 1j) for i in 1:D, j in 1:D]
digits3(D) = [Float64(100i + 10j + 1k) for i in 1:D, j in 1:D, k in 1:D]
digits4(D) = [Float64(1000i + 100j + 10k + 1l) for i in 1:D, j in 1:D, k in 1:D, l in 1:D]
digits1(D) = [Float64(i) for i in 1:D]
digits2(D) = [Float64(10i + j) for i in 1:D, j in 1:D]
digits3(D) = [Float64(100i + 10j + k) for i in 1:D, j in 1:D, k in 1:D]
digits4(D) = [Float64(1000i + 100j + 10k + l) for i in 1:D, j in 1:D, k in 1:D, l in 1:D]

function reference_IJK_KLP(A::TensorValue{3,9}, B::TensorValue{3,9})
D = size(A, 1)
Expand All @@ -136,7 +137,39 @@ end

A = TensorValue{3,9}(digits3(3)...)
B = TensorValue{3,9}(reverse(digits3(3))...)
@test A · B == reference_IJK_KLP(A,B)
@test A · B == reference_IJK_KLP(A, B)

function reference_IJK_K(A::TensorValue{3,9}, B::VectorValue{3})
D = size(A,1)
C = zeros(Float64, D, D)
for i in 1:D, j in 1:D
s = zero(Float64)
for k in 1:D
s += A[_flat_idx(i, j, k, D)] * B[k]
end
C[i, j] = s
end
TensorValue{3,3}(C...)
end

A = TensorValue{3,9}(digits3(3)...)
V = VectorValue{3}(digits1(3)...)
@test A · V == reference_IJK_K(A, V)

function reference_I_IJK(A::VectorValue{3}, B::TensorValue{3,9})
D = size(A,1)
C = zeros(Float64, D, D)
for j in 1:D, k in 1:D
s = zero(Float64)
for i in 1:D
s += A[i] * B[_flat_idx(i, j, k, D)]
end
C[j, k] = s
end
TensorValue{3,3}(C...)
end

@test V · A == reference_I_IJK(V, A)
end


Expand Down
Loading