Jump to content

Quaternion type: Difference between revisions

m (syntax highlighting fixup automation)
Line 6,966:
task 7: multiplication q1*q2 ──► -56+16i+24j+26k
task 8: multiplication q2*q1 ──► -56+18i+20j+28k
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
By considering quaternions as arrays, negation and addition can be directly achieved by resp. <code>NEG</code> and <code>+</code> instructions. Other operations need specific RPL words:
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ 0 1 4 '''FOR''' q OVER q GET SQ + '''NEXT''' √ SWAP DROP
≫ ''''QNORM'''' STO
≪ NEG 1 DUP2 GET NEG PUT ≫ ''''QCONJ'''' STO
DUP TYPE 3 == ≪ SWAP ≫ IFT
OVER 1 GET + 1 SWAP PUT
≫ ''''QRADD'''' STO
ARRY→ DROP 5 ROLL ARRY→ DROP → a2 b2 c2 d2 a1 b1 c1 d1
≪ 'a1*a2 − b1*b2 − c1*c2 − d1*d2' EVAL
'a1*b2 + b1*a2 + c1*d2 − d1*c2' EVAL
'a1*c2 − b1*d2 + c1*a2 + d1*b2' EVAL
'a1*d2 + b1*c2 − c1*b2 + d1*a2' EVAL
{ 4 } →ARRY
≫ ≫ ''''QMUL'''' STO
|
'''QNORM''' ''( [ a b c d ] -- √(a²+b²+c²+d²) )''
'''QCONJ''' ''( [ a b c d ] -- [ a -b -c -d ] )''
'''QRADD''' ''( [ a b c d ] r -- [ a+r b c d ] )''
switch arguments if quaternion is at stack level 1
replace a by a+r
'''QMUL''' ''( [Q1] [Q2] -- [Q1 x Q2] )''
put the 2 quaternions in local variables
do the math in stack
convert stack to a quaternion
|}
 
{{in}}
<pre>
[1 2 3 4] QNORM
[1 2 3 4] NEG
[1 2 3 4] QCONJ
[1 2 3 4] 7 QRADD
[2 3 4 5] [3 4 5 6] +
[1 2 3 4] 7 *
[2 3 4 5] [3 4 5 6] QMUL
[3 4 5 6] [2 3 4 5] QMUL
</pre>
 
{{out}}
<pre>
8: 5.47722557505
7: [ -1 -2 -3 -4 ]
6: [ 1 -2 -3 -4 ]
5: [ 8 2 3 4 ]
4: [ 5 7 9 11 ]
3: [ 7 14 21 28 ]
2: [ -56 16 24 26 ]
1: [ -56 18 20 28 ]
</pre>
 
1,151

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.