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... :-) 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. > A conversion then is just a division: > >>>> from sympy.physics.units import mega, kilo, second, giga, minute, Unit >>>> bit = Unit('bit', 'b') >>>> byte = 8*bit >>>> n = 94959 * kilo*byte >>>> n / (mega*byte) > 94959/1000 >>>> (6 *giga*byte/minute) / (mega*byte/second) > 100 > > The advantage of the sympy approach is that the representation is > always normalized; a disadvantage is that a value converted to a > non-base unit will be in dimensionless form. > > The suggested approach for Sage is better but you'll have to work out > what to do for example when comparing 100*m and 1/10*km, and whether > for example 1*N*s**2/kg should automatically simplify to 1*m. We will just do whatever Mathematica does, unless somebody can come up with a good argument while the Mathematica design is nonoptimal. This has the advantage that the millions of Mathematica users out there can more easily switch to Sage. Anyway, consulting with Mathematica we find that: (1) when comparing 100*m and 1/10*km: return the formal symbolic equation 100*m == 1/10*km, and (2) whether 1*N*s**2/kg should automatically simplify to 1*m: mathematica does not simplify it, at least if I used Mathematica correctly (pasted from the sage notebook in mathematica mode): sage: Needs["Miscellaneous`Units`"] sage: 100*Meter 100 Meter sage: 2 == 1+1 True sage: 100*Meter == 1/10*Kilo*Meter Kilo Meter 100 Meter == ---------- 10 sage: 1 * Kilo* Meter == 1 *Kilo * Meter True sage: 1*Newton*Second^2/(Kilo*Gram) 2 Newton Second -------------- Gram Kilo > > Fredrik > > > > -- William Stein Associate Professor of Mathematics University of Washington http://wstein.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---