數學之路-python計算實戰(3)-霍納法則

來源:互聯網
上載者:User

假設有n+2個實數a0,a1,…,an,和x的序列,要對多項式Pn(x)= anx ^n+a(n-1)x^(n-1)+…+a1x+a0求值,直接方法是對每一項分別求值,並把每一項求的值累加起來,這種方法十分低效,它需要進行n+(n-1)+…+1=n(n+1)/2次乘法運算和n次加法運算。有沒有更高效的演算法呢?答案是肯定的。通過如下變換我們可以得到一種快得多的演算法,即Pn(x)= anx ^n+a(n-1)x^(n-1)+…+a1x+a0=((…(((anx +an-1)x+an-2)x+ an-3)…)x+a1)x+a0,這種求值的安排我們稱為霍納法則。

本部落格所有內容是原創,如果轉載請註明來源http://blog.csdn.net/myhaspl/

a=[1,-6,8,8,4,-40]

myx=3

b=[]

b.append(a[0])

for ai in a[1:]:

       b.append(ai+myx*b[-1])

print b[-1]

deep@myddb:~/src$ pypy test.py
17

deep@myddb:~/src$




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.