Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: spatial: error in Rotation.align_vectors() with an infinite weight #20555

Closed
scottshambaugh opened this issue Apr 22, 2024 · 1 comment · Fixed by #20573
Closed

BUG: spatial: error in Rotation.align_vectors() with an infinite weight #20555

scottshambaugh opened this issue Apr 22, 2024 · 1 comment · Fixed by #20573
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.spatial
Milestone

Comments

@scottshambaugh
Copy link
Contributor

scottshambaugh commented Apr 22, 2024

Describe your issue.

I found a degenerate case when aligning vectors exactly with Rotation.align_vectors and an infinite weight.

Reproducing Code Example

import numpy as np
from scipy.spatial.transform import Rotation

# Wrong:
Rotation.align_vectors([[0, 1, 0], [-1, 0, 0]], [[0, -1, 0], [-1, 0, 0]], weights=[np.inf, 1])[0].as_rotvec()
# array([0., 0., 0.])

# Right:
Rotation.align_vectors([[0, 1, 0], [-1, 0, 0]], [[0, -1, 0], [-1, 0, 0]], weights=[1e6, 1])[0].as_rotvec()
# array([3.14159265, 0., 0.])

SciPy/NumPy/Python version and system information

import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info); scipy.show_config()
1.13.0 1.26.3 sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0)
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /usr/local/include
    lib directory: /usr/local/lib
    name: openblas
    openblas configuration: USE_64BITINT=0 DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= ZEN MAX_THREADS=64
    pc file directory: /usr/local/lib/pkgconfig
    version: 0.3.26.dev
  lapack:
    detection method: pkgconfig
    found: true
    include directory: /usr/local/include
    lib directory: /usr/local/lib
    name: openblas
    openblas configuration: USE_64BITINT=0 DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= ZEN MAX_THREADS=64
    pc file directory: /usr/local/lib/pkgconfig
    version: 0.3.26.dev
  pybind11:
    detection method: config-tool
    include directory: unknown
    name: pybind11
    version: 2.12.0
Compilers:
  c:
    commands: cc
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  c++:
    commands: c++
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 3.0.10
  fortran:
    commands: gfortran
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  pythran:
    include directory: ../../tmp/pip-build-env-0blqy1or/overlay/lib/python3.10/site-packages/pythran
    version: 0.15.0
Machine Information:
  build:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
  cross-compiled: false
  host:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
Python Information:
  path: /opt/python/cp310-cp310/bin/python
  version: '3.10'
@scottshambaugh scottshambaugh added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label Apr 22, 2024
@scottshambaugh scottshambaugh changed the title BUG: Error in Rotation.align_vectors() calculation BUG: Error in Rotation.align_vectors() calculation with an infinite weight Apr 22, 2024
@lucascolley lucascolley changed the title BUG: Error in Rotation.align_vectors() calculation with an infinite weight BUG: spatial: error in Rotation.align_vectors() with an infinite weight Apr 22, 2024
@scottshambaugh
Copy link
Contributor Author

scottshambaugh commented Apr 23, 2024

I'm seeing the issue here, working on a fix. Problem is here:

# We first find the minimum angle rotation between the primary
# vectors.
cross = np.cross(b_pri[0], a_pri[0])
theta = atan2(_norm3(cross), np.dot(a_pri[0], b_pri[0]))
if theta < 1e-3: # small angle Taylor series approximation
theta2 = theta * theta
r = cross * (1 + theta2 / 6 + theta2 * theta2 * 7 / 360)
else:
r = cross * theta / np.sin(theta)
R_pri = cls.from_rotvec(r)

There are two related problems:

  • A perfect 180 deg rotation results in r = cross = [0, 0, 0] (this bug, an error)
  • Potential numerical instabilities near a 180 deg rotation (not exactly this bug but will fix and add tests)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.spatial
Projects
None yet
4 participants
@tylerjereddy @scottshambaugh @j-bowhay and others