MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Extracting units from a list of values

  • To: mathgroup at
  • Subject: [mg27408] Re: Extracting units from a list of values
  • From: "Rasmus Debitsch" <debitsch at>
  • Date: Sun, 25 Feb 2001 00:53:31 -0500 (EST)
  • References: <9750ck$>
  • Sender: owner-wri-mathgroup at


you can look at David Park's V4ExtendedUnits Package:

    Information["ToUnitList", LongForm -> False]
    "ToUnitList[unit,options...] converts an entire list to the same unit."

    ToUnitList[Meter][{1*Meter, 1*Yard, 1*Meter}]
    {1.*Meter, 0.9144*Meter, 1.*Meter}

  ToUnitList[Meter][{1*Meter, 1*Degree, 1*Meter}]
  Convert::"incomp" : "Incompatible units in \!\(0.017453292519943295`\) and
 {1.*Meter, 0.017453292519943295*Meter, 1.*Meter}

gives an error message for incompatible units.


        Rasmus Debitsch
        Carl Zeiss Lithos GmbH
        Carl Zeiss Strasse
        D-73447 Oberkochen

        eMail : debitsch at

"Thomas Anderson" <tga at> schrieb im Newsbeitrag
news:9750ck$6ke at
> As part of the package I'm working on, one of the functions takes
> a list of measured values as an argument. For maximum flexibility,
> I wish to accept either a list of dimensionless numbers or a list
> of numbers with units, i.e.
>    {1, 2, 3}
> or
>    {1 Meter, 2 Meter, 3 Meter}
> for example. As part of the argument checking, I want to be able
> to test whether the units are consistent: everything should either
> be dimensionless or have the same units.
> The problem boils down to: how can I separate the units from the
> numeric part of the value? I've tried a few things, and so far my
> best attempt has been
>    Replace[vals, (_?NumericQ unit_.) :> unit, 1]
> where vals is the list of values. This works pretty well:
> dimensionless numbers give a list of "units" of 1, and values like
> "2 Meter" or "1 Elephant^2" give "Meter" and "Elephant^2" respectively.
> This method doesn't work, however, with input containing more
> complicated numerical values. For example, {1, 2 Pi, 3 E Meter^2} gets
> transformed into {1, 2, 3 Meter^2}, whereas I want {1, 1, Meter^2} for
> these values. I could apply N[] to the values before extracting the
> units, but then "Meter^2" becomes "Meter^2.", which I don't want.
> This isn't a huge problem, since I'm expecting the values to be
> integers
> or real numbers, but I want my code to be as bulletproof as possible.
> Thanks in advance for any suggestions.
> -Tom Anderson
>  tga at

  • Prev by Date: Re: Arrows on axes?
  • Next by Date: RE: Extracting units from a list of values
  • Previous by thread: Re: Extracting units from a list of values
  • Next by thread: RE: Extracting units from a list of values