[Date Index]
[Thread Index]
[Author Index]
RE: Extracting units from a list of values
*To*: mathgroup at smc.vnet.net
*Subject*: [mg27423] RE: [mg27398] Extracting units from a list of values
*From*: "David Park" <djmp at earthlink.net>
*Date*: Sun, 25 Feb 2001 00:53:50 -0500 (EST)
*Sender*: owner-wri-mathgroup at wolfram.com
Thomas,
I have a package at my web site below called Miscellaneous`V4ExtendUnits`.
One of the routines in the package is BaseSI which will convert any
expression with units to the standard base SI units (Second, Meter,
Kilogram, Ampere, Candela). Once this is done we can remove all numeric
quantities with the following rule.
Needs["Miscellaneous`V4ExtendUnits`"]
unitsextract[expr_] :=
Module[{t = BaseSI[expr]},
t = t //. HoldPattern[a___*(b_ /; NumericQ[b] &&
FreeQ[b, Second | Meter | Kilogram | Ampere | Candela])*
c___] :> a*c;
If[NumericQ[t], 1, t]]
Then it is easy to test. Here is one of your examples:
unitsextract /@ {1, 2 Pi, 3 E Meter^2}
SameQ @@ %
{1, 1, Meter^2}
False
Here is a more complicated example, where the list is expressed in different
but compatible units. (If only NASA had done this with their Mars probe!)
unitsextract /@ {2.*Meter/Second^2, Sin[3]*Meter/Second^2,
5*Pi*Feet/Minute^2, 3.*LightYear/Year^2}
SameQ @@ %
{Meter/Second^2, Meter/Second^2, Meter/Second^2, Meter/Second^2}
True
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
> -----Original Message-----
> From: Thomas Anderson [mailto:tga at stanford.edu]
To: mathgroup at smc.vnet.net
> Sent: Friday, February 23, 2001 2:34 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg27423] [mg27398] Extracting units from a list of values
>
>
> 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 stanford.edu
>
>
>
Prev by Date:
**Re: Extracting units from a list of values**
Next by Date:
**Re: A bug of Integrate[] in Mathematica 4.1 (and 4.0)**
Previous by thread:
**Re: Extracting units from a list of values**
Next by thread:
**Re:Extracting units from a list of values**
| |