SciPy - Constants
SciPy - Constants
Section titled “SciPy - Constants”The scipy.constants module in SciPy provides a comprehensive collection of physical and mathematical constants. These constants are invaluable in scientific computing and engineering applications, ensuring accuracy and consistency.
Accessing Constants
Section titled “Accessing Constants”Constants from scipy.constants can be directly imported and used. SciPy aims to provide higher precision for these constants compared to standard libraries like math where applicable, though for fundamental constants like pi, they are often equivalent due to standardization.
Let’s compare the value of ‘pi’ from scipy.constants and Python’s built-in math module:
import scipy.constantsimport math
# Accessing pi from scipy.constantsscipy_pi = scipy.constants.pi
# Accessing pi from math modulemath_pi = math.pi
print(f"scipy.constants.pi: {scipy_pi:.16f}")print(f"math.pi: {math_pi:.16f}")The above program will typically produce the following output, showing identical precision for pi:
scipy.constants.pi: 3.1415926535897931math.pi: 3.1415926535897931Available Constants
Section titled “Available Constants”SciPy offers a wide array of constants. Below are some common categories and examples.
Mathematical Constants
Section titled “Mathematical Constants”These include fundamental mathematical numbers.
| Constant Name | Description |
|---|---|
pi | Pi (π) |
golden or golden_ratio | The golden ratio, (1 + sqrt(5))/2 |
Physical Constants
Section titled “Physical Constants”A vast collection of precisely measured physical constants. Each physical constant in scipy.constants.physical_constants is a tuple: (value, unit, uncertainty).
Key in physical_constants (Examples) | Description |
|---|---|
'speed of light in vacuum' | Speed of light (c) |
'Planck constant' | Planck constant (h) |
'Newtonian constant of gravitation' | Gravitational constant (G) |
'elementary charge' | Elementary charge (e) |
'molar gas constant' | Molar gas constant (R) |
'Avogadro constant' | Avogadro constant (N_A) |
'Boltzmann constant' | Boltzmann constant (k) |
'electron mass' | Electron mass (m_e) |
'proton mass' | Proton mass (m_p) |
'neutron mass' | Neutron mass (m_n) |
Example of accessing a physical constant:
import scipy.constants
# Get the data for the speed of lightc_data = scipy.constants.physical_constants['speed of light in vacuum']value, unit, uncertainty = c_data
print(f"Speed of light: {value} {unit}")print(f"Uncertainty: {uncertainty}")Output:
Speed of light: 299792458.0 m s^-1Uncertainty: 0.0Units and Prefixes
Section titled “Units and Prefixes”scipy.constants also provides SI prefixes and conversion factors for common units.
| Prefix/Unit Name | Value (SI Factor or definition) |
|---|---|
milli | 1e-3 |
micro | 1e-6 |
kilo | 1e3 |
gram | 1e-3 (mass in kg) |
degree | pi/180 (angle in radians) |
minute | 60.0 (time in seconds) |
hour | 3600.0 (time in seconds) |
day | 24 * 3600.0 (time in seconds) |
inch | 0.0254 (length in meters) |
light_year | 9.4607304725808e+15 (length in meters) |
atm or atmosphere | 101325.0 (pressure in Pascals) |
eV or electron_volt | Value of elementary charge (energy in Joules) |
hp or horsepower | 745.6998715822701 (power in watts) |
These unit values can be used for conversions. For example, to convert 1500 grams to kilograms:
import scipy.constants
mass_grams = 1500mass_kg = mass_grams * scipy.constants.gram # gram is 0.001 kgprint(f"{mass_grams} grams is {mass_kg} kilograms")Output:
1500 grams is 1.5 kilogramsFinding Constants
Section titled “Finding Constants”With so many constants available, it can be hard to remember the exact key or name. The scipy.constants.find() function helps you search for constants based on keywords. It returns a list of keys from scipy.constants.physical_constants that match the search term.
Let’s find constants related to ‘alpha particle’:
import scipy.constants
# Find constants containing 'alpha particle'keys_found = scipy.constants.find(key='alpha particle')print("Found keys related to 'alpha particle':")for k in keys_found: print(f"- {k}")
# Example: Accessing one of the found constantsif 'alpha particle mass' in keys_found: alpha_mass_val, alpha_mass_unit, alpha_mass_unc = scipy.constants.physical_constants['alpha particle mass'] print(f"\nAlpha particle mass: {alpha_mass_val} {alpha_mass_unit}, uncertainty: {alpha_mass_unc}")The above program will generate output similar to this (exact list might vary slightly with SciPy versions):
Found keys related to 'alpha particle':- alpha particle mass- alpha particle mass energy equivalent- alpha particle mass energy equivalent in MeV- alpha particle mass in u- electron to alpha particle mass ratio
Alpha particle mass: 6.6446573357e-27 kg, uncertainty: 1.2e-35scipy.constants.find() is a powerful tool for exploring the available physical constants. For a full list, you can also iterate through scipy.constants.physical_constants.keys().
Further resources: Refer to the official SciPy documentation for a complete list and more details.