Quaternion multiplication is well-defined, and is known to me as "Hamilton product":
// hamilton product
vec4 qmul(in vec4 q1, in vec4 q2) {
return vec4(
q1.w * q2.xyz + q2.w * q1.xyz - cross(q1.xyz, q2.xyz),
q1.w*q2.w - dot(q1.xyz, q2.xyz)
);
}
However, for implementing qtanh()
quaternionic function, we need division. So far I've found this, and it is working OK. Could you help me to undestand, where does this comes from?
// division
// https://www.boost.org/doc/libs/1_67_0/boost/math/quaternion.hpp
vec4 qdiv(in vec4 q1, in vec4 q2) {
float denominator = dot(q2,q2);
return vec4(
vec3(
-q1.w*q2.x+q1.x*q2.w-q1.y*q2.z+q1.z*q2.y,
-q1.w*q2.y+q1.x*q2.z+q1.y*q2.w-q1.z*q2.x,
-q1.w*q2.z-q1.x*q2.y+q1.y*q2.x+q1.z*q2.w
),
q1.w*q2.w + dot(q1.xyz, q2.xyz)
) / denominator;
}
Also, as far as I am trying to implement tanh().. are you aware of more computationally vat, than dividing sinh and cosh? For reals I've used to use following formula: tanh(x)=-1+2/(1+exp(-x))
. And that involves only single exponential calculus, instead of two..