Quaternion type: Difference between revisions
Content deleted Content added
m →[[Simple Quaternion type and operations#ALGOL 68]]: more code ordering |
→{{header|PureBasic}}: Added PureBasic |
||
Line 533:
q2 mul q1
_56 18 20 28</lang>
=={{header|PureBasic}}==
<lang PureBasic>Structure Quaternion
a.f: b.f
c.f: d.f
EndStructure
Structure Quaternion2
Qa.Quaternion: Qb.Quaternion
Qc.Quaternion: Qd.Quaternion
EndStructure
Procedure.f QNorm(*Q.Quaternion)
Protected Result.f
Result=Sqr(Pow(*Q\a,2)+ Pow(*Q\b,2)+ Pow(*Q\c,2)+ Pow(*Q\d,2))
ProcedureReturn Result
EndProcedure
Procedure.i QNeg(*Q.Quaternion)
Protected *B.Quaternion=AllocateMemory(SizeOf(Quaternion))
If *B
With *Q
*B\a=-\a: *B\b=-\b
*B\c=-\c: *B\d=-\d
EndWith
EndIf
ProcedureReturn *B
EndProcedure
Procedure.i QConj(*Q.Quaternion)
Protected *B.Quaternion=AllocateMemory(SizeOf(Quaternion))
If *B
With *Q
*B\a= \a: *B\b=-\b
*B\c=-\c: *B\d=-\d
EndWith
EndIf
ProcedureReturn *B
EndProcedure
Procedure.i QAddReal(R.f, *Q.Quaternion)
Protected *B.Quaternion=AllocateMemory(SizeOf(Quaternion))
If *B
With *Q
*B\a=R+\a: *B\b= -\b
*B\c= -\c: *B\d= -\d
EndWith
EndIf
ProcedureReturn *B
EndProcedure
Procedure.i QAddQuaternion(*Q1.Quaternion, *Q2.Quaternion)
Protected *B.Quaternion=AllocateMemory(SizeOf(Quaternion))
If *B
*B\a=*Q1\a + *Q2\a: *B\b=*Q1\b + *Q2\b
*B\c=*Q1\c + *Q2\c: *B\d=*Q1\d + *Q2\d
EndIf
ProcedureReturn *B
EndProcedure
Procedure.i QMulReal_and_Quaternion(R.f, *Q.Quaternion)
Protected *B.Quaternion=AllocateMemory(SizeOf(Quaternion))
If *B
*B\a=*Q\a * R: *B\b=*Q\b * R
*B\c=*Q\c * R: *B\d=*Q\d * R
EndIf
ProcedureReturn *B
EndProcedure
Procedure.i QMulQuaternion(*Q1.Quaternion, *Q2.Quaternion)
Protected *B.Quaternion2=AllocateMemory(SizeOf(Quaternion2))
If *B
With *B
\Qa\a= *Q1\a * *Q2\a: \Qa\b=-*Q1\b * *Q2\b
\Qa\c=-*Q1\c * *Q2\c: \Qa\d=-*Q1\d * *Q2\d
;
\Qb\a= *Q1\a * *Q2\b: \Qb\b= *Q1\b * *Q2\a
\Qb\c= *Q1\c * *Q2\d: \Qb\d=-*Q1\d * *Q2\c
;
\Qc\a= *Q1\a * *Q2\c: \Qc\b=-*Q1\b * *Q2\d
\Qc\c= *Q1\c * *Q2\a: \Qc\d= *Q1\d * *Q2\b
;
\Qd\a= *Q1\a * *Q2\d: \Qd\b= *Q1\b * *Q2\c
\Qd\c=-*Q1\c * *Q2\b: \Qd\d= *Q1\d * *Q2\a
EndWith
EndIf
ProcedureReturn *B
EndProcedure</lang>
Implementation & test
<lang PureBasic>Macro Show(QQ,NN=0)
StrF(QQ\a,NN)+","+StrF(QQ\b,NN)+","+StrF(QQ\c,NN)+","+StrF(QQ\d,NN)
EndMacro
Define.Quaternion A, B, C
Define.f r=7
Define *X.Quaternion, *Y.Quaternion2
A\a=1: A\b=2: A\c=3: A\d=4
B\a=2: B\b=3: B\c=4: B\d=5
C\a=3: C\b=4: C\c=5: C\d=6
Debug "Q1= {"+Show(A,0)+"}"
Debug "Q2= {"+Show(B,0)+"}"
Debug "Q3= {"+Show(C,0)+"}"
Debug "Normal of Q1= "+StrF(QNorm(@A))
*X=QNeg(@A)
Debug "Neg(Q1) ={"+Show(*X)+"}" : FreeMemory(*X)
*X=QConj(@A)
Debug "Conj(Q1) ={"+Show(*X)+"}" : FreeMemory(*X)
*X=QAddReal(r,@A)
Debug "r+A ={"+Show(*X)+"}" : FreeMemory(*X)
*X=QAddQuaternion(@A,@B)
Debug "Q1+Q2 ={"+Show(*X)+"}" : FreeMemory(*X)
*X=QAddQuaternion(@B,@C)
Debug "Q2+Q3 ={"+Show(*X)+"}" : FreeMemory(*X)
*Y=QMulQuaternion(@A,@B)
Debug "Q1*Q2 ="
Debug "{{"+Show(*Y\Qa)+"}"
Debug " {"+Show(*Y\Qb)+"}"
Debug " {"+Show(*Y\Qc)+"}"
Debug " {"+Show(*Y\Qd)+"}}"
FreeMemory(*Y)
*Y=QMulQuaternion(@B,@A)
Debug "Q2*Q1 ="
Debug "{{"+Show(*Y\Qa)+"}"
Debug " {"+Show(*Y\Qb)+"}"
Debug " {"+Show(*Y\Qc)+"}"
Debug " {"+Show(*Y\Qd)+"}}"
FreeMemory(*Y)</lang>
Result
<pre>Q1= {1,2,3,4}
Q2= {2,3,4,5}
Q3= {3,4,5,6}
Normal of Q1= 5.4772257805
Neg(Q1) ={-1,-2,-3,-4}
Conj(Q1) ={1,-2,-3,-4}
r+A ={8,-2,-3,-4}
Q1+Q2 ={3,5,7,9}
Q2+Q3 ={5,7,9,11}
Q1*Q2 =
{{2,-6,-12,-20}
{3,4,15,-16}
{4,-10,6,12}
{5,8,-9,8}}
Q2*Q1 =
{{2,-6,-12,-20}
{4,3,16,-15}
{6,-12,4,10}
{8,9,-8,5}}
</pre>
=={{header|Python}}==
|