Repository navigation
Support opposite direction vectors in align_vectors - #1033
davrens101 wants to merge 2 commits into
Conversation
Signed-off-by: Davis Rens <166565828+davrens101@users.noreply.github.com>
Merging this PR will degrade performance by 43.55%
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance ChangesTip Investigate this regression by commenting Comparing Footnotes
|
Description
Currently,
biotite.structure.align_vectors()raises aValueErrorwhenorigin_directionandtarget_directionpoint in opposite directions:This limitation occurs because the standard rotation formula uses$(v \times w) / (1 + \cos\alpha)$ , where $1 + \cos\alpha = 0$ when $\cos\alpha = -1$ . Furthermore, in single-precision floating point arithmetic, directions that are nearly opposite can also evaluate to
cos_a == -1.0due to precision limits.Mathematically, aligning two anti-parallel vectors in 3D corresponds to a 180° rotation around any axis perpendicular to the vectors. This PR resolves the singularity by choosing an orthogonal axis and constructing the corresponding 180° reflection-rotation ($R = 2 u u^T - I \in \mathrm{SO}(3)$).
Changes
align_vectors(), whentest_align_vectors_opposite()verifying that anti-parallel vectors in arbitrary directions align to the target vector with determinant 1 and orthogonality preserved.AI assistance disclosure: Co-authored with AI coding assistant.