Skip to content

Bug: ak.linspace Fails for num == 1 (Scalar Case) #5267

Closed

Description

@ajpotts

Bug: ak.linspace Fails for num == 1 (Scalar Case)

Summary

ak.linspace raises a ZeroDivisionError when called with num == 1 and scalar start/stop values.
NumPy defines this as a valid case and returns a length‑1 array containing start.

This is a behavior mismatch with NumPy and breaks NumPy‑alignment tests.

Repro

NumPy

import numpy as np
np.linspace(0, 1, 1, endpoint=True)
# array([0.])

Arkouda

import arkouda as ak
ak.linspace(0, 1, 1, endpoint=True)
# ZeroDivisionError

Root Cause

In the scalar path, Arkouda computes:

divisor = num - 1 if endpoint else num
delta = (stop - start) / divisor

When num == 1 and endpoint == True, divisor == 0, leading to a divide‑by‑zero.

NumPy treats num == 1 as a special case and does not compute a step size.

Expected Behavior (NumPy‑Compatible)

  • num == 0 → empty array
  • num == 1 → array containing start
  • Works consistently for:
    • scalar start / stop
    • vector / broadcasted start / stop
    • both endpoint=True and endpoint=False

Scope

  • arkouda.numpy.pdarraycreation.linspace
  • Scalar and vector code paths

Proposed Fix

Add an early return before computing divisor:

if num == 0:
    return ak.array([], dtype=ak.float64)

if num == 1:
    if isinstance(start_, pdarray):
        return start_.reshape((1,) + start_.shape)
    else:
        return ak.array([start_], dtype=ak.float64)

Test Plan

  • Convert NumPy‑alignment test for num == 1 from xfail to passing
  • Add explicit tests:
    • ak.linspace(0, 1, 1)
    • ak.linspace(0, 1, 1, endpoint=False)
    • Vector start/stop with num == 1

Acceptance Criteria

  • ak.linspace(..., num=1) matches NumPy output
  • No ZeroDivisionError
  • Alignment tests pass without xfail

Activity

  1. added
    bug
    on Jan 8, 2026
  2. self-assigned this
    on Feb 2, 2026
  3. added a commit that references this issue on Feb 4, 2026
    93d7e5d
  4. added a commit that references this issue on Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions