units and addition or subtraction

22 views
Skip to first unread message

Stan Schymanski

unread,
Aug 2, 2011, 10:49:30 AM8/2/11
to sage-devel
Dear all,

This is a bug-report or feature request for the units package, taken
from sage-support. Basically, the units package does not handle
addition and subtraction in a sensible way, as units cancel out when
variables with the same units are subtracted from each other. Sage
should give an error message when adding or subtracting variables with
different units, while leaving the units intact if the variables have
the same units. Does anyone have an idea how this could be
accomplished? Thanks already!

Below is an example of the problem posted at
http://groups.google.com/group/sage-support/browse_thread/thread/a60c943c224dfe87/3d19ca65a82f2498?lnk=gst&q=units#3d19ca65a82f2498

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)

Eviatar

unread,
Aug 2, 2011, 3:21:10 PM8/2/11
to sage-devel
I don't really see this as a bug. Units are treated as variables, so
it makes sense. Are you suggesting that 0 * kelvin should be left
unevaluated, and then not give an error when it is the denominator?

On Aug 2, 7:49 am, Stan Schymanski <schym...@gmail.com> wrote:
> Dear all,
>
> This is a bug-report or feature request for the units package, taken
> from sage-support. Basically, the units package does not handle
> addition and subtraction in a sensible way, as units cancel out when
> variables with the same units are subtracted from each other. Sage
> should give an error message when adding or subtracting variables with
> different units, while leaving the units intact if the variables have
> the same units. Does anyone have an idea how this could be
> accomplished? Thanks already!
>
>  Below is an example of the problem posted athttp://groups.google.com/group/sage-support/browse_thread/thread/a60c...

Eviatar

unread,
Aug 2, 2011, 3:29:39 PM8/2/11
to sage-devel
It seems WolframAlpha evaluates the limit of the expression:
http://www.wolframalpha.com/input/?i=%28calorie%2Fcentimeter%5E2%2F+minute%29%2F%28kelvin-kelvin%29.

Stan Schymanski

unread,
Aug 3, 2011, 3:40:50 AM8/3/11
to sage-devel
Yes, this makes sense to me. Wolfram seems to treat the expression and
the units separately, which makes sense. In your example, any omitted
value is seen as 1, so the result is perfectly correct. The expression
is evaluated and the units are added after it, but they don't cancel
out by subtraction. I don't think it is sensible to treat units as
variables if this leads to results like the one I encountered. Then
nothing is gained by using the units package and I could just create
my own variables called m, K, J, W etc. Is there an easy way to get a
behaviour like in Mathematica?


On Aug 2, 9:29 pm, Eviatar <eviatarb...@gmail.com> wrote:
> It seems WolframAlpha evaluates the limit of the expression:http://www.wolframalpha.com/input/?i=%28calorie%2Fcentimeter%5E2%2F+m....

Stan Schymanski

unread,
Aug 3, 2011, 4:25:36 AM8/3/11
to sage-devel
EDIT: Nooo, I don't want a behaviour like in Wolfram Alpha. Check this
out:
http://www.wolframalpha.com/input/?i=%28calorie%2Fcentimeter^2%2F+minute%29%2F%281kelvin-2kelvin%29

By inserting numbers that lead to a negative denominator, suddenly I
got units of time^-3! Maybe it's trickier than I thought...

Maarten Derickx

unread,
Aug 3, 2011, 5:08:37 AM8/3/11
to sage-...@googlegroups.com
no it's not trickier then you thought. That is just one of the outputs, wolfram alpha tries to convert units in what it calls basic units (I guess they have chosen an independend subset of units so that all others can be expressed in them). There are also other outputs which make more sense.

Eviatar

unread,
Aug 3, 2011, 6:21:42 AM8/3/11
to sage-devel
I don't think there is an easy way.

It seems that in Mathematica division by zero does not return an
error, simply evaluates to infinity (http://www.wolframalpha.com/
input/?i=1%2F0), which only makes sense if you are using the limit
definition of equality.

So essentially, the way to fix this in Sage would be to have units be
"special" symbolic variables when evaluating limits, so that

sage: limit(1 / x, x=units.length.meter.mul(0, hold=True))
Infinity

would return Infinity*meter instead. I think this would be quite hard
to do.

The other option would be to operator overload the UnitExpression
class or modify the behaviour of symbolic variables when they are
detected to be units. In any case, dividing by zero would still return
an error.

Maybe it would be useful to have a function that just does dimensional
analysis and ignores coefficients? This wouldn't be hard to write.

Stan Schymanski

unread,
Aug 3, 2011, 7:46:15 AM8/3/11
to sage-devel
Ah, yes, thanks. Didn't realise.

On Aug 3, 11:08 am, Maarten Derickx <m.derickx.stud...@gmail.com>
wrote:

Stan Schymanski

