Quaternion type: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: changed whitespace, used a template for the output section, optimized the sqrt function.) |
|||
Line 6,332: | Line 6,332: | ||
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 |
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 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
qShow: procedure; parse arg x; call qXY; $= |
qShow: procedure; parse arg x; call qXY; $= |
||
do m=1 for 4; _=x.m; |
do m=1 for 4; _= x.m; if _==0 then iterate; if _>=0 then _= '+'_ |
||
if m\==1 then _= _ || substr(' |
if m\==1 then _= _ || substr('∙ijk', m, 1); $= strip($ || _, , "+") |
||
end /*m*/ |
end /*m*/ |
||
say left(arg(3), 9) right(arg(2), 20) |
say left(arg(3), 9) right(arg(2), 20) ' ──► ' $; return $ |
||
return $ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
qXY: |
qXY: do n=1 for 4; x.n= word( word(x, n) 0, 1)/1; end /*n*/ |
||
if arg()==1 then |
if arg()==1 then do m=1 for 4; y.m= word( word(y, m) 0, 1)/1; end /*m*/; return |
||
return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
sqrt: procedure; parse arg x; if x=0 then return 0; d= digits(); i=; m.=9; h=d+6 |
sqrt: procedure; parse arg x; if x=0 then return 0; d= digits(); i=; m.=9; h=d+6 |
||
numeric digits; numeric form; |
numeric digits; numeric form; if x<0 then parse value -x 'i' with x i |
||
parse value format(x, 2, 1, , 0) 'E0' with g |
parse value format(x, 2, 1, , 0) 'E0' with g "E" _ .; g= g *.5'e'_ % 2 |
||
do j=0 while h>9; m.j=h; h= h % 2 + 1; end /*j*/ |
do j=0 while h>9; m.j=h; h= h % 2 + 1; end /*j*/ |
||
do k=j+5 to 0 by -1; numeric digits m.k; g= (g + x/g)* .5; end /*k*/ |
do k=j+5 to 0 by -1; numeric digits m.k; g= (g + x/g)* .5; end /*k*/ |
||
numeric digits d; return (g/1)i /*make complex if X<0 |
numeric digits d; return (g/1)i /*make complex if X<0 */</lang> |
||
{{out|output|text= when using the internal default inputs:}} |
|||
<pre> |
<pre> |
||
q ──► 1+2i+3j+4k |
q ──► 1+2i+3j+4k |