简体   繁体   English

将数组转换为多项式

[英]Transforming an array into a polynomial

I am doing a Numerical Analysis, and I got a matrix as my result.我正在进行数值分析,结果得到了一个矩阵。 As a result, I'd like to use the matrix or the array to fast create the polynomials.因此,我想使用matrix或数组来快速创建多项式。

import matplotlib.pyplot as plt
import matplotlib.collections
import numpy as np

x1 = -2
x2 = -2 + (8 / 3)
x3 = -2 + (16 / 3)
x4 = -2 + (24 / 3)

A = np.array(
    [
        [1, x1, x1 ** 2, x1 ** 3],
        [1, x2, x2 ** 2, x2 ** 3],
        [1, x3, x3 ** 2, x3 ** 3],
        [1, x4, x4 ** 2, x4 ** 3],
    ]
)

R1 = np.array([[1], [0], [0], [0]])
N1 = np.linalg.solve(A, R1)


# The N1..N4 are my coefficients for my polynomium of third order.
# below I try to transform the N1 into a polynomium N1p`

N1p = np.array(N1.transpose()).tolist()
P1 = np.poly(N1p)

It gives me an error of dimension.它给了我一个尺寸错误。 Is there anyone to help me?有没有人可以帮助我?

Is this your expected output?这是您的预期输出吗?

N1p = N1.transpose().reshape(-1)
P1 = np.poly(N1p)
P1
array([ 1.00000000e+00,  3.41796875e-02, -3.67412567e-02,  2.04887241e-03,
    2.08630809e-05])

P1.tolist()

[1.0,
 0.0341796875,
 -0.03674125671386719,
 0.002048872411251068,
 2.0863080862909555e-05]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM