עבור לתוכן

כפל פולינומים c++

Featured Replies

פורסם

שלום

יש לי פה קוד לכפל פולינומים, מישהו יכול להסביר לי איך הוא עובד? עברתי עליו כמה פעמים ולא הצלחתי לעקוב ולהבין

/*******************************************/

[right]
#include<iostream>
/*******************************************/
const int SIZE = 7;//polynomial size
/*******************************************/
using namespace std;
/*******************************************/
void get(int*);//get values for polynom
void print(int*);//print polynom
void printResult(int*);//print result polynom
void mul(int,int*,int*,int*);//multiply function
void add(int*,int*,int*,int);//calculate intermediate result
/*******************************************/
int main()
{
int* first=NULL;
int* second=NULL;
int* result=NULL;
first=new int[SIZE];
second=new int[SIZE];
result=new int[SIZE*2-1];
for(int i=0;i<SIZE*2-1;i++)//init result array
result[i]=0;
get(first);
get(second);
cout<<"Result :"<<endl;
mul(SIZE,first,second,result);
printResult(result);
return 0;
}
/*******************************************/
void get(int* array)//get values for polynom
{
int num;
cout<<"Enter "<<SIZE<<" parameters: "<<endl;
for(int i=0;i<SIZE;i++){
cin>>num;
array[i]=num;
}
cout<<endl<<"Your equestion is: ";
print(array);
}
/*******************************************/
void print(int* array)//print polynom
{
int t=SIZE-1;
if(array[0]!=0)
cout<<array[0]<<"x^"<<t--;
for(int i=1;i<SIZE-1;i++,t--){
if(array[i]<0)
cout<<array[i]<<"x^"<<t;
else
cout<<"+"<<array[i]<<"x^"<<t;
}
if(array[SIZE-1]!=0){
if(array[SIZE-1]>0)
cout<<"+";
cout<<array[SIZE-1];
}
cout<<endl;
}
/*******************************************/
void printResult(int* array)//print result polynom
{
int t=SIZE*2-2;
if(array[0]!=0)
cout<<array[0]<<"x^"<<t--;
for(int i=1;i<SIZE*2-2;i++,t--){
if(array[i]<0)
cout<<array[i]<<"x^"<<t;
else
cout<<"+"<<array[i]<<"x^"<<t;
}
if(array[SIZE*2-2]!=0){
if(array[SIZE*2-2]>0)
cout<<"+";
cout<<array[SIZE*2-2];
}
cout<<endl;
}
/*******************************************/
void add(int* a,int* b,int* c,int n)//calculate intermediate result
{
for(int i=0;i<n;i++)
c[i]=a[i]+b[i];
}
/*******************************************/
void mul(int tempSize,int* first,int* second,int* result)//calculate intermediate result
{

if(tempSize==1){//stop condition
result[tempSize-1]=first[tempSize-1]*second[tempSize-1];
}
else{//divide polynom's on three equal parts each one
int t=tempSize/3;
int* a1=&first[(2*tempSize)/3];
int* a2=&first[tempSize/3];
int* a3=&first[0];
int* b1=&second[(2*tempSize)/3];
int* b2=&second[tempSize/3];
int* b3=&second[0];

int* t1=new int[2*t-1];//1
int* t2=new int[2*t-1];//2
int* t3=new int[2*t-1];//3
int* t4=new int[2*t-1];//4
int* t5=new int[2*t-1];//5
int* t6=new int[2*t-1];//6
int* zz1=new int[t];//array for intermediate result
int* zz2=new int[t];//array for intermediate result
int* zz3=new int[t];//array for intermediate result
int* zz4=new int[t];//array for intermediate result
int* zz5=new int[t];//array for intermediate result
int* zz6=new int[t];//array for intermediate result
for(int i=0;i<2*t-1;i++){//init temp arrays
t1[i]=0;
t2[i]=0;
t3[i]=0;
t4[i]=0;
t5[i]=0;
t6[i]=0;
}
//function calls for three first steps
mul(t,a1,b1,t1);
mul(t,a2,b2,t2);
mul(t,a3,b3,t3);
//calculate intermediate result for each one
add(a1,a2,zz1,t);
add(b1,b2,zz2,t);
add(a2,a3,zz3,t);
add(b2,b3,zz4,t);
add(a1,a3,zz5,t);
add(b1,b3,zz6,t);
//function calls for three first steps
mul(t,zz1,zz2,t4);
mul(t,zz3,zz4,t5);
mul(t,zz5,zz6,t6);

for(int i=0;i<t*2-1;i++){//merge all intermediate result in result array
result[i]+=t3[i];//highest power
result[i+tempSize/3]+=t5[i]-t2[i]-t3[i];//x^n
result[i+2*tempSize/3]+=t6[i]-t1[i]-t3[i]+t2[i];//x^2/3n
result[i+tempSize]+=t4[i]-t1[i]-t2[i];//x^1/3n
result[i+tempSize+t]+=t1[i];//lowest power
}
}
}
/*******************************************/[/right]

פורסם

אתה יודע מה זה פולינום ואיך מכפילים פולינומים?

אם אתה מנסה להכפיל את הפולינומים הבאים

a1x+a2

ב

b1x^2+b2x+b3

כאשר x הוא משתנה הפולינום, מה אתה מקבל?

a1b1x^3+a1b2x^2+a1b3x+a2b1x^2+a2b2x+a2b3

אם אתה מקבץ (חוק הקיבוץ) לפי משתנה אתה מקבל

a1b1x^3+(a1b2+a2b1)X^2+(a1b3+a2b2)x+a2b3

בקוד עושים את זה בדרך טיפשית וקבועה. מישהוא פתר את המכפלה עבור פולינום בדרגה 3 וזהו. שם עצר. הפתרון בפונקציה mult.

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים