// - edit Quat.numerics.tt to modify
#nullable enable
namespace Brutal.Numerics;
// INumberBase
public partial struct doubleQuat : INumberBase
{
///
public static doubleQuat Zero => default;
///
public static doubleQuat Identity => new(0, 0, 0, 1);
/* One is the multiplicative identity - the identity quaternion - which the concrete API
* already names Identity; a public One would read as all-ones. */
static doubleQuat INumberBase.One => Identity;
static doubleQuat IAdditiveIdentity.AdditiveIdentity => Zero;
static doubleQuat IMultiplicativeIdentity.MultiplicativeIdentity => Identity;
static int INumberBase.Radix => 2;
// Abs is the norm, embedded on the real axis as a scalar quaternion (cf. Complex.Abs)
static doubleQuat INumberBase.Abs(doubleQuat value) => new(0, 0, 0, value.Length());
static bool INumberBase.IsCanonical(doubleQuat value) => true;
static bool INumberBase.IsComplexNumber(doubleQuat value) =>
value.W != 0 && !(value.X == 0 && value.Y == 0 && value.Z == 0);
static bool INumberBase.IsImaginaryNumber(doubleQuat value) =>
value.W == 0 && double.IsRealNumber(value.X) && double.IsRealNumber(value.Y) && double.IsRealNumber(value.Z);
static bool INumberBase.IsRealNumber(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsRealNumber(value.W);
/// Determines if a value is zero.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsZero(doubleQuat value)
{
return value == Zero;
}
static bool INumberBase.IsEvenInteger(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsEvenInteger(value.W);
static bool INumberBase.IsOddInteger(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsOddInteger(value.W);
static bool INumberBase.IsInteger(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsInteger(value.W);
static bool INumberBase.IsNegative(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsRealNumber(value.W) && double.IsNegative(value.W);
static bool INumberBase.IsPositive(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsRealNumber(value.W) && double.IsPositive(value.W);
static bool INumberBase.IsFinite(doubleQuat value) =>
double.IsFinite(value.X) && double.IsFinite(value.Y) && double.IsFinite(value.Z) && double.IsFinite(value.W);
static bool INumberBase.IsInfinity(doubleQuat value) =>
double.IsInfinity(value.X) || double.IsInfinity(value.Y) || double.IsInfinity(value.Z) || double.IsInfinity(value.W);
static bool INumberBase.IsNegativeInfinity(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsNegativeInfinity(value.W);
static bool INumberBase.IsPositiveInfinity(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsPositiveInfinity(value.W);
// NaN present and no infinity - infinity dominates, per Complex
static bool INumberBase.IsNaN(doubleQuat value) =>
(double.IsNaN(value.X) || double.IsNaN(value.Y) || double.IsNaN(value.Z) || double.IsNaN(value.W))
&& !(double.IsInfinity(value.X) || double.IsInfinity(value.Y) || double.IsInfinity(value.Z) || double.IsInfinity(value.W));
static bool INumberBase.IsNormal(doubleQuat value) =>
double.IsNormal(value.W)
&& (value.X == 0 && value.Y == 0 && value.Z == 0 || (double.IsNormal(value.X) && double.IsNormal(value.Y) && double.IsNormal(value.Z)));
static bool INumberBase.IsSubnormal(doubleQuat value) =>
double.IsSubnormal(value.X) || double.IsSubnormal(value.Y) || double.IsSubnormal(value.Z) || double.IsSubnormal(value.W);
static doubleQuat INumberBase.MaxMagnitude(doubleQuat a, doubleQuat b)
{
var magnitudeA = a.LengthSquared();
var magnitudeB = b.LengthSquared();
if (double.IsNaN(magnitudeA))
return a;
if (double.IsNaN(magnitudeB))
return b;
return magnitudeA >= magnitudeB ? a : b;
}
static doubleQuat INumberBase.MaxMagnitudeNumber(doubleQuat a, doubleQuat b)
{
var magnitudeA = a.LengthSquared();
var magnitudeB = b.LengthSquared();
if (double.IsNaN(magnitudeA))
return b;
if (double.IsNaN(magnitudeB))
return a;
return magnitudeA >= magnitudeB ? a : b;
}
static doubleQuat INumberBase.MinMagnitude(doubleQuat a, doubleQuat b)
{
var magnitudeA = a.LengthSquared();
var magnitudeB = b.LengthSquared();
if (double.IsNaN(magnitudeA))
return a;
if (double.IsNaN(magnitudeB))
return b;
return magnitudeA <= magnitudeB ? a : b;
}
static doubleQuat INumberBase.MinMagnitudeNumber(doubleQuat a, doubleQuat b)
{
var magnitudeA = a.LengthSquared();
var magnitudeB = b.LengthSquared();
if (double.IsNaN(magnitudeA))
return b;
if (double.IsNaN(magnitudeB))
return a;
return magnitudeA <= magnitudeB ? a : b;
}
/* ++/-- add/subtract One = Identity, bumping W - exactly what Complex does. Generic-plane
* only: a footgun on a concrete quat. */
static doubleQuat IIncrementOperators.operator ++(doubleQuat value) => value + Identity;
static doubleQuat IDecrementOperators.operator --(doubleQuat value) => value - Identity;
static bool INumberBase.TryConvertFromChecked(TOther value, out doubleQuat result)
{
if (typeof(TOther) == typeof(doubleQuat))
{
result = (doubleQuat)(object)value!;
return true;
}
if (typeof(TOther) == typeof(floatQuat))
{
var other = (floatQuat)(object)value!;
result = new(double.CreateChecked(other.X), double.CreateChecked(other.Y), double.CreateChecked(other.Z), double.CreateChecked(other.W));
return true;
}
if (typeof(TOther) == typeof(sbyte))
{
result = new(0, 0, 0, double.CreateChecked((sbyte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(byte))
{
result = new(0, 0, 0, double.CreateChecked((byte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(short))
{
result = new(0, 0, 0, double.CreateChecked((short)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ushort))
{
result = new(0, 0, 0, double.CreateChecked((ushort)(object)value!));
return true;
}
if (typeof(TOther) == typeof(int))
{
result = new(0, 0, 0, double.CreateChecked((int)(object)value!));
return true;
}
if (typeof(TOther) == typeof(uint))
{
result = new(0, 0, 0, double.CreateChecked((uint)(object)value!));
return true;
}
if (typeof(TOther) == typeof(long))
{
result = new(0, 0, 0, double.CreateChecked((long)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ulong))
{
result = new(0, 0, 0, double.CreateChecked((ulong)(object)value!));
return true;
}
if (typeof(TOther) == typeof(float))
{
result = new(0, 0, 0, double.CreateChecked((float)(object)value!));
return true;
}
if (typeof(TOther) == typeof(double))
{
result = new(0, 0, 0, double.CreateChecked((double)(object)value!));
return true;
}
result = default;
return false;
}
static bool INumberBase.TryConvertToChecked(doubleQuat value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
static bool INumberBase.TryConvertFromSaturating(TOther value, out doubleQuat result)
{
if (typeof(TOther) == typeof(doubleQuat))
{
result = (doubleQuat)(object)value!;
return true;
}
if (typeof(TOther) == typeof(floatQuat))
{
var other = (floatQuat)(object)value!;
result = new(double.CreateSaturating(other.X), double.CreateSaturating(other.Y), double.CreateSaturating(other.Z), double.CreateSaturating(other.W));
return true;
}
if (typeof(TOther) == typeof(sbyte))
{
result = new(0, 0, 0, double.CreateSaturating((sbyte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(byte))
{
result = new(0, 0, 0, double.CreateSaturating((byte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(short))
{
result = new(0, 0, 0, double.CreateSaturating((short)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ushort))
{
result = new(0, 0, 0, double.CreateSaturating((ushort)(object)value!));
return true;
}
if (typeof(TOther) == typeof(int))
{
result = new(0, 0, 0, double.CreateSaturating((int)(object)value!));
return true;
}
if (typeof(TOther) == typeof(uint))
{
result = new(0, 0, 0, double.CreateSaturating((uint)(object)value!));
return true;
}
if (typeof(TOther) == typeof(long))
{
result = new(0, 0, 0, double.CreateSaturating((long)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ulong))
{
result = new(0, 0, 0, double.CreateSaturating((ulong)(object)value!));
return true;
}
if (typeof(TOther) == typeof(float))
{
result = new(0, 0, 0, double.CreateSaturating((float)(object)value!));
return true;
}
if (typeof(TOther) == typeof(double))
{
result = new(0, 0, 0, double.CreateSaturating((double)(object)value!));
return true;
}
result = default;
return false;
}
static bool INumberBase.TryConvertToSaturating(doubleQuat value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
static bool INumberBase.TryConvertFromTruncating(TOther value, out doubleQuat result)
{
if (typeof(TOther) == typeof(doubleQuat))
{
result = (doubleQuat)(object)value!;
return true;
}
if (typeof(TOther) == typeof(floatQuat))
{
var other = (floatQuat)(object)value!;
result = new(double.CreateTruncating(other.X), double.CreateTruncating(other.Y), double.CreateTruncating(other.Z), double.CreateTruncating(other.W));
return true;
}
if (typeof(TOther) == typeof(sbyte))
{
result = new(0, 0, 0, double.CreateTruncating((sbyte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(byte))
{
result = new(0, 0, 0, double.CreateTruncating((byte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(short))
{
result = new(0, 0, 0, double.CreateTruncating((short)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ushort))
{
result = new(0, 0, 0, double.CreateTruncating((ushort)(object)value!));
return true;
}
if (typeof(TOther) == typeof(int))
{
result = new(0, 0, 0, double.CreateTruncating((int)(object)value!));
return true;
}
if (typeof(TOther) == typeof(uint))
{
result = new(0, 0, 0, double.CreateTruncating((uint)(object)value!));
return true;
}
if (typeof(TOther) == typeof(long))
{
result = new(0, 0, 0, double.CreateTruncating((long)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ulong))
{
result = new(0, 0, 0, double.CreateTruncating((ulong)(object)value!));
return true;
}
if (typeof(TOther) == typeof(float))
{
result = new(0, 0, 0, double.CreateTruncating((float)(object)value!));
return true;
}
if (typeof(TOther) == typeof(double))
{
result = new(0, 0, 0, double.CreateTruncating((double)(object)value!));
return true;
}
result = default;
return false;
}
static bool INumberBase.TryConvertToTruncating(doubleQuat value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Add(doubleQuat a, doubleQuat b)
{
return a + b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Divide(doubleQuat a, doubleQuat b)
{
return a / b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Multiply(doubleQuat a, doubleQuat b)
{
return a * b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Negate(doubleQuat value)
{
return -value;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Subtract(doubleQuat a, doubleQuat b)
{
return a - b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat operator +(doubleQuat a, doubleQuat b)
{
if (!Vector256.IsHardwareAccelerated)
return new(a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W);
return Unsafe.BitCast, doubleQuat>(Unsafe.BitCast>(a) + Unsafe.BitCast>(b));
}
///
public static doubleQuat operator /(doubleQuat a, doubleQuat b)
{
doubleQuat ans;
var ax = a.X;
var ay = a.Y;
var az = a.Z;
var aw = a.W;
//-------------------------------------
// Inverse part.
var ls = b.X * b.X + b.Y * b.Y + b.Z * b.Z + b.W * b.W;
var invNorm = 1 / ls;
var bx = -b.X * invNorm;
var by = -b.Y * invNorm;
var bz = -b.Z * invNorm;
var bw = b.W * invNorm;
//-------------------------------------
// Multiply part.
// cross(av, bv)
var cx = ay * bz - az * by;
var cy = az * bx - ax * bz;
var cz = ax * by - ay * bx;
var dot = ax * bx + ay * by + az * bz;
ans.X = ax * bw + bx * aw + cx;
ans.Y = ay * bw + by * aw + cy;
ans.Z = az * bw + bz * aw + cz;
ans.W = aw * bw - dot;
return ans;
}
///
public static doubleQuat operator *(doubleQuat a, doubleQuat b)
{
// off AVX2: scalar arm, in the kernel's accumulation order
if (!Vector256.IsHardwareAccelerated)
return new doubleQuat(
b.X * a.W + b.W * a.X + b.Z * a.Y - b.Y * a.Z,
b.Y * a.W - b.Z * a.X + b.W * a.Y + b.X * a.Z,
b.Z * a.W + b.Y * a.X - b.X * a.Y + b.W * a.Z,
b.W * a.W - b.X * a.X - b.Y * a.Y - b.Z * a.Z);
var left = Unsafe.BitCast>(a);
var right = Unsafe.BitCast>(b);
var result = right * left.GetElement(3);
result = Vector256.MultiplyAddEstimate(Vector256.ShuffleNative(right, Vector256.Create(3L, 2L, 1L, 0L)) * left.GetElement(0), Vector256.Create(+1.0, -1.0, +1.0, -1.0), result);
result = Vector256.MultiplyAddEstimate(Vector256.ShuffleNative(right, Vector256.Create(2L, 3L, 0L, 1L)) * left.GetElement(1), Vector256.Create(+1.0, +1.0, -1.0, -1.0), result);
result = Vector256.MultiplyAddEstimate(Vector256.ShuffleNative(right, Vector256.Create(1L, 0L, 3L, 2L)) * left.GetElement(2), Vector256.Create(-1.0, +1.0, +1.0, -1.0), result);
return Unsafe.BitCast, doubleQuat>(result);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat operator -(doubleQuat a, doubleQuat b)
{
if (!Vector256.IsHardwareAccelerated)
return new(a.X - b.X, a.Y - b.Y, a.Z - b.Z, a.W - b.W);
return Unsafe.BitCast, doubleQuat>(Unsafe.BitCast>(a) - Unsafe.BitCast>(b));
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat operator -(doubleQuat value)
{
return Zero - value;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat operator +(doubleQuat value)
{
return value;
}
}
// IMultiplyOperators, IDivisionOperators
public partial struct doubleQuat
: IMultiplyOperators
, IDivisionOperators
{
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Multiply(doubleQuat a, double b)
{
return a * b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat operator *(doubleQuat a, double b)
{
if (!Vector256.IsHardwareAccelerated)
return new(a.X * b, a.Y * b, a.Z * b, a.W * b);
return Unsafe.BitCast, doubleQuat>(Unsafe.BitCast>(a) * Vector256.Create(b));
}
/// Divides a quaternion by a scalar, componentwise.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat operator /(doubleQuat a, double b)
{
if (!Vector256.IsHardwareAccelerated)
return new(a.X / b, a.Y / b, a.Z / b, a.W / b);
return Unsafe.BitCast, doubleQuat>(Unsafe.BitCast>(a) / Vector256.Create(b));
}
}