Converting of units is one useful function, but I'd also like a 
possibility to perform simple unit checks on functions. For example, if 
one gets a nice equation for the total energy of a system after some 
symbolic acrobatics, one would like to know whether the units of this 
equation are still the units of energy. Or, what units I would get out 
if I use non-SI units for the variables.

EXAMPLE:

----------------------------------------------------------------------
| Sage Version 4.1, Release Date: 2009-07-09                         |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
sage: var('S T P V M mu g h rho')
(S, T, P, V, M, mu, g, h, rho)
sage: Etot = T*S - P*V + M*mu + h*g*M
sage: var('J K N m kg K s') # units
(J, K, N, m, kg, K, s)
sage: unitsdict = {S: J/K, T: K, P:N/m^2, V: m^3, M: kg, mu: J/kg, g: 
m/s^2, h: m} # couple units with variables
sage: Etot.subs(unitsdict)
-N*m + 2*J + kg*m^2/s^2
sage: physdict = {N: kg*m/s^2, J: kg*m/s^2*m} # convert to base units
sage: Etot.subs(unitsdict).subs(physdict)
2*kg*m^2/s^2

The above tells me that the expression for Etot returns units of kg * 
m^2/s^2 if I use SI units for all the variables. These are indeed the 
same as J. However, this method would not work if I only had the first 
two terms of the expression for Etot:

sage: ( T*S - P*V ).subs(unitsdict).subs(physdict)
0

A unit package that does not simply add and subtract units from each 
other, but checks whether each summand has the same units would be very 
helpful here. For example, it could return the units of every summand, 
allowing the user to find which one of them has the wrong units and 
where to look for the error.

I think that SI units would be a nice and coherent choice for the 
'simplest possible unit'. Obviously, m^2/s^2 could not be converted 
automatically to J/kg, but it would be easy for the user to verify that 
J/kg is indeed the same as m^2/s^2. Therefore, I would be happy if we 
had a function that shows the basic units of an expression, in terms of 
mass (or particle number), length and time. For this, I would strongly 
favour the SI units kg, moles, m and s, but it should be possible to 
allow the user to set his/her preferred system. I wouldn't know how 
something like the above example could be done with non-SI units, though.

I hope that this can be made part of the units package.

Good luck!

Cheers,
Stan



Fredrik Johansson wrote:
> On Wed, Aug 12, 2009 at 9:19 AM, William Stein<wst...@gmail.com> wrote:
>   
>> On Wed, Aug 12, 2009 at 12:11 AM, Fredrik
>> Johansson<fredrik.johans...@gmail.com> wrote:
>>     
>>> On Wed, Aug 12, 2009 at 8:44 AM, William Stein<wst...@gmail.com> wrote:
>>>       
>>>> On Tue, Aug 11, 2009 at 11:30 PM, Jason
>>>> Grout<jason-s...@creativetrax.com> wrote:
>>>>         
>>>>> William Stein wrote:
>>>>>           
>>>>>> Hi,
>>>>>>
>>>>>> I just wanted to let people know that David Ackerman -- a UW student who
>>>>>> took my course on Sage last quarter -- is working (funded by NSF) on
>>>>>> creating a "units package" for Sage right _now_.  If anybody has any
>>>>>> pseudo-sage code that involves units that they wished work, please post
>>>>>> it to this thread. What do you want units to *do*?
>>>>>>
>>>>>>             
>>>>> Is he doing something from scratch or is he working from the packages
>>>>> that have come up from time to time here?
>>>>>           
>>>> He is doing something from scratch.
>>>>
>>>> I had planned to have him work on integrating some existing package,
>>>> but after evaluating that approach he said it didn't seem to make
>>>> sense (at least he has a lot of experience using Sage, since he took
>>>> my class).   I didn't have an opinion one way or the other, but we
>>>> decided to ask -- "what is a so called units package supposed to do
>>>> anyways" since I honestly couldn't really tell you.  I would use one
>>>> to convert between Fahrenheit and Celcius, and maybe dollars and euros
>>>> :-).   We google around and found that Matlab didn't seem to come with
>>>> a units package, but there were several third party packages -- anyway
>>>> Sage isn't much like Matlab so looking there seemed pointless.  Then
>>>> we looked at Mathematica's included units package and read all the
>>>> documentation and examples of it.    That was enlightening, and
>>>> suggested doing almost precisely what Robert Dodier keeps telling us
>>>> to do, which is make the different units just be symbolic variables,
>>>> plus have some extra table somewhere of conversions (where everything
>>>> is kept as exact fractions), and add a few functions to do
>>>> conversions.
>>>>         
>>> +1 on this design.
>>>
>>> It's similar to what sympy does, except sympy always expands units to
>>> multiples of base units.
>>>       
>> Sage definitely will not always expand units to multiples of base
>> units.  Some backward people in the world like to work with miles and
>> feet... :-)
>>     
>
> Agreed. Even in the metric parts of the world, non-SI units are
> standard in fields like particle physics and astronomy.
>
> On a related note, it would be handy to have a function that
> automatically converts a given quantity to the simplest possible unit:
> for example kg*m/s^2 -> N and kg*m/(A*s^3) -> V/m.
>
> Of course, the "simplest possible unit" isn't uniquely defined, so
> such a function probably requires additional parameters. You have to
> decide on the set of base units, and derived units are not unique (for
> example V/m = N/C).
>
> It would perhaps be sufficient if convert accepts partial units so
> that (kg*m/(A*s^3)).convert(V) -> V/m, but I'd expect convert to raise
> an exception when given incompatible units, so it's not quite the same
> thing.
>
>   
>> That said, I would be fine with Sage having a mode where it could
>> always expand to base units -- that's massively easier to implement
>> anyways.
>>     
>
>
> Fredrik
>
> >
>   


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to