unread,
Aug 3, 2011, 7:55:05 AM8/3/11
to sage-devel
The division by zero is not the issue here, but the disappearance of
the units when two variables with the same units are subtracted from
each other. Wolfram gives 0 K for (kelvin - kelvin):
http://www.wolframalpha.com/input/?i=%28kelvin+-+kelvin%29

Sage would simply give 0 without any units, so that multiplication
with the term (kelvin-kelvin) would give 0, too. No units.

The function for a dimensional analysis without coefficients sounds
like a great idea. If you could post something quick and dirty, I
could see a bit better what you mean. Thanks for thinking about it,
anyway!

Stan

Stan Schymanski

unread,
Aug 17, 2011, 8:37:05 AM8/17/11
to sage-devel
I have received a couple of helpful hints about functional existing
open source packages that treat units in the desired way (ezunits in
maxima and DesignerUnits.com), but I am still after the quick and
dirty way for using the existing units package in sage. The only thing
that really stops me right now is that I don't know how to get it to
return:
units.x.y + units.x.y = units.x.y
units.x.y - units.x.y = units.x.y

Is there an easy way to write a function that would do that while
keeping the rest (multiplication, division, exponents etc.) the way it
is?

Thanks again for your help!

On Aug 3, 1:55 pm, Stan Schymanski <schym...@gmail.com> wrote:
> The division by zero is not the issue here, but the disappearance of
> theunitswhen two variables with the sameunitsare subtracted from
> each other. Wolfram gives 0 K for (kelvin - kelvin):http://www.wolframalpha.com/input/?i=%28kelvin+-+kelvin%29
>
> Sage would simply give 0 without anyunits, so that multiplication
> with the term (kelvin-kelvin) would give 0, too. Nounits.
>
> The function for a dimensional analysis without coefficients sounds
> like a great idea. If you could post something quick and dirty, I
> could see a bit better what you mean. Thanks for thinking about it,
> anyway!
>
> Stan
>
> On Aug 3, 12:21 pm, Eviatar <eviatarb...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I don't think there is an easy way.
>
> > It seems that in Mathematica division by zero does not return an
> > error, simply evaluates to infinity (http://www.wolframalpha.com/
> > input/?i=1%2F0), which only makes sense if you are using the limit
> > definition of equality.
>
> > So essentially, the way to fix this in Sage would be to haveunitsbe
> > "special" symbolic variables when evaluating limits, so that
>
> > sage: limit(1 / x, x=units.length.meter.mul(0, hold=True))
> > Infinity
>
> > would return Infinity*meter instead. I think this would be quite hard
> > to do.
>
> > The other option would be to operator overload the UnitExpression
> > class or modify the behaviour of symbolic variables when they are
> > detected to beunits. In any case, dividing by zero would still return
> > an error.
>
> > Maybe it would be useful to have a function that just does dimensional
> > analysis and ignores coefficients? This wouldn't be hard to write.
>
> > On Aug 3, 12:40 am, Stan Schymanski <schym...@gmail.com> wrote:
>
> > > Yes, this makes sense to me. Wolfram seems to treat the expression and
> > > theunitsseparately, which makes sense. In your example, any omitted
> > > value is seen as 1, so the result is perfectly correct. The expression
> > > is evaluated and theunitsare added after it, but they don't cancel
> > > out by subtraction. I don't think it is sensible to treatunitsas
> > > variables if this leads to results like the one I encountered. Then
> > > nothing is gained by using theunitspackage and I could just create
> > > my own variables called m, K, J, W etc. Is there an easy way to get a
> > > behaviour like in Mathematica?
>
> > > On Aug 2, 9:29 pm, Eviatar <eviatarb...@gmail.com> wrote:
>
> > > > It seems WolframAlpha evaluates the limit of the expression:http://www.wolframalpha.com/input/?i=%28calorie%2Fcentimeter%5E2%2F+m....
>
> > > > On Aug 2, 12:21 pm, Eviatar <eviatarb...@gmail.com> wrote:
>
> > > > > I don't really see this as a bug.Unitsare treated as variables, so
> > > > > it makes sense. Are you suggesting that 0 * kelvin should be left
> > > > > unevaluated, and then not give an error when it is the denominator?
>
> > > > > On Aug 2, 7:49 am, Stan Schymanski <schym...@gmail.com> wrote:
>
> > > > > > Dear all,
>
> > > > > > This is a bug-report or feature request for theunitspackage, taken
> > > > > > from sage-support. Basically, theunitspackage does not handle
> > > > > > addition and subtraction in a sensible way, asunitscancel out when
> > > > > > variables with the sameunitsare subtracted from each other. Sage
> > > > > > should give an error message when adding or subtracting variables with
> > > > > > differentunits, while leaving theunitsintact if the variables have
> > > > > > the sameunits. Does anyone have an idea how this could be
Reply all
Reply to author
Forward
0 new messages