[yt-dev] Splitting a compound unit into its atoms

Nathan Goldbaum nathan12343 at gmail.com
Tue Sep 30 09:45:31 PDT 2014


You can do something like Mike's example, but you need to remember that the
sympy expr is contained in the expr attribute of the unit object:

In [1]: q = yt.YTQuantity(3, 'g/cm**2/s')
In [2]: q.units.expr.atoms()
Out[2]: {-2, -1, cm, g, s}
In [3]: srepr(q.units.expr)
Out[3]: "Mul(Pow(Symbol('cm'), Integer(-2)), Symbol('g'), Pow(Symbol('s'),
Integer(-1)))"
In [4]: q.units.expr.args
Out[4]: (g, cm**(-2), 1/s)

The unit object itself is actually (a little embarrassingly) a sympy
expression, but a trivial one that doesn't actually contain any unit
information.  Admittedly this is not the best for discoverability of what's
going on under the hood, but making it a class that inherits from object
will require a bit of work and refactoring.

Note that if you want to transform these sympy subexpressions into unit
objects, you'll need to pass them through the Unit() constructor.  See e.g.
the __mul__ or __div__ function definitions in the Unit class definition
(in yt/units/unit_object.py).

Hope that helps,

Nathan

On Tue, Sep 30, 2014 at 7:44 AM, Michael Zingale <
michael.zingale at stonybrook.edu> wrote:

> There is a sympy atoms method.  If x and y are symbols, then:
>
>
> > a = x*y
> > print a.atoms(sympy.Symbol)
> set([x, y])
>
> I suspect that this would work with the units defined in yt.  There is
> probably a way to get powers too.
>
>
>
> On Tue, Sep 30, 2014 at 10:38 AM, John ZuHone <jzuhone at gmail.com> wrote:
>
>> This should be obvious to me, but I can't seem to figure it out.
>>
>> Is there a way to take a compound units expression like:
>>
>> dyne*cm**2/g**2
>>
>> and split it into its constituent parts, so that I have a list like this:
>>
>> [dyne, cm, g]
>>
>> and maybe even containing the information about the powers?
>> _______________________________________________
>> yt-dev mailing list
>> yt-dev at lists.spacepope.org
>> http://lists.spacepope.org/listinfo.cgi/yt-dev-spacepope.org
>>
>
>
>
> --
> Michael Zingale
> Associate Professor
>
> Dept. of Physics & Astronomy • Stony Brook University • Stony Brook, NY
> 11794-3800
> *phone*:  631-632-8225
> *e-mail*: Michael.Zingale at stonybrook.edu
> *web*: http://www.astro.sunysb.edu/mzingale
>
> _______________________________________________
> yt-dev mailing list
> yt-dev at lists.spacepope.org
> http://lists.spacepope.org/listinfo.cgi/yt-dev-spacepope.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.spacepope.org/pipermail/yt-dev-spacepope.org/attachments/20140930/483fc2bc/attachment.html>


More information about the yt-dev mailing list