Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

real and imag attributes of Quantities do not preserve units #171

Closed
natezb opened this issue Jul 10, 2014 · 2 comments
Closed

real and imag attributes of Quantities do not preserve units #171

natezb opened this issue Jul 10, 2014 · 2 comments

Comments

@natezb
Copy link
Contributor

natezb commented Jul 10, 2014

This is perhaps a feature request. Currently, if you create a complex Quantity and take its real or imaginary part using the real or imag attributes, the magnitude is returned, but the units are lost.

Presumably this is not yet implemented because real and imag are in the unique position of being attributes of ndarrays, rather than methods, which are currently dealt with via __copy_units and friends.

Looking through numpy's docs, there appear to be very few attributes should have units, so the implementation should not be too bad. The only ones I see are real, imag, T, and maybe flat.

@hgrecco
Copy link
Owner

hgrecco commented Jul 10, 2014

Good idea. We will implement this for the next bugfix release.

@hgrecco
Copy link
Owner

hgrecco commented Jul 11, 2014

I am deferring the implementation of flat because it returns a flatiter object (not an array) as described here and I am playing with some ideas regarding (even) better numpy support in this temporary branch (which is aimed to fix #37 and #39)

cpascual pushed a commit to cpascual/pint that referenced this issue Apr 13, 2018
np.isreal(v) is implemented (at least in np >=1.13) as a check that
v.imag != 0 . Since Quantity reimplements imag to return units (see
issue hgrecco#171) , np.isreal always returns False for any quantity.
Therefore, this test is marked as expected to fail for now (note that
the tests with np <1.13 will now yield an unexpected success).
bors bot added a commit that referenced this issue Apr 13, 2018
630: Fix unit tests errors with numpy >=1.13 (Fix 577) r=cpascual a=cpascual

This PR fixes the 4 test errors/failiures reported in #577 and adds new travis builds with numpy 1.14

3 of the 4 problems are relatively trivial to solve.

The remaining one (`test_isreal`) is not so trivial because it  involves allowing to compare a *non-dimensionless* quantity against zero. The reason is that `numpy.isreal(x)` essentially does `x.imag !=0` if the `imag` method is implemented in x... and since Quantity reimplements .imag to return units (see #171), the check always returns False if x is a non-dimensionless quantity.

As mentioned, this can be solved by allowing comparisons against zero. Actually, this has already been requested in #194, but it was not implemented.

In this PR, I implemented the comparison-to-zero feature and provide several new unit tests for it. AFAIK this implementation takes into account the concerns expressed by @hgrecco in #194. But if the feature is not wanted, we can just revert the last 3 commits of this PR and then `test_isreal` will left with an expected failure flag.

The rules that I implemented for the comparison-with-zero are:
```    
    a) comparing against (non-quantity) zero is allowed for any quantity
    b) comparing against zero-magnitude quantities of incompatible
    dimensionality raises a Dimensionality error, except for the case
    of equality, for which it always returns False
    
    Notes:
    1.- Numpy arrays of zeros are also supported and the comparison rules
    apply elementwise
    2.- In the case of non-multiplicative units, the rules above
    apply after converting to base units if the autoconvert offset flag
    is set. Otherwise they raise an OffsetUnitCalculusError.
```

Fixes #577


Co-authored-by: Carlos Pascual <cpascual@cells.es>
znicholls pushed a commit to znicholls/pint that referenced this issue Aug 29, 2018
np.isreal(v) is implemented (at least in np >=1.13) as a check that
v.imag != 0 . Since Quantity reimplements imag to return units (see
issue hgrecco#171) , np.isreal always returns False for any quantity.
Therefore, this test is marked as expected to fail for now (note that
the tests with np <1.13 will now yield an unexpected success).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants