Add Euler Mascheroni Constant to Each Element of a NumPy Array – Python

In this tutorial, we will learn how to add the Euler Mascheroni constant to each element of a NumPy array in Python.

Introduction:

>>>Euler-Mascheroni constant also called as “Euler’s constant” which is a mathematical constant. It is denoted by ‘γ‘. 

The numerical value of Euler’s constant is :

0.57721566490153286060651209008240243104215933593992…

It is represented by following expression:

γ = lim(n->∞)(1+1/2+1/3+1/4+……+1/n)-ln(n)

Euler’s constant appears in numerous areas of mathematics, such as number theory, analysis and calculus.

>>>NumPy array is a multi-dimensional array object in the NumPy library, which stands for “Numerical Python”. The NumPy arrays have fixed size of creation and elements in NumPy array are all of the same data type.

Step-By-step process of adding Euler Mascheroni constant to each element of NumPy Array:

step 1: Import NumPy:

import numpy as np

step 2: create an array:

arr = np.array([9,8,7,6,5])

step 3: Import Euler’s Mascheroni constant:

from scipy.constants import euler_gamma

step 4 : Add the constant;

arr_with_gamma=arr+euler_gamma

Uses of adding Euler’s constant to each element of a NumPy Array:

  • Useful in mathematical analysis (ex: integrals or series)
  • Useful in certain algorithms like logarithms and gamma functions.
  • Statistical analysis.
  • Research and experimentations related to number theory, analysis etc.,

Here ‘s an example of adding Euler’s constant to each element of a NumPy array:

import numpy as np
arr = np.array([9,8,7,6,5])
euler_mascheroni = 0.577
res=arr+euler_mascheroni
print(res)

output:

[9.577 8.577 7.577 6.577 5.577]

Methods for adding Euler’s constant to each element of a NumPy Array:

Method 1: List Comprehension

arr_with_gamma = np.array([x + euler-gamma for x in arr])

Method 2: Using Broadcasting

import numpy as np
from spicy.constants import euler_gamma
arr = np.array([9,8,7,6,5])
arr_with_gamma = arr + euler_gamma

By using the above methods we can easily add Euler’s constant to each element of a NumPy array.

Leave a Reply

Your email address will not be published. Required fields are marked *