Python’s numpy.log[]
is a mathematical function that computes
the natural logarithm of an input array’s elements.
The natural logarithm is the inverse of the exponential function, such that log [exp[x]] = x.
Syntax
numpy.log
is declared as shown below:
numpy.log[x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]] =
In the syntax above, x
is the non-optional parameter and the rest are optional parameters.
A universal function [ufunc] is a function that operates on ndarrays in an element-by-element fashion. The
log[]
method is a universal function.
Parameters
The numpy.log[]
method takes the following compulsory parameter:
x
[array-like] - input array.
The numpy.log[]
method takes the following optional parameters:
Return value
numpy.log[]
returns the
element-wise natural logarithm of values in x
. If x
is scalar, the return type is also scalar.
If
x
is a real-valued data type, the return type will also be a real value. If a value cannot be written as a real value, thenNaN
is returned.If
x
is a complex-valued input, thenumpy.log
method has a branch cut [-inf
,0
], and it is continuous above it.
Examples
In the example below, numpy.log[]
computes the natural logarithm of two numbers, a
and b
:
import numpy as np
a = 0.5
b = np.exp[8.9]
print [np.log[a]]
print [np.log[b]]
In the example below, numpy.log[]
computes the element-wise natural logarithm of the array arr1
:
import numpy as np
arr1 = np.array[[1,2,0,3,4]]
print [np.log[arr1]]
CONTRIBUTOR
Umme Ammara
Copyright ©2022 Educative, Inc. All rights reserved
Python can be used just like a pocket calculator. In addition to addition, subtraction, multiplication and division, you can calculate exponents and logarithms with Python. Exponent and logarithm functions are imported from the math module which is part of the Python Standard Library. This means all the functions in the math module are available in any Python installation.
In this short post, you'll learn how to calculate exponents and logarithms in Python.
Exponent and Logarithm Functions¶
The following exponent and logarithm functions can be imported from Python's math module:
log
log10
exp
e
pow[x,y]
sqrt
Note that Python's
logfunction calculates the natural log of a number. Python's
log10function calculates the base-10 log of a number. Python doesn't have an
lnfunction, use
logfor natural logarithms.
Let's try a couple of examples. In the examples below, the >>>
prompt is not meant to be typed. >>>
is used to denote the Python REPL or Python prompt. Alternatively, you can enter these examples into
a Jupyter notebook code cell. Note how the first line of code imports logarithm and power functions from Python's math
module.
>>> from math import log, log10, exp, e, pow, sqrt >>> log[3.0*e**3.4] # note: natural log 4.4986122886681095
Now let's try a problem: A right triangle has side lengths 3 and 4. What is the length of the hypotenuse?
>>> sqrt[3**2 + 4**2] 5.0
The power function pow[]
works like the **
operator. pow[]
raises a number to a power.
Summary¶
The following exponent and logarithm functions are part of Python's math module:
math.e
| Euler's number | mathematical constant $e$ | math.e
| 2.718
|
math.exp[]
| exponent | $e$ raised to a power | math.exp[2.2]
| 9.025
|
math.log[]
| natural logarithm | log base $e$ | math.log[3.1]
| 400
|
math.log10[]
| base 10 logarithm | log base 10 | math.log10[100]
| 2.0
|
math.pow[]
| power | raises a number to a power | math.pow[2,3]
| 8.0
|
math.sqrt[]
| square root | square root of a number | math.sqrt[16]
| 4.0
|
Want to learn more about doing math with Python? Check out my book Problem Solving with Python on Amazon [Affiliate Link]: