[SciPy-user] Does Scipy or Numpy have contemporary physical constants?

Travis Vaught travis@enthought....
Thu Jun 5 12:59:18 CDT 2008


On Jun 4, 2008, at 8:04 PM, Anne Archibald wrote:

> 2008/6/4 HuiChang MOON <topengineer@gmail.com>:
>> Hello, users.
>> A lot of physical constants such as elementary charge and Planck's  
>> constant
>> is used to make some physical calculations.
>> It is annoying to define the constants so often.
>> Does Numpy or Scipy include some physical constants?
>
> No, unfortunately. I has been discussed from time to time, and the
> conclusion is that they're not very useful without an automatic
> unit-tracking system. Such a system does exist, and it provides
> standard constants; it's in Scientific (a slightly dusty package
> somewhat complementary to scipy). There is even some support for units
> built into ipython, though personally I don't use it.
>
> The biggest limitation of the unit-tracking system is that it doesn't
> really work with arrays. There are definitely applications for a
> subclass of ndarray that keeps track of units, but I don't know of a
> publicly-released one. For pocket-calculator type calculations,
> Scientific is pretty handy.
>

FWIW I'll mention the units package in ETS:

https://svn.enthought.com/enthought/browser/SciMath/trunk/enthought/units

It's based on some good work that Michael Avaizis at Caltech did.

The upper layers are not very mature and could do with a refactor to  
make them more general (the notion of a unit manager and unit families  
is a nice one, but the current examples are very specific).

You can do things like this:

In [1]: # This allows me to use strings and it finds the symbols in  
the namespace for me

In [2]: from enthought.units.unit_parser import unit_parser

In [3]: import numpy

In [4]: a = numpy.arange(0., 1., 0.01)

In [5]: b = unit_parser.parse_unit("m/s")

In [6]: c = b * a

In [7]: c
Out[7]: <array>*m*s**-1

In [8]: d = unit_parser.parse_unit("minutes") * 15.

In [9]: d # didn't work (minutes not in namespace)
Out[9]: 15.0

In [10]: d = unit_parser.parse_unit("seconds") * 15. * 60. # 15 minutes

In [11]: d
Out[11]: 900.0*s

In [12]: d * c
Out[12]: <array>*m

In [13]: e = d*c

In [14]: e.value
Out[14]:
array([   0.,    9.,   18.,   27.,   36.,   45.,   54.,   63.,   72.,
          81.,   90.,   99.,  108.,  117.,  126.,  135.,  144.,  153.,
         162.,  171.,  180.,  189.,  198.,  207.,  216.,  225.,  234.,
         243.,  252.,  261.,  270.,  279.,  288.,  297.,  306.,  315.,
         324.,  333.,  342.,  351.,  360.,  369.,  378.,  387.,  396.,
         405.,  414.,  423.,  432.,  441.,  450.,  459.,  468.,  477.,
         486.,  495.,  504.,  513.,  522.,  531.,  540.,  549.,  558.,
         567.,  576.,  585.,  594.,  603.,  612.,  621.,  630.,  639.,
         648.,  657.,  666.,  675.,  684.,  693.,  702.,  711.,  720.,
         729.,  738.,  747.,  756.,  765.,  774.,  783.,  792.,  801.,
         810.,  819.,  828.,  837.,  846.,  855.,  864.,  873.,   
882.,  891.])



This doesn't work quite right if you reverse the multiplication  
because of NumPy's aggressive casting, but after talking with Travis  
O. it seems like I can add an __rmul__ method to fix this.

Best,

Travis

> Anne
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>



More information about the SciPy-user mailing list