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 ed965f828..f62cfee19 100644 --- a/src/TensorValues/Operations.jl +++ b/src/TensorValues/Operations.jl @@ -260,6 +260,20 @@ end const ⊗ = outer +############################################################### +# Cross Product +############################################################### + +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 + +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") + ############################################################### # Linear Algebra ############################################################### @@ -450,4 +464,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..6f9de4da0 100644 --- a/test/TensorValuesTests/OperationsTests.jl +++ b/test/TensorValuesTests/OperationsTests.jl @@ -263,6 +263,26 @@ 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 + +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)