Running Startup Code on Mathematica
I wanted to have quicker access to the physics constants that I use most
often.
Therefore, I edited the init.m
file of the Kernel. There are several
init.m
files - for the Kernel and for the FrontEnd, and in each case
for the User, for the system, ...
I chose the init.m
of the User&Kernel, because it's in a folder that
I back up, and because it's reloaded when I restart the Kernel. In my
case, the file path was
"C:\Users\myusername\AppData\Roaming\Mathematica\FrontEnd\init.m"
. You
can find the User directory by running $UserBaseDirectory
in
Mathematica.
I added the following lines to this file:
(** User Mathematica initialization file **)
loadPhysicsShortcuts := (
me = Quantity[1, "ElectronMass"];
mp = Quantity[1, "ProtonMass"];
u = Quantity[1, "AtomicMassUnit"];
e = Quantity[1, "ElementaryCharge"];
kB = Quantity [1, "BoltzmannConstant"];
T = Quantity[1, "Teslas"];
Hz = Quantity[1, "Hertz" ];
mHz = Quantity[1, "Millihertz"];
MHz = Quantity[1, "Megahertz"];
m = Quantity[1, "Meters"];
eV = Quantity[1, "Electronvolts"];
c = Quantity[1, "SpeedOfLight"];
mm = Quantity[1, "Millimeters"];
V = Quantity[1, "Volts"];
s = Quantity[1, "Seconds"];
)
I use setDelayed
( :=
) to define the variable (or rather, the
expression!) loadPhysicsShortcuts
. Since it's defined through
setDelayed
, the definitions in it are only exececuted when I execute
loadPhysicsShortcuts
. Thereby my definitions don't pollute my
workspace unless I need them.
I guess professionals would package their definitions, but I am not at that level yet. Also, I like how easy it is to call these definitions when I need them.