【问题标题】:Multiplication of polynomials in c++c++中多项式的乘法
【发布时间】:2019-04-26 19:10:59
【问题描述】:

我输入一个数组,代码应该将该数组乘以(x^2 + 2*A*X + A^A)。 例如,如果A = 1,数组1, 3, 3, 1 应该有答案1, 5, 10, 10, 5, 1。我试图编写代码,但我不知道我应该如何在 C++ 中乘以多项式。

代码

#include "pch.h"
#include <iostream>

int main()
{
int a, n;

std::cout << "Enter the Power n: "; //Power decides the size of array.
std::cin >> n;

std::cout << "Enter the number A: ";
std::cin >> a;

int arr_size = n + 1;       

int *array = new int[2 * arr_size];

std::cout << "Enter the array P(X): ";

for (int i = 0; i < arr_size; i++)
{
    std::cin >> array[i];
}

for (int i = 0; i < arr_size; i++)
    std::cout << array[i] << " ";

const int x = 1;
for (int i = 0; i < arr_size; i++) {
    array[i] = array[i] * x ^ 2;
    array[i] = array[i] * (2 * a*x);
    array[i] = array[i] * (a * a);

    std::cout << array[i] << " ";
}
return 0;
}

【问题讨论】:

  • 只需将 std::transform() 与函子一起使用即可。
  • ^ 运算符是按位或,而不是求幂。只需将x 与自身相乘即可。当然,Aᴬ 不等于 A * A
  • 哦,请阅读how to ask good questionsthis question checklist。然后请编辑您的问题以实际包含一个问题。目前还不清楚您的问题是什么,您实际上在问什么。
  • 我问清楚,多项式如何相乘?有什么好暧昧的?
  • @Someprogrammerdude 这是按位异或,可能是自动更正更改?

标签: c++ arrays c++11 matrix-multiplication vector-multiplication


【解决方案1】:
#include <iostream>
#include <vector>

int main() {
    // coefs before multiplying by powers of A
    std::vector<int> a = {1,2,1};

    // make polynomial coefs vector
    int A = 1;
    const int as = a.size();
    int ap = 1;
    for(int ai = 0; ai<as; ++ai){
        a[ai] *= ap;
        ap *= A;
    }

    std::vector<int> b = {1,3,3,1};
    const int bs = b.size();

    // result vector
    std::vector<int> c(as + bs - 1, 0);

    // multiply vectors
    for(int ai = 0; ai<as; ++ai)
        for(int bi = 0; bi<bs; ++bi){
            c[ai+bi] += a[ai]*b[bi];
        }

    for(int cn: c)
       std::cout << cn << ' ';
    return 0;
}

【讨论】:

    【解决方案2】:

    您应该为此使用一个输出数组并适当地乘以您的结果。

    我正在考虑索引 = 多项式指数(即 [0]x**0):

    std::vector<int> array(arr_size, 0);
    
    std::cout << "Enter the array P(X): ";
    
    for (int i = 0; i < arr_size; i++)
    {
        std::cin >> array[i];
    }
    
    for (int i = 0; i < arr_size; i++)
        std::cout << array[i] << " ";
    std::cout << std::endl;
    
    std::vector<int> out(arr_size+2, 0); // We add two new coefficients
    
    for (int i = 0; i < arr_size; i++) {
        out[i+2] += array[i]; // bumps the coefficient for x**2
        out[i+1] += array[i] * (2 * a); // add the coefficient for 2ax
        out[i] += array[i] * (a * a); // Adds the coefficient for a**2
    
    }
    for(int coeff: out)
        std::cout << coeff << " ";
    std::cout << std::endl;
    

    【讨论】:

      【解决方案3】:

      在我看来,您可以通过std::valarray 解决问题

      我的意思是……如果你的 array

      std::valarray<int>  array(arr_size);
      

      接下来你可以简单地写

      array *= x*x + 2*a*x + a*a;
      

      离题建议:您使用的是 C++11,因此请避免直接管理您的动态内存。

      在你的代码中你已经分配了一个数组

      int *array = new int[2 * arr_size];
      

      但你还没有删除它。

      尽可能使用标准容器(std::valarraystd::vector),当您必须管理动态内存时,使用智能指针(std::unique_ptrstd::shared_ptr 等)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多