Using physical constants in Python scipy.constants ~ constants e ~

0. Environment

\bullet macOS Catalina \bullet Jupyter notebook \bullet Python 3.8.6 \bullet NumPy 1.18.5 \bullet SciPy 1.5.4[^1] [^ 1]: print (scipy.constants.physical_constants ["hyperfine transition frequency of Cs-133 "]) and print (scipy.constants.physical_constants ["luminous efficacy "]) are output as empty strings. In that case, an update is required. SciPy 1.4.0 Release Notes — SciPy v1.5.4 Reference Guide

  1. scipy.constants

1.1 Physical constant

Physical constants can be introduced by doing from scipy.constants import *.

SI_2019


from scipy.constants import * 
print(physical_constants["hyperfine transition frequency of Cs-133"])
print(c) #speed of light in vacuum
print(h) #the Planck constant
print(e) #elementary charge
print(k) #Boltzmann constant
print(N_A) #Avogadro constant
print(physical_constants["luminous efficacy"])

This is the output result.

Results


(9192631770.0, 'Hz', 0.0)
299792458.0
6.62607015e-34
1.602176634e-19
1.380649e-23
6.02214076e+23
(683.0, 'lm W^-1', 0.0)

If you say from scipy import constants, you have to do something like constants.h or scipy.constant.physical_constants ["hyperfine transition frequency of Cs-133 "], but you get the same result. ..

from scipy import constants
print(constants.h)
#6.62607015e-34

1.2 Finer calculations

In addition, various units such as combined units can also be used in calculations.

units


from scipy.constants import * 
print(day) #one day is 86400 seconds
print(gram) # one gram is 0.001 kilogram
print(eV) #1 eV equals e coulomb times 1 volt
#86400.0
#0.001
#1.602176634e-19

Unit calculation using a prefix is also possible. You can also convert the wavelength-frequency of electromagnetic waves by using lambda2nu ().

prefix_and_lambda2nu


print('{:e}'.format(lambda2nu(370 * nano)), 'Hz') #ultraviolet
print(lambda2nu(370 * nano) / tera, 'THz')
print('{:e}'.format(lambda2nu(1 * milli)), 'Hz') #radio waves (microwaves)
print(lambda2nu(1 * milli) / giga, 'GHz')
#8.102499e+14 Hz
#810.2498864864865 THz
#2.997925e+11 Hz
#299.792458 GHz

2. Constant e

2.1 Elementary charge and number of napiers

Elementary charges and Napier numbers are confusing because they both have the same symbol, $ e $ [^ 2]. [^ 2]: There is also a style of making it three-dimensional like e and i, but this is not the case in all cases, but rather italics are customarily used.

If you just want to print it, you can add an annotation, but you cannot use the same name in the code.

from scipy.constants import * 
print(physical_constants["elementary charge"])
print(e)
print(math.e) #the Euler's number

Especially in LaTeX, you don't type math.e, so it's very troublesome to copy a cleanly written formula to Python code.

2.2 E power example: Fermi-Dirac statistics

Preparation



%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
import math
from scipy.constants import *

Consider the Fermi-Dirac distribution as an example of the appearance of exponents. Suppose you want to plot a graph of the following formula:

 \frac{1}{e^{\beta(\varepsilon - \mu)} + 1}= \frac{1}{e^{\mu(x - 1)/(kT)} + 1}\quad(x = \varepsilon/ \mu )

Where $ k $ is the Boltzmann constant. Let's draw with Jupyter notebook.

Fermi_Dirac_distribution


from scipy.constants import * 
def E(x, mu):
    return 1/(e**(mu*(x - 1)/(k*T)) + 1)
example_elementary.png

That is a strange graph (the graph drawing settings are omitted). This is a qualitatively impossible graph because the particles can take as much energy as possible. Since e is defined as the value of the elementary charge infrom scipy.constants import *, the graph was output, but the calculation was incorrect.

Of course, you can play with import to solve the problem, but here we will use the most intuitive, convenient, and convenient method. In other words, now according to the formula

e = math.e

Let's do it. Since the elementary charge is not used in this calculation, there is no problem locally (if the elementary charge is used elsewhere, write from scipy.constants import * again at that time, etc. You should do it).

from scipy.constants import * 
e = math.e
def E(x, mu):
    return 1/(e**(mu*(x - 1)/(k*T)) + 1)
example_math_e.png

The correct graph is output!

Related / reference articles

Constants (scipy.constants) — SciPy v1.5.3 Reference Guide [SciPy Constants] (https://www.w3schools.com/python/scipy_constants.asp) [Python math.e Constant] (https://www.w3schools.com/python/ref_math_e.asp) [python memo] (http://tomohiro_abe.droppages.com/misc/python.html) I referred to from scipy.constants import *. [Fermi–Dirac statistics - Wikipedia] (https://en.wikipedia.org/wiki/Fermi–Dirac_statistics) I referred to the Fermi-Dirac statistics.

Recommended Posts

Using physical constants in Python scipy.constants ~ constants e ~
Solve ABC176 E in Python
Translate using googletrans in Python
Using Python mode in Processing
GUI programming in Python using Appjar
Precautions when using pit in Python
Try using LevelDB in Python (plyvel)
Using global variables in python functions
Let's see using input in python
Infinite product in Python (using functools)
Edit videos in Python using MoviePy
Handwriting recognition using KNN in Python
Try using Leap Motion in Python
Depth-first search using stack in Python
When using regular expressions in Python
GUI creation in python using tkinter 2
Mouse operation using Windows API in Python
Try using the Wunderlist API in Python
Get Suica balance in Python (using libpafe)
(Bad) practice of using this in Python
Slowly hash passwords using bcrypt in Python
Try using the Kraken API in Python
Using venv in Windows + Docker environment [Python]
[FX] Hit oanda-API in Python using Docker
Tweet using the Twitter API in Python
[Python] [Windows] Serial communication in Python using DLL
I tried using Bayesian Optimization in Python
Get Youtube data in Python using Youtube Data API
Scraping a website using JavaScript in Python
Develop slack bot in python using chat.postMessage
Write python modules in fortran using f2py
Draw a tree in Python 3 using graphviz
Notes for using python (pydev) in eclipse
Disease classification in Random Forest using Python
Download files in any format using Python
Parallel task execution using concurrent.futures in Python
Learn dynamic programming in Python (A ~ E)
Notes on using code formatter in Python
Meaning of using DI framework in Python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Create a GIF file using Pillow in Python
Meta-analysis in Python
Email attachments using your gmail account in python.
Unittest in python
Creating numbering process using python in DynamoDB Local Numbering process
Discord in Python
Get image URL using Flickr API in Python
Start using Python
DCI in Python
Notes on using dict in python [Competition Pro]
quicksort in python
nCr in python
Let's judge emotions using Emotion API in Python
N-Gram in Python
Perform entity analysis using spaCy / GiNZA in Python
Programming in python