Skip to content

Support exponential and logarithmic functions in umath and allow compound dimensionless units - #287

Open
pro-laxmi wants to merge 1 commit into
python-quantities:masterfrom
pro-laxmi:feat/umath-exp-log-compound-dimensionless
Open

pro-laxmi wants to merge 1 commit into
python-quantities:masterfrom
pro-laxmi:feat/umath-exp-log-compound-dimensionless

Conversation

@pro-laxmi

Copy link
Copy Markdown

Summary

  • Adds exp, exp2, expm1, log, log2, log10, and log1p to quantities.umath (and exported by quantities), matching existing trigonometric and hyperbolic functions like sinh and sin, with automatic rescaling to dimensionless quantities.
  • Fixes Quantities that should be dimensionless are showing as still having units, causing errors with exp function #217: in quantities.dimensionality._d_dimensionless, checks whether dimensionality simplifies to dimensionless (dim.simplified != Dimensionality()), allowing expressions whose compound units cancel out (such as 2*pi*Z_0 / sqrt(mu0/epsilon) or J / (N*m)).
  • Validates all positional arguments in _d_dimensionless(*args) so binary ufuncs like logaddexp and logaddexp2 properly validate the second argument as well.
  • Adds comprehensive unit tests in quantities/tests/test_umath.py.

Tests

  • Full test suite passes: 196 passed, 3 xfailed.

…ound dimensionless units

- Add exp, exp2, expm1, log, log2, log10, and log1p functions in quantities.umath
  with automatic rescaling to dimensionless quantities.
- Update _d_dimensionless in quantities.dimensionality to check all positional arguments
  (handling binary ufuncs like logaddexp and logaddexp2) and verify whether
  dimensionality simplifies to dimensionless (fixing python-quantities#217).
- Add unit tests in test_umath.py covering the new functions and compound dimensionless
  inputs.
@apdavison apdavison added bug Wrong result or crash numpy-integration NumPy ufuncs and functions labels Nov 17, 2026
@apdavison apdavison added this to the 0.17.0 milestone Nov 17, 2026

@apdavison apdavison left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this. The new umath functions are a useful addition, and they rescale their input correctly.

However, the change to _d_dimensionless and _d_arctrig lets NumPy's own ufuncs give wrong results without any error. quantities computes a ufunc on the raw magnitudes and only checks the units afterwards, in __array_wrap__, so these functions can't rescale the input. Accepting any input that simplifies to dimensionless therefore uses the magnitude unscaled whenever the conversion factor isn't 1:

Call With this PR Correct master
np.exp(1*pq.s/pq.ms) 2.718… inf (e¹⁰⁰⁰) ValueError
np.log10(500*pq.ms/pq.s) 2.699 -0.301 ValueError
np.arcsin(500*pq.ms/pq.s) nan 0.5236 ValueError
np.exp(2*pq.J/pq.cal) 7.389 1.613 ValueError

The tests don't catch this because J/(N*m) has a conversion factor of exactly 1.

Could you accept a compound dimensionless input only when its conversion factor is 1 (for example, when Quantity(1.0, dim).simplified is dimensionless with a magnitude of 1, allowing for rounding), and keep raising ValueError otherwise, perhaps with a message suggesting .simplified or the new umath functions? The example in #217 has a factor of exactly 1, so it would still work. Please also add tests with scaled units such as s/ms and J/cal: they should raise for the np.* functions, and give the correct values with umath. Handling these properly in the ufuncs themselves needs __array_ufunc__, which is planned for 0.18.0.

@apdavison apdavison modified the milestones: 0.17.0, 0.18.0 Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Wrong result or crash numpy-integration NumPy ufuncs and functions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quantities that should be dimensionless are showing as still having units, causing errors with exp function

2 participants