exp
means exponential function
exp
in math module
: https://docs.python.org/2/library/math.html
exp
in numpy module
: http://docs.scipy.org/doc/numpy/reference/generated/numpy.exp.html
Why do numpy
creators introduce this function again?
math is part of the standard python library. It provides functions for basic mathematical operations as well as some commonly used constants. numpy on the other hand is a third party package geared towards scientific computing. It is the defacto package for numerical and vector operations in python.
The exp function in NumPy is used to compute the exponent of all values present in the given array. e refers to Euler's constant. It has an approximate value of 2.718.
The math. exp() method returns E raised to the power of x (Ex). 'E' is the base of the natural system of logarithms (approximately 2.718282) and x is the number passed to it.
NumPy contains a large number of various mathematical operations. NumPy provides standard trigonometric functions, functions for arithmetic operations, handling complex numbers, etc. NumPy has standard trigonometric functions which return trigonometric ratios for a given angle in radians.
The math.exp
works only for scalars as EdChum mentions. Whereas numpy.exp
will work for arrays.
Example:
>>> import math >>> import numpy as np >>> x = [1.,2.,3.,4.,5.] >>> math.exp(x) Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> math.exp(x) TypeError: a float is required >>> np.exp(x) array([ 2.71828183, 7.3890561 , 20.08553692, 54.59815003, 148.4131591 ]) >>>
It is the same case for other math
functions.
>>> math.sin(x) Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> math.sin(x) TypeError: a float is required >>> np.sin(x) array([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 , -0.95892427]) >>>
Also refer to THIS ANSWER to check out how numpy
is faster than math
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With