Skip to content

uncertainty is zero for np.sqrt but pow(x, 0.5) is okay #74

Open

Description

@mikofski
>>> import numpy as np
>>> import quantities as pq
11.0905365064 m/s
>>> x = pq.UncertainQuantity(123, pq.m**2/pq.s**2, 1.0)
>>> print x**0.5
11.0905365064 m/s
+/-0.0450834817334 m/s (1 sigma)

>>> print np.sqrt(x)
11.0905365064 m/s
+/-0.0 dimensionless (1 sigma)

Activity

  1. FFspiky commented on Nov 16, 2026

    @FFspiky
    Contributor

    Reproduced on current master (3748f450) with NumPy 2.0.2 and 2.5.5. Before preparing a production patch, there is a ufunc-dispatch question that affects a correct fix.

    The existing __array_wrap__ hook does not receive where. For input [4, 9] +/- [0.4, 0.6] m**2 and an existing output [2, 3] +/- [1, 2] m, masks [True, False] and [False, True] leave identical magnitudes visible to that hook, but require different uncertainties: [0.1, 2] versus [1, 0.1]. A wrap-only derivative update cannot preserve the unselected entries.

    A sqrt-only __array_ufunc__ override with a simple super() fallback also makes previously working multiplication, addition and reductions return NotImplemented. Converting all fallback arguments to plain Quantity avoids that error but loses the uncertain subclass and output identity. These cases were reproduced on both NumPy versions.

    A local numerical prototype passes 26 cases covering units, dtype, masks, output identity, broadcasting and in-place output, but it is not integrated into the production class. Would a shared Quantity/UncertainQuantity ufunc-dispatch implementation be an acceptable direction, preserving existing non-sqrt behavior and foreign/subclass dispatch? This would be broader than a change to the sqrt uncertainty formula alone.

    AI assistance: Codex prepared the investigation, prototype and tests and ran the checks described here. This is design evidence, not a completed fix or a claim of human review.

  2. added
    bug
    uncertainty
    on Nov 17, 2026
  3. added this to the 0.18.0 milestone on Nov 17, 2026
  4. added
    numpy-integration
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugnumpy-integrationuncertainty

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions