(******************************************************************* This file was generated automatically by the Mathematica front end. It contains Initialization cells from a Notebook file, which typically will have the same name as this file except ending in ".nb" instead of ".m". This file is intended to be loaded into the Mathematica kernel using the package loading commands Get or Needs. Doing so is equivalent to using the Evaluate Initialization Cells menu command in the front end. DO NOT EDIT THIS FILE. This entire file is regenerated automatically each time the parent Notebook file is saved in the Mathematica front end. Any changes you make to this file will be overwritten. ***********************************************************************) (* :Title: Units2 *) (* :Context: Miscellaneous`Units` *) (* :Author: Ted R.Ersek Author's email address = ted.ersek@navy.mil *) (* :Summary: This package is an improvement on the standard package Miscellaneous`Units. *) (* :Copyright: Copyright 2002 by Ted Ersek. Permission is hereby granted to use this package or any portion \ thereof provided this MathSource item and it's author are referenced.An \ exception is made for Wolfram Research who can use any portion of the package \ without giving credit. *) (* :Package Version: 1.1 *) (* :History: Version 1.0 was posted on MathSource in May 2002. Version 1.1 was posted on MathSource in Nov 2002. After loading version 1.0 of Miscellaneous`Units2` one would get error messages after evaluating something like the following: In[4]:= foo[expr_,n_Integer]:=Part[expr,n] This was due to a bug in Version 1.0 and was corrected for Version 1.1. *) (* :Mathematica Version: 4.1 *) (* :Keywords: *) (* :Warning: Makes use of the system symbols Second, Newton, Degree, Circle, Point, \ Last, Drop, Gamma, and Byte.*) (* :Requirements: *) (* The next Begin and End are here just to ensure the kernel doesn't complain \ that the context was not created. *) BeginPackage["Miscellaneous`Units2`"]; EndPackage[]; Needs["Miscellaneous`Units`"] BeginPackage["Miscellaneous`Units`"]; UnitList::usage= "UnitList is a list of symbols such as Second, Meter, Gram which the \ Miscellaneous`Units2` package recognizes as units."; UnitsWrapper::usage= "UnitsWrapper[expr,units] is used to show that expr has specified units. \ A typical example is UnitsWrapper[23, Foot/Second]. UnitsWrapper[ex] returns \ ex in the form UnitsWrapper[expr,units] if ex has units and otherwise returns \ ex unchanged."; ConvertTemperature::usage= "ConvertTemperature[temp, old, new] converts temp from the old scale to \ the new scale."; dB::usage= "dB is short for decibel and is a unit for the ratio of powers. Decibel \ is computed as \n 10*Log[10,power2/power1]."; Miscellaneous`Units`UnitList= Union[ {Miscellaneous`SIUnits`Amp,Miscellaneous`SIUnits`Ampere, Miscellaneous`SIUnits`Becquerel,Miscellaneous`SIUnits`Candela, Miscellaneous`SIUnits`Coulomb,Miscellaneous`SIUnits`Farad, Miscellaneous`SIUnits`Gray,Miscellaneous`SIUnits`Henry, Miscellaneous`SIUnits`Hertz,Miscellaneous`SIUnits`Joule, Miscellaneous`SIUnits`Kelvin,Miscellaneous`SIUnits`Kilogram, Miscellaneous`SIUnits`Lumen,Miscellaneous`SIUnits`Lux, Miscellaneous`SIUnits`Meter,Miscellaneous`SIUnits`Mole, Miscellaneous`SIUnits`Ohm,Miscellaneous`SIUnits`Pascal, Miscellaneous`SIUnits`Siemens,Miscellaneous`SIUnits`Tesla, Miscellaneous`SIUnits`Volt,Miscellaneous`SIUnits`Watt, Miscellaneous`SIUnits`Weber},{Abampere,Abcoulomb,Abfarad,Abhenry, Abmho,Abohm,Abvolt,Acre,AMU,Angstrom,Apostilb,ArcMinute,ArcSecond,Are, AssayTon,AstronomicalUnit,Atmosphere,AtomicMassUnit,Atto,AU, AvoirdupoisOunce,AvoirdupoisPound,Bag,BakersDozen,Bale,Bar,Barn, Barrel,Barye,Baud,Biot,Bit,BoardFoot,BohrMagneton,Bolt, BritishThermalUnit,BTU,Bucket,Bushel,Butt,Byte,Cable,Caliber,Calorie, Candle,Carat,Celsius,Cental,Centi,Centigrade,Centimeter,Century,Chain, ChevalVapeur,Cicero,Circle,Cord,Cubit,Cup,Curie,Dalton,Day,dB,Deca, Decade,Deci,Denier,Didot,DidotPoint,Diopter,Dozen,Drachma,Drop,Dyne, ElectronVolt,Ell,Ephah,Erg,Exa,Fahrenheit,Fathom,Feet,Femto,Fermi, Fifth,Firkin,FluidDram,FluidOunce,Foot,FootCandle,Fortnight,Furlong, Gal,Gallon,Gamma,Gauss,Geepound,Giga,Gilbert,Gill,Grade,Grain,Gram, GramWeight,Gravity,Gross,GrossHundredweight,Hand,Hectare,Hecto,Hefner, Hogshead,Horsepower,Hour,Hundredweight,ImperialGallon,ImperialPint, Inch,InchMercury,Jeroboam,Jigger,Kayser,Kilo,KilogramForce, KilogramWeight,Knot,Lambert,Last,League,Libra,LightYear,Link,Liter, Lumerg,Magnum,Maxwell,Mega,MetricTon,Mho,Micro,Micron,Mil,Mile, Millennium,Milli,MillimeterMercury,Mina,Minim,Minute,Month,Nano, NauticalMile,NetHundredweight,Newton,Nibble,Nit,Noggin, NuclearMagneton,Obolos,Oersted,Omer,Ounce,Parsec,Peck,Pennyweight, Percent,Perch,Peta,Phot,Pica,Pico,Pint,Point,Poise,Pole,Pondus,Pony, Pound,Poundal,PoundForce,PoundsPerSquareInch,PoundWeight, PrintersPoint,PSI,Puncheon,Quadrant,Quart,Quintal,Rad,Radian,Rankine, RegisterTon,Reyn,Rhes,RightAngle,Rod,Roentgen,Rontgen,Rood,Rope, Rutherford,Rydberg,Seam,Second,Section,Shekel,ShortHundredweight, ShortTon,Shot,SI,SiderealSecond,SiderealYear,Skein,Slug,SolarMass, Span,Stadion,Stadium,Statampere,Statcoulomb,Statfarad,Stathenry, Statohm,StatuteMile,Statvolt,Steradian,Stere,Stilb,Stokes,Stone, SurveyMile,Tablespoon,Talbot,Talent,Teaspoon,Tera,Therm,Ton,TonForce, Tonne,Torr,Township,TropicalYear,TroyOunce,Tun,UKGallon,UKPint,Week, Wey,WineBottle,XUnit,Yard,Year,Yocto,Yotta,Zepto,Zetta}]; Begin["`Private`"]; Parse::auto= "Unable to ensure input such as (12 Second) is automatically parsed into \ UnitsWrapper[12,Second] because $Pre is already used for other things."; Miscellaneous`Units`Convert[expr_?(Head[#]=!=UnitsWrapper&),u_UnitsWrapper]:= Miscellaneous`Units`Convert[UnitsWrapper[expr],u] Miscellaneous`Units`Convert[UnitsWrapper[expr_,Radian],Degree]:= Miscellaneous`Units`Convert[UnitsWrapper[expr,Radian], UnitsWrapper[1,Degree]]; Miscellaneous`Units`Convert[UnitsWrapper[expr_,old_], UnitsWrapper[Degree,new_]]:= With[{scalar= old/(Degree* new)//.Miscellaneous`Units`Private`$ToIntermediate//.\ Miscellaneous`Units`Private`$SIPrefixes//.Miscellaneous`Units`Private`$ToSI}, If[ MemberQ[scalar,Alternatives@@UnitList,{0,-1},Heads\[Rule]False], Message[Convert::incomp,old,new], If[ MemberQ[new,Celsius|Centigrade|Fahrenheit|Kelvin|Rankine,{0,-1}, Heads\[Rule]False], Message[Convert::temp] ]; UnitsWrapper[scalar*expr,new*Degree] ] ]; Miscellaneous`Units`Convert[UnitsWrapper[expr_,old_],UnitsWrapper[1,new_]]:= With[{scalar=(old/ new)//.Miscellaneous`Units`Private`$ToIntermediate//.\ Miscellaneous`Units`Private`$SIPrefixes//.Miscellaneous`Units`Private`$ToSI}, If[ MemberQ[scalar,Alternatives@@UnitList,{0,-1},Heads\[Rule]False], Message[Convert::incomp,old,new], If[ MemberQ[new,Celsius|Centigrade|Fahrenheit|Kelvin|Rankine,{0,-1}, Heads\[Rule]False], Message[Convert::temp] ]; UnitsWrapper[scalar*expr,new] ] ]; DownValues[Miscellaneous`Units`Convert]= Reverse[DownValues[Miscellaneous`Units`Convert]]; Attributes[UnitsWrapper]={NHoldRest}; MakeBoxes[UnitsWrapper[z:Complex[_?(#=!=0&),_],u_], form:(StandardForm|TraditionalForm)]:= TagBox[RowBox[{ RowBox[{"(",MakeBoxes[z,form],")" }], MakeBoxes[u,form]}],UnitsWrapper] MakeBoxes[UnitsWrapper[expr_Plus,u_],form:(StandardForm|TraditionalForm)]:= TagBox[RowBox[{ RowBox[{"(",MakeBoxes[expr,form],")" }], MakeBoxes[u,form]}],UnitsWrapper] MakeBoxes[UnitsWrapper[expr_,u_],form:(StandardForm|TraditionalForm)]:= TagBox[RowBox[{MakeBoxes[expr,form]," ",MakeBoxes[u,form]}],UnitsWrapper] UnitsWrapper/:Plus[UnitsWrapper[x_,u_],UnitsWrapper[y_,u_]]:= UnitsWrapper[x+y,u]; UnitsWrapper/:Times[UnitsWrapper[x_,u1_],UnitsWrapper[y_,u2_]]:= UnitsWrapper[x*y,u1*u2]; UnitsWrapper[x1_, u1:Power[_?(MemberQ[#,Power[_,_Integer?EvenQ],{0,-1}]&),_Rational?(EvenQ[ Denominator[#]]&)]]:=UnitsWrapper[x1,PowerExpand[u1]] UnitsWrapper/:Power[UnitsWrapper[expr_,u_],n_]:= UnitsWrapper[expr^n,u^n] UnitsWrapper/:Sign[UnitsWrapper[x_,u_]]:=Sign[x] UnitsWrapper/:Abs[UnitsWrapper[x_,u_]]:=UnitsWrapper[Abs[x],u] UnitsWrapper/:Ceiling[UnitsWrapper[x_,u_]]:=UnitsWrapper[Ceiling[x],u] UnitsWrapper/:Floor[UnitsWrapper[x_,u_]]:=UnitsWrapper[Floor[x],u] UnitsWrapper/:Round[UnitsWrapper[x_,u_]]:=UnitsWrapper[Round[x],u] UnitsWrapper/:Conjugate[UnitsWrapper[x_,u_]]:=UnitsWrapper[Conjugate[x],u] UnitsWrapper/:Im[UnitsWrapper[x_,u_]]:=UnitsWrapper[Im[x],u] UnitsWrapper/:Re[UnitsWrapper[x_,u_]]:=UnitsWrapper[Re[x],u] UnitsWrapper/:IntegerPart[UnitsWrapper[x_,u_]]:=UnitsWrapper[IntegerPart[x],u] UnitsWrapper/:FractionalPart[UnitsWrapper[x_,u_]]:=UnitsWrapper[FractionalPart[x],u] UnitsWrapper/: Sin[UnitsWrapper[x_?(Function[a,Head[Sin[a*Degree]]=!=Sin,{HoldAll}]), Degree]]:=Sin[x*Degree] UnitsWrapper/: Cos[UnitsWrapper[x_?(Function[a,Head[Cos[a*Degree]]=!=Cos,{HoldAll}]), Degree]]:=Cos[x*Degree] UnitsWrapper/: Tan[UnitsWrapper[x_?(Function[a,Head[Tan[a*Degree]]=!=Tan,{HoldAll}]), Degree]]:=Tan[x*Degree] UnitsWrapper/: Sec[UnitsWrapper[x_?(Function[a,Head[Sec[a*Degree]]=!=Sec,{HoldAll}]), Degree]]:=Sec[x*Degree] UnitsWrapper/: Csc[UnitsWrapper[x_?(Function[a,Head[Csc[a*Degree]]=!=Csc,{HoldAll}]), Degree]]:=Csc[x*Degree] UnitsWrapper/: Cot[UnitsWrapper[x_?(Function[a,Head[Cot[a*Degree]]=!=Cot,{HoldAll}]), Degree]]:=Cot[x*Degree] UnitsWrapper/: Sin[UnitsWrapper[x_?(Function[a,Head[Sin[a]]=!=Sin,{HoldAll}]),Radian]]:= Sin[x]; UnitsWrapper/: Cos[UnitsWrapper[x_?(Function[a,Head[Cos[a]]=!=Cos,{HoldAll}]),Radian]]:= Cos[x]; UnitsWrapper/: Tan[UnitsWrapper[x_?(Function[a,Head[Tan[a]]=!=Tan,{HoldAll}]),Radian]]:= Tan[x]; UnitsWrapper/: Sec[UnitsWrapper[x_?(Function[a,Head[Sec[a]]=!=Sec,{HoldAll}]),Radian]]:= Sec[x]; UnitsWrapper/: Csc[UnitsWrapper[x_?(Function[a,Head[Csc[a]]=!=Csc,{HoldAll}]),Radian]]:= Csc[x]; UnitsWrapper/: Cot[UnitsWrapper[x_?(Function[a,Head[Cot[a]]=!=Cot,{HoldAll}]),Radian]]:= Cot[x]; UnitsWrapper/:Times[x1_?UnitFreeQ, UnitsWrapper[x2_,u_]]:=UnitsWrapper[x1*x2,u]; NumberTest=Function[{x},NumericQ[x]&&(x=!=Degree)]; UnitsWrapper[expr_,1]:=expr UnitsWrapper[UnitsWrapper[expr_,u1_],u2_]:=UnitsWrapper[expr,u1*u2] UnitsWrapper[x_,UnitsWrapper[y_,u_]]:=UnitsWrapper[x*y,u] UnitsWrapper[x1_,x2_?NumberTest*u_]:=UnitsWrapper[x1*x2,u] $RemoveExcessWrapper=True; UnitsWrapper[expr_, u_?(MemberQ[#,UnitsWrapper,{1,-1}, Heads\[Rule]True]&)]/;$RemoveExcessWrapper:= Block[{$RemoveExcessWrapper}, UnitsWrapper[expr,u/.UnitsWrapper\[Rule]Times] ] UnitsWrapper[expr_]:=Replace[Unevaluated[expr], { (* Change things like t*Second to UnitsWrapper[t,Second]. *) Times[x_?UnitFreeQ,u_?AllUnitsQ]\[RuleDelayed]UnitsWrapper[x,u], (* Change things like Foot/Second to UnitsWrapper[1,Foot/Second]. *) u_Times?AllUnitsQ :> UnitsWrapper[1,u], (* Change things like Meter^2 to UnitsWrapper[1,Meter^2]. *) Power[u_AllUnitsQ,n_] :> UnitsWrapper[1, Power[u,n] ], (* Change things like Meter to UnitsWrapper[1,Meter]. *) u_Symbol?UnitsMatchQ :> UnitsWrapper[1,u] }, {0,-1}, Heads->False] UnitFreeQ=Function[{expr}, FreeQ[Unevaluated[expr],Alternatives@@UnitList,{0,-1},Heads\[Rule]False], {HoldAll} ]; AllUnitsQ=Function[{expr}, With[{un=Cases[Unevaluated[#1],_Symbol,{-1},Heads\[Rule]False]},Complement[un,UnitList]==={}&&un\[Intersection]UnitList=!={}], {HoldAll} ]; UnitsMatchQ=Function[{expr}, MatchQ[Unevaluated[expr],Alternatives@@UnitList], {HoldAll} ]; If[Head[$Pre]===Symbol, $Pre= Function[expr,Replace[Unevaluated[expr], { (* Change things like t*Second to UnitsWrapper[t,Second]. *) Times[x_?UnitFreeQ,u_?AllUnitsQ]\[RuleDelayed]UnitsWrapper[x,u], (* Change things like Foot/Second to UnitsWrapper[1,Foot/Second]. *) u_Times?AllUnitsQ :> UnitsWrapper[1,u], (* Change things like Meter^2 to UnitsWrapper[1,Meter^2]. *) Power[u_AllUnitsQ,n_] :> UnitsWrapper[1, Power[u,n] ], (* Change things like Meter to UnitsWrapper[1,Meter]. *) u_Symbol?UnitsMatchQ :> UnitsWrapper[1,u] }, {0,-1}, Heads->False], {HoldAll}], (* else *) Message[Parse::auto] ]; Unprotect["Miscellaneous`Units`SI","Miscellaneous`Units`CGS", "Miscellaneous`Units`ConvertTemperature"]; Miscellaneous`Units`ConvertTemperature[x_,UnitsWrapper[1,old_], UnitsWrapper[1,new_]]:=Miscellaneous`Units`ConvertTemperature[x,old,new] DownValues[Miscellaneous`Units`ConvertTemperature]= RotateRight[DownValues[Miscellaneous`Units`ConvertTemperature],1]; Miscellaneous`Units`SI[UnitsWrapper[expr_,u_]]:= UnitsWrapper[expr,Miscellaneous`Units`SI[u]] Miscellaneous`Units`CGS[UnitsWrapper[expr_,u_]]:= UnitsWrapper[expr,Miscellaneous`Units`CGS[u]] End[]; EndPackage[]; Protect["Miscellaneous`Units`*"];