Mathematics deals with a vast number of important but complex and time-consuming concepts. To resolve this issue, Python provides the full-fledged SciPy library.
Scipy stands for Scientific Python. SciPy is an open-source library that is a collection of mathematical algorithms and convenience functions built on the NumPy extension of Python. Scipy allows users to manipulate and visualize data with a wide range of high-level commands and classes. Scipy provides significant power to Python sessions. As mentioned earlier, SciPy is built on NumPy, so if we import SciPy, there is no purpose for importing NumPy.
Why use SciPy
SciPy contains varieties of sub-packages that help solve the issue related to Scientific Computation.
SciPy package in Python is the most used Scientific library, only second to GNU Scientific Library for C/C++ or Matlab's.
Scipy is easy to use and understand and has fast computational power.
Scipy can operate on an array of NumPy libraries.
Numpy v/s SciPy
NumPy and SciPy are both Python libraries used for mathematical and numerical analysis. NumPy contains array data and essential operations such as sorting, indexing, etc. whereas, SciPy consists of all the numerical code. Though NumPy provides several functions that can help resolve linear algebra, Fourier transforms, etc., SciPy is the library that contains fully-featured versions of these functions along with many others. However, if we are doing scientific analysis using Python, we must install NumPy and SciPy since SciPy builds on NumPy.
Installation
If our system already has pip or Python installed, we can download scipy by using the below command:
pip install scipy
Install Scipy on Linux
sudo apt-get install python-scipy python-numpy
Install SciPy in Mac
sudo port install py35-scipy py35-numpy
Import
Once SciPy is installed, we can import the SciPy module(s) in our applications by adding them from the scipy import module statement:
from scipy import modules
Example:
from scipy import special from scipy import constants
Let us some basic functionalities of scipy.
Basic Functions
Interaction with NumPy
SciPy is built on NumPy, so we can use NumPy functions to tackle arrays. We can make use of help(), info(), or source() functions to know in-depth of about functions.
help()
This function provides us the information about any other function. There are two ways to use the help function:
without parameters
with parameters
Example:
from scipy import cluster help(ndimage) #with parameter help() #without parameter
When we run the above code, the first help() provides us the information about the ndimage submodule. The second help() asks us to enter the name of any module, keyword, etc., for which we desire to seek information. Type 'quit' and hit enter to stop the function's execution.
info()
This function provides information about the desired functions, modules, keywords.
scipy.info(cluster)
source()
This function returns source code for objects written in Python. source function does not yield useful information in case the methods or objects are written in any other language such as C. To use this function, write:
scipy.source(cluster)
Constants
SciPy is more focused on scientific implementations, and it has many built-in scientific constants.
The scipy.constants package provides us with various constants. We have to import the required constant as per the requirement. Let us see how these constant variables are imported and used.
Example:
import scipy from scipy.constants import pi print(scipy.constants.pi)
Output
3.141592653589793
Many different constants are available, like mass units, length, time, force, power, temperature, speed, and many more. We can see the list of all units under the constants module using the dir() function.
from scipy import constants
print(dir(constants))
Remembering all constants is troublesome. An easy way is to use the scipy.constants.find() method to find which key is used for which function.
Integration
SciPy provides several functions to solve integration problems. SciPy is a storehouse of various functions to solve all types of integrals ranging from ordinary differential integrators to using trapezoidal rules to compute integrals. We can import scipy.integrate library for this purpose.
Single Integration
We can use the quad function in scipy to calculate the integral with one variable. The general syntax of the quad is:
scipy.integrate.quad(F, lo, up)
Where
F is the function to be integrated
lo and up are the lower and upper limits, respectively.
Example:
from scipy import special from scipy import integrate a= lambda x:special.exp10(x) b = scipy.integrate.quad(a, 0, 2) print(b)
Output:
(42.99515370842193, 4.773420959438771e-13)
Double Integration
We can use SciPy dblquad to calculate double integrals. As many of us know, a double integral consists of two actual variables. dblquad() function takes the input function to be integrated as its parameter along with four other variables, i.e., the upper and lower limits and the derivative functions dx and dy.
Example:
from scipy import integrate f = lambda y, x: x*y**2 dx = lambda x: 1 dy = lambda x: -1 integrate.dblquad(f, 0, 3, dx, dy)
Output
(-3.0, 3.3306690738754696e-14)
SciPy provides several other functions to evaluate triple integrals, n integrals, Romberg Integrals, etc., that we can explore in detail. We can use the help function to have all the details about these functions.
Linear Algebra
Linear algebra deals with linear equations and their representations using vector spaces and matrices. SciPy is built on ATLAS LAPACK and BLAS libraries. It is extremely fast in solving problems related to linear algebra. In addition to all the functions from numpy.linalg, scipy.linalg also provides several other advanced functions. Also, if numpy.linalg is not used along with ATLAS LAPACK and BLAS support, scipy.linalg is faster than numpy.linalg.
The inverse of a Matrix
import numpy as np from scipy import linalg A = np.array([[1,5], [6,3]]) B = linalg.inv(A) print(B)
Output
[[-0.11111111 0.18518519]
[ 0.22222222 -0.03703704]]
Determinants
The value derived arithmetically from the matrix coefficients is the determinant of a square matrix.
Syntax:
det(a, overwrite_a=False, check_finite=True)
where,
a : (M, M) Is a square matrix
overwrite_a( bool, optional) : Allow overwriting data in a
check_finite ( bool, optional): To check whether the input matrix consists only of finite numbers
Example:
import numpy as np from scipy import linalg A = np.array([[1,5], [6,3]]) B = linalg.det(A) print(B)
OUTPUT:
-27.0
That is all from the basics of the scipy. For further details, you can refer to scipy documentation.
Frequently Asked Questions
What is scipy? SciPy is an open-source scientific and numerical tool for Python. It supports special functions, integration, ordinary differential equation (ODE) solvers, gradient optimization, parallel programming tools.
What language is SciPy written? Python, C, C++, Fortran.
What scientific libraries are available in SciPy? The scientific libraries included in SciPy are cluster, constants, fft pack, interpolate, io, linalg, signal, spare, and many more.
Key takeaways
Let us brief the article.
Firstly, we saw scipy, why we need scipy, and the significant differences between scipy and numpy. Furthermore, we saw some of the essential basic functions of scipy and various packages.
That's all from my end. I hope you all like this article.