## NumPy–Functions

Numpy is a python package used for scientific computing. So certainly, it supports a vast variety of functions used for computation. The various functions supported by numpy are mathematical, financial, universal, windows, and logical functions. Universal functions are used for array broadcasting, typecasting, and several other standard features. While windows functions are used in signal processing. We will be learning mathematical functions in detail in this article.

## Mathematical Functions in NumPy

Numpy is written purely in C language. Hence, its mathematical functions are closely associated with functions present is math.h library in C.

1. ## Arithmetic Functions

Function Description
reciprocal(arr) Returns reciprocal of elements of the argument array
negative(arr) Returns numerical negative of elements of an array
multiply(arr1,arr2,…) Multiply arrays element wise
divide(arr1,arr2) Divide arrays element wise
power(arr1,arr2) Return the first array with its each of its elements raised to the power of elements in the second array (element wise)
subtract(arr1,arr2,…) Subtract arrays element wise
true_divide(arr1,arr2) Returns true_divide of an array element wise
floor_divide(arr1,arr2) Returns floor after dividing an array element wise
float_power(arr1,arr2) Return the first array with its each of its elements raised to the power of elements in the second array (elementwise)
fmod(arr1,arr2) Returns floor of the remainder after division elementwise
mod(arr1,arr2) Returns remainder after division elementwise
remainder(arr1,arr2) Returns remainder after division elementwise
divmod(arr1,arr2) Returns remainder and quotient after division elementwise

The above-mentioned operations can be performed in the following ways :

In the given code snippet, we try to do some basic operations on the arguments, array a and array b.

## Code:

```import numpy as np
a = np.array([10,20,30])
b= np.array([1,2,3])
print("multiplication of a and b :",np.multiply(a,b))
print("subtraction of a and b :",np.subtract(a,b))
print("a raised to b is:",np.power(a,b))
```

## Output:

In this code snippet, we try to perform division and related operations on the arguments, array a and array b. We can notice the difference between mod, remainder, divmod and simple division.

## Code:

```import numpy as np
a = np.array([10,20,30])
b= np.array([2,3,4])
print("division of a and b :",np.divide(a,b))
print("true division of a  :",np.true_divide(a,b))
print("floor_division of a and b :",np.floor_divide(a,b))
print("float_power of a raised to b :",np.float_power(a,b))
print("fmod of a and b :",np.fmod(a,b))
print("mod of a and b :",np.mod(a,b))
print("quotient and remainder of a and b :",np.divmod(a,b))
print("remainders when a/b :",np.remainder(a,b))
```

2. ## Trigonometric Functions

Function Description
sin(arr) Returns trigonometric sine element wise
cos(arr) Returns trigonometric cos element wise
tan(arr) Returns trigonometric tan element wise
arcsin(arr) Returns trigonometric inverse sine element wise
arccos(arr) Returns trigonometric inverse cosine element wise
arctan(arr) Returns trigonometric inverse tan element wise
hypot(a,b) Returns hypotenuse of a right triangle with perpendicular and base as arguments
degrees(arr)

Covert input angles from radians to degrees

Covert input angles from degrees to radians

Here is an example of how to use trigonometric functions.

## Code:

```import numpy as np
angles = np.array([0,np.pi/2, np.pi])     -----> input array angles
sin_angles = np.sin(angles)
cosine_angles = np.cos(angles)
tan_angles = np.tan(angles)
print("sin of angles:",sin_angles)
print("cosine of angles:",cosine_angles)
print("tan of angles:",tan_angles)
```

3. ## Logarithmic and Exponential Functions

Function Description
exp(arr) Returns exponential of an input array element wise
expm1(arr) Returns exponential exp(x)-1 of an input array element wise
exp2(arr) Returns exponential 2**x of all elements in an array
log(arr) Returns natural log of an input array element wise
log10(arr) Returns log base 10 of an input array element wise
log2(arr) Returns log base 2 of an input array element wise
logaddexp(arr) Returns logarithm of the sum of exponentiations of all inputs
logaddexp2(arr) Returns logarithm of the sum of exponentiations of the inputs in base 2

Here is an example of using logarithmic functions:

## Code:

```import numpy as np
a = np.array([1,2,3,4,5])
a_log = np.log(a)
a_exp = np.exp(a)
print("log of input array a is:",a_log)
print("exponent of input array a is:",a_exp)
```

4. ## Rounding Functions

Function Description
around(arr,decimal) Rounds the elements of an input array upto given decimal places
round_(arr,decimal) Rounds the elements of an input array upto given decimal places
rint(arr) Round the elements of an input array to the nearest integer towards zero
fix(arr) Round the elements of an input array to the nearest integer towards zero
floor(arr) Returns floor of input array element wise
ceil(arr) Returns ceiling of input array element wise
trunc(arr) Return the truncated value of an input array element wise

Example of using rounding functions with numpy arrays:

## Code:

```importnumpy as np
a = np.array([1.23,4.165,3.8245])
rounded_a = np.round_(a,2)
print(rounded_a)
```

## Code:

```floor_a = np.floor(a)
print(floor_a)
```

5. ## Miscellaneous Functions

Function Description
sqrt(arr) Returns the square root of an input array element wise
cbrt(arr) Returns cube root of an input array element wise
absolute(arr) Returns absolute value each element in an input array
maximum(arr1,arr2,…) Returns element wise maximum of the input arrays
minimum(arr1,arr2,…) Returns element wise minimum of the input arrays
interp(arr, xp, fp) Calculates one-dimensional linear interpolation
convolve(arr, v) Returns linear convolution of two one-dimensional sequences
clip(arr, arr_min, arr_max) Limits the values in an input array

Some examples using the above functions.

## Code:

```import numpy as np
a = [1,2,3] b = [3,1,2] maximum_elementwise = np.maximum(a,b)
print("maxima are:",maximum_elementwise)
```

## Code:

```import numpy as np
a = [1,2,3] b = [3,1,2] limiting_a = np.clip(a,0,2)
print("limiting a between 0 and 2:",limiting_a)
```

### Output:

Apply now for Advanced Python Training Course