Vector products: Difference between revisions

Refactored code
No edit summary
(Refactored code)
Line 1,721:
 
=={{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.
<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
 
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
 
const a = [3, 4, 5]
const b = [4, 3, 5]
const c = [-5, -12, -13]
 
println("Test Vectors:")
@show a b c
println(" a = ", a)
println(" b = ", a)
println(" c = ", a)
 
println("\nVector Products:")
println(" a dot b = ",@show dot(a, b))
println(" a cross 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}}
<pre>Test Vectors:
ca = [3, 4, 5]
Test Vectors:
ab = [4, 3,4, 5]
bc = [3-5,4 -12,5 -13]
c = [3,4,5]
 
Vector Products:
dot(a dot, b) = 49
cross(a cross, b) = [5, 5, -7]
scalarproduct(a dot, b cross, c) = 6
vectorproduct(a cross, b cross, c) = [-267, 204, -3]</pre>
</pre>
 
=={{header|Kotlin}}==
Anonymous user