Python Python Tutorial File Handling NumPy Tutorial NumPy Random NumPy ufunc Pandas Tutorial Pandas Cleaning Data Pandas Correlations Pandas Plotting SciPy Tutorial



NumPy ufunc Simple Arithmetic

NumPy is a popular Python library used for scientific computing. It provides a powerful array object and a large collection of mathematical functions to operate on these arrays. One of the most important features of NumPy is its universal functions (ufuncs) which are used to perform element-wise operations on arrays. In this article, we will discuss NumPy ufunc simple arithmetic.

NumPy ufunc simple arithmetic includes basic arithmetic operations such as addition, subtraction, multiplication, and division. These operations can be performed on arrays of any shape and size. The ufunc functions are designed to work efficiently on large arrays and provide a significant performance boost over traditional Python operations.

Code Examples

Let's take a look at some code examples to understand how NumPy ufunc simple arithmetic works:

Addition

The add() function is used to perform element-wise addition of two arrays:

<pre>
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

c = np.add(a, b)

print(c)
</pre>

The output of the above code will be:

[5 7 9]

Subtraction

The subtract() function is used to perform element-wise subtraction of two arrays:

<pre>
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

c = np.subtract(a, b)

print(c)
</pre>

The output of the above code will be:

[-3 -3 -3]

Multiplication

The multiply() function is used to perform element-wise multiplication of two arrays:

<pre>
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

c = np.multiply(a, b)

print(c)
</pre>

The output of the above code will be:

[ 4 10 18]

Division

The divide() function is used to perform element-wise division of two arrays:

<pre>
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

c = np.divide(a, b)

print(c)
</pre>

The output of the above code will be:

[0.25 0.4  0.5 ]

Conclusion

NumPy ufunc simple arithmetic provides a fast and efficient way to perform basic arithmetic operations on arrays. These operations can be performed on arrays of any shape and size, making NumPy a powerful tool for scientific computing.

Reference

NumPy documentation: https://numpy.org/doc/stable/reference/ufuncs.html

Activity