0% found this document useful (0 votes)
2 views2 pages

NumPy Arithmetic Array Operations

The document provides an overview of arithmetic operations in NumPy, including addition, subtraction, multiplication, division, exponentiation, and modulus. It explains how to perform these operations using both operators and built-in functions, with examples for each operation. The document highlights that both methods yield the same results for element-wise calculations on arrays.

Uploaded by

anya023bteceai25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

NumPy Arithmetic Array Operations

The document provides an overview of arithmetic operations in NumPy, including addition, subtraction, multiplication, division, exponentiation, and modulus. It explains how to perform these operations using both operators and built-in functions, with examples for each operation. The document highlights that both methods yield the same results for element-wise calculations on arrays.

Uploaded by

anya023bteceai25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NumPy Arithmetic Array Operations

NumPy provides a wide range of operations that # using the add() function
can perform on arrays, including arithmetic result2 = [Link](first_array, second_array)
operations. print("Using the add() function:",result2)
NumPy's arithmetic operations are widely used due Output
to their ability to perform simple and efficient
calculations on arrays. Using the + operator: [ 3 7 11 15]
In this tutorial, we will explore some commonly Using the add() function: [ 3 7 11 15]
used arithmetic operations in NumPy and learn
how to use them to manipulate arrays. In the above example, first we created two arrays
named: first_array and second_array.
List of Arithmetic Operations Then, we used the + operator and
Here's a list of various arithmetic operations along
the add() function to perform element-wise
with their associated operators and built-in
functions: addition respectively.
As we can see, both + and add() give the same
Element- result.
Opera Functi
wise
tor on
Operation NumPy Array Element-Wise Subtraction
In NumPy, we can either use the - operator or
the subtract() function to perform element-wise
Addition + add()
subtraction between two NumPy arrays. For
example,
Subtractio subtra import numpy as np
-
n ct()
first_array = [Link]([3, 9, 27, 81])
second_array = [Link]([2, 4, 8, 16])
Multiplica multip
*
tion ly() # using the - operator
result1 = first_array - second_array
print("Using the - operator:",result1)
divide(
Division /
) # using the subtract() function
result2 = [Link](first_array, second_array)
print("Using the subtract() function:",result2)
Exponenti power( Output
**
ation )
Using the - operator: [ 1 5 19 65]
Modulus % mod() Using the subtract() function: [ 1 5 19 65]

To perform each operation, we can either use the Here, we have performed subtraction
associated operator or built-in functions. For between first_array and second_array using both
example, to perform addition, we can either use the - operator and the subtract() function.
the + operator or the add() built-in function.
Next, we will see examples of each of these NumPy Array Element-Wise Multiplication
operations. For element-wise multiplication, we can use
the * operator or the multiply() function. For
NumPy Array Element-Wise Addition example,
As mentioned earlier, we can use the import numpy as np
both + operator and the built-in function add() to
perform element-wise addition between two first_array = [Link]([1, 3, 5, 7])
NumPy arrays. For example, second_array = [Link]([2, 4, 6, 8])
import numpy as np
# using the * operator
first_array = [Link]([1, 3, 5, 7]) result1 = first_array * second_array
second_array = [Link]([2, 4, 6, 8]) print("Using the * operator:",result1)

# using the + operator # using the multiply() function


result1 = first_array + second_array result2 = [Link](first_array, second_array)
print("Using the + operator:",result1) print("Using the multiply() function:",result2)
In the above example, we have used
Output the ** operator and the power() function to
perform exponentiation operation respectively.
Using the * operator: [ 2 12 30 56] Here, ** and power() both raise each element
Using the multiply() function: [ 2 12 30 56] of array1 to the power of 2.

Here, we have used * and multiply() to NumPy Array Element-Wise Modulus


demonstrate two ways of multiplying the two We can perform a modulus operation in NumPy
arrays element-wise. arrays using the % operator or the mod() function.
This operation calculates the remainder of element-
wise division between two arrays.
Let's see an example.
import numpy as np
NumPy Array Element-Wise Division
We can use either the / operator or
first_array = [Link]([9, 10, 20])
the divide() function to perform element-wise second_array = [Link]([2, 5, 7])
division between two numpy arrays. For example,
import numpy as np # using the % operator
result1 = first_array % second_array
first_array = [Link]([1, 2, 3]) print("Using the % operator:",result1)
second_array = [Link]([4, 5, 6])
# using the mod() function
# using the / operator result2 = [Link](first_array, second_array)
result1 = first_array / second_array print("Using the mod() function:",result2)
print("Using the / operator:",result1)
Output
# using the divide() function
result2 = [Link](first_array, second_array) Using the % operator: [1 0 6]
print("Using the divide() function:",result2) Using the mod() function: [1 0 6]
Output
Here, we have performed the element-wise
Using the / operator: [0.25 0.4 0.5 ]
modulus operation using both the % operator and
Using the divide() function: [0.25 0.4 0.5 ]
the mod() function.
Here, we can see both / and divide() give the same
result.

NumPy Array Element-Wise Exponentiation


Array exponentiation refers to raising each element
of an array to a given power.
In NumPy, we can use either the ** operator or
the power() function to perform the element-wise
exponentiation operation. For example,
import numpy as np

array1 = [Link]([1, 2, 3])

# using the ** operator


result1 = array1 ** 2
print("Using the ** operator:",result1)

# using the power() function


result2 = [Link](array1, 2)
print("Using the power() function:",result2)
Output

Using the ** operator: [1 4 9]


Using the power() function: [1 4 9]

You might also like