我被python中的多项式除法困住了。这是我修改过的代码。while循环不起作用。此代码只将原始L输出为r。如果删除while循环,则只输出第一次除法的剩余部分。我尝试了很多方法来实现它,但都失败了。任何建议将不胜感激。谢谢!def GetDegree(poly):

while poly and poly[-1] == 0:

poly.pop() # normalize

return len(poly)-1

def division(p1,p2):

d1 = GetDegree(p1)

d2 = GetDegree(p2)

if d2 < 0 or d1<0:

raise ZeroDivisionError

if d1 > d2:

S,L = p2,p1#obtain S with lower degree, L with higher degree

else:

S,L = p1,p2

d1 = GetDegree(L)

d2 = GetDegree(S)

while d1>0:

q = [0]*d1

d = [0]*(d1 - d2) + S#shift short towards right by d1-d2

mult = q[d1 - d2] = L[-1] / float(d[-1])#get the result by dividing the first term of the dividend by the highest term of the divisor

d = [coef*mult for coef in d]#multiply the above result by short

L = [fabs( coefL - coefd ) for coefL, coefd in zip(L, d)]#return a new long by subtracting long with d

d1 = GetDegree(L)#return new d1

r = L#return new long and keeping looping for there is no variable left and return as remainder

return r

我想输入任意随机多项式进行计算。但是,当我修改它时,结果仍然不正确。这是我运行的测试:num:[2,1,1,1]den:[1,1,2]。打印结果为:引号:[0.25,0.5],rem:[1.75,0.25]。

以下是我为输入情况修改的代码,基于PM 2Ring的答案:def normalize(poly):

while poly and poly[-1] == 0:

poly.pop()

if poly == []:

poly.append(0)

def poly_divmod(num, den):

#Create normalized copies of the args

num = num[:]

normalize(num)

den = den[:]

normalize(den)

if len(num) >= len(den):

#Shift den towards right so it's the same degree as num

shiftlen = len(num) - len(den)

den = [0] * shiftlen + den

else:

return [0], num

quot = []

divisor = float(den[-1])

for i in range(shiftlen + 1):

#Get the next coefficient of the quotient.

mult = num[-1] / divisor

quot = [mult] + quot

#Subtract mult * den from num, but don't bother if mult == 0

#Note that when i==0, mult!=0; so quot is automatically normalized.

if mult != 0:

d = [mult * u for u in den]

num = [u - v for u, v in zip(num, d)]

num.pop()

den.pop(0)

normalize(num)

return quot, num

def test(num, den):

print ("%s / %s ->" % (num, den))

q, r = poly_divmod(num, den)

print ("quot: %s, rem: %s\n" % (q, r))

return q, r

def main():

degree = int(input('Enter the degree of your polynomial 1:'))

num = []

for i in range (0,degree+1):

coefficient = int(input('Enter the coefficient for x^ %i ? ' %i))

num.append(coefficient)

degree = int(input('Enter the degree of your polynomial 2:'))

den = []

for i in range (0,degree+1):

coefficient = int(input('Enter the coefficient for x^ %i ? ' %i))

den.append(coefficient)

test(num, den)

if __name__ == '__main__':

main()

