diff --git a/src/ops.jl b/src/ops.jl index b19a1d8..6144b56 100644 --- a/src/ops.jl +++ b/src/ops.jl @@ -444,15 +444,11 @@ end end """ - cross(x::Vec{3}, y::Vec{3}) -> Vec{3} - cross(x::Vec{2}, y::Vec{2}) -> Vec{3} - cross(x::Vec{1}, y::Vec{1}) -> Vec{3} + cross(x::Vec, y::Vec) x × y Compute the cross product between two vectors. -The vectors are expanded to 3D frist for dimensions 1 and 2. -The infix operator `×` (written `\\times`) can also be used. -`x × y` (where `×` can be typed by `\\times`) is a synonym for `cross(x, y)`. +The infix operation `x × y` (where `×` can be typed by `\\times`) is a synonym for `cross(x, y)`. # Examples ```jldoctest @@ -475,16 +471,14 @@ julia> x × y -0.37588028973385323 ``` """ -@inline cross(x::Vec{1, T1}, y::Vec{1, T2}) where {T1, T2} = zero(Vec{3, promote_type(T1, T2)}) -@inline function cross(x::Vec{2, T1}, y::Vec{2, T2}) where {T1, T2} - z = zero(promote_type(T1, T2)) - @inbounds Vec(z, z, x[1]*y[2] - x[2]*y[1]) -end @inline function cross(x::Vec{3}, y::Vec{3}) @inbounds Vec(x[2]*y[3] - x[3]*y[2], x[3]*y[1] - x[1]*y[3], x[1]*y[2] - x[2]*y[1]) end +@inline function cross(x::Vec{2}, y::Vec{2}) + @inbounds x[1]*y[2] - x[2]*y[1] +end # power @inline Base.literal_pow(::typeof(^), x::AbstractSquareTensor, ::Val{-1}) = inv(x) diff --git a/test/ops.jl b/test/ops.jl index 3602c68..f32e576 100644 --- a/test/ops.jl +++ b/test/ops.jl @@ -203,21 +203,12 @@ end end end @testset "cross" begin - for T in (Float32, Float64), dim in 1:3 + for T in (Float32, Float64), dim in 2:3 x = rand(Vec{dim, T}) y = rand(Vec{dim, T}) - @test (@inferred x × x)::Vec{3, T} ≈ zero(Vec{3, T}) @test x × y ≈ -y × x - if dim == 2 - a = Vec{2, T}(1,0) - b = Vec{2, T}(0,1) - @test (@inferred a × b)::Vec{3, T} ≈ Vec{3, T}(0,0,1) - end - if dim == 3 - a = Vec{3, T}(1,0,0) - b = Vec{3, T}(0,1,0) - @test (@inferred a × b)::Vec{3, T} ≈ Vec{3, T}(0,0,1) - end + dim == 2 && @test (@inferred x × x)::T ≈ zero(T) + dim == 3 && @test (@inferred x × x)::Vec{3, T} ≈ zero(Vec{3, T}) end end @testset "pow" begin