w3resource

NumPy: Mathematics Exercises, Practice, Solution

NumPy Mathematics [41 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

NumPy Math

1. Write a NumPy program to add, subtract, multiply, divide arguments element-wise.
Expected Output:
Add:
5.0
Subtract:
-3.0
Multiply:
4.0
Divide:
0.25
Click me to see the sample solution

2. Write a NumPy program to compute logarithm of the sum of exponentiations of the inputs, sum of exponentiations of the inputs in base-2.
Expected Output:
Logarithm of the sum of exponentiations:
-113.876491681
Logarithm of the sum of exponentiations of the inputs in base-2:
-113.599555228
Click me to see the sample solution

3. Write a NumPy program to get true division of the element-wise array inputs.
Expected Output:
Original array:
[0 1 2 3 4 5 6 7 8 9]
Division of the array inputs, element-wise:
[ 0. 0.33333333 0.66666667 1. 1.33333333 1.6666666
7 2. 2.33333333 2.66666667 3. ]
Click me to see the sample solution

4. Write a NumPy program to get the largest integer smaller or equal to the division of the inputs.
Expected Output:
Original array:
[1.0, 2.0, 3.0, 4.0]
Largest integer smaller or equal to the division of the inputs:
[ 0. 1. 2. 2.]
Click me to see the sample solution

5. Write a NumPy program to get the powers of an array values element-wise.
Note: First array elements raised to powers from second array
Expected Output:
Original array
[0 1 2 3 4 5 6]
First array elements raised to powers from second array, element-wise:
[ 0 1 8 27 64 125 216]
Click me to see the sample solution

6. Write a NumPy program to get the element-wise remainder of an array of division.
Sample Output:
Original array:
[0 1 2 3 4 5 6]
Element-wise remainder of division:
[0 1 2 3 4 0 1]
Click me to see the sample solution

7. Write a NumPy program to calculate the absolute value element-wise.
Sample output:
Original array:
[ -10.2 122.2 0.2]
Element-wise absolute value:
[ 10.2 122.2 0.2]
Click me to see the sample solution

8. Write a NumPy program to round array elements to the given number of decimals.
Sample Output:
[ 1. 2. 2.]
[ 0.3 0.5 0.6]
[ 0. 2. 2. 4. 4.]
Click me to see the sample solution

9. Write a NumPy program to round elements of the array to the nearest integer.
Sample Output:
Original array:
[-0.7 -1.5 -1.7 0.3 1.5 1.8 2. ]
Round elements of the array to the nearest integer:
[-1. -2. -2. 0. 2. 2. 2.]
Click me to see the sample solution

10. Write a NumPy program to get the floor, ceiling and truncated values of the elements of a numpy array.
Sample Output:
Original array:
[-1.6 -1.5 -0.3 0.1 1.4 1.8 2. ]
Floor values of the above array elements:
[-2. -2. -1. 0. 1. 1. 2.]
Ceil values of the above array elements:
[-1. -1. -0. 1. 2. 2. 2.]
Truncated values of the above array elements:
[-1. -1. -0. 0. 1. 1. 2.]
Click me to see the sample solution

11. Write a NumPy program to multiply a 5x3 matrix by a 3x2 matrix and create a real matrix product.
Sample output:
First array:
[[ 0.44349753 0.81043761 0.00771825]
[ 0.64004088 0.86774612 0.19944667]
[ 0.61520091 0.24796788 0.93798297]
[ 0.22156999 0.61318856 0.82348994]
[ 0.91324026 0.13411297 0.00622696]]
Second array:
[[ 0.73873542 0.06448186]
[ 0.90974982 0.06409165]
[ 0.22321268 0.39147412]]
Dot product of two arrays:
[[ 1.06664562 0.08356133]
[ 1.30677176 0.17496452]
[ 0.88942914 0.42275803]
[ 0.90534318 0.37596252]
[ 0.79804212 0.06992065]]
Click me to see the sample solution

12. Write a NumPy program to multiply a matrix by another matrix of complex numbers and create a new matrix of complex numbers.
Sample output:
First array:
[ 1.+2.j 3.+4.j]
Second array:
[ 5.+6.j 7.+8.j]
Product of above two arrays:
(70-8j)
Click me to see the sample solution

13. Write a NumPy program to create an inner product of two arrays.
Sample Output:
Array x:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
Array y:
[0 1 2 3]
Inner of x and y arrays:
[[ 14 38 62]
[ 86 110 134]]
Click me to see the sample solution

14. Write a NumPy program to generate inner, outer, and cross products of matrices and vectors.
Expected Output:
Matrices and vectors.
x:
[ 1. 4. 0.]
y:
[ 2. 2. 1.]
Inner product of x and y:
10.0
Outer product of x and y:
[[ 2. 2. 1.]
[ 8. 8. 4.]
[ 0. 0. 0.]]
Cross product of x and y:
[ 4. -1. -6.]
Click me to see the sample solution

15. Write a NumPy program to generate a matrix product of two arrays.
Sample Output:
Matrices and vectors.
x:
[[1, 0], [1, 1]]
y:
[[3, 1], [2, 2]]
Matrix product of above two arrays:
[[3 1]
[5 3]]
Click me to see the sample solution

16. Write a NumPy program to find the roots of the following polynomials.
a) x2 - 4x + 7.
b) x4 - 11x3 + 9x2 + 11x ? 10
Sample output:
Roots of the first polynomial:
[ 1. 1.]
Roots of the second polynomial:
[ 11.04461946+0.j -0.87114210+0.j 0.91326132+0.4531004j
0.91326132-0.4531004j]
Click me to see the sample solution

