quaternionMultiplication function

Array quaternionMultiplication(
  1. Array q1,
  2. Array q2
)

Implementation

Array quaternionMultiplication(Array q1, Array q2) {
  // Both q1 and q2 are represented as 4x1 column vectors
  assert(listEqual(q1.shape.toList(), [4, 1]));
  assert(listEqual(q2.shape.toList(), [4, 1]));

  var q1Values = q1.flat();
  var q2Values = q2.flat();
  var x1 = q1Values[0];
  var y1 = q1Values[1];
  var z1 = q1Values[2];
  var w1 = q1Values[3];

  var x2 = q2Values[0];
  var y2 = q2Values[1];
  var z2 = q2Values[2];
  var w2 = q2Values[3];

  return Array(values: [
    [w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2],
    [w1 * y2 + y1 * w2 + z1 * x2 - x1 * z2],
    [w1 * z2 + z1 * w2 + x1 * y2 - y1 * x2],
    [w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2],
  ]);
}