Numeric with units

John Hunter jdhunter at ace.bsd.uchicago.edu
Fri May 16 16:20:34 EDT 2003


>>>>> "Fernando" == Fernando Perez <fperez528 at yahoo.com> writes:

    Fernando> John Hunter wrote:
    >> Has anyone written some code to integrate Numeric arrays with
    >> unum, or another units package?

    Fernando> Hi John,

    Fernando> take a look at ScientificPython's PhysicalQuantities.
    Fernando> It's not integrated with Numeric, but it is a really
    Fernando> solid units package.  You might be able to do the
    Fernando> integration with a bit of work.

Thanks for the suggestion!  It appears that there is a partial
implementation already in PhysicalQuantity

  from Scientific.Physics.PhysicalQuantities import PhysicalQuantity as Quant
  from Numeric import *

  f = Quant(5, 'Hz')
  t = Quant(arange(0,2,0.2), 's')
  print sin(2*pi*f*t)

This works great.  But now when I do

  print cumsum(arange(0,2,0.2))  # works as expected
  print cumsum(t)                # prints t


The cumsum of the quant returns t itself and not the cumulative sum of
t.  Hmm...  I'm poking around the innards of PhysicalQuantity just now
trying to figure out what is causing this ....

My working hypothesis is that because sin is a ufunc, it works like
umath and will call instance.sin if it doesn't know how to handle the
instance.  But cumsum is an array function, can so can't be defined in
the same way as a class method instance.cumsum.  However, it is not
clear to me why the array functions appear to be identity functions
when operating on PhysicalQuantity instances.

In any case, it would be nicer to derive a class from UserArray and
PhysicalQuantity, so that attributes and instance methods would be
available from both classes.  When you do something like

  t = Quant(arange(0,2,0.2), 's')

you lose the array attributes and methods.  I'll think about this over
the weekend....

Thanks again for the tip,
JDH





More information about the Python-list mailing list