-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
np.arange and np.linspace do not work properly with log units #12548
Comments
Is it related to this known issue: https://docs.astropy.org/en/latest/known_issues.html#numpy-array-creation-functions-cannot-be-used-to-initialize-quantity ? |
Partially, The docs state that linspace should work. |
Either the doc needs updating or this is some new compatibility problem... Hopefully @mhvk et al. can advise. |
Maybe wait until I could re-check this once we have a numpy 1.21 in Debian testing. |
Sounds good. I added a |
No need to wait, this does not depend on numpy, but is currently not supported. For For More relevantly perhaps, what does work is
|
Actually, my diagnosis of the |
@pllim Just for completeness: The behavior didn't change with numpy 1.21.4. >>> a = 1 * u.dex()
>>> a
<Dex 1. dex>
>>> 1 * a
<Quantity 1. dex>
>>> 2 * a
<Quantity 2. dex>
>>> a + a
<Dex 2. dex> i.e. multiplication of |
@olebole - the above are examples of what makes it hard to get the logarithmic units right. If they have a unit inside, multiplication and division make little sense, yet when dimensionless, things can work (like |
Why don't make multiplication and division sense? I would formally expect, that the factor goes into the power of the physical unit: >>> u.Dex(10 * u.Angstrom)
<Dex 1. dex(Angstrom)>
>>> u.Dex(10 * u.Angstrom) + u.Dex(10 * u.Angstrom)
<Dex 2. dex(Angstrom2)> # This works
>>> 2 * u.Dex(10 * u.Angstrom)
# Should also be:
<Dex 2. dex(Angstrom2)> This should have a generic solution, as the "downcasting" to Quantities is IMO ugly here: >>> u.Dex(10*u.one)
<Dex 1. dex>
>>> u.Dex(10*u.one).physical
<Quantity 10.>
>>> u.Dex(10*u.one) + u.Dex(10*u.one)
<Dex 2. dex>
>>> (u.Dex(10*u.one) + u.Dex(10*u.one)).physical
<Quantity 100.>
>>> 2 * u.Dex(10*u.one)
<Quantity 2. dex>
>>> (2 * u.Dex(10*u.one)).physical
AttributeError: 'Quantity' object has no 'physical' member This all works already for addition and subtraction; IMO it should just be extended to multiplication/division. def __mul__(self, other):
return self._copy(self.physical_unit**other)
def __rmul__(self, other):
return self._copy(self.physical_unit**other)
def __div__(self, other):
return self._copy(self.physical_unit**(1/other)) and proagate this through astropy/astropy/units/function/logarithmic.py Lines 315 to 316 in ce74be8
Do you have a case where this would lead to error? I would see the only major problem that multiplication is not associatve with units:
but this seems obvious to me. |
Would it be useful if I would just open a PR to play with multiplication of log units? |
@olebole - I think in my note I may have partially worried exactly about associativity, but perhaps you are right that it is fairly obvious (or at least something the documentation can explain). The other possible issue is with Anyway, if you are up for a PR, that would definitely be welcome! |
p.s. Just should add I'm really happy to see this used! |
Some more progress towards this in #12579 |
Description
When trying to create an array that is equally spaced in log units, both attempts fail:
linspace converts to a simple (non-logarithmic) quantity with dimensionless dex:
When applying a unit to dex, already linspace fails:
arange does something completely weird without units:
and with units, arange raises an exception as well:
Expected behavior
There should be a simple way to create arrays that are equally spaced in log units, or at least
NotImplemented
should be raised.System Details
Linux-5.14.0-2-amd64-x86_64-with-glibc2.32
Python 3.9.9 (main, Nov 16 2021, 10:24:31)
[GCC 11.2.0]
Numpy 1.19.5
pyerfa 2.0.0.1
astropy 5.0
Scipy 1.7.1
The text was updated successfully, but these errors were encountered: