×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Add Euler Mascheroni Constant to Each Element of a NumPy Array

By IncludeHelp Last updated : March 8, 2024

The Euler-Mascheroni constant or γ (gamma) is a mathematical constant value that originated from the experiments over harmonic series and logarithmic functions.

Problem Statement

Given a NumPy array, we are going to learn how to Euler-Mascheroni constant for each element of the array.

Adding Euler Mascheroni Constant to Each Element of a NumPy Array

To add the Euler-Mascheroni constant value to each element of the array, we may follow two simple approaches. First, we can directly add the result of np.euler_gamma to the original array. The np.euler_gamma constant returns the universally accepted value of the Euler-Mascheroni constant which is approximately 0.57721.

Secondly, we can create a complete array with only the Euler-Mascheroni constant and add this array to the original array by using the np.add() function.

Code to Add Euler Mascheroni Constant to Each Element of a NumPy Array

# Importing numpy
import numpy as np

# Creating an array
arr = np.array([1, 2, 3, 4, 5])
# Display original array
print("Original array", arr, "\n")

# Using for loop to add constant
arr = arr + np.euler_gamma

# Display result
print("Result:\n", arr)

Output

Original array [1 2 3 4 5] 

Result:
 [1.57721566 2.57721566 3.57721566 4.57721566 5.57721566]

Code to Create an Array from np.euler_gamma Result

# Importing numpy
import numpy as np

# Creating an array from np.eular_gamma
arr = np.zeros(5) + np.euler_gamma

# Creating an array of random values
arr2 = np.array([4, 6, 4, 2, 6])

# Display original array
print("Original array", arr2, "\n")

# Adding eular_gamma values to each value of arr2
res = np.add(arr, arr2)

# Display result
print("Result:\n", res)

Output

Original array [4 6 4 2 6] 

Result:
 [4.57721566 6.57721566 4.57721566 2.57721566 6.57721566]

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python NumPy Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.