2020-09-12

多项式定义,相乘,合并同类项并结合随机系数。多项式定义参考博客

def polynomial(n):
    #指定随机种子,保证每次随机数都相同
    random.seed(0)
    #index 生成一个一维n的随机数矩阵
    index = random.sample(range(0,n),n)
    #生成一个一维n个的矩阵,每个值都为1
    pol = np.ones(n)
    #利用for循环将随机一维矩阵中的随机数和pol中的每一个元素相乘得到随机系数的多项式
    for i in range(n):
        pol[i] = pol[i] * index[i]
    #index随机数,pol为随机多项式
    return index,pol

def multiplication(pol1,pol2,remainder_num=None):
    #传入参数:为两个多项式pol1,pol2
    #mulp = list(np.convolve(pol1, pol2))
    mulp =np.convolve(pol1,pol2)
    n= len(pol1)
    bigger_n =mulp[0:n-1]
    bigger_n1 = mulp[n-1:]
    n2 =len(bigger_n)
    for i in range(n2):
        bigger_n1[i] =bigger_n[i] +bigger_n1[i]
    #remainder = [x % 3 for x in bigger_n1]
    if remainder_num is None:
        return bigger_n1
    else:
        remainder = bigger_n1 % remainder_num
        return bigger_n1, remainder

def test_of_time(n,remainder_num):
    pol1 =polynomial(n)[1]
    pol2=polynomial(n)[1]
    time_b= time.perf_counter()
    out_put = multiplication(pol1,pol2,remainder_num)
    time_e =time.perf_counter()
    time_cost =time_e -time_b
    return out_put, time_cost

if __name__ == '__main__':
    import random
    import numpy as np
    import time

    pol1 = np.array([1, 2, 3])
    pol2 = np.array([1, 2, 3])
    print(multiplication(pol1, pol2,None))
    print(test_of_time(10,3))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值