Quaternion type: Difference between revisions
Content deleted Content added
m →[[Simple Quaternion type and operations#ALGOL 68]]: reorder declarations |
m →[[Simple Quaternion type and operations#ALGOL 68]]: more code ordering |
||
Line 164: | Line 164: | ||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''}} |
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''}} |
||
<lang algol68>MODE QUAT = STRUCT(REAL re, i, j, k); |
<lang algol68>MODE QUAT = STRUCT(REAL re, i, j, k); |
||
⚫ | |||
MODE |
MODE SUBQUAT = UNION(QUAT, #COMPL, # REAL#, INT, [4]REAL, [4]INT # ); |
||
MODE CLASSQUAT = STRUCT( |
MODE CLASSQUAT = STRUCT( |
||
PROC (REF QUAT #new#, REAL #re#, REAL #i#, REAL #j#, REAL #k#)REF QUAT new, |
PROC (REF QUAT #new#, REAL #re#, REAL #i#, REAL #j#, REAL #k#)REF QUAT new, |
||
PROC (REF QUAT #self#)QUAT conjugate, |
PROC (REF QUAT #self#)QUAT conjugate, |
||
Line 174: | Line 175: | ||
PROC (REF QUAT #self#)STRING repr, |
PROC (REF QUAT #self#)STRING repr, |
||
PROC (REF QUAT #self#)QUAT neg, |
PROC (REF QUAT #self#)QUAT neg, |
||
PROC (REF QUAT #self#, |
PROC (REF QUAT #self#, SUBQUAT #other#)QUAT add, |
||
PROC (REF QUAT #self#, |
PROC (REF QUAT #self#, SUBQUAT #other#)QUAT radd, |
||
PROC (REF QUAT #self#, |
PROC (REF QUAT #self#, SUBQUAT #other#)QUAT sub, |
||
PROC (REF QUAT #self#, |
PROC (REF QUAT #self#, SUBQUAT #other#)QUAT mul, |
||
PROC (REF QUAT #self#, |
PROC (REF QUAT #self#, SUBQUAT #other#)QUAT rmul, |
||
PROC (REF QUAT #self#, |
PROC (REF QUAT #self#, SUBQUAT #other#)QUAT div, |
||
PROC (REF QUAT #self#, |
PROC (REF QUAT #self#, SUBQUAT #other#)QUAT rdiv, |
||
PROC (REF QUAT #self#)QUAT exp |
PROC (REF QUAT #self#)QUAT exp |
||
); |
); |
||
Line 208: | Line 209: | ||
# PROC repr =#(REF QUAT self)STRING: ( |
# PROC repr =#(REF QUAT self)STRING: ( |
||
# 'Shorter form of Quaternion as string' # |
# 'Shorter form of Quaternion as string' # |
||
FILE f; STRING s; associate(f,s); |
FILE f; STRING s; associate(f, s); |
||
putf(f, (squat fmt, re OF self>=0, re OF self, |
putf(f, (squat fmt, re OF self>=0, re OF self, |
||
i OF self>=0, i OF self, j OF self>=0, j OF self, k OF self>=0, k OF self)); |
i OF self>=0, i OF self, j OF self>=0, j OF self, k OF self>=0, k OF self)); |
||
Line 218: | Line 219: | ||
(-re OF self, -i OF self, -j OF self, -k OF self), |
(-re OF self, -i OF self, -j OF self, -k OF self), |
||
# PROC add =#(REF QUAT self, |
# PROC add =#(REF QUAT self, SUBQUAT other)QUAT: |
||
CASE other IN |
CASE other IN |
||
(QUAT other): (re OF self + re OF other, i OF self + i OF other, j OF self + j OF other, k OF self + k OF other), |
(QUAT other): (re OF self + re OF other, i OF self + i OF other, j OF self + j OF other, k OF self + k OF other), |
||
Line 224: | Line 225: | ||
ESAC, |
ESAC, |
||
# PROC radd =#(REF QUAT self, |
# PROC radd =#(REF QUAT self, SUBQUAT other)QUAT: |
||
(add OF class quat)(self, other), |
(add OF class quat)(self, other), |
||
# PROC sub =#(REF QUAT self, |
# PROC sub =#(REF QUAT self, SUBQUAT other)QUAT: |
||
CASE other IN |
CASE other IN |
||
(QUAT other): (re OF self - re OF other, i OF self - i OF other, j OF self - j OF other, k OF self - k OF other), |
(QUAT other): (re OF self - re OF other, i OF self - i OF other, j OF self - j OF other, k OF self - k OF other), |
||
Line 233: | Line 234: | ||
ESAC, |
ESAC, |
||
# PROC mul =#(REF QUAT self, |
# PROC mul =#(REF QUAT self, SUBQUAT other)QUAT: |
||
CASE other IN |
CASE other IN |
||
(QUAT other):( |
(QUAT other):( |
||
Line 244: | Line 245: | ||
ESAC, |
ESAC, |
||
# PROC rmul =#(REF QUAT self, |
# PROC rmul =#(REF QUAT self, SUBQUAT other)QUAT: |
||
CASE other IN |
CASE other IN |
||
(QUAT other): (mul OF class quat)(LOC QUAT := other, self), |
(QUAT other): (mul OF class quat)(LOC QUAT := other, self), |
||
Line 250: | Line 251: | ||
ESAC, |
ESAC, |
||
# PROC div =#(REF QUAT self, |
# PROC div =#(REF QUAT self, SUBQUAT other)QUAT: |
||
CASE other IN |
CASE other IN |
||
(QUAT other): (mul OF class quat)(self, (reciprocal OF class quat)(LOC QUAT := other)), |
(QUAT other): (mul OF class quat)(self, (reciprocal OF class quat)(LOC QUAT := other)), |
||
Line 256: | Line 257: | ||
ESAC, |
ESAC, |
||
# PROC rdiv =#(REF QUAT self, |
# PROC rdiv =#(REF QUAT self, SUBQUAT other)QUAT: |
||
CASE other IN |
CASE other IN |
||
(QUAT other): (div OF class quat)(LOC QUAT := other, self), |
(QUAT other): (div OF class quat)(LOC QUAT := other, self), |
||
(REAL other): (div OF class quat)(LOC QUAT := (other,0,0,0), self) |
(REAL other): (div OF class quat)(LOC QUAT := (other, 0, 0, 0), self) |
||
ESAC, |
ESAC, |
||
Line 272: | Line 273: | ||
); |
); |
||
FORMAT real fmt = $g(-0,4)$; |
FORMAT real fmt = $g(-0, 4)$; |
||
⚫ | |||
FORMAT quat fmt = $f(real fmt)"+"f(real fmt)"i+"f(real fmt)"j+"f(real fmt)"k"$; |
FORMAT quat fmt = $f(real fmt)"+"f(real fmt)"i+"f(real fmt)"j+"f(real fmt)"k"$; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
OP INIT = (REF QUAT new, []REAL rijk)REF QUAT: |
|||
(new OF class quat)(LOC QUAT := new, rijk[1], rijk[2], rijk[3], rijk[4]); |
|||
OP + = (QUAT q)QUAT: q, |
|||
PRIO INITQ = 1; |
|||
- = (QUAT q)QUAT: (neg OF class quat)(LOC QUAT := q), |
|||
( |
CONJ = (QUAT q)QUAT: (conjugate OF class quat)(LOC QUAT := q), |
||
⚫ | |||
⚫ | |||
⚫ | |||
OP + |
OP +:= = (REF QUAT a, QUAT b)QUAT: a:=( add OF class quat)(a, b), |
||
+:= = (REF QUAT a, REAL b)QUAT: a:=( add OF class quat)(a, b), |
|||
+=: = (QUAT a, REF QUAT b)QUAT: b:=(radd OF class quat)(b, a), |
|||
+=: = (REAL a, REF QUAT b)QUAT: b:=(radd OF class quat)(b, a); |
|||
⚫ | |||
⚫ | |||
OP -:= = (REF QUAT a, QUAT b)QUAT: a:=( sub OF class quat)(a, b), |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
OP |
OP *:= = (REF QUAT a, QUAT b)QUAT: a:=( mul OF class quat)(a, b), |
||
*:= = (REF QUAT a, REAL b)QUAT: a:=( mul OF class quat)(a, b), |
|||
*=: = (QUAT a, REF QUAT b)QUAT: b:=(rmul OF class quat)(b, a), |
|||
⚫ | |||
⚫ | |||
⚫ | |||
OP |
OP /:= = (REF QUAT a, QUAT b)QUAT: a:=( div OF class quat)(a, b), |
||
/:= = (REF QUAT a, REAL b)QUAT: a:=( div OF class quat)(a, b), |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
OP |
OP + = (QUAT a, b)QUAT: ( add OF class quat)(LOC QUAT := a, b), |
||
+ = (QUAT a, REAL b)QUAT: ( add OF class quat)(LOC QUAT := a, b), |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
OP |
OP - = (QUAT a, b)QUAT: ( sub OF class quat)(LOC QUAT := a, b), |
||
- = (QUAT a, REAL b)QUAT: ( sub OF class quat)(LOC QUAT := a, b), |
|||
- = (REAL a, QUAT b)QUAT:-( sub OF class quat)(LOC QUAT := b, a); |
|||
⚫ | |||
OP |
OP * = (QUAT a, b)QUAT: ( mul OF class quat)(LOC QUAT := a, b), |
||
* = (QUAT a, REAL b)QUAT: ( mul OF class quat)(LOC QUAT := a, b), |
|||
* = (REAL a, QUAT b)QUAT: (rmul OF class quat)(LOC QUAT := b, a); |
|||
OP |
OP / = (QUAT a, b)QUAT: ( div OF class quat)(LOC QUAT := a, b), |
||
/ = (QUAT a, REAL b)QUAT: ( div OF class quat)(LOC QUAT := a, b), |
|||
/ = (REAL a, QUAT b)QUAT: ( div OF class quat)(LOC QUAT := b, 1/a); |
|||
PROC quat exp = (QUAT q)QUAT: (exp OF class quat)(LOC QUAT := q); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
CO - OPerators to do: |
|||
⚫ | |||
PLUSAB, MINUSAB, TIMESAB, DIVAB, PLUSTO, TIMESTO, |
|||
+:=, -:=, *:=, /:=, +=:, *=: etc |
|||
END CO |
|||
⚫ | |||
test:( |
test:( |
||
⚫ | |||
QUAT q = (1, 2, 3, 4), |
QUAT q = (1, 2, 3, 4), |
||
q1 = (2, 3, 4, 5), |
q1 = (2, 3, 4, 5), |
||
q2 = (3, 4, 5, 6) |
q2 = (3, 4, 5, 6); |
||
⚫ | |||
printf(( |
printf(( |
||
⚫ | |||
$"q = " f(quat fmt)l$, q, |
$"q = " f(quat fmt)l$, q, |
||
$"q1 = " f(quat fmt)l$, q1, |
$"q1 = " f(quat fmt)l$, q1, |
||
$"q2 = " f(quat fmt)l$, q2, |
$"q2 = " f(quat fmt)l$, q2, |
||
⚫ | |||
$"ABS q = " f(real fmt)", "$, ABS q, |
$"ABS q = " f(real fmt)", "$, ABS q, |
||
$"ABS q1 = " f(real fmt)", "$, ABS q1, |
$"ABS q1 = " f(real fmt)", "$, ABS q1, |
||
Line 365: | Line 363: | ||
END CO |
END CO |
||
QUAT i=(0,1,0,0), |
QUAT i=(0, 1, 0, 0), |
||
j=(0,0,1,0), |
j=(0, 0, 1, 0), |
||
k=(0,0,0,1); |
k=(0, 0, 0, 1); |
||
printf(( |
printf(( |
||
Line 383: | Line 381: | ||
$"quat exp(pi * i) = " f(quat fmt)l$, quat exp(pi * k) |
$"quat exp(pi * i) = " f(quat fmt)l$, quat exp(pi * k) |
||
)); |
)); |
||
print((REPR(-q1*q2),",",REPR(-q2*q1),new line)) |
print((REPR(-q1*q2), ", ", REPR(-q2*q1), new line)) |
||
)</lang> |
)</lang> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
⚫ | |||
q = 1.0000+2.0000i+3.0000j+4.0000k |
q = 1.0000+2.0000i+3.0000j+4.0000k |
||
q1 = 2.0000+3.0000i+4.0000j+5.0000k |
q1 = 2.0000+3.0000i+4.0000j+5.0000k |
||
q2 = 3.0000+4.0000i+5.0000j+6.0000k |
q2 = 3.0000+4.0000i+5.0000j+6.0000k |
||
⚫ | |||
ABS q = 5.4772, ABS q1 = 7.3485, ABS q2 = 9.2736 |
ABS q = 5.4772, ABS q1 = 7.3485, ABS q2 = 9.2736 |
||
-q = -1.0000+-2.0000i+-3.0000j+-4.0000k |
-q = -1.0000+-2.0000i+-3.0000j+-4.0000k |
||
Line 414: | Line 412: | ||
quat exp(pi * j) = -1.0000+.0000i+.0000j+.0000k |
quat exp(pi * j) = -1.0000+.0000i+.0000j+.0000k |
||
quat exp(pi * i) = -1.0000+.0000i+.0000j+.0000k |
quat exp(pi * i) = -1.0000+.0000i+.0000j+.0000k |
||
+56.0000-16.0000i-24.0000j-26.0000k,+56.0000-18.0000i-20.0000j-28.0000k |
+56.0000-16.0000i-24.0000j-26.0000k, +56.0000-18.0000i-20.0000j-28.0000k |
||
</pre> |
</pre> |
||