Skip to content

BUG/ENH?: scipy.linalg.norm: overflow/underflow when axis is provided #20136

Open
@mdhaber

Description

@mdhaber

Describe your issue.

I'm working on adding an axis argument to scipy.stats.pearsonr. It uses scipy.linalg.norm instead of (e.g.) np.linalg.norm because scipy.linalg.norm tends to avoid premature overflow.

from scipy import linalg
x = [-5e210, 5e210, 3e200, -3e200]
linalg.norm(x)
# 7.071067811865475e+210

Unfortunately, that advantage is lost when axis is not None.

linalg.norm(x, axis=0)  # warning and inf

Similarly, premature underflow can occur when the argument has small magnitude elements and axis is not None.

I can work around it if need be (e.g. manually scale), but can linalg.norm avoid premature under/overflow regardless of axis?

Reproducing Code Example

from scipy import linalg
x = [-5e210, 5e210, 3e200, -3e200]
linalg.norm(x, axis=0)

Error message

RuntimeWarning: overflow encountered in multiply s = (x.conj() * x).real

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.dev0+1376.8360bac 1.26.0 sys.version_info(major=3, minor=11, micro=6, releaselevel='final', serial=0)
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /Users/matthaberland/miniforge3/envs/scipy-dev/include
    lib directory: /Users/matthaberland/miniforge3/envs/scipy-dev/lib
    name: openblas
    openblas configuration: USE_64BITINT=0 DYNAMIC_ARCH=0 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK=0 NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP=1 VORTEX MAX_THREADS=128
    pc file directory: /Users/matthaberland/miniforge3/envs/scipy-dev/lib/pkgconfig
    version: 0.3.24
  lapack:
    detection method: pkgconfig
    found: true
    include directory: /Users/matthaberland/miniforge3/envs/scipy-dev/include
    lib directory: /Users/matthaberland/miniforge3/envs/scipy-dev/lib
    name: openblas
    openblas configuration: USE_64BITINT=0 DYNAMIC_ARCH=0 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK=0 NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP=1 VORTEX MAX_THREADS=128
    pc file directory: /Users/matthaberland/miniforge3/envs/scipy-dev/lib/pkgconfig
    version: 0.3.24
  pybind11:
    detection method: pkgconfig
    include directory: /Users/matthaberland/miniforge3/envs/scipy-dev/include
    name: pybind11
    version: 2.11.1
Compilers:
  c:
    args: -ftree-vectorize, -fPIC, -fPIE, -fstack-protector-strong, -O2, -pipe, -isystem,
      /Users/matthaberland/miniforge3/envs/scipy-dev/include, -D_FORTIFY_SOURCE=2,
      -isystem, /Users/matthaberland/miniforge3/envs/scipy-dev/include
    commands: arm64-apple-darwin20.0.0-clang
    linker: ld64
    linker args: -Wl,-pie, -Wl,-headerpad_max_install_names, -Wl,-dead_strip_dylibs,
      -Wl,-rpath,/Users/matthaberland/miniforge3/envs/scipy-dev/lib, -L/Users/matthaberland/miniforge3/envs/scipy-dev/lib,
      -ftree-vectorize, -fPIC, -fPIE, -fstack-protector-strong, -O2, -pipe, -isystem,
      /Users/matthaberland/miniforge3/envs/scipy-dev/include, -D_FORTIFY_SOURCE=2,
      -isystem, /Users/matthaberland/miniforge3/envs/scipy-dev/include
    name: clang
    version: 15.0.7
  c++:
    args: -ftree-vectorize, -fPIC, -fPIE, -fstack-protector-strong, -O2, -pipe, -stdlib=libc++,
      -fvisibility-inlines-hidden, -fmessage-length=0, -isystem, /Users/matthaberland/miniforge3/envs/scipy-dev/include,
      -D_FORTIFY_SOURCE=2, -isystem, /Users/matthaberland/miniforge3/envs/scipy-dev/include
    commands: arm64-apple-darwin20.0.0-clang++
    linker: ld64
    linker args: -Wl,-pie, -Wl,-headerpad_max_install_names, -Wl,-dead_strip_dylibs,
      -Wl,-rpath,/Users/matthaberland/miniforge3/envs/scipy-dev/lib, -L/Users/matthaberland/miniforge3/envs/scipy-dev/lib,
      -ftree-vectorize, -fPIC, -fPIE, -fstack-protector-strong, -O2, -pipe, -stdlib=libc++,
      -fvisibility-inlines-hidden, -fmessage-length=0, -isystem, /Users/matthaberland/miniforge3/envs/scipy-dev/include,
      -D_FORTIFY_SOURCE=2, -isystem, /Users/matthaberland/miniforge3/envs/scipy-dev/include
    name: clang
    version: 15.0.7
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 3.0.4
  fortran:
    args: -march=armv8.3-a, -ftree-vectorize, -fPIC, -fno-stack-protector, -O2, -pipe,
      -isystem, /Users/matthaberland/miniforge3/envs/scipy-dev/include
    commands: /Users/matthaberland/miniforge3/envs/scipy-dev/bin/arm64-apple-darwin20.0.0-gfortran
    linker: ld64
    linker args: -Wl,-pie, -Wl,-headerpad_max_install_names, -Wl,-dead_strip_dylibs,
      -Wl,-rpath,/Users/matthaberland/miniforge3/envs/scipy-dev/lib, -L/Users/matthaberland/miniforge3/envs/scipy-dev/lib,
      -march=armv8.3-a, -ftree-vectorize, -fPIC, -fno-stack-protector, -O2, -pipe,
      -isystem, /Users/matthaberland/miniforge3/envs/scipy-dev/include
    name: gcc
    version: 12.3.0
  pythran:
    include directory: ../../../miniforge3/envs/scipy-dev/lib/python3.11/site-packages/pythran
    version: 0.14.0
Machine Information:
  build:
    cpu: aarch64
    endian: little
    family: aarch64
    system: darwin
  cross-compiled: false
  host:
    cpu: aarch64
    endian: little
    family: aarch64
    system: darwin
Python Information:
  path: /Users/matthaberland/miniforge3/envs/scipy-dev/bin/python
  version: '3.11'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions