Unit systems: getting Dummy argument for Unit when solving equation

73 views
Skip to first unread message

Harold E.

unread,
Feb 26, 2014, 5:29:02 AM2/26/14
to sy...@googlegroups.com
As a test for the unit system module, I have written some codes using the solve method, since Units (and other objects defined in the module) are supposed to behave as normal sympy objects.
But when I call solve, it tries to create an intermediate Unit object where one of the argument is Dummy, so it returns an error since this argument should be a Unit or a Dimension object [2]. I tried to understand how solve is working, but I don't get it. Here is the full traceback
File "/mnt/data/Documents/Projets/Programmation/sympy/doc/src/modules/physics/unitsystems/examples.rst", line 76, in examples.rst
Failed example:
   
Tr = solve(T**2/venus_a**3 - 4*pi**2 / mks["G"] / solar_mass, T)[1]
Exception raised:
   
Traceback (most recent call last):
     
File "/usr/lib/python2.7/doctest.py", line 1289, in __run
        compileflags
, 1) in test.globs
     
File "<doctest examples.rst[25]>", line 1, in <module>
       
Tr = solve(T**2/venus_a**3 - 4*pi**2 / mks["G"] / solar_mass, T)[1]
     
File "sympy/solvers/solvers.py", line 893, in solve
        f
[i] = nsimplify(fi, rational=True)
     
File "sympy/simplify/simplify.py", line 3830, in nsimplify
       
return _real_to_rational(expr, tolerance)
     
File "sympy/simplify/simplify.py", line 3785, in _real_to_rational
       
return p.subs(reps, simultaneous=True)
     
File "sympy/core/basic.py", line 941, in subs
        rv
= rv._subs(old, d, **kwargs)
     
File "sympy/core/cache.py", line 93, in wrapper
        r
= func(*args, **kw_args)
     
File "sympy/core/basic.py", line 1063, in _subs
        rv
= fallback(self, old, new)
     
File "sympy/core/basic.py", line 1035, in fallback
        arg
= arg._subs(old, new, **hints)
     
File "sympy/core/cache.py", line 93, in wrapper
        r
= func(*args, **kw_args)
     
File "sympy/core/basic.py", line 1063, in _subs
        rv
= fallback(self, old, new)
     
File "sympy/core/basic.py", line 1035, in fallback
        arg
= arg._subs(old, new, **hints)
     
File "sympy/core/cache.py", line 93, in wrapper
        r
= func(*args, **kw_args)
     
File "sympy/core/basic.py", line 1063, in _subs
        rv
= fallback(self, old, new)
     
File "sympy/core/basic.py", line 1035, in fallback
        arg
= arg._subs(old, new, **hints)
     
File "sympy/core/cache.py", line 93, in wrapper
        r
= func(*args, **kw_args)
     
File "sympy/core/basic.py", line 1063, in _subs
        rv
= fallback(self, old, new)
     
File "sympy/core/basic.py", line 1040, in fallback
        rv
= self.func(*args)
     
File "sympy/physics/unitsystems/units.py", line 72, in __new__
       
"instance; found %s" % type(dim))
   
TypeError: 'dim' object should be Unit or Dimension instance; found <class 'sympy.core.symbol.Dummy'>

When I wrote the test (commit 05ddf1aeac22eb1f01c9db1c7ab61d6ee8cc955b), this was working since I just copied-pasted the results I was seeing on IPython. One of the main thing which change since this moment is that Unit inherits from Expr instead of AtomicExpr. I tried to go back to the mentioned commit, but now I get a recursive call...

This is the only remaining bug in the proposed pull request [3]. You can find the branch there [4].

Do you have some ideas? if not, maybe we should give up (temporarily) the use of Units in equations, etc. (since we can still do it with abstract symbol and then replace).

[1] https://github.com/melsophos/sympy/blob/units/doc/src/modules/physics/unitsystems/examples.rst
[2] https://github.com/melsophos/sympy/blob/units/sympy/physics/unitsystems/units.py
[3] https://github.com/sympy/sympy/pull/2628
[4] https://github.com/melsophos/sympy/tree/units

Best,

Harold

Aaron Meurer

unread,
Mar 1, 2014, 7:25:34 PM3/1/14
to sy...@googlegroups.com
solve just assumes that any Expr can be replaced with Dummy, I think.
I don't fully understand how your code works, but maybe you could let
a non-Dimension Expr object just be interpreted as dimensionless (you
may also need to delay some evaluation so that things don't collapse
when doing temporary dummy substitution).

Aaron Meurer
> --
> 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 sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/96a1e62d-95d6-4dd7-8c1d-f1cb9400d438%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Harold Erbin

unread,
Apr 4, 2014, 6:05:10 PM4/4/14
to sy...@googlegroups.com
Thank you for your comment Aaron.
I have thought to this problem for the last month, but I did not come up
with a solution. If I say that any non-Dimension Expr is equivalent to a
dimensionless object, then it is equivalent to returning just the
factor. But doing so I get the same problem with the Quantity class,
where now the factor can be a Dummy. So here it is more subtle to
handle, but as a first try I return just the unit.
Now the solve command succeed, but the Dummy symbol where very important
to get the unit right (here the unit is missing) and to get the factor
correct (I have a 10^17 missing).
The problem is that I don't see how to handle arbitrary Symbol/Expr in
my code directly (even if it would be nice).

I'm hesitating to remove this example to avoid blocking all this module
just for this: then I could precise in the doc that one should not use
solve for now, and try to find a better solution.

What do you mean by delaying the evaluation?

Best,

Harold

Aaron Meurer

unread,
Apr 4, 2014, 6:08:16 PM4/4/14
to sy...@googlegroups.com
On Fri, Apr 4, 2014 at 5:05 PM, Harold Erbin <harold...@gmail.com> wrote:
Thank you for your comment Aaron.
I have thought to this problem for the last month, but I did not come up
with a solution. If I say that any non-Dimension Expr is equivalent to a
dimensionless object, then it is equivalent to returning just the
factor. But doing so I get the same problem with the Quantity class,
where now the factor can be a Dummy. So here it is more subtle to
handle, but as a first try I return just the unit.
Now the solve command succeed, but the Dummy symbol where very important
to get the unit right (here the unit is missing) and to get the factor
correct (I have a 10^17 missing).
The problem is that I don't see how to handle arbitrary Symbol/Expr in
my code directly (even if it would be nice).

I'm hesitating to remove this example to avoid blocking all this module
just for this: then I could precise in the doc that one should not use
solve for now, and try to find a better solution.

What do you mean by delaying the evaluation?

I mean making it so that things don't evaluate when a Subs is performed. This is e.g., how Derivative works (otherwise Derivative(f(x), x).subs(f(x), Dummy('y')) would automatically go to 0, which is not what is wanted.

Aaron Meurer
 
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/533F2C96.3060200%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages