Quaternion type: Difference between revisions

→‎{{header|REXX}}: added the REXX language. -- ~~~~
(Added BBC BASIC)
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
Line 3,455:
 
</lang>
 
=={{header|REXX}}==
The REXX language has no native quaternion support, but subroutines can be easily written.
<lang rexx>/*REXX program to perform simple operations of quaternion type numbers.*/
q = 1 2 3 4 ; q1 = 2 3 4 5
r = 7 ; q2 = 3 4 5 6
call quatShow q , 'q'
call quatShow q1 , 'q1'
call quatShow q2 , 'q2'
call quatShow r , 'r'
call quatShow quatNorm(q) , 'norm q' , "task 1:"
call quatShow quatNeg(q) , 'negative q' , "task 2:"
call quatShow quatConj(q) , 'conjugate q' , "task 3:"
call quatShow quatAdd( r, q ) , 'addition r+q' , "task 4:"
call quatShow quatAdd(q1, q2 ) , 'addition q1+q2' , "task 5:"
call quatShow quatMul( q, r ) , 'multiplication q*r' , "task 6:"
call quatShow quatMul(q1, q2 ) , 'multiplication q1*q2' , "task 7:"
call quatShow quatMul(q2, q1 ) , 'multiplication q2*q1' , "task 8:"
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────QUATADD─────────────────────────────*/
quatAdd: procedure; parse arg x,y; call quatXY y
return x.1+y.1 x.2+y.2 x.3+y.3 x.4+y.4
/*──────────────────────────────────QUATCONJ────────────────────────────*/
quatConj: procedure; parse arg x; call quatXY
return x.1 (-x.2) (-x.3) (-x.4)
/*──────────────────────────────────QUATNORM────────────────────────────*/
quatMul: procedure; parse arg x,y; call quatXY y
return x.1 * y.1 - x.2 * y.2 - x.3 * y.3 - x.4 * y.4 ,
x.1 * y.2 + x.2 * y.1 + x.3 * y.4 - x.4 * y.3 ,
x.1 * y.3 - x.2 * y.4 + x.3 * y.1 + x.4 * y.2 ,
x.1 * y.4 + x.2 * y.3 - x.3 * y.2 + x.4 * y.1
/*──────────────────────────────────QUATNEG─────────────────────────────*/
quatNeg: procedure; parse arg x; call quatXY
return -x.1 (-x.2) (-x.3) (-x.4)
/*──────────────────────────────────QUATNORM────────────────────────────*/
quatNorm: procedure; parse arg x; call quatXY
s=0; do m=1 for 4; s=s+x.m**2
end /*m*/
return sqrt(s)
/*──────────────────────────────────QUATSHOW────────────────────────────*/
quatShow: procedure; parse arg x; call quatXY
quat=
do m=1 for 4; _=x.m; if _==0 then iterate
if _ >=0 then _='+'_
if m\==1 then _=_ || substr('~ijk',m,1)
quat=strip(quat || _,,'+')
end /*m*/
 
say left(arg(3),9) right(arg(2),20) ' ──► ' quat
return quat
/*──────────────────────────────────QUATXY──────────────────────────────*/
quatXY: do n=1 for 4; x.n=word(word(x,n) 0,1)/1; end /*n*/
if arg()==1 then do m=1 for 4; y.m=word(word(y,m) 0,1)/1; end /*m*/
return
/*──────────────────────────────────SQRT subroutine─────────────────────*/
sqrt: procedure;parse arg x; if x=0 then return 0; d=digits(); numeric digits 11; g=.sqrtGuess()
do j=0 while p>9; m.j=p; p=p%2+1; end; do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k
g=.5*(g+x/g); end; numeric digits d; return g/1
.sqrtGuess: numeric form; m.=11; p=d+d%4+2
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; return g*.5'E'_%2
</lang>
'''output''' when using the default input
<pre style="overflow:scroll">
q ──► 1+2i+3j+4k
q1 ──► 2+3i+4j+5k
q2 ──► 3+4i+5j+6k
r ──► 7
task 1: norm q ──► 5.47722558
task 2: negative q ──► -1-2i-3j-4k
task 3: conjugate q ──► 1-2i-3j-4k
task 4: addition r+q ──► 8+2i+3j+4k
task 5: addition q1+q2 ──► 5+7i+9j+11k
task 6: multiplication q*r ──► 7+14i+21j+28k
task 7: multiplication q1*q2 ──► -56+16i+24j+26k
task 8: multiplication q2*q1 ──► -56+18i+20j+28k
</pre>
 
 
=={{header|Ruby}}==