caseywstark / dimensionful

Attach symbolic units to any data
BSD 2-Clause "Simplified" License
5 stars 0 forks source link

sqrt() #1

Closed petigura closed 12 years ago

petigura commented 12 years ago

Following your example in the README, I tried to use Kepler's third law:

PP = 4*pi**2 / G / mass_sun * distance**3 PP.convert_to_cgs() 9.95860849489e+14 s**2

However

sqrt(PP)

Fails

caseywstark commented 12 years ago

Hey Erik.

I tried to reproduce:

from dimensionful import *
from numpy import sqrt, pi

distance = Quantity(1.0, "AU")
mass_sun = Quantity(1.0, "Msun")
PP = 4 * pi**2 / G / mass_sun * distance**3
PP.convert_to_cgs()
sqrt(PP)

This throws AttributeError: sqrt. This is because numpy.sqrt is a ufunc, so it makes an array([PP], dtype=object), and tries to call PP.sqrt(). Quantities currently have no sqrt attribute, and this is the reason for the cryptic error.

This is definitely a design issue I came across and wasn't sure what to do about. I can't possibly cover every operation, but I think the best thing to do is to attach common operation methods to Quantity. Thoughts on this are very welcome.

For now, PP**(1.0/2) works and I will drop a sqrt method in soon.

caseywstark commented 12 years ago

Fixed in 87109104a75f8d5dbfac4193a67daf0fa415d9e8.