How to use Quaternions in Mobius?



  • Dear reader,

    Have to create digital questions in Mobius on quaternions. Aim is to teach students of robot engineering about rotations besides the matrix rotations. Made some code, it works but very simplistic. I could not load a (maple) quarternion-toolbox.
    Any suggestions?

    # quaternion product
    # define two vecotrs p and q:
    $p0=range(3,3,1);
    $px=range(1,1,1);
    $py=range(-2,-2,1);
    $pz=range(1,1,1);
    $q0=range(2,2,1);
    $qx=range(-1,-1,1);
    $qy=range(2,2,1);
    $qz=range(3,3,1);
    $p=maple("Vector([$px,$py,$pz])");
    $q=maple("Vector([$qx,$qy,$qz])");
    $displayp=maple("printf(MathML:-ExportPresentation($p))");
    $displayq=maple("printf(MathML:-ExportPresentation($q))");
    # p.q=p0.q0-(p.q)+p0.q+q0.p+p*q
    # scalar part
    $dot=maple("LinearAlgebra[DotProduct]($p,$q,conjugate=false)");
    $scalar_part=$p0*$q0-$dot;
    # vector part
    $cross=maple("LinearAlgebra[CrossProduct]($p,$q)");
    $p0q=maple("LinearAlgebra[MatrixMatrixMultiply]($p0,$q)");
    $q0p=maple("LinearAlgebra[MatrixMatrixMultiply]($q0,$p)");
    $pq=maple("LinearAlgebra[VectorAdd]($p0q,$q0p)");
    $vector_part=maple("LinearAlgebra[VectorAdd]($pq,$cross)");
    $displayvector_part=maple("printf(MathML:-ExportPresentation($vector_part))");
    # unit vectors
    $i=maple("Vector([1,0,0])");
    $j=maple("Vector([0,1,0])");
    $k=maple("Vector([0,0,1])");
    # generate answers
    $x=maple("LinearAlgebra[DotProduct]($vector_part,$i)");
    $y=maple("LinearAlgebra[DotProduct]($vector_part,$j)");
    $z=maple("LinearAlgebra[DotProduct]($vector_part,$k)");

    ###

    # rotation using quaternions
    # unit vectors
    $i=maple("Vector([1,0,0])");
    $j=maple("Vector([0,1,0])");
    $kk=maple("Vector([0,0,1])");
    # rotation axis k:
    $kx=range(1,1,1);
    $ky=range(1,1,1);
    $kz=range(1,1,1);
    $k=maple("Vector([$kx,$ky,$kz])");
    $displayk=maple("printf(MathML:-ExportPresentation($k))");
    # unit vector:
    $length=sqrt($kx^2+$ky^2+$kz^2);
    $u=maple("LinearAlgebra[Multiply](1/$length,$k)");
    $displayu=maple("printf(MathML:-ExportPresentation($u))");
    # rotation angle:
    $angledeg=range(120,120,1);
    $angle=$angledeg*pi/180;
    # quaternion:
    $scalar=cos($angle/2);
    $vector=maple("LinearAlgebra[Multiply](sin($angle/2),$u)");
    $displayvector=maple("printf(MathML:-ExportPresentation($vector))");
    $qx=maple("LinearAlgebra[DotProduct]($vector,$i)");
    $qy=maple("LinearAlgebra[DotProduct]($vector,$j)");
    $qz=maple("LinearAlgebra[DotProduct]($vector,$kk)");
    # base vector (to be rotated around k)
    $bx=range(1,1,1);
    $by=range(0,0,1);
    $bz=range(0,0,1);
    $b=maple("Vector([$bx,$by,$bz])");
    $displayb=maple("printf(MathML:-ExportPresentation($b))");
    # rotation:
    $dot=maple("LinearAlgebra[DotProduct]($u,$b,conjugate=false)");
    $cross=maple("LinearAlgebra[CrossProduct]($u,$b)");
    $term1=maple("LinearAlgebra[Multiply](cos($angle),$b)");
    $displayterm1=maple("printf(MathML:-ExportPresentation($term1))");
    $term2=maple("LinearAlgebra[Multiply]((1-cos($angle))*$dot,$u)");
    $displayterm2=maple("printf(MathML:-ExportPresentation($term2))");
    $term3=maple("LinearAlgebra[Multiply](sin($angle),$cross)");
    $displayterm3=maple("printf(MathML:-ExportPresentation($term3))");
    $L1=maple("LinearAlgebra[VectorAdd]($term1,$term2)");
    $L=maple("LinearAlgebra[VectorAdd]($L1,$term3)");
    $displayL=maple("printf(MathML:-ExportPresentation($L))");
    # generate answers
    $Lx=maple("LinearAlgebra[DotProduct]($L,$i)");
    $Ly=maple("LinearAlgebra[DotProduct]($L,$j)");
    $Lz=maple("LinearAlgebra[DotProduct]($L,$kk)");

    Best regards,

    Nico Booij



  • From ntpbooij: Questions are shared in the Mobius Cloud!

    +Quaterions_Rotation and +Quaternions_multiplication

    The + means question in develop phase.




  • Hi Nico,

    Firstly, thanks for the effort in designing the nice questions! I would like to ask about +Quaterions_Rotation question, because of normalizing $k to get $u, the answer that the students need to enter is a decimal approximation. Are you looking to make that exact, or will you add tolerance so the student just enters correct to a number of decimal places?

    I think the exact answers make more sense, so I would suggest putting certain variables in quotation marks or evaluating them using maple calls:

    $length='sqrt($kx^2+$ky^2+$kz^2)';
    .... $angle='$angledeg*Pi/180';

Log in to reply