17. Write a NumPy program to compute the following polynomial values.
Sample output:
Polynomial value when x = 2:
1
Polynomial value when x = 3:
-142
Click me to see the sample solution

18. Write a NumPy program to add one polynomial to another, subtract one polynomial from another, multiply one polynomial by another and divide one polynomial by another.
Sample output:
Add one polynomial to another:
[ 40. 60. 80.]
Subtract one polynomial from another:
[-20. -20. -20.]
Multiply one polynomial by another:
[ 300. 1000. 2200. 2200. 1500.]
Divide one polynomial by another:
(array([ 0.6]), array([-8., -4.]))
Click me to see the sample solution

19. Write a NumPy program to calculate mean across dimension, in a 2D numpy array.
Sample output:
Original array:
[[10 30]
[20 60]]
Mean of each column:
[ 15. 45.]
Mean of each row:
[ 20. 40.]
Click me to see the sample solution

20. Write a NumPy program to create a random array with 1000 elements and compute the average, variance, standard deviation of the array elements.
Sample output:
Average of the array elements:
-0.0255137240796
Standard deviation of the array elements:
0.984398282476
Variance of the array elements:
0.969039978542
Click me to see the sample solution

21. Write a NumPy program to compute the trigonometric sine, cosine and tangent array of angles given in degrees.
Sample output:
sine: array of angles given in degrees
[ 0. 0.5 0.70710678 0.8660254 1. ]
cosine: array of angles given in degrees
[ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
6.12323400e-17]
tangent: array of angles given in degrees
[ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
1.63312394e+16]
Click me to see the sample solution

22. Write a NumPy program to calculate inverse sine, inverse cosine, and inverse tangent for all elements in a given array.
Sample output:
Inverse sine: [-1.57079633 0. 1.57079633]
Inverse cosine: [3.14159265 1.57079633 0. ]
Inverse tangent: [-0.78539816 0. 0.78539816]
Click me to see the sample solution

23. Write a NumPy program to convert angles from radians to degrees for all elements in a given array.
Input: [-np.pi, -np.pi/2, np.pi/2, np.pi]
Sample output:
[-180. -90. 90. 180.]
Click me to see the sample solution

24. Write a NumPy program to convert angles from degrees to radians for all elements in a given array.
Input: Input: [-180., -90., 90., 180.]
Sample output:
[-3.14159265 -1.57079633 1.57079633 3.14159265]
Click me to see the sample solution

25. Write a NumPy program to calculate hyperbolic sine, hyperbolic cosine, and hyperbolic tangent for all elements in a given array.
Input: Input: Input: [-1., 0, 1.]
Sample output:
[-1.17520119 0. 1.17520119]
[1.54308063 1. 1.54308063]
[-0.76159416 0. 0.76159416]
Click me to see the sample solution

26. Write a NumPy program to calculate round, floor, ceiling, truncated and round (to the given number of decimals) of the input, element-wise of a given array.
Sample output:
Original array:
[ 3.1 3.5 4.5 2.9 -3.1 -3.5 -5.9]
around: [ 3. 4. 4. 3. -3. -4. -6.]
floor: [ 3. 3. 4. 2. -4. -4. -6.]
ceil: [ 4. 4. 5. 3. -3. -3. -5.]
trunc: [ 3. 3. 4. 2. -3. -3. -5.]
round: [3.0, 4.0, 4.0, 3.0, -3.0, -4.0, -6.0]
Click me to see the sample solution

