Unit balancing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AbrahamLincolnIllinois@yahoo.com

    #1

    Unit balancing

    Greetings!

    I am working with a researcher who would be very happy if he could
    include units in his calculations. Then, if his complicated expression
    didn't result in "kg/yr" the program would stop and point out his
    error.

    Does Python (or SciPy or ..) offer this feature?

    Thanks for your help.


    Abraham

  • Tim Chase

    #2
    Re: Unit balancing

    I am working with a researcher who would be very happy if he
    could include units in his calculations. Then, if his
    complicated expression didn't result in "kg/yr" the program
    would stop and point out his error.
    >
    Does Python (or SciPy or ..) offer this feature?

    Well, it's a bit of a hack, but you could try something like

    ############### ############### #############
    class InvalidUnits(Ex ception): pass

    class Unit(object):
    def __init__(self, value, units = ''):
    self.value = value
    self.units = units
    def __sub__(self, other):
    if isinstance(othe r, Unit):
    if self.units <other.units:
    raise InvalidUnits
    return Unit(self.value - other.value, self.units)
    return Unit(self.value - other, self.units)
    def __add__(self, other):
    if isinstance(othe r, Unit):
    if self.units <other.units:
    raise InvalidUnits
    return Unit(self.value + other.value, self.units)
    return Unit(self.value + other, self.units)
    def __mul__(self, other):
    if isinstance(othe r, Unit):
    return Unit(self.value * other.value,
    "(%s)%s" % (self.units, other.units))
    else:
    return Unit(self.value * other, self.units)
    def __div__(self, other):
    if isinstance(othe r, Unit):
    return Unit(self.value / other.value,
    "%s/%s" % (self.units, other.units))
    else:
    return Unit(self.value / other, self.units)
    def __radd__(self, other): return self.__add__(ot her)
    def __rsub__(self, other): return self.__sub__(ot her)
    def __rmul__(self, other): return self.__mul__(ot her)
    def __rdiv__(self, other): return self.__div__(ot her)
    def __repr__(self):
    return '%i %s' % (self.value, self.units)
    def __str__(self): return repr(self)
    ############### ############### ############### ###############

    which allows you to do things like

    distance = Unit(80, 'm')
    print "Distance: %s" % distance
    time = Unit(2, 'h')
    print "Time: %s" % time
    speed = distance/time
    print "Speed: %s" % speed
    speed = 3 * speed
    print "Speed: %s" % speed
    print "Error:",
    # can't add dissimilar units
    print distance + time



    I'm sure there are loads of other edge cases one would want to
    consider, such as analyzing and trimming redundant units, but
    that should give a pretaste of what could be done.

    -tkc



    Comment

    • Simon Brunning

      #3
      Re: Unit balancing

      On 13 Sep 2006 07:47:41 -0700, AbrahamLincolnI llinois@yahoo.c om
      <AbrahamLincoln Illinois@yahoo. comwrote:
      I am working with a researcher who would be very happy if he could
      include units in his calculations. Then, if his complicated expression
      didn't result in "kg/yr" the program would stop and point out his
      error.
      This what you were looking for?

      <http://home.tiscali.be/be052320/Unum.html>

      --
      Cheers,
      Simon B,
      simon@brunningo nline.net

      Comment

      • Claudio Grondi

        #4
        Re: Unit balancing

        AbrahamLincolnI llinois@yahoo.c om wrote:
        Greetings!
        >
        I am working with a researcher who would be very happy if he could
        include units in his calculations. Then, if his complicated expression
        didn't result in "kg/yr" the program would stop and point out his
        error.
        >
        Does Python (or SciPy or ..) offer this feature?
        >
        Thanks for your help.
        >
        >
        Abraham
        >
        I mean I run one time already into a Python library or a class which was
        designed to handle such things as this request is common to many
        researcher.
        Sorry, don't have any link at my fingertips or even an idea where to
        look after it, so Googling or waiting until someone else using such a
        tool posts here is all I can offer.

        Inbetween Frink is maybe an option?
        A practical calculating tool, unit conversion utility, and programming language designed to make physical calculations simple. It tracks units of measure (feet, meters, kilograms, watts, etc.) through all calculations, and helps ensure that answers are correct.


        Claudio Grondi


        Comment

        • AbrahamLincolnIllinois@yahoo.com

          #5
          Re: Unit balancing

          Thanks, all.

          We will investigate Unum.


          Abraham

          Comment

          Working...