subtraction of variables with same units should not make the unit disappear

15 views
Skip to first unread message

Stan Schymanski

unread,
Jul 29, 2011, 5:29:59 AM7/29/11
to sage-support
Dear all,

I am not sure if I am using the units package correctly, but I have
the following problem:

I want to assign different units to variables, then get the units of
derived variables that are functions of others and check for
consistency at the end. Here is a simple example. I know the units of
H_l, T_a and T_l and want to know the units of h_c in H_l == h_c*(T_a
- T_l). For this, I create a dictionary udict with the units of the
variables I know, then I solve the equation for the unknown variable
and substitute the units for the known variables. However, subtraction
of two variables with the same units makes the units cancel out!!!
This is not very useful. Am I doing something wrong, or is this
desired behaviour? I hope there is an easy way around it. Thanks for
your help already!

sage: udict = {}
sage: udict[H_l] = units.energy.calorie/units.length.centimeter^2/
units.time.minute
sage: udict[T_a] = units.temperature.kelvin
sage: udict[T_l] = units.temperature.kelvin
sage: soln = solve(H_l == h_c*(T_a - T_l), h_c)[0]; soln
h_c == H_l/(T_a - T_l)
sage: soln.subs(udict)
Traceback (most recent call last):
...
RuntimeError: power::eval(): division by zero

---
This works:
sage: (H_l/T_a).subs(udict)
calorie/(centimeter^2*kelvin*minute)

Maarten Derickx

unread,
Aug 2, 2011, 5:41:04 AM8/2/11
to sage-s...@googlegroups.com
According to the documentation http://www.sagemath.org/doc/reference/sage/symbolic/units.html the unit behave like elements from the symbolic ring, in which x-x will be replaced by 0. I considder this a bug since clearly you can add two elements only if the have the same unit, and the result will again have the same unit. Currently within sage there is not a neat workaround, I guess you just need to use your brain as you did in your "This works" example.

Stan Schymanski

unread,
Aug 2, 2011, 10:52:09 AM8/2/11
to sage-support
Thanks for the confirmation, Maarten! Using my brain is not an option
as it has very limited capacities when it comes to units...
As you point out, addition or subtraction of different units should
raise an error. I forwarded this to sage-devel here:
http://groups.google.com/group/sage-devel/browse_thread/thread/81884a53ab28212.
Hope that someone knows an easy way how to fix it.

Cheers
Stan

On Aug 2, 11:41 am, Maarten Derickx <m.derickx.stud...@gmail.com>
wrote:
> According to the documentationhttp://www.sagemath.org/doc/reference/sage/symbolic/units.htmlthe unit

Maarten Derickx

unread,
Aug 2, 2011, 11:17:58 AM8/2/11
to sage-s...@googlegroups.com
I thought a bit longer about it, and I actually think it's not really a big bug. It think the way the units system is meant to be used is like this:

sage: var("H_l, h_c,T_a,T_l")
(H_l, h_c, T_a, T_l)
sage: sol=solve([H_l*units.energy.calorie/units.length.centimeter^2/units.time.minute == h_c*((T_a - T_l)*units.temperature.kelvin)],h_c)
sage: sol
[h_c == H_l*calorie/(T_a*centimeter^2*kelvin*minute - T_l*centimeter^2*kelvin*minute)]

The interpretation of this should be that 2*units.length.meter should be read as 2 * (1 meter) etc. This interpretation works very well if you have to convert explicit values. Note that you indeed cannot divide by 1 kelvin - 1 kelvin = 0 kelvin. The framework will give sensible answers if you fill in distinct values for T_a and T_l.

sage: sol[0](T_l=1,T_a=2,H_l=3)
h_c == 3*calorie/(centimeter^2*kelvin*minute)

Stan Schymanski

unread,
Aug 3, 2011, 4:20:55 AM8/3/11
to sage-support
The problem is that you cannot check the units of your result in
general, as you have to make sure that the values you enter do not
lead to a cancellation. This is non-trivial for more complicated
expressions. I agree, it is not a bug in terms of coding but the
concept is not suitable for this purpose. Could you add your thoughts
to the sage-devel thread I started? It would be nice to add this
functionality to sage in one way or another.

Cheers
Stan

On Aug 2, 5:17 pm, Maarten Derickx <m.derickx.stud...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages