PtPoly PolySubtraction (PtPoly ppoly1, PtPoly ppoly2) { PtPoly Sub; unsigned int Degree, I, Invert = 0; if (!ValidPolys (ppoly1, ppoly2)) return NULL; if(ppoly1->Degree < ppoly2->Degree) { Degree = ppoly1->Degree ; Invert = 1; }else{ Degree = ppoly2-> Degree; } if ((Sub = PolyCopy ((ppoly1->Degree >= ppoly2->Degree) ? ppoly1 : ppoly2)) == NULL) { Error = NO_MEM; return NULL; } Error = OK; if (Invert) Sub = PolySymmetrical(Sub); for (I = 0; I <= Degree; I++) Sub->Poly[I] = ppoly1->Poly[I] - ppoly2->Poly[I]; ReducePoly(Sub); return Sub; }
PtPoly PolyMultiplication (PtPoly ppoly1, PtPoly ppoly2) { if(!ValidPolys(ppoly1,ppoly2)) return NULL; if(ppoly1==NULL || ppoly2==NULL){ Error = NO_POLY; return NULL; } PtPoly mul; int degree = ppoly1->Degree + ppoly2->Degree; if((mul = PolyCreate(degree)) == NULL){ return NULL; } for (int i = 0; i <= ppoly1->Degree ; ++i) { for (int j = 0; j <= ppoly2->Degree ; ++j) { mul->Poly[i + j] += ppoly1->Poly[i] * ppoly2->Poly[j]; } } Error = OK; ReducePoly(mul); return mul; }
int PolyEquals (PtPoly ppoly, PtPoly ppoly2) { bool sameDegree = false; bool sameElem = false; ReducePoly(ppoly); ReducePoly(ppoly2); if(ppoly->Degree == ppoly2->Degree){ sameDegree = true; }else{ return 0; } for (int i = 0; i <= ppoly2->Degree; ++i) { if(ppoly->Poly[i]!=ppoly2->Poly[i]){ sameElem = false; return 0; } } return 1; }
PtPoly PolyAddition (PtPoly ppoly1, PtPoly ppoly2) { if(!ValidPolys(ppoly1,ppoly2)) return NULL; if(ppoly1==NULL || ppoly2==NULL){ Error = NO_POLY; } int smallestDegree; int biggestDegree; int biggest = 0; /* considera-se o ppoly1 */ if(ppoly1->Degree > ppoly2->Degree){ biggestDegree = ppoly1->Degree; smallestDegree = ppoly2->Degree; }else{ biggestDegree = ppoly2->Degree; smallestDegree = ppoly1->Degree; biggest = 1; } PtPoly sum; if((sum = PolyCreate(biggestDegree)) == NULL){ return NULL; } for (int i = 0; i <= smallestDegree ; ++i) { sum->Poly[i] = ppoly1->Poly[i] + ppoly2->Poly[i]; } for (int j = smallestDegree + 1; j <= biggestDegree; ++j) { if(biggest) sum->Poly[j] = ppoly2->Poly[j]; else sum->Poly[j] = ppoly1->Poly[j]; } Error = OK; ReducePoly(sum); return sum; }
PtPoly PolyCopy (PtPoly ppoly) { PtPoly Copy; int I; /* verifica se o vector existe - verifies if vector exists */ if (ppoly == NULL) { Error = NO_POLY; return NULL; } /* criação do vector copia nulo - creating an empty vector */ if ((Copy = PolyCreate (ppoly->Degree+1)) == NULL) return NULL; /* fazer a copia do vector - copying the components */ for (I = 0; I < ppoly->Degree+1; I++) Copy->Poly[I] = ppoly->Poly[I]; ReducePoly(Copy); return Copy; /* devolve o vector copia - returning the new vector */ }
PtPoly PolySubtraction (PtPoly ppoly1, PtPoly ppoly2) { PtPoly sub; /* Check validity of polynomials */ if (!ValidPolys(ppoly1, ppoly2)) return NULL; int smallestDegree; int biggestDegree; int biggest = 0; /* considera-se o ppoly1 */ if(ppoly1->Degree > ppoly2->Degree){ biggestDegree = ppoly1->Degree; smallestDegree = ppoly2->Degree; }else{ biggestDegree = ppoly2->Degree; smallestDegree = ppoly1->Degree; biggest = 1; } if((sub = PolyCreate(biggestDegree)) == NULL){ return NULL; } for (int i = 0; i <= smallestDegree ; ++i) { sub->Poly[i] = ppoly1->Poly[i] - ppoly2->Poly[i]; } for (int j = smallestDegree + 1; j <= biggestDegree; ++j) { if (biggest && sub->Poly[j] != 0) sub->Poly[j] = ppoly2->Poly[j] * (-1); else sub->Poly[j] = ppoly1->Poly[j]; } ReducePoly(sub); Error = OK; return sub; }
PtPoly PolyAddition (PtPoly ppoly1, PtPoly ppoly2) { PtPoly Add; unsigned int I, Degree; if (!ValidPolys (ppoly1, ppoly2)) return NULL; Degree = (ppoly1->Degree < ppoly2->Degree) ? ppoly1->Degree : ppoly2-> Degree; if ((Add = PolyCopy ((ppoly1->Degree >= ppoly2->Degree) ? ppoly1 : ppoly2)) == NULL) { Error = NO_MEM; return NULL; } Error = OK; for (I = 0; I <= Degree; I++){ Add->Poly[I] = ppoly1->Poly[I] + ppoly2->Poly[I]; } ReducePoly(Add); return Add; }
PtPoly PolyMultiplication (PtPoly ppoly1, PtPoly ppoly2) { PtPoly Mult; unsigned int I,J,TotalDegree; if (!ValidPolys (ppoly1, ppoly2)) return NULL; TotalDegree = ppoly1->Degree + ppoly2->Degree; if ((Mult = PolyCreate(TotalDegree)) == NULL) { Error = NO_MEM; return NULL; } Error = OK; for (I = 0; I <= TotalDegree; I++) Mult->Poly[I] = 0; for (I = 0; I <= ppoly1->Degree; I++ ){ for(J = 0; J <= ppoly2->Degree; J++) Mult->Poly[I+J] += ppoly1->Poly[I] * ppoly2->Poly[J]; } ReducePoly(Mult); return Mult; }