Skip to content

Commit

Permalink
Check vector length in cross() (JuliaLang#26308)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrison authored and JeffBezanson committed Mar 7, 2018
1 parent d87e2b5 commit 9e8cd03
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 8 additions & 2 deletions stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ julia> cross(a,b)
0
```
"""
cross(a::AbstractVector, b::AbstractVector) =
[a[2]*b[3]-a[3]*b[2], a[3]*b[1]-a[1]*b[3], a[1]*b[2]-a[2]*b[1]]
function cross(a::AbstractVector, b::AbstractVector)
if !(length(a) == length(b) == 3)
throw(DimensionMismatch("cross product is only defined for vectors of length 3"))
end
a1, a2, a3 = a
b1, b2, b3 = b
[a2*b3-a3*b2, a3*b1-a1*b3, a1*b2-a2*b1]
end

"""
triu(M)
Expand Down
2 changes: 2 additions & 0 deletions stdlib/LinearAlgebra/test/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ end
@test !issymmetric(fill(1,5,3))
@test !ishermitian(fill(1,5,3))
@test (x = fill(1,3); cross(x,x) == zeros(3))
@test_throws DimensionMismatch cross(fill(1,3), fill(1,4))
@test_throws DimensionMismatch cross(fill(1,2), fill(1,3))

@test trace(Bidiagonal(fill(1,5),fill(0,4),:U)) == 5

Expand Down

0 comments on commit 9e8cd03

Please sign in to comment.