Quaternion type: Difference between revisions

Content added Content deleted
(added Julia example)
Line 2,777: Line 2,777:


=={{header|Julia}}==
=={{header|Julia}}==
This is from the quaternion example file included with Julia 0.2 which implements a quaternion type complete with arithmetic, type conversions and promotion rules, and pretty-printing:
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
<lang julia>import Base: promote_rule, convert


Line 2,857: Line 2,857:


Example usage and output: <lang julia>julia> q = Quaternion(1,0,0,0)
Example usage and output: <lang julia>julia> q = Quaternion(1,0,0,0)
julia> x = Quaternion(0,1,1,1)
julia> q = Quaternion (1, 2, 3, 4)
q1 = Quaternion(2, 3, 4, 5)
q2 = Quaternion(3, 4, 5, 6)
r = 7.


julia> println("q = $q")
julia> norm(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
(8.0 + 2.0i + 3.0j + 4.0k,8.0 + 2.0i + 3.0j + 4.0k)

julia> q1 + q2
5 + 7i + 9j + 11k

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}}==
=={{header|Liberty BASIC}}==