Vector products: Difference between revisions

Content added Content deleted
No edit summary
(Refactored code)
Line 1,721: Line 1,721:


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
Julia provides dot and cross products as built-ins. It's easy enough to use these to construct the triple products.
Julia provides dot and cross products as built-ins. It's easy enough to use these to construct the triple products.
<lang julia>function scalarproduct(a::AbstractVector{T}, b::AbstractVector{T}, c::AbstractVector{T}) where {T<:Number}
<lang Julia>
return dot(a, cross(b, c))
function scltrip{T<:Number}(a::AbstractArray{T,1},
b::AbstractArray{T,1},
c::AbstractArray{T,1})
dot(a, cross(b, c))
end
end


function vectorproduct(a::AbstractVector{T}, b::AbstractVector{T}, c::AbstractVector{T}) where {T<:Number}
function vectrip{T<:Number}(a::AbstractArray{T,1},
return cross(a, cross(b, c))
b::AbstractArray{T,1},
c::AbstractArray{T,1})
cross(a, cross(b, c))
end
end


a = [3, 4, 5]
const a = [3, 4, 5]
b = [4, 3, 5]
const b = [4, 3, 5]
c = [-5, -12, -13]
const c = [-5, -12, -13]


println("Test Vectors:")
println("Test Vectors:")
@show a b c
println(" a = ", a)
println(" b = ", a)
println(" c = ", a)


println("\nVector Products:")
println("\nVector Products:")
println(" a dot b = ", dot(a, b))
@show dot(a, b)
println(" a cross b = ", cross(a, b))
@show cross(a, b)
@show scalarproduct(a, b, c)
println(" a dot b cross c = ", scltrip(a, b, c))
@show vectorproduct(a, b, c)</lang>
println(" a cross b cross c = ", vectrip(a, b, c))
</lang>


{{out}}
{{out}}
<pre>
<pre>Test Vectors:
a = [3, 4, 5]
Test Vectors:
a = [3,4,5]
b = [4, 3, 5]
b = [3,4,5]
c = [-5, -12, -13]
c = [3,4,5]


Vector Products:
Vector Products:
a dot b = 49
dot(a, b) = 49
a cross b = [5,5,-7]
cross(a, b) = [5, 5, -7]
a dot b cross c = 6
scalarproduct(a, b, c) = 6
a cross b cross c = [-267,204,-3]
vectorproduct(a, b, c) = [-267, 204, -3]</pre>
</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==