from math import log
from math import e
print e #自然对数
print log(e) #log函数默认是以e为底
print log(100,10) #以10为底,对100取对数
print log(4,2) #以2为底,对4取对数
from math import log
from math import e
print e #自然对数
print log(e) #log函数默认是以e为底
print log(100,10) #以10为底,对100取对数
print log(4,2) #以2为底,对4取对数
转载于:https://www.cnblogs.com/100thMountain/p/4771201.html
python对数函数
Logarithms are used to depict and represent large numbers. The log is an inverse of the exponent. This article will dive into the Python log() functions. The logarithmic functions of Python help the users to find the log of numbers in a much easier and efficient manner.
对数用于描述和表示大数。 对数是指数的倒数。 本文将深入探讨Python log()函数 。 Python的对数函数可帮助用户以更容易和有效的方式查找数字的对数。
In order to use the functionalities of Log functions, we need to import the math
module using the below statement.
为了使用Log函数的功能,我们需要使用以下语句导入 math
模块。
import math
We all need to take note of the fact that the Python Log functions cannot be accessed directly. We need to use the math
module to access the log functions in the code.
我们都需要注意一个事实,即Python Log函数不能直接访问。 我们需要使用math
模块来访问代码中的日志功能。
Syntax:
句法:
math.log(x)
The math.log(x)
function is used to calculate the natural logarithmic value i.e. log to the base e (Euler’s number) which is about 2.71828, of the parameter value (numeric expression), passed to it.
math.log(x)
函数用于计算自然对数值,即,将传递给它的参数值( 数字表达式 ) 的底数e (欧拉数)记录为约2.71828。
Example:
例:
import math
print("Log value: ", math.log(2))
In the above snippet of code, we are requesting the logarithmic value of 2.
在上面的代码段中,我们要求对数值为2。
Output:
输出:
Log value: 0.6931471805599453
The following are the variants of the basic log function in Python:
以下是Python中基本日志功能的变体:
The math.log2(x)
function is used to calculate the logarithmic value of a numeric expression of base 2.
math.log2(x)
函数用于计算以2为底的数字表达式的对数值 。
Syntax:
句法:
math.log2(numeric expression)
Example:
例:
import math
print ("Log value for base 2: ")
print (math.log2(20))
Output:
输出:
Log value for base 2:
4.321928094887363
The math.log(x,Base)
function calculates the logarithmic value of x i.e. numeric expression for a particular (desired) base value.
math.log(x,Base)
函数计算x的对数值,即特定(所需)基值的数字表达式。
Syntax:
句法:
math.log(numeric_expression,base_value)
This function accepts two arguments:
此函数接受两个参数:
Note: If no base value is provided to the function, the math.log(x,(Base)) acts as a basic log function and calculates the log of the numeric expression to the base e.
注意 :如果没有为函数提供基值 ,则math.log(x,(Base))充当基本对数函数,并计算数字表达式对基e的对数 。
Example:
例:
import math
print ("Log value for base 4 : ")
print (math.log(20,4))
Output:
输出:
Log value for base 4 :
2.1609640474436813
The math.log10(x)
function calculates the logarithmic value of the numeric expression to the base 10.
math.log10(x)
函数将数字表达式的对数值计算为以10为底 。
Syntax:
句法:
math.log10(numeric_expression)
Example:
例:
import math
print ("Log value for base 10: ")
print (math.log10(15))
In the above snippet of code, the logarithmic value of 15 to the base 10 is calculated.
在上面的代码片段中,计算了以10为底的对数值15 。
Output:
输出:
Log value for base 10 :
1.1760912590556813
The math.log1p(x)
function calculates the log(1+x) of a particular input value i.e. x
math.log1p(x)
函数计算特定输入值(即x )的log(1 + x)
Note: math.log1p(1+x) is equivalent to math.log(x)
注意: math.log1p(1 + x)等同于math.log(x)
Syntax:
句法:
math.log1p(numeric_expression)
Example:
例:
import math
print ("Log value(1+15) for x = 15 is: ")
print (math.log1p(15))
In the above snippet of code, the log value of (1+15) for the input expression 15 is calculated.
在上面的代码片段中,计算了输入表达式15的对数(1 + 15)。
Thus, math.log1p(15)
is equivalent to math.log(16)
.
因此, math.log1p(15)
等同于math.log(16)
。
Output:
输出:
Log value(1+15) for x = 15 is:
2.772588722239781
Python NumPy enables us to calculate the natural logarithmic values of the input NumPy array elements simultaneously.
Python NumPy使我们能够同时计算输入NumPy数组元素的自然对数值 。
In order to use the numpy.log() method, we need to import the NumPy module using the below statement.
为了使用numpy.log()方法,我们需要使用以下语句导入NumPy模块 。
import numpy
Syntax:
句法:
numpy.log(input_array)
The numpy.log()
function accepts input array as a parameter and returns the array with the logarithmic value of elements in it.
numpy.log()
函数接受输入数组作为参数,并返回其中元素为对数的数组。
Example:
例:
import numpy as np
inp_arr = [10, 20, 30, 40, 50]
print ("Array input elements:\n", inp_arr)
res_arr = np.log(inp_arr)
print ("Resultant array elements:\n", res_arr)
Output:
输出:
Array input elements:
[10, 20, 30, 40, 50]
Resultant array elements:
[ 2.30258509 2.99573227 3.40119738 3.68887945 3.91202301]
In this article, we have understood the working of Python Log functions and have unveiled the variants of the logarithmic function in Python.
在本文中,我们了解了Python Log函数的工作原理,并揭示了Python中对数函数的变体。
翻译自: https://www.journaldev.com/36109/python-log-function-logarithm
python对数函数
上一期,我们在探讨对数函数性质的时候,我们画了几个非常漂亮的图,这一期,就把画图的python放上来,设对数函数为
y
=
l
o
g
a
x
y=log_a x
y=logax
其中a>0,且a≠1。对应的指数函数为
y
=
a
x
y=a^x
y=ax。现在来分情况画出对数函数的图像
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 17 17:32:49 2020
project name:draw_logarithm_figure
@author: 帅帅de三叔
"""
import math
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist #导入坐标轴加工模块
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
fig=plt.figure(figsize=(6,4)) #新建画布
ax=axisartist.Subplot(fig,111) #使用axisartist.Subplot方法创建一个绘图区对象ax
fig.add_axes(ax) #将绘图区对象添加到画布中
def logarithm_func(x, a=1/2): #定义指数函数
y=math.log(x, a)
return y
X=np.linspace(0.01, 4, 100) #构造自变量组
Y=[logarithm_func(x) for x in X] #求函数值
ax.plot(X, Y, label=r'$0<a<1$') #绘制指数函数
ax.scatter(1, 0, color='red')
plt.legend()
plt.show()
print(max(X), max(Y)) #测试一下自变量最大值和因变量最大值,为后面的坐标轴设置依据
ax.axis[:].set_visible(False) #隐藏原来的实线矩形
ax.axis["x"]=ax.new_floating_axis(0, 0, axis_direction="bottom") #添加x轴
ax.axis["y"]=ax.new_floating_axis(1, 0, axis_direction="bottom") #添加y轴
ax.axis["x"].set_axisline_style("-|>", size=1.0) #给x坐标轴加箭头
ax.axis["y"].set_axisline_style("-|>", size=1.0) #给y坐标轴加箭头
ax.annotate(s='x', xy=(max(X), 0), xytext=(max(X)+1, 0.3)) #标注x轴
ax.annotate(s='y', xy=(0, 1.0), xytext=(-0.2, max(Y)+1)) #标注y轴
plt.xlim(-1, 5) #设置横坐标范围
plt.ylim(-5, 3) #设置纵坐标范围
X_lim=np.arange(int(min(X)), max(X)+1, 1)
ax.set_xticks(X_lim) #设置x轴刻度
Y_lim=np.arange(-3, max(Y)+1, 1)
ax.set_yticks(Y_lim) #设置y轴刻度
ax.annotate(s=r'$y=log_a x$',xy=(1, 1), xytext=(1, 1)) #r'$y=(\frac{1}{2})^x$'
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 17 17:32:49 2020
project name:draw_logarithm_figure
@author: 帅帅de三叔
"""
import math
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist #导入坐标轴加工模块
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
fig=plt.figure(figsize=(6,4)) #新建画布
ax=axisartist.Subplot(fig,111) #使用axisartist.Subplot方法创建一个绘图区对象ax
fig.add_axes(ax) #将绘图区对象添加到画布中
def logarithm_func(x, a=2): #定义指数函数
y=math.log(x, a)
return y
X=np.linspace(0.01, 4, 100) #构造自变量组
Y=[logarithm_func(x) for x in X] #求函数值
ax.plot(X, Y, label=r'$a>1$') #绘制指数函数
ax.scatter(1, 0, color='red')
plt.legend()
plt.show()
print(max(X), max(Y)) #测试一下自变量最大值和因变量最大值,为后面的坐标轴设置依据
ax.axis[:].set_visible(False) #隐藏原来的实线矩形
ax.axis["x"]=ax.new_floating_axis(0, 0, axis_direction="bottom") #添加x轴
ax.axis["y"]=ax.new_floating_axis(1, 0, axis_direction="bottom") #添加y轴
ax.axis["x"].set_axisline_style("-|>", size=1.0) #给x坐标轴加箭头
ax.axis["y"].set_axisline_style("-|>", size=1.0) #给y坐标轴加箭头
ax.annotate(s='x', xy=(max(X), 0), xytext=(max(X)+1, 0.3)) #标注x轴
ax.annotate(s='y', xy=(0, 1.0), xytext=(-0.2, max(Y)+1)) #标注y轴
plt.xlim(-1, 5) #设置横坐标范围
plt.ylim(-5, 3) #设置纵坐标范围
X_lim=np.arange(int(min(X)), max(X)+1, 1)
ax.set_xticks(X_lim) #设置x轴刻度
Y_lim=np.arange(-3, max(Y)+1, 1)
ax.set_yticks(Y_lim) #设置y轴刻度
ax.annotate(s=r'$y=log_a x$',xy=(1, 1), xytext=(1, 1)) #r'$y=(\frac{1}{2})^x$'
两个代码对比,可以看到,仅改了参数a的初始值和label标签,因为两个函数的定义域和值域是一致的。
这两个函数虽然互为反函数,但是定义域和值域不一致,所以需要在定义函数的时候给出不同的自变量取值范围,否则会出现math domain error 。
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 18 08:49:21 2020
project name:logarithm_vs_exponential
@author: 帅帅de三叔
"""
import math
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist #导入坐标轴加工模块
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
fig=plt.figure(figsize=(6,4)) #新建画布
ax=axisartist.Subplot(fig,111) #使用axisartist.Subplot方法创建一个绘图区对象ax
fig.add_axes(ax) #将绘图区对象添加到画布中
def logarithm_func(x, a=2): #定义指数函数
y=math.log(x, a)
return y
def exponential_func(x, a=2): #定义指数函数
y=math.pow(a, x)
return y
X=np.linspace(0.01, 4, 100) #构造自变量组
Y=[logarithm_func(x) for x in X] #求函数值
ax.plot(X, Y, label='对数函数') #绘制指数函数
ax.scatter(1, 0, color='red')
X1=np.linspace(-4, 4, 100)
Y1=[exponential_func(x) for x in X1]
ax.plot(X1, Y1, label='指数函数')
ax.scatter(0, 1, color='red')
ax.plot(X1, X1, color='green', label=r'$y=x$')
plt.legend(loc=2)
plt.show()
print(max(X), max(Y)) #测试一下自变量最大值和因变量最大值,为后面的坐标轴设置依据
ax.axis[:].set_visible(False) #隐藏原来的实线矩形
ax.axis["x"]=ax.new_floating_axis(0, 0, axis_direction="bottom") #添加x轴
ax.axis["y"]=ax.new_floating_axis(1, 0, axis_direction="bottom") #添加y轴
ax.axis["x"].set_axisline_style("-|>", size=1.0) #给x坐标轴加箭头
ax.axis["y"].set_axisline_style("-|>", size=1.0) #给y坐标轴加箭头
ax.annotate(s='x', xy=(max(X), 0), xytext=(max(X)+1, 0.3)) #标注x轴
ax.annotate(s='y', xy=(0, 1.0), xytext=(-0.2, 5)) #标注y轴
plt.xlim(-5, 5) #设置横坐标范围
plt.ylim(-5, 5) #设置纵坐标范围
X_lim=np.arange(-4, 4, 1)
ax.set_xticks(X_lim) #设置x轴刻度
Y_lim=np.arange(-4, 4, 1)
ax.set_yticks(Y_lim) #设置y轴刻度
只需要在定义指数函数和对数函数的时候给a赋予不同的参数便可出现不同的形状,下图是a=1/2 时的效果图
如果你不会写代码或者还有什么不懂的欢迎来“三行科创”微信公众号留言,同时交流群免费向大家开放,入群讲缘分。
1,https://blog.csdn.net/zengbowengood/article/details/104338878
2,https://blog.csdn.net/zengbowengood/article/details/102862072
3,https://blog.csdn.net/zengbowengood/article/details/104360150
4,https://blog.csdn.net/zengbowengood/article/details/104260155
5,https://matplotlib.org/api/_as_gen/matplotlib.pyplot.html
6,https://blog.csdn.net/qq_17528659/article/details/82152530
详细内容
log() 返回 x 的自然对数。math.log(x) 就相当于数学中的ln(x),x>0,求底数为e的对数,e = 2.718281828459;math.log10(x) 就相当于数学中的lg(x),x>0,求底数为10的对数。可以通过log(x[, base])来设置底数,如 log(x, 10) 表示以10为底的对数。
语法
以下是 log() 方法的语法:import math
math.log(x[, base])
注意:log()是不能直接访问的,需要导入 math 模块,通过静态对象调用该方法。
参数
x -- 数值表达式。base -- 可选,底数,默认为 e。
相关推荐:《Python视频教程》
返回值
返回 x 的自然对数,x>0。
实例
以下展示了使用 log() 方法的实例:#!/usr/bin/python
# -*- coding: UTF-8 -*-
import math # 导入 math 模块
print "math.log(100.12) : ", math.log(100.12)
print "math.log(100.72) : ", math.log(100.72)
print "math.log(119L) : ", math.log(119L)
print "math.log(math.pi) : ", math.log(math.pi)# 设置底数
print "math.log(10,2) : ", math.log(10,2)
以上实例运行后输出结果为:math.log(100.12) : 4.60636946656
math.log(100.72) : 4.61234438974
math.log(119L) : 4.77912349311
math.log(math.pi) : 1.14472988585
math.log(10,2) : 3.32192809489