Numpy mathematical operations on array using Python:

              Mathematical operations on array uses matrices. It uses Numpy to perform element-wise arithmetic, aggregate functions and linear algebra functions in effective ways.

  • ·       This program starts with including numpy object.
  • ·       Create two arrays for input purpose.
  • ·       Next,element wise operations like Addition, Subtraction ,Multiplication and Division.
  • ·       A scalar function is done by multiplying the array elements with 5.
  • ·       Aggregate functions like sum, min, max and mean are done using built-in functions.
  • ·       The advanced functions like square root and exponential are also done.

Python Code:

import numpy as np

# Create arrays

s_arr1 = np.array([1, 2, 3, 4])

s_arr2 = np.array([10, 20, 30, 40])

print("The original arrays:")

print("Array1:",s_arr1)

print("Array2:",s_arr2)

# Element-wise operations

print("Element-wise Operations:")

print("Addition:", s_arr1 + s_arr2)       

print("Subtraction:", s_arr2 - s_arr1)   

print("Multiplication:", s_arr1 * s_arr2) 

print("Division:", s_arr2 / s_arr1)       

# Scalar operations

print("Scalar Operations:")

print("Multiply by 5:", s_arr1 * 5)    

# Aggregate functions

print("Aggregate functions:")

print("Sum:", np.sum(s_arr1))           

print("Mean:", np.mean(s_arr1))         

print("Max:", np.max(s_arr2))           

print("Min:", np.min(s_arr2))           

# Advanced operations

print("Advanced operations:")

print("Dot product:", np.dot(s_arr1, s_arr2))  

print("Square root:", np.sqrt(s_arr1))      

print("Exponential:", np.exp(s_arr1))        

Output:

The original arrays:

Array1: [1 2 3 4]

Array2: [10 20 30 40]

Element-wise Operations:

Addition: [11 22 33 44]

Subtraction: [ 9 18 27 36]

Multiplication: [ 10  40  90 160]

Division: [10. 10. 10. 10.]

Scalar Operations:

Multiply by 5: [ 5 10 15 20]

Aggregate functions:

Sum: 10

Mean: 2.5

Max: 40

Min: 10

Advanced operations:

Dot product: 300

Square root: [1.         1.41421356 1.73205081 2.        ]

Exponential: [ 2.71828183  7.3890561  20.08553692 54.59815003]

Yes, the python code for implementing Numpy mathematical operations on array was done. Hope, this code is useful and understood. Keep Coding!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

Employee record management in C

Datatypes and Variables in java script