27. Write a NumPy program to calculate cumulative sum of the elements along a given axis, sum over rows for each of the 3 columns and sum over columns for each of the 2 rows of a given 3x3 array.
Sample output:
Original array:
[[1 2 3]
[4 5 6]]
Cumulative sum of the elements along a given axis:
[ 1 3 6 10 15 21]
Sum over rows for each of the 3 columns:
[[1 2 3]
[5 7 9]]
Sum over columns for each of the 2 rows:
[[ 1 3 6]
[ 4 9 15]]
Click me to see the sample solution

28. Write a NumPy program to calculate cumulative product of the elements along a given axis, sum over rows for each of the 3 columns and product over columns for each of the 2 rows of a given 3x3 array.
Sample output:
Original array:
[[1 2 3]
[4 5 6]]
Cumulative product of the elements along a given axis:
[ 1 2 6 24 120 720]
Product over rows for each of the 3 columns:
[[ 1 2 3]
[ 4 10 18]]
Product over columns for each of the 2 rows:
[[ 1 2 6]
[ 4 20 120]]
Click me to see the sample solution

29. Write a NumPy program to calculate the difference between neighboring elements, element-wise of a given array.
Sample output:
Original array:
[1 3 5 7 0]
Difference between neighboring elements, element-wise of the said array.
[ 2 2 2 -7]
Click me to see the sample solution

30. Write a NumPy program to calculate the difference between neighboring elements, element-wise, and prepend [0, 0] and append[200] to a given array.
Sample output:
Original array:
[1 3 5 7 0]
Difference between neighboring elements, element-wise, and prepend [0, 0] and append[200] to the said array:
[ 0 0 2 2 2 -7 200]
Click me to see the sample solution

31. Write a NumPy program to compute ex, element-wise of a given array.
Sample output:
Original array:
[1. 2. 3. 4.]
e^x, element-wise of the said:
[ 2.7182817 7.389056 20.085537 54.59815 ]
Click me to see the sample solution

32. Write a NumPy program to calculate exp(x) - 1 for all elements in a given array.
Sample output:
Original array: [1. 2. 3. 4.] exp(x)-1 for all elements of the said array: [ 1.7182817 6.389056 19.085537 53.59815 ]
Click me to see the sample solution

33. Write a NumPy program to calculate 2p for all elements in a given array.
Sample output:
Original array:
[1. 2. 3. 4.]
2^p for all the elements of the said array:
[ 2. 4. 8. 16.]
Click me to see the sample solution

34. Write a NumPy program to compute natural, base 10, and base 2 logarithms for all elements in a given array.
Sample output:
Original array:
Original array:
[1. 2.71828183 7.3890561 ]
Natural log = [0. 1. 2.]
Common log = [0. 0.43429448 0.86858896]
Base 2 log = [0. 1.44269504 2.88539008]
Click me to see the sample solution

35. Write a NumPy program to compute the natural logarithm of one plus each element of a given array in floating-point accuracy.
Sample output:
Original array:
[1.e-099 1.e-100]
Natural logarithm of one plus each element:
[1.e-099 1.e-100]
Click me to see the sample solution

36. Write a NumPy program to check element-wise True/False of a given array where signbit is set.
Sample array: [-4, -3, -2, -1, 0, 1, 2, 3, 4]
Sample output:
Original array:
[-4 -3 -2 -1 0 1 2 3 4]
[ True True True True False False False False False]
Click me to see the sample solution

37. Write a NumPy program to change the sign of a given array to that of a given array, element-wise.
Sample output:
Original array:
[-1 0 1 2]
Sign of x1 to that of x2, element-wise:
[-1. 0. 1. 2.]
Click me to see the sample solution

38. Write a NumPy program to compute numerical negative value for all elements in a given array.
Sample output:
Original array:
[ 0 1 -1]
Numerical negative value for all elements of the said array:
[ 0 -1 1]
Click me to see the sample solution

39. Write a NumPy program to compute the reciprocal for all elements in a given array.
Sample output:
Original array:
[1. 2. 0.2 0.3]
Reciprocal for all elements of the said array:
[1. 0.5 5. 3.33333333]
Click me to see the sample solution

40. Write a NumPy program to compute xy, element-wise where x, y are two given arrays.
Sample output:
Array1:
[[1 2]
[3 4]]
Array1:
[[1 2]
[1 2]]
Result- x^y:
[[ 1 4]
[ 3 16]]
Click me to see the sample solution

41. Write a NumPy program to compute an element-wise indication of the sign for all elements in a given array.
Sample output:
Original array;
[ 1 3 5 0 -1 -7 0 5]
Element-wise indication of the sign for all elements of the said array:
[ 1 1 1 0 -1 -1 0 1]
Click me to see the sample solution

Python-Numpy Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/numpy/python-numpy-math.php