注これは、古い多項式APIの一部を形成します。バージョン1.4以降、numpy.polynomialで定義された新しい多項式APIが優先されます。違いの概要は、移行ガイドに記載されています。 numpy.poly1d()関数を使用すると、多項式関数を定義できます。したがって、多項式で自然演算を使用するのが簡単になります。 これは、多項式の「自然演算」をカプセル化するために使用される便利なクラスです。コード内の通常の形式。 構文: numpy.poly1d(arr、root、var) パラメーター: arr: [array_like]多項式係数は、累乗の降順で指定されます。2番目のパラメーター(root)がTrueに設定されている場合、配列値は多項式の根になります。 root: [bool、optional]Trueは多項式の根を意味します。デフォルトはFalseです。 var:多項式で必要なx、y、zなどの変数[defaultはx]です。 引数: c:多項式係数。係数:多項式係数。 係数:多項式係数。次数:多項式の次数または次数。 o:多項式の次数または次数。 r:多項式の根。根:多項式の根。 戻り値:多項式と適用された演算 iframe> Numpypoly1dの例 np.poly1dの例#1 def _break_points(num、den):" ""実際の軸上のブレークポイントを抽出し、これらの位置でゲインを取得 "" "#タイプ:(np.poly1d、np.poly1d)->(np.array、np.array)dnum = num.deriv(m = 1)dden = den.deriv(m = 1)polynom = den * dnum --num * dden real_break_pts = polynom.r#無限のブレークポイントを気にしないreal_break_pts = real_break_pts [num(real_break_pts)!= 0] k_break = -den(real_break_pts )/ num(real_break_pts)idx = k_break> = 0#正のみゲインk_break = k_break [idx] real_break_pts = real_break_pts [idx] if len(k_break)== 0:k_break = [0] real_break_pts = den.roots return k_break、 real_break_pts np.poly1dの例#2 def test_poly1d_math(self):#ここでは、計算を簡単にするためにいくつかの単純な係数を使用していますp = np.poly1d([1.、2、 4])q = np.poly1d([4.、2、1])assert_equal(p / q、(np.poly1d([0.25])、np.poly1d([1.5、3.75])))assert_equal(p。 integ()、np.p oly1d([1/3、1.、4.、0.]))assert_equal(p.integ(1)、np.poly1d([1/3、1.、4.、0.]))p = np .poly1d([1.、2、3])q = np.poly1d([3.、2、1])assert_equal(p * q、np.poly1d([3.、8.、14.、8.、 3。]))assert_equal(p + q、np.poly1d([4.、4.、4.]))assert_equal(p --q、np.poly1d([-2。、0.、2.])) assert_equal(p ** 4、np.poly1d([1.、8.、36.、104.、214.、312.、324.、216.、81.]))assert_equal(p(q)、np。 poly1d([9.、12.、16.、8.、6.]))assert_equal(q(p)、np.poly1d([3.、12.、32.、40.、34.]))assert_equal (p.deriv()、np.poly1d([2。、2.]))assert_equal(p.deriv(2)、np.poly1d([2。]))assert_equal(np.polydiv(np.poly1d([ 1、0、-1])、np.poly1d([1、1]))、(np.poly1d([1。、-1。])、np.poly1d([0。]))) np.poly1dの例#3 def test_poly1d_str_and_repr(self):p = np.poly1d([1。、2、3])assert_equal(repr(p)、`poly1d([1 。、2.、3。])`)assert_equal(str(p)、` 2`` 1 x + 2 x + 3`)q = np.poly1d([3。、2、1])assert_equal(repr( q)、`poly1d([3。、2.、1.])`)assert_equal(str(q)、 `2``3 x + 2 x + 1`)r = np.poly1d([1.89999 + 2j、-3j、-5.12345678、2 + 1j])assert_equal(str(r)、 `3 2``(1.9 + 2j)x-3j x-5.123 x +(2 + 1j)`)assert_equal(str(np .poly1d([-3、-2、-1]))、 `2``-3 x --2 x --1`) np.poly1dの例#4 def data_analysis(e_ph、flux、method = "least"):if method == "least":coeffs = np.polyfit(x = e_ph、y = flux、deg = 11)polynom = np.poly1d(coeffs)x = np.linspace(e_ph [0]、e_ph [-1]、num = 100)pd = np.polyder(polynom、m = 1)indx = np.argmax(np.abs(pd(x)))eph_c = x [indx] pd2 = np.polyder(polynom、m = 2)p2_roots = np.roots(pd2)p2_roots = p2_roots [p2_roots [:]。imag == 0] p2_roots = np.real(p2_roots)Eph_fin = find_nearest(p2_roots) 、eph_c)return Eph_fin、polynom elif method == "new method":pass#plt.plot(Etotal、total、 "ro")#plt.plot(x、polynom(x)) np .poly1dの例#5 def _systopoly1d(sys): """システムの分子と分母の多項式を抽出"""#信号処理ツールボックスからの入力を許可するif(isinstance(sys、scipy.signal。 lti)):nump = sys.num denp = s ys.den else:#必要に応じて伝達関数に変換しますsys = _convert_to_transfer_function(sys)#SISOシステムがあることを確認しますif(sys.inputs>1またはsys.outputs>1):raise ControlMIMONotImplemented()#開始システムオブジェクトから分子と分母を抽出します。nump=sys.num[0] [0] denp = sys.den [0] [0]#num、denがすでに多項式であるかどうかを確認します。それ以外の場合は、if(not isinstance(nump、poly1d)):nump = poly1d(nump)if(not isinstance(denp、poly1d)):denp = poly1d(denp)return(nump、denp) np .poly1dの例#6 def quadraticInterpolation(valueList2d、numDegrees、n、startTime = None、endTime = None):```指定されたポイントnumDegreesと交差する滑らかな曲線上に一連のポイントを生成します-近似多項式の度数-この値が入力に対して高すぎる場合、曲線は奇妙になりますn-出力するポイントの数startTime / endTime / n-nポイントは、startTimeとendTimeの間に等間隔で生成されます```_numpyCheck( )x、y = zip(* valueList2d)startTimeがNoneの場合:startTime = x [0] endTimeがNoneの場合:endTime = x [-1] polyFunc = np.poly1d(np.polyfit(x、y、numDegrees)) newX = np.linspace(startTime、endTime、n)retList = [(n、polyFunc(n))for n in newX] return retList np.poly1d example#7 def fit_y(self、X、Y、x1、x2):len(X)!= 0#Xに1つのポイントのみが含まれる場合、関数onは、np.sum(X == X [0])== len(X)の場合に行y = Y [0]を取得します:return Y [0]、Y [0] p = np.poly1d(np.polyfit( X、Y、1))return p(x1)、p(x2) np.poly1d example#8 def remove_linear_BG_XAS_preedge(xmcd_data、scanparams、process_parameters = None、process_number = -1):"""プレエッジ平均に基づいて線形bgを削除する必要があります"""preedge_spectrum = get_preedge_spectrum(process_parameters、xmcd_data)preedge_poly = np.poly1d(np.polyfit(preedge_spectrum ["Energy"]、preedge_spectrum ["XAS" ]、1))xas_bg = preedge_poly(xmcd_data ["Energy"])for xas in ["XAS +"、 "XAS-"、 "XAS"]:xmcd_data [xas]-= xas_bg return(xmcd_data、{"xas_bg_poly_coeffs": "" .join(map(str、preedge_poly.coeffs))}) np.poly1dの例#9 def fit_y(self、X、Y、x1、x2): len(X)!= 0#Xに1つのポイントしか含まれていない場合、関数は行y = Y [0]を取得します。np.sum(X == X [0])== len(X):return Y [0 ]、Y [0] p = np.poly1d(np.polyfit(X、Y、1))return p(x1)、p(x2) np.poly1d例#10 def __init __( self、roots、weights = None、hn = 1.0、kn = 1.0、wfunc = None、limits = None、monic = 0、eval_func = None):np.poly1d .__ init __(self、roots、r = 1)equiv_weights = [ weights [k] / wfunc(roots [k])for k in range(len(roots))] self .__ dict __ [`weights`] = np.array(list(zip(roots、weights、equiv_weights)))self。 __dict __ [`weight_func`] = wfunc self .__ dict __ [`limits`] = Limits mu = sqrt(hn)if monic:evf = eval_func if evf:eval_func = lambda x:evf(x)/ kn mu = mu / abs( kn)kn = 1.0 self .__ dict __ [`normcoef`] = mu self .__ dict __ [`coeffs`] * = kn#注:eval_funcは連想自己で破棄されます。__dict __ [`_ eval_func`] = eval_func
これは、古い多項式APIの一部を形成します。バージョン1.4以降、numpy.polynomialで定義された新しい多項式APIが優先されます。違いの概要は、移行ガイドに記載されています。
numpy.poly1d()関数を使用すると、多項式関数を定義できます。したがって、多項式で自然演算を使用するのが簡単になります。
numpy.poly1d()
これは、多項式の「自然演算」をカプセル化するために使用される便利なクラスです。コード内の通常の形式。
構文: numpy.poly1d(arr、root、var) パラメーター: arr: [array_like]多項式係数は、累乗の降順で指定されます。2番目のパラメーター(root)がTrueに設定されている場合、配列値は多項式の根になります。 root: [bool、optional]Trueは多項式の根を意味します。デフォルトはFalseです。 var:多項式で必要なx、y、zなどの変数[defaultはx]です。 引数: c:多項式係数。係数:多項式係数。 係数:多項式係数。次数:多項式の次数または次数。 o:多項式の次数または次数。 r:多項式の根。根:多項式の根。 戻り値:多項式と適用された演算 iframe> Numpypoly1dの例 np.poly1dの例#1 def _break_points(num、den):" ""実際の軸上のブレークポイントを抽出し、これらの位置でゲインを取得 "" "#タイプ:(np.poly1d、np.poly1d)->(np.array、np.array)dnum = num.deriv(m = 1)dden = den.deriv(m = 1)polynom = den * dnum --num * dden real_break_pts = polynom.r#無限のブレークポイントを気にしないreal_break_pts = real_break_pts [num(real_break_pts)!= 0] k_break = -den(real_break_pts )/ num(real_break_pts)idx = k_break> = 0#正のみゲインk_break = k_break [idx] real_break_pts = real_break_pts [idx] if len(k_break)== 0:k_break = [0] real_break_pts = den.roots return k_break、 real_break_pts np.poly1dの例#2 def test_poly1d_math(self):#ここでは、計算を簡単にするためにいくつかの単純な係数を使用していますp = np.poly1d([1.、2、 4])q = np.poly1d([4.、2、1])assert_equal(p / q、(np.poly1d([0.25])、np.poly1d([1.5、3.75])))assert_equal(p。 integ()、np.p oly1d([1/3、1.、4.、0.]))assert_equal(p.integ(1)、np.poly1d([1/3、1.、4.、0.]))p = np .poly1d([1.、2、3])q = np.poly1d([3.、2、1])assert_equal(p * q、np.poly1d([3.、8.、14.、8.、 3。]))assert_equal(p + q、np.poly1d([4.、4.、4.]))assert_equal(p --q、np.poly1d([-2。、0.、2.])) assert_equal(p ** 4、np.poly1d([1.、8.、36.、104.、214.、312.、324.、216.、81.]))assert_equal(p(q)、np。 poly1d([9.、12.、16.、8.、6.]))assert_equal(q(p)、np.poly1d([3.、12.、32.、40.、34.]))assert_equal (p.deriv()、np.poly1d([2。、2.]))assert_equal(p.deriv(2)、np.poly1d([2。]))assert_equal(np.polydiv(np.poly1d([ 1、0、-1])、np.poly1d([1、1]))、(np.poly1d([1。、-1。])、np.poly1d([0。]))) np.poly1dの例#3 def test_poly1d_str_and_repr(self):p = np.poly1d([1。、2、3])assert_equal(repr(p)、`poly1d([1 。、2.、3。])`)assert_equal(str(p)、` 2`` 1 x + 2 x + 3`)q = np.poly1d([3。、2、1])assert_equal(repr( q)、`poly1d([3。、2.、1.])`)assert_equal(str(q)、 `2``3 x + 2 x + 1`)r = np.poly1d([1.89999 + 2j、-3j、-5.12345678、2 + 1j])assert_equal(str(r)、 `3 2``(1.9 + 2j)x-3j x-5.123 x +(2 + 1j)`)assert_equal(str(np .poly1d([-3、-2、-1]))、 `2``-3 x --2 x --1`) np.poly1dの例#4 def data_analysis(e_ph、flux、method = "least"):if method == "least":coeffs = np.polyfit(x = e_ph、y = flux、deg = 11)polynom = np.poly1d(coeffs)x = np.linspace(e_ph [0]、e_ph [-1]、num = 100)pd = np.polyder(polynom、m = 1)indx = np.argmax(np.abs(pd(x)))eph_c = x [indx] pd2 = np.polyder(polynom、m = 2)p2_roots = np.roots(pd2)p2_roots = p2_roots [p2_roots [:]。imag == 0] p2_roots = np.real(p2_roots)Eph_fin = find_nearest(p2_roots) 、eph_c)return Eph_fin、polynom elif method == "new method":pass#plt.plot(Etotal、total、 "ro")#plt.plot(x、polynom(x)) np .poly1dの例#5 def _systopoly1d(sys): """システムの分子と分母の多項式を抽出"""#信号処理ツールボックスからの入力を許可するif(isinstance(sys、scipy.signal。 lti)):nump = sys.num denp = s ys.den else:#必要に応じて伝達関数に変換しますsys = _convert_to_transfer_function(sys)#SISOシステムがあることを確認しますif(sys.inputs>1またはsys.outputs>1):raise ControlMIMONotImplemented()#開始システムオブジェクトから分子と分母を抽出します。nump=sys.num[0] [0] denp = sys.den [0] [0]#num、denがすでに多項式であるかどうかを確認します。それ以外の場合は、if(not isinstance(nump、poly1d)):nump = poly1d(nump)if(not isinstance(denp、poly1d)):denp = poly1d(denp)return(nump、denp) np .poly1dの例#6 def quadraticInterpolation(valueList2d、numDegrees、n、startTime = None、endTime = None):```指定されたポイントnumDegreesと交差する滑らかな曲線上に一連のポイントを生成します-近似多項式の度数-この値が入力に対して高すぎる場合、曲線は奇妙になりますn-出力するポイントの数startTime / endTime / n-nポイントは、startTimeとendTimeの間に等間隔で生成されます```_numpyCheck( )x、y = zip(* valueList2d)startTimeがNoneの場合:startTime = x [0] endTimeがNoneの場合:endTime = x [-1] polyFunc = np.poly1d(np.polyfit(x、y、numDegrees)) newX = np.linspace(startTime、endTime、n)retList = [(n、polyFunc(n))for n in newX] return retList np.poly1d example#7 def fit_y(self、X、Y、x1、x2):len(X)!= 0#Xに1つのポイントのみが含まれる場合、関数onは、np.sum(X == X [0])== len(X)の場合に行y = Y [0]を取得します:return Y [0]、Y [0] p = np.poly1d(np.polyfit( X、Y、1))return p(x1)、p(x2) np.poly1d example#8 def remove_linear_BG_XAS_preedge(xmcd_data、scanparams、process_parameters = None、process_number = -1):"""プレエッジ平均に基づいて線形bgを削除する必要があります"""preedge_spectrum = get_preedge_spectrum(process_parameters、xmcd_data)preedge_poly = np.poly1d(np.polyfit(preedge_spectrum ["Energy"]、preedge_spectrum ["XAS" ]、1))xas_bg = preedge_poly(xmcd_data ["Energy"])for xas in ["XAS +"、 "XAS-"、 "XAS"]:xmcd_data [xas]-= xas_bg return(xmcd_data、{"xas_bg_poly_coeffs": "" .join(map(str、preedge_poly.coeffs))}) np.poly1dの例#9 def fit_y(self、X、Y、x1、x2): len(X)!= 0#Xに1つのポイントしか含まれていない場合、関数は行y = Y [0]を取得します。np.sum(X == X [0])== len(X):return Y [0 ]、Y [0] p = np.poly1d(np.polyfit(X、Y、1))return p(x1)、p(x2) np.poly1d例#10 def __init __( self、roots、weights = None、hn = 1.0、kn = 1.0、wfunc = None、limits = None、monic = 0、eval_func = None):np.poly1d .__ init __(self、roots、r = 1)equiv_weights = [ weights [k] / wfunc(roots [k])for k in range(len(roots))] self .__ dict __ [`weights`] = np.array(list(zip(roots、weights、equiv_weights)))self。 __dict __ [`weight_func`] = wfunc self .__ dict __ [`limits`] = Limits mu = sqrt(hn)if monic:evf = eval_func if evf:eval_func = lambda x:evf(x)/ kn mu = mu / abs( kn)kn = 1.0 self .__ dict __ [`normcoef`] = mu self .__ dict __ [`coeffs`] * = kn#注:eval_funcは連想自己で破棄されます。__dict __ [`_ eval_func`] = eval_func
構文: numpy.poly1d(arr、root、var) パラメーター: arr: [array_like]多項式係数は、累乗の降順で指定されます。2番目のパラメーター(root)がTrueに設定されている場合、配列値は多項式の根になります。
root: [bool、optional]Trueは多項式の根を意味します。デフォルトはFalseです。 var:多項式で必要なx、y、zなどの変数[defaultはx]です。
引数: c:多項式係数。係数:多項式係数。 係数:多項式係数。次数:多項式の次数または次数。 o:多項式の次数または次数。 r:多項式の根。根:多項式の根。
戻り値:多項式と適用された演算
def _break_points(num、den):" ""実際の軸上のブレークポイントを抽出し、これらの位置でゲインを取得 "" "#タイプ:(np.poly1d、np.poly1d)->(np.array、np.array)dnum = num.deriv(m = 1)dden = den.deriv(m = 1)polynom = den * dnum --num * dden real_break_pts = polynom.r#無限のブレークポイントを気にしないreal_break_pts = real_break_pts [num(real_break_pts)!= 0] k_break = -den(real_break_pts )/ num(real_break_pts)idx = k_break> = 0#正のみゲインk_break = k_break [idx] real_break_pts = real_break_pts [idx] if len(k_break)== 0:k_break = [0] real_break_pts = den.roots return k_break、 real_break_pts
def test_poly1d_math(self):#ここでは、計算を簡単にするためにいくつかの単純な係数を使用していますp = np.poly1d([1.、2、 4])q = np.poly1d([4.、2、1])assert_equal(p / q、(np.poly1d([0.25])、np.poly1d([1.5、3.75])))assert_equal(p。 integ()、np.p oly1d([1/3、1.、4.、0.]))assert_equal(p.integ(1)、np.poly1d([1/3、1.、4.、0.]))p = np .poly1d([1.、2、3])q = np.poly1d([3.、2、1])assert_equal(p * q、np.poly1d([3.、8.、14.、8.、 3。]))assert_equal(p + q、np.poly1d([4.、4.、4.]))assert_equal(p --q、np.poly1d([-2。、0.、2.])) assert_equal(p ** 4、np.poly1d([1.、8.、36.、104.、214.、312.、324.、216.、81.]))assert_equal(p(q)、np。 poly1d([9.、12.、16.、8.、6.]))assert_equal(q(p)、np.poly1d([3.、12.、32.、40.、34.]))assert_equal (p.deriv()、np.poly1d([2。、2.]))assert_equal(p.deriv(2)、np.poly1d([2。]))assert_equal(np.polydiv(np.poly1d([ 1、0、-1])、np.poly1d([1、1]))、(np.poly1d([1。、-1。])、np.poly1d([0。])))
def test_poly1d_str_and_repr(self):p = np.poly1d([1。、2、3])assert_equal(repr(p)、`poly1d([1 。、2.、3。])`)assert_equal(str(p)、` 2`` 1 x + 2 x + 3`)q = np.poly1d([3。、2、1])assert_equal(repr( q)、`poly1d([3。、2.、1.])`)assert_equal(str(q)、 `2``3 x + 2 x + 1`)r = np.poly1d([1.89999 + 2j、-3j、-5.12345678、2 + 1j])assert_equal(str(r)、 `3 2``(1.9 + 2j)x-3j x-5.123 x +(2 + 1j)`)assert_equal(str(np .poly1d([-3、-2、-1]))、 `2``-3 x --2 x --1`)
def data_analysis(e_ph、flux、method = "least"):if method == "least":coeffs = np.polyfit(x = e_ph、y = flux、deg = 11)polynom = np.poly1d(coeffs)x = np.linspace(e_ph [0]、e_ph [-1]、num = 100)pd = np.polyder(polynom、m = 1)indx = np.argmax(np.abs(pd(x)))eph_c = x [indx] pd2 = np.polyder(polynom、m = 2)p2_roots = np.roots(pd2)p2_roots = p2_roots [p2_roots [:]。imag == 0] p2_roots = np.real(p2_roots)Eph_fin = find_nearest(p2_roots) 、eph_c)return Eph_fin、polynom elif method == "new method":pass#plt.plot(Etotal、total、 "ro")#plt.plot(x、polynom(x))
def _systopoly1d(sys): """システムの分子と分母の多項式を抽出"""#信号処理ツールボックスからの入力を許可するif(isinstance(sys、scipy.signal。 lti)):nump = sys.num denp = s ys.den else:#必要に応じて伝達関数に変換しますsys = _convert_to_transfer_function(sys)#SISOシステムがあることを確認しますif(sys.inputs>1またはsys.outputs>1):raise ControlMIMONotImplemented()#開始システムオブジェクトから分子と分母を抽出します。nump=sys.num[0] [0] denp = sys.den [0] [0]#num、denがすでに多項式であるかどうかを確認します。それ以外の場合は、if(not isinstance(nump、poly1d)):nump = poly1d(nump)if(not isinstance(denp、poly1d)):denp = poly1d(denp)return(nump、denp)
def quadraticInterpolation(valueList2d、numDegrees、n、startTime = None、endTime = None):```指定されたポイントnumDegreesと交差する滑らかな曲線上に一連のポイントを生成します-近似多項式の度数-この値が入力に対して高すぎる場合、曲線は奇妙になりますn-出力するポイントの数startTime / endTime / n-nポイントは、startTimeとendTimeの間に等間隔で生成されます```_numpyCheck( )x、y = zip(* valueList2d)startTimeがNoneの場合:startTime = x [0] endTimeがNoneの場合:endTime = x [-1] polyFunc = np.poly1d(np.polyfit(x、y、numDegrees)) newX = np.linspace(startTime、endTime、n)retList = [(n、polyFunc(n))for n in newX] return retList
def fit_y(self、X、Y、x1、x2):len(X)!= 0#Xに1つのポイントのみが含まれる場合、関数onは、np.sum(X == X [0])== len(X)の場合に行y = Y [0]を取得します:return Y [0]、Y [0] p = np.poly1d(np.polyfit( X、Y、1))return p(x1)、p(x2)
def remove_linear_BG_XAS_preedge(xmcd_data、scanparams、process_parameters = None、process_number = -1):"""プレエッジ平均に基づいて線形bgを削除する必要があります"""preedge_spectrum = get_preedge_spectrum(process_parameters、xmcd_data)preedge_poly = np.poly1d(np.polyfit(preedge_spectrum ["Energy"]、preedge_spectrum ["XAS" ]、1))xas_bg = preedge_poly(xmcd_data ["Energy"])for xas in ["XAS +"、 "XAS-"、 "XAS"]:xmcd_data [xas]-= xas_bg return(xmcd_data、{"xas_bg_poly_coeffs": "" .join(map(str、preedge_poly.coeffs))})
def fit_y(self、X、Y、x1、x2): len(X)!= 0#Xに1つのポイントしか含まれていない場合、関数は行y = Y [0]を取得します。np.sum(X == X [0])== len(X):return Y [0 ]、Y [0] p = np.poly1d(np.polyfit(X、Y、1))return p(x1)、p(x2)
def __init __( self、roots、weights = None、hn = 1.0、kn = 1.0、wfunc = None、limits = None、monic = 0、eval_func = None):np.poly1d .__ init __(self、roots、r = 1)equiv_weights = [ weights [k] / wfunc(roots [k])for k in range(len(roots))] self .__ dict __ [`weights`] = np.array(list(zip(roots、weights、equiv_weights)))self。 __dict __ [`weight_func`] = wfunc self .__ dict __ [`limits`] = Limits mu = sqrt(hn)if monic:evf = eval_func if evf:eval_func = lambda x:evf(x)/ kn mu = mu / abs( kn)kn = 1.0 self .__ dict __ [`normcoef`] = mu self .__ dict __ [`coeffs`] * = kn#注:eval_funcは連想自己で破棄されます。__dict __ [`_ eval_func`] = eval_func