Python實(shí)現(xiàn)之初等函數(shù)一
本文轉(zhuǎn)載自微信公眾號「python與大數(shù)據(jù)分析」,作者一只小小鳥鳥。轉(zhuǎn)載本文請聯(lián)系python與大數(shù)據(jù)分析公眾號。
初等函數(shù)是由基本初等函數(shù)經(jīng)過有限次的四則運(yùn)算和復(fù)合運(yùn)算所得到的函數(shù)。基本初等函數(shù)和初等函數(shù)在其定義區(qū)間內(nèi)均為連續(xù)函數(shù)。高等數(shù)學(xué)將基本初等函數(shù)歸為五類:冪函數(shù)、指數(shù)函數(shù)、對數(shù)函數(shù)、三角函數(shù)、反三角函數(shù)。
比較頭疼的是numpy中的冪函數(shù)不支持負(fù)數(shù)定義域,所以找了很多辦法來解決該問題。
主函數(shù)代碼如下:
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- # _ooOoo_
- # o8888888o
- # 88" . "88
- # ( | - _ - | )
- # O\ = /O
- # ____/`---'\____
- # .' \\| |// `.
- # / \\|||:|||// \
- # / _|||||-:- |||||- \
- # | | \\\ - /// | |
- # | \_| ''\---/'' | _/ |
- # \ .-\__ `-` ___/-. /
- # ___`. .' /--.--\ `. . __
- # ."" '< `.___\_<|>_/___.' >'"".
- # | | : `- \`.;`\ _ /`;.`/ - ` : | |
- # \ \ `-. \_ __\ /__ _/ .-` / /
- # ==`-.____`-.___\_____/___.-`____.-'==
- # `=---='
- '''
- @Project :pythonalgorithms
- @File :basicfunction.py
- @Author :不勝人生一場醉
- @Date :2021/7/19 17:39
- '''
- import matplotlib.pyplot as plt
- import numpy as np
- import math
- import mpl_toolkits.axisartist as axisartist # 導(dǎo)入坐標(biāo)軸加工模塊
- # -----------------函數(shù)------------------
- # 給定一個(gè)數(shù)集A,對A施加一個(gè)對應(yīng)的法則/映射f,記做:f(A),那么可以得到另外一個(gè)數(shù)集B,也就是可以認(rèn)為B=f(A);
- # 那么這個(gè)關(guān)系就叫做函數(shù)關(guān)系式,簡稱函數(shù)。
- # 三個(gè)重要的因素: 定義域A、值域B、對應(yīng)的映射法則f。
- if __name__ == "__main__":
- # 一次函數(shù)
- Linearfunction()
- # 二次函數(shù)
- Quadraticfunction()
- # 冪函數(shù)
- powerfunction()
- # 指數(shù)函數(shù)
- exponentialfunction()
- # 對數(shù)函數(shù)
- Logarithmicfunction()
一次函數(shù)代碼如下:
- # ---------------一次函數(shù)------------------
- # 一次函數(shù)是函數(shù)中的一種,一般形如y=ax+b(a,b是常數(shù),a≠0),其中x是自變量,y是因變量。
- # 當(dāng)b=0時(shí),y=kx(k為常數(shù),k≠0),y叫做x的正比例函數(shù)
- # 當(dāng)a=0時(shí),y=b,則為常函數(shù)
- def Linearfunction():
- fig = plt.figure(figsize=(10, 8))
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負(fù)號
- x = np.linspace(-10, 10, 100)
- i = 1
- alist = [-1, 0, 1]
- blist = [-1, 0, 1]
- for a in alist:
- for b in blist:
- plt.subplot(3, 3, i)
- y = x * a + b
- label = '{}*x+{}'.format(a, b)
- plt.plot(x, y, label=label)
- plt.legend()
- i += 1
- plt.show()
- for a in alist:
- for b in blist:
- y = x * a + b
- label = '{}*x+{}'.format(a, b)
- plt.plot(x, y, label=label)
- i += 1
- plt.title("一次函數(shù)")
- plt.legend()
- plt.show()
二次函數(shù)代碼如下:
- # 二次函數(shù)(quadratic function)的基本表示形式為y=ax²+bx+c(a≠0)。
- # 二次函數(shù)最高次必須為二次, 二次函數(shù)的圖像是一條對稱軸與y軸平行或重合于y軸的拋物線。
- # 其中a稱為二次項(xiàng)系數(shù),b為一次項(xiàng)系數(shù),c為常數(shù)項(xiàng)。x為自變量,y為因變量。等號右邊自變量的最高次數(shù)是2。
- # 如果令y值等于零,則可得一個(gè)二次方程。該方程的解稱為方程的根或函數(shù)的零點(diǎn)。
- # 頂點(diǎn)坐標(biāo) = (-b/2a,(4ac-b²)/4a
- def Quadraticfunction():
- fig = plt.figure(figsize=(10, 8))
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負(fù)號
- x = np.linspace(-2, 2, 100)
- alist = [-1, 1]
- blist = [-1, 0, 1]
- clist = [-1, 0, 1]
- for a in alist:
- for b in blist:
- for c in clist:
- y = a * x * x + b * x + c
- label = '{}*x*x+{}*x+{}'.format(a, b, c)
- plt.plot(x, y, label=label)
- plt.title("二次函數(shù)")
- plt.legend()
- plt.show()
冪函數(shù)代碼如下:
- # 冪函數(shù)是基本初等函數(shù)之一。
- # 一般地,y=xα(α為有理數(shù))的函數(shù),即以底數(shù)為自變量,冪為因變量,指數(shù)為常數(shù)的函數(shù)稱為冪函數(shù)。
- # 例如函數(shù)y=x0 、y=x、y=x²、y=x³。
- # a = 正數(shù)
- # a 為>0 的自然數(shù) x定義域(-∞,∞)
- # a 為<0 的整數(shù) X定義域(-∞,0),(0,∞)
- #
- # a >0 的分?jǐn)?shù)
- # a=n/m m為奇數(shù),n為偶數(shù),x定義域(-∞,∞),y定義域[0,+∞)
- # a=n/m m為奇數(shù),n為奇數(shù),x定義域(-∞,∞),y定義域(-∞,∞)
- # a=n/m m為偶數(shù),n不限,x定義域[0,∞),y定義域[0,+∞)
- #
- # a <0 的分?jǐn)?shù)
- # a=n/m m為奇數(shù),n為偶數(shù),x定義域(-∞,0),(0,∞),y定義域(0,+∞)
- # a=n/m m為奇數(shù),n為奇數(shù),x定義域(-∞,0),(0,∞),y定義域(-∞,0),(0,∞)
- # a=n/m m為偶數(shù),n不限,x定義域(0,∞),y定義域(0,+∞)
- def powerfunction():
- plt.figure(figsize=(10, 8))
- ax = plt.gca() # 通過gca:get current axis得到當(dāng)前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負(fù)號
- x = np.linspace(-2, 2, 100)
- alist = [1, 2, 3, 4]
- for a in alist:
- y = np.power(x, a)
- label = 'math.pow(x,{}'.format(a)
- plt.plot(x, y, label=label)
- # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置
- # ax.spines['left'].set_position(('axes', 0.5))
- ax.spines['left'].set_position(('data', 0))
- plt.title("冪指數(shù),a為正整數(shù)")
- plt.legend()
- plt.show()
- plt.figure(figsize=(10, 8))
- ax = plt.gca() # 通過gca:get current axis得到當(dāng)前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負(fù)號
- x = np.append(np.linspace(-1, -0.01, 100), np.linspace(0.01, 1, 100))
- alist = [-1, -2, -3]
- for a in alist:
- y = np.power(x, a)
- label = 'math.pow(x,{}'.format(a)
- plt.plot(x, y, label=label)
- # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置
- # ax.spines['left'].set_position(('axes', 0.5))
- ax.spines['left'].set_position(('data', 0))
- plt.title("冪指數(shù),a為負(fù)整數(shù)")
- plt.legend()
- plt.show()
- # a >0 的分?jǐn)?shù)
- # a=n/m m為奇數(shù),n為奇數(shù),x定義域(-∞,∞),y定義域(-∞,∞) 4/3
- # a=n/m m為奇數(shù),n為偶數(shù),x定義域[0,+∞),y定義域[0,+∞)4/3
- # a=n/m m為偶數(shù),n不限,x定義域(-∞,∞),y定義域[0,+∞) 1/2,3/2
- plt.figure(figsize=(10, 8))
- ax = plt.gca() # 通過gca:get current axis得到當(dāng)前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負(fù)號
- x = np.linspace(-2, 2, 100)
- alist = [1 / 3, 5 / 3, 7 / 3]
- for a in alist:
- # y = np.power(x, a)
- # RuntimeWarning: invalid value encountered in power
- y = np.float_power(abs(x), a) * np.sign(x)
- label = 'math.pow(x,{}'.format(a)
- plt.plot(x, y, label=label)
- # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置
- # ax.spines['left'].set_position(('axes', 0.5))
- ax.spines['left'].set_position(('data', 0))
- plt.title("冪指數(shù),分子分母為奇數(shù)")
- plt.legend()
- plt.show()
- plt.figure(figsize=(10, 8))
- ax = plt.gca() # 通過gca:get current axis得到當(dāng)前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負(fù)號
- x = np.linspace(0, 2, 100)
- alist = [1 / 8, 1 / 4, 1 / 2]
- for a in alist:
- y = np.power(x, a)
- # y = np.float_power(abs(x), a) * np.sign(x)
- label = 'math.pow(x,{}'.format(a)
- plt.plot(x, y, label=label)
- # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置
- # ax.spines['left'].set_position(('axes', 0.5))
- ax.spines['left'].set_position(('data', 0))
- plt.title("冪指數(shù),分母為偶數(shù)")
- plt.legend()
- plt.show()
- plt.figure(figsize=(10, 8))
- ax = plt.gca() # 通過gca:get current axis得到當(dāng)前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負(fù)號
- x = np.linspace(-2, 2, 100)
- alist = [2 / 3, 4 / 5, 6 / 7, 4 / 3, 8 / 5]
- for a in alist:
- y = np.power(abs(x), a)
- label = 'math.pow(x,{}'.format(a)
- plt.plot(x, y, label=label)
- # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置
- # ax.spines['left'].set_position(('axes', 0.5))
- ax.spines['left'].set_position(('data', 0))
- plt.title("冪指數(shù),分子為偶數(shù)分母為奇數(shù)")
- plt.legend()
- plt.show()
- # 關(guān)于負(fù)數(shù)就不再重復(fù)敘述了
- # a <0 的分?jǐn)?shù)
- # a=n/m m為奇數(shù),x定義域(-∞,0),(0,∞),y定義域(0,+∞)
- # a=n/m m為偶數(shù),x定義域(-∞,0),(0,∞),y定義域(-∞,0),(0,∞)
- # a=n/m m為偶數(shù),n為不限,x定義域(0,∞),y定義域(0,+∞)
指數(shù)函數(shù)代碼如下:
- # 指數(shù)函數(shù)是重要的基本初等函數(shù)之一。
- # 一般地,y=ax函數(shù)(a為常數(shù)且以a>0,a≠1)叫做指數(shù)函數(shù),函數(shù)的定義域是 R 。 [1]
- # 注意,在指數(shù)函數(shù)的定義表達(dá)式中,在ax前的系數(shù)必須是數(shù)1,自變量x必須在指數(shù)的位置上,且不能是x的其他表達(dá)式,
- # 否則,就不是指數(shù)函數(shù)
- def exponentialfunction():
- plt.figure(figsize=(10, 8))
- ax = plt.gca() # 通過gca:get current axis得到當(dāng)前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負(fù)號
- # a>0 a!=1
- # 定義域?yàn)?(-∞,∞),值域?yàn)?0, +∞),都通過(0, 1)點(diǎn)
- x = np.linspace(-2, 2, 100)
- alist = [1 / 4, 1 / 3, 1 / 2, 2, 3, 4]
- for a in alist:
- y = np.power(a, x)
- label = 'math.pow(x,{}'.format(a)
- plt.plot(x, y, label=label)
- # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置
- # ax.spines['left'].set_position(('axes', 0.5))
- ax.spines['left'].set_position(('data', 0))
- plt.title("指數(shù)指數(shù)")
- plt.legend()
- plt.show()
對數(shù)函數(shù)代碼如下:
- # 一般地,對數(shù)函數(shù)是以冪(真數(shù))為自變量,指數(shù)為因變量,底數(shù)為常量的函數(shù)。
- # 對數(shù)函數(shù)是6類基本初等函數(shù)之一。其中對數(shù)的定義:
- # 如果ax =N(a>0,且a≠1),那么數(shù)x叫做以a為底N的對數(shù),記作x=logaN,讀作以a為底N的對數(shù),其中a叫做對數(shù)的底數(shù),N叫做真數(shù)。
- # 一般地,函數(shù)y=logaX(a>0,且a≠1)叫做對數(shù)函數(shù),也就是說以冪(真數(shù))為自變量,指數(shù)為因變量,底數(shù)為常量的函數(shù),叫對數(shù)函數(shù)。
- # 其中x是自變量,函數(shù)的定義域是(0,+∞),即x>0。它實(shí)際上就是指數(shù)函數(shù)的反函數(shù),可表示為x=ay。因此指數(shù)函數(shù)里對于a的規(guī)定,同樣適用于對數(shù)函數(shù)。
- def Logarithmicfunction():
- plt.figure(figsize=(10, 8))
- ax = plt.gca() # 通過gca:get current axis得到當(dāng)前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負(fù)號
- # a>0 a!=1
- # 定義域?yàn)?(-∞,∞),值域?yàn)?0, +∞),都通過(0, 1)點(diǎn)
- # 當(dāng)a>1時(shí),單調(diào)遞增
- # 當(dāng)0<a<1時(shí),單調(diào)遞減
- x = np.linspace(0.0001, 2, 100)
- alist = [1 / 4, 1 / 3, 1 / 2, 2, 3, 4]
- for a in alist:
- y = np.log(x) / np.log(a)
- label = 'np.log(x) / np.log({})'.format(a)
- plt.plot(x, y, label=label)
- # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置
- # ax.spines['left'].set_position(('axes', 0.5))
- ax.spines['left'].set_position(('data', 0))
- plt.title("對數(shù)指數(shù)")
- plt.legend()
- plt.show()