Quaternion type: Difference between revisions

Content added Content deleted
(added Julia example)
Line 2,777:
 
=={{header|Julia}}==
This is from the [https://github.com/JuliaLang/julia/blob/master/examples/quaternion.jl quaternion example file] included with Julia 0.2, which implements a quaternion type complete with arithmetic, type conversions and promotion rules, and pretty-printing:
<lang julia>import Base: promote_rule, convert
 
Line 2,857:
 
Example usage and output: <lang julia>julia> q = Quaternion(1,0,0,0)
julia> xq = Quaternion (0,1,1 2,1 3, 4)
q1 = Quaternion(2, 3, 4, 5)
q2 = Quaternion(3, 4, 5, 6)
r = 7.
 
julia> printlnnorm("q = $q")
5.477225575051661
println("q*2.0+2 = $(q*2.0+2)")
println("abs((-q+x*2)/4) = ", abs((-q+x*2)/4))
 
julia> -q
q = 1 + 0i + 0j + 0k
-1 - 2i - 3j - 4k
q*2.0+2 = 4.0 + 0.0i + 0.0j + 0.0k
 
abs((-q+x*2)/4) = 0.9013878188659973</lang>
julia> conj(q)
1 - 2i - 3j - 4k
 
julia> r + q, q + r
q*2(8.0 + 2.0i =+ 3.0j + 4.0k,8.0 + 02.0i + 03.0j + 04.0k)
 
julia> q1 + q2
q = 15 + 0i7i + 0j9j + 0k11k
 
julia> r*q, q*r
(7.0 + 14.0i + 21.0j + 28.0k,7.0 + 14.0i + 21.0j + 28.0k)
 
julia> q1*q2, q2*q1, q1*q2 != q2*q1
(-56 + 16i + 24j + 26k,-56 + 18i + 20j + 28k,true)</lang>
 
=={{header|Liberty BASIC}}==