Class LinearAlgebra
- Namespace
- MathAssertions
- Assembly
- MathAssertions.dll
Linear-algebra invariants over Matrix4x4 and Vector3:
matrix symmetry, orthogonality, identity, determinant, trace, invertibility, plus
vector-pair orthogonality and parallelism and the
AreLinearlyIndependent(ReadOnlySpan<Vector3>, double) triple-product
check for sets of up to three vectors in R^3.
public static class LinearAlgebra
- Inheritance
-
LinearAlgebra
- Inherited Members
Remarks
0.1.0 Cluster 5. Sits alongside MathTolerance, Sequences,
and Statistics in the same package; the MathAssertions.TUnit
adapter delegates to these helpers from its [GenerateAssertion] extensions.
Methods
AngleBetween(Vector3, Vector3)
Returns the unsigned angle, in radians on [0, pi], between two vectors, computed as
atan2(|u x v|, u . v). This form stays numerically accurate across the whole range,
unlike acos((u . v) / (|u| |v|)), which loses precision near 0 and pi
where the cosine flattens. A zero vector yields an angle of 0 by convention (the angle
is otherwise undefined).
public static double AngleBetween(Vector3 u, Vector3 v)
Parameters
Returns
- double
The angle between the vectors in radians, on
[0, pi].
AreLinearlyIndependent(ReadOnlySpan<Vector3>, double)
Returns true when the supplied vectors are linearly independent
in R^3. Up to three vectors can be linearly independent; spans of four or
more vectors in R^3 are always dependent and return false.
The empty span is vacuously independent and returns true.
public static bool AreLinearlyIndependent(ReadOnlySpan<Vector3> vectors, double tolerance)
Parameters
vectorsReadOnlySpan<Vector3>Vectors to test.
tolerancedoubleThreshold for the length and triple-product checks. Must be non-negative and not NaN.
Returns
Remarks
Length-1: the single vector is independent iff its length exceeds tolerance.
Length-2: equivalent to AreParallel(Vector3, Vector3, double)
returning false.
Length-3: triple-product test
|v1 . (v2 x v3)| > tolerance. The absolute scalar triple product is the
volume of the parallelepiped spanned by the three vectors and is non-zero iff the
three are linearly independent.
The length-1 case compares vector length directly against tolerance rather than length-squared against tolerance-squared, so the verdict is well-defined for extreme tolerance magnitudes (where squaring would underflow or overflow).
Exceptions
- ArgumentOutOfRangeException
toleranceis NaN or negative.
AreOrthogonal(Vector3, Vector3, double)
Returns true when the two vectors are orthogonal:
their dot product is within tolerance of zero.
public static bool AreOrthogonal(Vector3 u, Vector3 v, double tolerance)
Parameters
uVector3First vector.
vVector3Second vector.
tolerancedoubleMaximum allowed absolute deviation of the dot product from zero. Must be non-negative and not NaN.
Returns
Exceptions
- ArgumentOutOfRangeException
toleranceis NaN or negative.
AreParallel(Vector3, Vector3, double)
Returns true when the two vectors are parallel (or anti-parallel):
the sine of the angle between their directions, |u x v| / (|u| |v|), is within
tolerance of zero. The measure is scale-invariant, so parallelism
depends only on direction, not on the vectors' magnitudes. A zero (or shorter-than-tolerance)
vector is treated as parallel to every other vector, both because 0 x v = 0 and to
avoid dividing by a zero length.
public static bool AreParallel(Vector3 u, Vector3 v, double tolerance)
Parameters
uVector3First vector.
vVector3Second vector.
tolerancedoubleMaximum allowed sine of the angle between the directions. Must be non-negative and not NaN.
Returns
Remarks
The tolerance is an angular measure (the sine of the maximum permitted deviation from exactly
parallel), not an absolute cross-product magnitude. For small angles sin(theta) ~ theta,
so a tolerance of 1e-3 admits directions within roughly a milliradian of parallel
regardless of how long the vectors are.
Exceptions
- ArgumentOutOfRangeException
toleranceis NaN or negative.
HasDeterminantApproximately(Matrix4x4, double, double)
Returns true when the matrix determinant is within
tolerance of expected. Delegates the
computation to GetDeterminant().
public static bool HasDeterminantApproximately(Matrix4x4 m, double expected, double tolerance)
Parameters
mMatrix4x4Matrix to test.
expecteddoubleExpected determinant value.
tolerancedoubleMaximum allowed absolute difference. Must be non-negative and not NaN.
Returns
Exceptions
- ArgumentOutOfRangeException
toleranceis NaN or negative.
HasTraceApproximately(Matrix4x4, double, double)
Returns true when the matrix trace (sum of diagonal elements)
is within tolerance of expected.
public static bool HasTraceApproximately(Matrix4x4 m, double expected, double tolerance)
Parameters
mMatrix4x4Matrix to test.
expecteddoubleExpected trace value.
tolerancedoubleMaximum allowed absolute difference. Must be non-negative and not NaN.
Returns
Exceptions
- ArgumentOutOfRangeException
toleranceis NaN or negative.
IsIdentity(Matrix4x4, double)
Returns true when the matrix equals the Identity matrix element-wise within tolerance. Convenience wrapper over IsApproximatelyEqual(Matrix4x4, Matrix4x4, double) with the identity as the second operand.
public static bool IsIdentity(Matrix4x4 m, double tolerance)
Parameters
mMatrix4x4Matrix to test.
tolerancedoubleMaximum allowed absolute difference per element. Must be non-negative and not NaN.
Returns
Exceptions
- ArgumentOutOfRangeException
toleranceis NaN or negative.
IsInvertible(Matrix4x4, double)
Returns true when the absolute value of the determinant exceeds
tolerance. The threshold expresses "the matrix is far enough
from singular to invert numerically"; choose a tolerance that reflects the
expected condition number of the inputs.
public static bool IsInvertible(Matrix4x4 m, double tolerance)
Parameters
mMatrix4x4Matrix to test.
tolerancedoubleMinimum acceptable absolute determinant for the matrix to be considered invertible. Must be non-negative and not NaN.
Returns
Exceptions
- ArgumentOutOfRangeException
toleranceis NaN or negative.
IsOrthogonal(Matrix4x4, double)
Returns true when the matrix is orthogonal: M * M^T = I
within tolerance. Orthogonal matrices preserve angles and lengths and have
determinant +/- 1.
public static bool IsOrthogonal(Matrix4x4 m, double tolerance)
Parameters
mMatrix4x4Matrix to test.
tolerancedoubleMaximum allowed absolute difference between
M * M^Tand the identity per element. Must be non-negative and not NaN.
Returns
Remarks
Translation matrices are not orthogonal because the translation column makes the
product M * M^T deviate from identity in the off-diagonals. A pure rotation
is orthogonal; rotations composed with uniform scaling are not (the diagonal of
M * M^T picks up the squared scale).
Exceptions
- ArgumentOutOfRangeException
toleranceis NaN or negative.
IsRotation(Matrix4x4, double)
Returns true when the matrix is a proper rotation: orthogonal
(M * M^T = I, so it preserves lengths and angles) and has determinant
+1 within tolerance. The determinant condition rules out reflections (an improper
orthogonal matrix has determinant -1). A matrix carrying a non-zero translation is not
orthogonal and so is not a rotation under this definition.
public static bool IsRotation(Matrix4x4 m, double tolerance)
Parameters
mMatrix4x4Matrix to test.
tolerancedoubleMaximum allowed absolute deviation, applied to both the orthogonality check (per element of
M * M^T - I) and the determinant (from+1). Must be non-negative and not NaN.
Returns
Exceptions
- ArgumentOutOfRangeException
toleranceis NaN or negative.
IsSymmetric(Matrix4x4, double)
Returns true when the matrix is symmetric: every off-diagonal
element equals its transpose partner within tolerance, that is
m[i, j] = m[j, i] for all i != j.
public static bool IsSymmetric(Matrix4x4 m, double tolerance)
Parameters
mMatrix4x4Matrix to test.
tolerancedoubleMaximum allowed absolute difference between mirrored off-diagonal elements. Must be non-negative and not NaN.
Returns
Exceptions
- ArgumentOutOfRangeException
toleranceis NaN or negative.