Skip to content

BUG: spatial.distance.cosine with complex arguments raises ValueError #20994

@luxedo

Description

@luxedo

Describe your issue.

When trying to calculate the cosine distance between [1+2j, 3+4j] and [5 + 6j, 7+8j] scipy raises a ValueError.
WolframAlpha gives a solution:

image

Reproducing Code Example

from scipy.spatial.distance import cosine
cosine([1+2j, 3+4j], [5+6j, 7+8j])

Error message

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[14], line 1
----> 1 cosine([1+2j, 3+4j], [5+6j, 7+8j])

File /usr/lib/python3.11/site-packages/scipy/spatial/distance.py:695, in cosine(u, v, w)
    653 """
    654 Compute the Cosine distance between 1-D arrays.
    655 
   (...)
    690 
    691 """
    692 # cosine distance is also referred to as 'uncentered correlation',
    693 #   or 'reflective correlation'
    694 # clamp the result to 0-2
--> 695 return max(0, min(correlation(u, v, w=w, centered=False), 2.0))

File /usr/lib/python3.11/site-packages/scipy/spatial/distance.py:647, in correlation(u, v, w, centered)
    645 uu = np.dot(u, uw)
    646 vv = np.dot(v, vw)
--> 647 dist = 1.0 - uv / math.sqrt(uu * vv)
    648 # Return absolute value to avoid small negative value due to rounding
    649 return abs(dist)

ValueError: math domain error

SciPy/NumPy/Python version and system information

1.12.0 1.23.5 sys.version_info(major=3, minor=11, micro=8, releaselevel='final', serial=0)
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /usr/include
    lib directory: /usr/lib
    name: blas
    openblas configuration: unknown
    pc file directory: /usr/lib/pkgconfig
    version: 3.12.0
  lapack:
    detection method: pkgconfig
    found: true
    include directory: /usr/include
    lib directory: /usr/lib
    name: lapack
    openblas configuration: unknown
    pc file directory: /usr/lib/pkgconfig
    version: 3.12.0
  pybind11:
    detection method: pkgconfig
    include directory: /usr/lib/pkgconfig/../..//include
    name: pybind11
    version: 2.11.1
Compilers:
  c:
    args: -march=x86-64, -mtune=generic, -O2, -pipe, -fno-plt, -fexceptions, -Wp,-D_FORTIFY_SOURCE=2,
      -Wformat, -Werror=format-security, -fstack-clash-protection, -fcf-protection,
      -g, -ffile-prefix-map=/build/python-scipy/src=/usr/src/debug/python-scipy, -flto=auto
    commands: cc
    linker: ld.bfd
    linker args: -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now, -flto=auto, -march=x86-64,
      -mtune=generic, -O2, -pipe, -fno-plt, -fexceptions, -Wp,-D_FORTIFY_SOURCE=2,
      -Wformat, -Werror=format-security, -fstack-clash-protection, -fcf-protection,
      -g, -ffile-prefix-map=/build/python-scipy/src=/usr/src/debug/python-scipy, -flto=auto
    name: gcc
    version: 13.2.1
  c++:
    args: -march=x86-64, -mtune=generic, -O2, -pipe, -fno-plt, -fexceptions, -Wp,-D_FORTIFY_SOURCE=2,
      -Wformat, -Werror=format-security, -fstack-clash-protection, -fcf-protection,
      -Wp,-D_GLIBCXX_ASSERTIONS, -g, -ffile-prefix-map=/build/python-scipy/src=/usr/src/debug/python-scipy,
      -flto=auto
    commands: c++
    linker: ld.bfd
    linker args: -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now, -flto=auto, -march=x86-64,
      -mtune=generic, -O2, -pipe, -fno-plt, -fexceptions, -Wp,-D_FORTIFY_SOURCE=2,
      -Wformat, -Werror=format-security, -fstack-clash-protection, -fcf-protection,
      -Wp,-D_GLIBCXX_ASSERTIONS, -g, -ffile-prefix-map=/build/python-scipy/src=/usr/src/debug/python-scipy,
      -flto=auto
    name: gcc
    version: 13.2.1
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 3.0.8
  fortran:
    commands: gfortran
    linker: ld.bfd
    linker args: -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now, -flto=auto
    name: gcc
    version: 13.2.1
  pythran:
    include directory: ../../../../../usr/lib/python3.11/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: /usr/bin/python
  version: '3.11'

Metadata

Metadata

Assignees

No one assigned

    Labels

    defectA clear bug or issue that prevents SciPy from being installed or used as expectedscipy.spatial

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions