From c64872a919dd7ca09195d8175b5c5205b7565123 Mon Sep 17 00:00:00 2001 From: Jesus Bonilla Date: Thu, 11 Jun 2020 19:33:04 +0200 Subject: [PATCH 1/2] Added cross product operation for vectors * Implemented the cross product for R2 and R3 VectorValue gridap types * Added tests to assess its implementation --- src/TensorValues/Operations.jl | 16 +++++++++++++++- test/TensorValuesTests/OperationsTests.jl | 11 +++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/TensorValues/Operations.jl b/src/TensorValues/Operations.jl index ed965f828..77f427c9e 100644 --- a/src/TensorValues/Operations.jl +++ b/src/TensorValues/Operations.jl @@ -260,6 +260,21 @@ end const ⊗ = outer +############################################################### +# Cross Product +############################################################### + +@generated function cross(a::MultiValue{Tuple{3}}, b::MultiValue{Tuple{3}}) + str = "a[2]b[3]-a[3]b[2], a[3]b[1]-a[1]b[3], a[1]b[2]-a[2]b[1]" + Meta.parse("VectorValue{3}($str)") +end + +@generated function cross(a::MultiValue{Tuple{2}}, b::MultiValue{Tuple{2}}) + Meta.parse("a[1]b[2]-a[2]b[1]") +end + +cross(a::MultiValue,b::MultiValue) = error("Cross product only defined for R2 and R3 vectors") + ############################################################### # Linear Algebra ############################################################### @@ -450,4 +465,3 @@ for op in (:inner,:outer)#,:(:)) ($op)(a::Function, b::GridapType) = operate($op,a,b) end end - diff --git a/test/TensorValuesTests/OperationsTests.jl b/test/TensorValuesTests/OperationsTests.jl index 2fb8e86db..40566282d 100644 --- a/test/TensorValuesTests/OperationsTests.jl +++ b/test/TensorValuesTests/OperationsTests.jl @@ -263,6 +263,17 @@ c = outer(e,k) @test tr(c) == VectorValue(50,110) +# Cross product + +a = VectorValue(1,2,3) +b = VectorValue(3,1,6) +c = VectorValue(9,3,-5) +@test a × b == c + +a = VectorValue(2,3) +b = VectorValue(5,2) +@test a × b == -11 + # Linear Algebra t = TensorValue(10,2,30,4,5,6,70,8,9) From cfbb71d1ba58214425b207fffa2491914861c5a4 Mon Sep 17 00:00:00 2001 From: Jesus Bonilla Date: Fri, 12 Jun 2020 13:15:37 +0200 Subject: [PATCH 2/2] Removed @generated from new cross methods * Added tests for cross() function * Updated NEWS.md --- NEWS.md | 2 ++ src/TensorValues/Operations.jl | 9 ++++----- test/TensorValuesTests/OperationsTests.jl | 9 +++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/NEWS.md b/NEWS.md index 2575dddc3..28f817cd5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Operator `⊗` (\otimes) as an alias of `outer`. Since PR [#239](https://github.com/gridap/Gridap.jl/pull/239). - Support for (symmetric) 4th order tensors. Since PR [#239](https://github.com/gridap/Gridap.jl/pull/239). - Optimizations for symmetric 2nd order tensors. Since PR [#239](https://github.com/gridap/Gridap.jl/pull/239). + - Methods for `cross` function (aka `×` (\times)) to operate with `VectorValues`. + Since PR [#280](https://github.com/gridap/Gridap.jl/pull/280). ### Changed diff --git a/src/TensorValues/Operations.jl b/src/TensorValues/Operations.jl index 77f427c9e..f62cfee19 100644 --- a/src/TensorValues/Operations.jl +++ b/src/TensorValues/Operations.jl @@ -264,13 +264,12 @@ const ⊗ = outer # Cross Product ############################################################### -@generated function cross(a::MultiValue{Tuple{3}}, b::MultiValue{Tuple{3}}) - str = "a[2]b[3]-a[3]b[2], a[3]b[1]-a[1]b[3], a[1]b[2]-a[2]b[1]" - Meta.parse("VectorValue{3}($str)") +function cross(a::MultiValue{Tuple{3}}, b::MultiValue{Tuple{3}}) + VectorValue{3}(a[2]b[3]-a[3]b[2], a[3]b[1]-a[1]b[3], a[1]b[2]-a[2]b[1]) end -@generated function cross(a::MultiValue{Tuple{2}}, b::MultiValue{Tuple{2}}) - Meta.parse("a[1]b[2]-a[2]b[1]") +function cross(a::MultiValue{Tuple{2}}, b::MultiValue{Tuple{2}}) + a[1]b[2]-a[2]b[1] end cross(a::MultiValue,b::MultiValue) = error("Cross product only defined for R2 and R3 vectors") diff --git a/test/TensorValuesTests/OperationsTests.jl b/test/TensorValuesTests/OperationsTests.jl index 40566282d..6f9de4da0 100644 --- a/test/TensorValuesTests/OperationsTests.jl +++ b/test/TensorValuesTests/OperationsTests.jl @@ -274,6 +274,15 @@ a = VectorValue(2,3) b = VectorValue(5,2) @test a × b == -11 +a = VectorValue(1.0,5.0,-4.0) +b = VectorValue(6.0,2.0,3.0) +c = VectorValue(23.0,-27.0,-28.0) +@test cross(a, b) == c + +a = VectorValue(4.0,1.0) +b = VectorValue(3.0,-2.0) +@test cross(a, b) == -11.0 + # Linear Algebra t = TensorValue(10,2,30,4,5,6,70,8,9)