多元多项式除法python_python中多项式的除法相关推荐

  1. c语言中多项式的除法,【Luogu4512】多项式除法(FFT)

    题面 题解 模板题... 我直接蒯我写的东西... 这个除法是带余除法,所以并不能直接求逆解决. 要求的就是给定两个多项式$A(x),B(x)$,其项数为$n,m$ 求解一个$n-m$项的多项式$C( ...

  2. android java 除法_android中如何实现除法的保留小数点后...

    2016-09-01 00:47龚家贱 客户经理 JAVA中如何对double或者float的浮点数进行精度计算, 在JAVA中提供了多种参数来实现精度的不同控制方式. 具体例子如下: package ...

  3. python2.7除法_对python中的float除法和整除法的实例详解

    从python2.2开始,便有两种除法运算符:"/"."//".两者最大区别在: python2.2前的版本和python2.2以后3.0以前的版本的默认情况下 ...

  4. SQLServer中进行sql除法运算结果为小数时显示0的解决方案

    SQLServer中进行sql除法运算结果为小数时显示0的解决方案 参考文章: (1)SQLServer中进行sql除法运算结果为小数时显示0的解决方案 (2)https://www.cnblogs. ...

  5. python语言的取余运算符_Python 中用于整数除法取余的运算符是()_学小易找答案...

    [填空题]隐球菌病多由()传播. [单选题]下列选项中,哪一个符号是管道符号. ( ) [多选题]可采用分批法计算产品成本的企业有( ) [填空题]现有字符串 s="1234567" ...

  6. python除法编程_Java和Python中的整数除法,取余,舍入

    关于除法,你也许觉得没什么值得谈论的,毕竟小学的时候体育老师就教过我们了.然而对于编程中使用的除法,我觉得还是有很多值得注意的细节的.为什么我想深究一下?因为我日常主要使用Java和Python编程, ...

  7. 计算机中的二进制除法

    文章目录 计算机中的二进制除法 一.原码除法的规则 二.恢复余数法 三.加减交替法 计算机中的二进制除法 一.原码除法的规则 原码除法运算规则如下: (1)除数≠0.对于定点纯小数,|被除数|< ...

  8. MatLab中多项式

    MatLab中多项式 多项式在MatLab中的表达 多项式的求根方法 多项式的加减乘除运算 多项式的导数和积分 多项式的估值函数 多项式运算函数的总结 多项式的其他操作函数 多项式在MatLab中的表 ...

  9. c语言中有余数的除法,《有余数的除法》课堂实录

    [ 前言]有余数的除法是位于三下第二章<除数是一位数的除法>中较后面的部分,是对除法竖式运算理解模式的一个完型,这堂课整体是建立在师生对话的基础上的,以学生为中心,老师为引导者和主持者. ...

最新文章

  1. 部署 instance 到 VXLAN - 每天5分钟玩转 OpenStack(112)
  2. 《HelloGitHub》第 20 期
  3. PostgreSQL9中stream同步与Slony同步的比较
  4. Mysql8.0.12安装教程方法 Mysql8.0.12安装教程
  5. 奇异递归模板模式(Curiously Recurring Template Pattern,CRTP)
  6. AD维护管理工具详解(一)dcdiag
  7. 《从零开始学习jQuery》及《jQuery风暴》学习笔记
  8. 工控补丁星期二:西门子、施耐德电气修复40个漏洞
  9. urllib携带登录信息
  10. windows批量修改文件后缀名
  11. 演化算法与适应度地形分析——再度思考
  12. 美团面试小感——认知撑起的格局
  13. 【Flutter小记10】apk 提交各大应用市场,出现armeabi与arm64 版本标识/版本号不一致无法上传审核的解决方案
  14. 云计算是什么通俗解释_什么是云? 解释
  15. 史克鲁克唱诗班-我最爱的!
  16. 面试-操作系统-进程管理-进程-进程调度-死锁
  17. EA周报 | Libra将与支付宝微信展开竞争;Google终止中国版搜索引擎;滴滴柳青:顺风车目前不能上线是因为害怕...
  18. java程序设计模拟题_《Java程序设计》东师模拟题题目及答案
  19. js根据年份计算年龄
  20. FPGA学习经验(BBS论坛)

热门文章

  1. 前端js——定时器、定时跑、加速运动
  2. 安卓app调试工具(chrome)
  3. Centos Linux没有显示IP
  4. Vue的基本使用步骤
  5. Rust权威指南之编写自动化测试
  6. Python 脚本备份华为交换机
  7. Java:2022年全球使用的15种最流行的Java应用
  8. 掘金、聚宽和米筐各量化平台优缺点2
  9. python实现寻找最长回文子串
  10. 安卓系统双屏异显_Android 双屏异显