0% found this document useful (0 votes)
6 views

Lecture 7

Uploaded by

onyinyechijoel9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 7

Uploaded by

onyinyechijoel9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SCIENTIFIC PROGRAMMING

(CSC231)
CHAPTER 8: SCIPY

J. D. Akinyemi (Ph.D.)
Department of Computer Science,
University of Ibadan, Ibadan, Nigeria
OUTLINE

1. Physical Constants and Special Functions

2
SCIPY
• SciPy is a library of Python modules for scientific computing that provides more specific
functionality than the generic data structures and mathematical algorithms of NumPy.
• For example, it contains modules for the evaluation of special functions frequently
encountered in science and engineering, optimization, integration, interpolation and image
manipulation.
• As with the NumPy library, many of SciPy’s underlying algorithms are executed as
compiled C code, so they are fast.
• Also like NumPy and Python itself, SciPy is free software.
• There is little new syntax to learn in using the SciPy routines, so this chapter will focus on
examples of the library’s use in short programs of relevance to science and engineering.

3
SCIPY
• Physical Constants and Special Functions:
• The useful scipy.constants package provides the internationally agreed standard
values and uncertainties for physical constants.
• The scipy.special package also supplies a large number of algorithms for
calculating functions that appear in science, mathematical analysis and engineering.
• Some of the algorithms are listed in the next slide. They are described in detail in the
documentation.
• This section focuses on a few representative examples.
• Most of these special functions are implemented in SciPy as universal functions:
• that is, they support broadcasting and vectorization (automatic array-looping), and so
work as expected with NumPy arrays.
4
SCIPY
• Airy functions • Legendre functions and associated
• elliptic functions and integrals Legendre functions;

• Bessel functions, their zeros, derivatives • a variety of orthogonal polynomials;


and integrals • hypergeometric functions;
• spherical Bessel functions • parabolic cylinder functions;
• a variety of statistical functions and • Matheiu functions;
distributions • spheroidal functions.
• gamma and beta functions
• the error function
• Fresnel integrals

5
SCIPY
• Physical Constants:
• SciPy contains the 2018 CODATA internationally recommended values of many physical
constants.
• They are held, with their units and uncertainties, in a dictionary,
scipy.constants.physical_constants, keyed by an identifying string.
• For example,
In [x]: import scipy.constants as pc
In [x]: pc.physical_constants['Avogadro constant']
Out[x]: (6.022140857e+23, 'mol^-1', 7400000000000000.0)

6
SCIPY
• Physical Constants:
• The convenience methods value, unit and precision retrieve the corresponding
properties on their own:
In [x]: pc.value('electron mass')
Out[x]: 9.1093837015e-31
In [x]: pc.unit('electron mass')
Out[x]: 'kg'
In [x]: pc.precision('electron mass')
3.0737534961217373e-10

7
SCIPY
• Physical Constants:
• To save typing, it is usual to assign the value to a variable name at the start of a
program, for example,
In [x]: muB = pc.value('Bohr magneton')

• A full list of the constants and their names is given in the SciPy documentation,
but Table 8.1 lists the more important ones.
• Some particularly important constants have a direct variable assignment within
scipy.constants (in SI units) and so can be imported directly:
In [x]: from scipy.constants import c, R, k
In [x]: c, R, k # speed of light , gas constant , Boltzmann constant
Out[x]: (299792458.0, 8.314462618, 1.380649e-23)

8
SCIPY
• Physical Constants:
• Where this is the case, the variable name is given in the table.
• You will probably find it convenient to use the scipy.constants values, but
should be aware that if and when newer values are released, the package may be
updated – this means that your code may produce slightly different results for
different versions of SciPy.
• The values given below are from SciPy version 1.4, which includes the 2019
redefinition of the SI base units

9
SCIPY
• Physical Constants:

10
SCIPY
• Physical Constants:
• There are one or two useful conversion factors and methods defined within the
scipy.constants package, which also includes representations of the SI prefixes.

• For example,
In [x]: import scipy.constants as pc
In [x]: pc.atm
Out[x]: 101325.0 # 1 atm in Pa
In [x]: pc.bar
Out[x]: 100000.0 # 1 bar in Pa
In [x]: pc.torr
Out[x]: 133.32236842105263 # 1 torr in Pa
In [x]: pc.zero_Celsius
Out[x]: 273.15 # 0 degC in K
In [x]: pc.micro # also nano , pico , mega , giga , etc.
Out[x]: 1e-06

11
REFERENCE TEXTS AND ONLINE RESOURCES
• Scientific Programming in Python
1. ** Christian Hill. Learn Scientific Programming with Python. 2nd edition, 2020.
Cambridge Press
2. Hans Petter Langtangen. A Primer on Scientific Prorgramming with Python. 4th edition
2014.
3. Robert Johansson. Numerical Python: Scientific Computing and Data Science
Applications with Numpy, Scipy and Matplotlib. 2nd edition. 2019. Apress.

• Python Basics
4. Finxter: https://app.finxter.com/learn/computer/science/ (Here, you will have fun while
you learn with python code puzzles)
5. Data Camp: https://www.datacamp.com/courses/intro-to-python-for-data-science

12

You might also like