Re: Re: Mathematica 3.0 Palette for Physics SI Fundamental Constants
- To: mathgroup@smc.vnet.net
- Subject: [mg12433] Re: Re: [mg12311] Mathematica 3.0 Palette for Physics SI Fundamental Constants
- From: Bob Hanlon <BobHanlon@aol.com>
- Date: Thu, 14 May 1998 11:15:45 -0400
There was an error in my previous submission. Please use this instead. Sorry for the inconvenience. Bob Hanlon _________________ A user-defined constant should behave similarly to a built-in constant such as Pi, that is, it should act like a symbol unless its numeric properties are explicitly or implicitly invoked. For example, {Pi, 2*Pi, (2*Pi)/3, (1 + I)*Pi, Pi/3, Pi^2, Pi/3., Pi^2., N[Pi], 1.*Pi, (1. + I)*Pi, (1 + 1.*I)*Pi, D[Pi, x], Dt[Pi, x], NumericQ[Pi], 3^Pi, 3.^Pi, 3 + Pi, 3. + Pi} {Pi, 2*Pi, (2*Pi)/3, (1 + I)*Pi, Pi/3, Pi^2, 1.047197551196597, 9.869604401089357, 3.141592653589793, 3.141592653589793, 3.141592653589793 + 3.141592653589793*I, 3.141592653589793 + 3.141592653589793*I, 0, 0, True, 3^Pi, 31.54428070019753, 3 + Pi, 6.141592653589793} This behavior can be implemented along the following lines (that is, I haven't checked this out thoroughly enough to be sure that I have covered all the various situations required to cause a user-defined constant to behave exactly like a built-in constant): Off[General::spell1]; RealQ[(x_)?NumberQ] := (Head[x] === Real); Real2Q[x_] := (RealQ[x] || Head[x] === Complex && (RealQ[Re[x]] || RealQ[Im[x]])); Real2Q[x_+y_] := (Real2Q[x] || Real2Q[y]); Real2Q[x_*y_] := (Real2Q[x] || Real2Q[y]); defineConstant[s_Symbol, (value_)?NumberQ, units_:1] := Module[{const = value*units}, Unprotect[s]; Clear[s]; s/: NumericQ[s] = True; s/: N[s] := N[const]; s/: N[s, n_?NumberQ] := N[const, n]; s/: (a_)?Real2Q+s := a+const; s/: (a_)?Real2Q*s := a*const; s/: s^(a_)?Real2Q := const^a; s/: (a_)?Real2Q^s := a^const; SetAttributes[s, {Constant, Protected}]; ] defineConstant[c, 3.0*^8, Meter/Second]; (* simplified constant value *) {c, 2*c, (2*c)/3, (1 + I)*c, c/3, c^2, c/3., c^2., N[c], 1.*c, (1. + I)*c, (1 + 1.*I)*c, D[c, x], Dt[c, x], NumericQ[c], 3^c, 3.^c, c - 3*^6*Meter/Second, c - 3.*^6*Meter/Second} {c, 2*c, (2*c)/3, (1 + I)*c, c/3, c^2, (1.*^8*Meter)/Second, 9.*^16*(Meter/Second)^2., (3.*^8*Meter)/Second, (3.*^8*Meter)/Second, ((3.*^8 + 3.*^8*I)*Meter)/Second, ((3.*^8 + 3.*^8*I)*Meter)/Second, 0, 0, True, 3^c, 3.^((3.*^8*Meter)/Second), c - (3000000*Meter)/Second, (2.969999999999999*^8*Meter)/Second} f[\[Lambda]_] := c/\[Lambda] {f[\[Lambda]], N[f[\[Lambda]]], f[3*Meter], f[3.*Meter]} {c/\[Lambda], (3.*^8*Meter)/(Second*\[Lambda]), c/(3*Meter), 1.*^8/Second}