Also take a look at http://stackoverflow.com/a/14667919/161801.

Aaron Meurer

On Sat, Nov 23, 2013 at 2:18 PM,  <[email protected]> wrote:
>
> import sympy.physics.units as un
>
>
>
>
> def freeze_units(expr):
>
>
>     if isinstance(expr, list):
>
>
>         return [ freeze_units(i) for i in expr ]
>
>
>     elif isinstance(expr, tuple):
>
>
>         return tuple([ freeze_units(i) for i in expr ])
>
>
>     elif isinstance(expr, dict):
>
>
>         return { key: freeze_units(expr[key]) for key in expr.keys()}
>
>
>     elif isinstance(expr, Add):
>
>
>         result = 0
>
>
>         for arg in expr.args:
>
>
>             result += freeze_units(arg)
>
>
>         return result
>
>
>     elif isinstance(expr, Mul):
>
>
>         contain_add = False
>
>
>         for arg in expr.args:
>
>
>             if isinstance(arg, Add):
>
>
>                 contain_add = True
>
>
>                 break
>
>
>         if contain_add:
>
>
>             result = 1
>
>
>             for arg in expr.args:
>
>
>                 result *= freeze_units(arg)
>
>
>         else:
>
>
>             value = erase_units(expr)
>
>
>             units = subs(expr, {value: 1})
>
>
>             result = value*un.Unit("Bloque de unidades",latex(units))
>
>
>         return result
>
>
>     elif isinstance(expr, Equality):
>
>
>         return Eq(freeze_units(expr.lhs), freeze_units(expr.rhs))
>
>
>     elif isinstance(expr, Matrix):
>
>
>         return Matrix([ [ freeze_units(expr[i,j]) for j in xrange(expr.cols)
> ] for i in xrange(expr.rows) ])
>
>
>     # Comienzo de funciones.
>
>
>     elif isinstance(expr, Function):
>
>
>         return type(expr)(*( freeze_units(i) for i in expr.args ))
>
>
>     else:
>
>
>         return expr
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sympy.
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to