Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn Simple Arithmetic

NumPy provides built-in functions for performing simple arithmetic operations on arrays efficiently. These operations include addition, subtraction, multiplication, division, and exponentiation. Here's how to perform these operations using NumPy:


Addition: Use numpy.add() or the + operator.

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Using numpy.add()
result_add = np.add(arr1, arr2)
# Using the + operator
result_add_operator = arr1 + arr2
print("Using numpy.add():", result_add) b
print("Using + operator:", result_add_operator)


Subtraction Use numpy.subtract() or the - operator.

import numpy as np
arr1 = np.array([4, 5, 6])
arr2 = np.array([1, 2, 3])
# Using numpy.subtract()
result_subtract = np.subtract(arr1, arr2)
# Using the - operator
result_subtract_operator = arr1 - arr2
print("Using numpy.subtract():", result_subtract)
print("Using - operator:", result_subtract_operator)


Multiplication: Use numpy.multiply() or the * operator.

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Using numpy.multiply()
result_multiply = np.multiply(arr1, arr2)
# Using the * operator
result_multiply_operator = arr1 * arr2
print("Using numpy.multiply():", result_multiply)
print("Using * operator:", result_multiply_operator)


Division :Use numpy.divide() or the / operator.

import numpy as np
arr1 = np.array([6, 12, 18])
arr2 = np.array([2, 3, 6])
# Using numpy.divide()
result_divide = np.divide(arr1, arr2)
# Using the / operator
result_divide_operator = arr1 / arr2
print("Using numpy.divide():", result_divide)
print("Using / operator:", result_divide_operator)


Exponentiation: Use numpy.power() or the ** operator.

import numpy as np
arr = np.array([2, 3, 4])
# Using numpy.power()
result_power = np.power(arr, 2)
# Using the ** operator
result_power_operator = arr ** 2
print("Using numpy.power():", result_power)
print("Using ** operator:", result_power_operator)


These are some of the basic arithmetic operations you can perform on NumPy arrays. NumPy provides efficient implementations for these operations, making it suitable for numerical computations and scientific computing tasks.







Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java