1、输入[2,1,3],[4,5] 表示$$(2x^2+x+3)\times(4x+5)$$
输出[8, 14, 17, 15] 表示$$ 8x^3+14x^2+17x+15 $$
2、输入[2,1,3],[4,5],'x' 表示$$(2x^2+x+3)\times(4x+5)$$
输出 $$ 8x^3+14x^2+17x+15 $$
'''
#a=[1,-2,5,6]
#b=[3,1,4]
#note = 'x'
#note="" 输出 [9,-5,6,4,3]
#note="x" 输出 "9x^4-5x^3+6x^2+4x+3"
'''
def ployMul(a,b,note=""): #多项式乘法 输入 系数list
c = []
zeros=0
for i in range(len(a)):
t = []
for bz in range(zeros):
t.append(0)
for bb in range(len(b)):
t.append(b[bb])
for bz in range(len(a)-zeros-1):
t.append(0)
zeros += 1
c.append(t)
t = []
#print(c)
for i in range(len(a)):
for j in range(len(c[0])):
c[i][j] = c[i][j]*a[i]
#print (c)
d=[]
for i in range(len(c[0])):
t = 0
for j in range(len(a)):
t += c[j][i]
d.append(t)
#print(d)
if len(note)==0:
return d
else:
s = ""
power=len(d)-1
for i in d:
if power == 0:
if power == len(d)-1:
if i != 0:
s+=co0(i)
else:
if i != 0:
s+=co3(i)
elif power == 1:
if power == len(d)-1:
if i != 0:
s+=co1(i)+note
else:
if i != 0:
s+=co2(i)+note
elif power == len(d)-1:
if i != 0:
s+=co1(i)+note+"^"+str(power)
else:
if i != 0:
s+=co2(i)+note+"^"+str(power)
power -= 1
return s
其中用到的函数
"""
返回从 a 到 b 的随机数
sign 显示正负号+- one 1 是否 显示 比如 1x中不用显示 ,zero 0是否显示,比如0x中用显示
b 不输入 表示 只有一个数 a 进行处理
"""
def randint(a,b = 9999919999,sign = True, one = True, zero = True):
if b==9999919999:
b = a
i = random.randint(a,b)
ii = str(i)
if sign:
if i>1:
ii = "+"+str(i)
elif i == -1:
if one :
ii = str(i)
else:
ii = "-"
elif i == 1:
if one :
ii = "+"+str(i)
else:
ii = "+"
elif i == 0:
if zero:
ii = "+"+str(i)
else:
ii = ""
else :#<0
ii = str(i)
else :
if i>1:
ii = str(i)
elif i == 1 :
if one :
ii = str(i)
else:
ii = ""
elif i == -1 :
if one :
ii = str(i)
else:
ii = "-"
elif i == 0:
if zero:
ii = str(i)
else:
ii = ""
else :#<0
ii = str(i)
return ii
#普通的数字a*x方+bx+c=d中的 d
def co0(a,b= 9999919999):
return randint(a,b,sign = False, one = True, zero = True)
#第一个系数 a*x方+bx+c=d中的 a
def co1(a,b= 9999919999):
if a==0 and b==0:
return ("0")
if b == 9999919999:
return randint(a,a,sign = False, one = False, zero = True)
if(a*b<0):
if random.random()> (b/(b-a)):
return randint(a,-1,sign = False, one = False, zero = True)
else:
return randint(1,b,sign = False, one = False, zero = True)
else:
return randint(a,b,sign = False, one = False, zero = True)
#中间系数a*x方+bx+c=d中的 b
def co2(a,b= 9999919999):
return randint(a,b,sign = True, one = False, zero = True)
#常数 系数a*x方+bx+c=d中的 c
def co3(a,b= 9999919999):
return randint(a,b,sign = True, one = True, zero = False)
随机生成多项式,最高m次,其中至少n个实数根(ax+b)
def randPloyMul(m,n=1,note='x'): #随机生成多项式,最高m次,其中至少n个实数根(ax+b)
if m<n:
return
k = m-n
kk = []
for i in range(k+1):# 余式
if random.random()>0.5:
kk.append(random.randint(1,9))
else:
kk.append(random.randint(-9,-1))
for i in range(n):#一次项式
d = []
d.append(random.choice([-3,-2,2,3,5,1]))
tt=random.choice([-3,-2,2,3,5,1,-1])
while tt == d[0] or tt == -d[0]:
tt=random.choice([-3,-2,2,3,5])
d.append(tt)
kk = ployMul(kk,d)
return ployMul(kk,[1],note) #只是加了个 x notes
简单:其中至少有一项为0,系数不大于MAXCO 15
def randPloyMulSmall(m,n=1,note='x'):#随机生成多项式,最高m次,其中至少n个实数根(ax+b)
#######至少有一个0系数项, 系数不大于MAXCO = 15
p = randPloyMul(m,n,"")
MAXCO = 15
havezero = False
havebig = False
for i in p:
if i == 0:
havezero = True
if abs(i) > MAXCO:
havebig = True
while ( not havezero )or havebig:
p = randPloyMul(m,n,"")
havezero = False
havebig = False
for i in p:
if i == 0:
havezero = True
if abs(i) > MAXCO:
havebig = True
return ployMul(p,[1],"x") #只是加了个 x notes