This blog on Python discusses various integrations that the language goes through. Starting from integration with various technologies, to third party providers to databases, this blog discusses everything in detail. Besides discussing the benefits of integration, it also gives an insight into the integration functions and SciPy library in Python.
Table of contents
Python is very easy in integrating with various other components. Python can easily be applied to many upcoming and latest trends in the world of software development that favor modular programming.
Most of the programmers opt for Python for developing applications among various organizations that use a set of different technologies, tools, systems as well as existing databases.
Many startups support the versatility of Python as they allow them in making the instant use of third party characteristics and rapidly design an app from the base. In Fact, Python For Data Science is also a great trend to explore.
Python offers native support for C and it also has some of the advanced services to integrate with various programming languages such as Java, C++, Go as well as Rust.
But one of the best features of Python, when we talk about integrating with existing technologies, is the “glue language” factor.
Instead of redeveloping the existing technologies as well as systems from the beginning, the Python app development can use the REST API or Foreign Language Interface (FFI) of Python for binding them all together.
Another biggest benefit of integrating the existing technologies is that when an organization having two different groups working equally on the same project.
Both the teams can work as per their own methodologies using technologies they are comfortable with. And finally, they are able to integrate the efficiency of their work by using Python.
On the other hand, if an enterprise has its own in house app which was developed using Python and they want to convert it into other languages such as Java, C++, and Rust.
This is for modules where these languages are suited in a much better way, then Python is efficient enough in joining them together. This can be done without the requirement of building a large codebase.
This approach will save both times as well as money for the businesses because they are able to use Python Integration rather than just rebuilding them.
Generally using an Application Programming Interface, popularly known as API that is offered by a third party, is all about some simple lines of code in Python with the help of easy to use tools such as JSON or REST API.
Python has been improving its free tooling bases for the efficient use of APIs. It has been done by a group of passionate programmers for over 20 years since Python has made its presence in the market.
This clearly means the program is so advanced that it is capable enough to handle a wide range of third party products without any sort of difficulties. Knowing What Is Integration in Python is highly essential.
In most of the cases, Python app development may need to integrate with third-party products or companies for accessing their features.
For instance, Dotpay or PayPal which lets its users make in-app purchases instantly, safely, and easily. Another example of third-party providers is some APIs that are able to track the location of the nearby restaurants through an app or connecting with friends from another app as well as services.
For a programmer or a developer to add this kind of storefront coding from scratch would be time and cost consuming.
Not only this, but it could also be less secure and accessible as compared to other established solutions such as PayPal. Apart from this, it is also mandatory to have a look at the risks as well as legal restrictions that come with certain functionalities.
But the best part of these services is that they can provide APIs which can easily be integrated into an application instantly with the help of Python.
Now, how is it beneficial for a business? Well, we have already mentioned that integrating an existing solution through API does not cost much time and money. It also doesn’t carry many risks as compared to other in-house solutions.
Through the Integration in Python, it will be very easy in incorporating all these functionalities from third-party providers.
What is the integral of a function f(x) from point a to point b,
∫baf(x)dx∫abf(x)dx
So in Python, we have to use numerical quadrature for achieving this along with the scripy. Integrate.uad command.
For example, let us integrate
y=x2y=x2
From x=0 to x=1. You should prove that the answer is 1/3.
from scipy.integrate import quad
def integrand(x): return x**2 ans, err = quad(integrand, 0, 1) Print ans 0.333333333333
1 double integrals
We use scipy.integrate.dblquad command
Integrate f(x,y)=ysin(x)+xcos(y) over
π<=x<=2π
0<=y<=π
i.e.
∫2πx=π∫πy=0ysin(x)+xcos(y)dydx
The syntax in dblquad is complicated as compared to the one in Matlab. We need to provide callable functions for the y-variable range. In this case, they are constants.
Hence we created the lambda functions for Python Integration which return the constants. You also have to remember that the order of the arguments in the integrand is different than in Matlab.
from scipy.integrate import dblquad import numpy as np def integrand(y, x): 'y must be the first argument, and x the second.' return y * np.sin(x) + x * np.cos(y) ans, err = dblquad(integrand, np.pi, 2*np.pi, lambda x: 0, lambda x: np.pi) Print ans -9.86960440109
We will be using tplquad command to integrate
F(x,y,z) = ysin(x) + zcos(x) over the region
0<=x<=π
0<=y<=1
−1<=z<=1
from scipy.integrate import tplquad import numpy as np def integrand(z, y, x): return y * np.sin(x) + z * np.cos(x) ans, err = tplquad(integrand, 0, np.pi, # x limits lambda x: 0, lambda x: 1, # y limits lambda x,y: -1, lambda x,y: 1) # z limits print ans 2.0
Summary
Scipy.integrate provides the same basic functions as Matlab does. The syntax differs specifically for such simple examples. You should know these functions for Integration in Python.
Brief About SciPy
SciPy is basically the Python’s scientific library that is an open-source, BSD licensed library for mathematics as well as engineering.
It totally depends on NumPy that offers convenience as well as fast N-dimensional array manipulation. The prime reason for developing the SciPy library is it must operate along with NumPy arrays.
It also offers numbers of easy to use as well as efficient mathematical practices like a routine for integration as well as optimization.
So in this tutorial, we will be covering the fundamentals of SciPy as well as the ways on how to deal with various modules.
Audience
This tutorial is all about helping the readers who are willing to understand the basic features with various functions of SciPy. It will help in Python Integration Testing.
After the completion of this tutorial, you will be gaining the basic level of expertise and you can gain a higher level of expertise through proper practices.
Prerequisites
Now before going through different concepts that are provided in this tutorial, it is being understood that you have some sort of idea about Python.
Along with this, it would be even more helpful, if you also have a basic level of knowledge of various other programming languages.
The SciPy library depends on the NumPy library. This is why you must have little understanding of NumPy as well.
SciPy Integrate
When you are not able to Python Integration function analytically or you are facing difficulty in integrating analytically, you generally turn into numerical integration methods.
SciPy has various methods for performing numerical integration. Many of them are seen in the scipy.integrate library. Following is the list of commonly used functions.
Sr No. | Function & Description |
1 | quad
Single integration |
2 | dblquad
Double integration |
3 | tplquad
Triple integration |
4 | nquad
n-fold multiple integration |
5 | fixed_quad
Gaussian quadrature, order n |
6 | quadrature
Gaussian quadrature to tolerance |
7 | romberg
Romberg integration |
8 | trapz
Trapezoidal rule |
9 | cumtrapz
Trapezoidal rule to cumulatively compute integral |
10 | simps
Simpson’s rule |
11 | romb
Romberg integration |
12 | polyint
Analytical polynomial integration (NumPy) |
13 | poly1d
Helper function for polyint (NumPy) |
Single Integrals
The Quad function is the powerhouse of integral functions of SciPy. The numerical integration is also known as quadrature.
It is also the default choice to perform single integrals of a function known as f(x) on a given fixed range from point a to b.
$$int_{a}^{b} f(x)dx$$
The general form of quad is scipy.integrate.uad(f,a,b). Here ‘f’ is the name of the function that is to be integrated. On the other hand, ‘a’, as well as ‘b’, are the upper as well as lower limits respectively.
Now let us look at the example of the Gaussian function that is integrated on a range of 0 and 1.
We have to define the function → $f(x) = e^{-x^2}$. This can be performed by using lambda expression and then apply the quad method on it.
import scipy.integrate from numpy import exp f= lambda x:exp(-x**2) i = scipy.integrate.quad(f, 0, 1) print I
The above-mentioned program will be providing the following output.
(0.7468241328124271, 8.291413475940725e-15)
The quad function offers the two values and out of this, the first number is the value of integral as well as the second number is the expected absolute error in the value of integral.
As the quad needs the functions in the form of the first argument, so we can’t pass exp as an argument.
The Quad function accepts both positive as well as negative infinity in the form of limits. It is able to integrate standard predefined NumPy functions of a single variable like exp, sin as well as cos.
Read also: R vs Python: Choosing the Best Option for Machine Learning and Data Science
Double Integrals
The general way of dblquad is scipy.dblquad (func, a, b, gfun, hfun), where, the term ‘func’ is all about the function that is to be integrated, the points ‘a’, as well as ‘b’, are the lower as well as the upper limits of the x variable.
On the other hand, gfun as well as hfun are the names of the functions which define the upper as well as lower limits of the y variable.
Now let us perform the double integral method.
$$int_{0}^{1/2} dy int_{0}^{sqrt{1-4y^2}} 16xy :dx$$
We will be defining the functions named f, g, as well as h by using the lambda expressions. Even if the expressions g and h are constants, you have to define it as functions.
import scipy.integrate from numpy import exp from math import sqrt f = lambda x, y : 16*x*y g = lambda x : 0 h = lambda y : sqrt(1-4*y**2) i = scipy.integrate.dblquad(f, 0, 0.5, g, h) print i
The above-mentioned function will provide the following result
(0.5, 1.7092350012594845e-14)
As per the routines that are described above, scipy.integrate has various other integration routines which include nquad that performs n-fold multiple integrations and different routines that implement integration algorithms.
But quad, as well as dblquad, will be meeting most of the needs for Integration in Python.
Python also improves a passionate and massive community of enthusiasts as well as coders which makes it easy in finding the programmers.
It helps in ensuring a set of new tools as well as libraries and continuous refinement for ensuring.
This can be done easily as Python solutions are mature enough and it benefits everyone who makes use of it.
We hope you had a great time reading this article and it proves to be of great benefit for any Python Web Development Company in the near future. Thank You.!
Your information is safe with us. eSparkBiz guarantees 100% data security. We don’t use emails for spamming.