// like test_intersect1, but different orientation static int test_intersect7(double EPS) { struct geo_Point p1[4]; struct geo_Point p2[4]; struct geo_Point pi[8]; struct geo_Point psoll[4]; int num_points; int ok; p1[0].x = 1; p1[0].y = 1; p1[1].x = 1; p1[1].y = 3; p1[2].x = 3; p1[2].y = 3; p1[3].x = 3; p1[3].y = 1; p2[0].x = 2; p2[0].y = 2; p2[1].x = 2; p2[1].y = 4; p2[2].x = 4; p2[2].y = 4; p2[3].x = 4; p2[3].y = 2; ok = geo_intersect_polygons(pi, &num_points, p1, 4, p2, 4); if (!ok) return 0; psoll[0].x = 2; psoll[0].y = 3; psoll[1].x = 3; psoll[1].y = 3; psoll[2].x = 3; psoll[2].y = 2; psoll[3].x = 2; psoll[3].y = 2; if (!poly_equals(pi, num_points, psoll, 4, EPS)) { printf("Result:\n"); poly_print(pi, num_points); printf("\nBut expected:\n"); poly_print(psoll, 4); return 0; } return 1; }
int root_laguerre(double tol, int maxit, const polyr *p, double x0, double *res) { polyr pd, pdd; int deg = poly_deg(p); double x = x0; double y = poly_eval(p, x); int n = 0; poly_diff(p, &pd); poly_diff(&pd, &pdd); #ifdef DEBUG fprintf(stderr, "tol: %g maxit: %d x0: %g\n", tol, maxit, x0); poly_print(stderr, p); poly_print(stderr, &pd); poly_print(stderr, &pdd); #endif while (fabs(y) > tol && n < maxit) { double g = poly_eval(&pd, x) / poly_eval(p, x); double h = g*g - poly_eval(&pdd, x) / poly_eval(p, x); double sqrtarg = (deg-1)*(deg*h - g*g); double sq = sqrt(sqrtarg); double denom = (g > 0 ? g+sq : g-sq); x = x - deg/denom; y = poly_eval(p, x); n++; #ifdef DEBUG fprintf(stderr, "g: %g h: %g sqa: %g sq: %g x: %g y: %g\n", g, h, sqrtarg, sq, x, y); #endif /* complex roots => EDOM, divide by zero => NAN etc */ if (sqrtarg < 0 || isinf(x) || isnan(x) || errno == EDOM || errno == ERANGE) return 0; } if (fabs(y) > tol) return 0; *res = x; return 1; }
// if they are it prints that polinomial and its derivative are // both zero int main(void) { int degree=0; // Polynomial degree. int i=0; // Counter variable. int *coeff; // points at the base of dynamic array used for storring coefficients of polynomial. while (degree>=0) // Loop that repeat the program unless degree<0 { degree = poly_d(); coeff = calloc(degree, sizeof(int)); // Cretes required number of spaces in memory if (degree>=0) { poly_c(coeff, degree); poly_print(coeff, degree); deriv_print(coeff, degree); } free(coeff); //Frees the memory. } return 0; }
int main(void) { int degree=0; // Polynomial degree. int i=0; // Counter variable. int coeff[11], c; // An array for storring degrees of polynomial. int *p; // pointers to an array coeff[]. p = coeff; while ((degree>=0) && (degree<=10)) { degree=poly_d(); if (degree>=0) { poly_c(p, degree); poly_print(coeff, degree); c = poly_r(coeff, degree); if (c == 0) return 0; } } return 0; }
main() { ListHeader list1, list2, list3; init(&list1); init(&list2); init(&list3); // 다항식 1을 생성 insert_node_last(&list1, 3,12); insert_node_last(&list1, 2,8); insert_node_last(&list1, 1,0); // 다항식 2를 생성 insert_node_last(&list2, 8,12); insert_node_last(&list2, -3,10); insert_node_last(&list2, 10,6); // 다항식 3 = 다항식 1 + 다항식 2 poly_add(&list1, &list2, &list3); poly_print(&list3); }
int main(void) { DList listhdr1, listhdr2, listhdr3, listhdr4; int coef, expon; initList(&listhdr1); initList(&listhdr2); initList(&listhdr3); initList(&listhdr4); //다항식 p(x) 입력 printf("다항식 p(x)의 계수, 차수 쌍을 입력하세요. 끝이면 0 0을 입력하세요. \n"); while(1){ scanf("%d %d", &coef, &expon); if(coef==0){ if(expon!=0) printf("계수가 0인 값은 입력 할 필요가 없습니다! \n"); else break; } else insert_node(&listhdr1, coef, expon); } poly_sort(&listhdr1); //p(x) 내림차순 정렬 poly_dup(&listhdr1); //p(x) 중복 차수 제거 fflush(stdin); //입력 버퍼 비우기(입력값 오류 방지) //다항식 q(x) 입력 printf("다항식 q(x)의 계수, 차수 쌍을 입력하세요. 끝이면 0 0을 입력하세요. \n"); while(1){ scanf("%d %d", &coef, &expon); if(coef==0){ if(expon!=0) printf("계수가 0인 값은 입력 할 필요가 없습니다! \n"); else break; } else insert_node(&listhdr2, coef, expon); } poly_sort(&listhdr2); //q(x) 내림차순 정렬 poly_dup(&listhdr2); //q(x) 중복 차수 제거 fflush(stdin); //입력 버퍼 비우기(입력값 오류 방지) poly_add(&listhdr1, &listhdr2, &listhdr3); poly_sub(&listhdr1, &listhdr2, &listhdr4); printf("**************** Input value ****************\n"); printf("p(x)="); poly_print(&listhdr1); printf("\n"); printf("q(x)="); poly_print(&listhdr2); printf("\n"); printf("**************** Result ****************\n"); printf("p(x)+q(x)="); poly_print(&listhdr3); printf("\n"); printf("p(x)-q(x)="); poly_print(&listhdr4); printf("\n"); system("pause"); return 0; }
int main (void) { poly *P0, *P1, *P2, *P3, *M1, *M2, *P4, *P5, *P6, *P7; P0=poly_create(4,2,1,3,2,6,5,1,7); //4 terms: 1x^7 + 6x^5 + 3x^2 + 2x^1 P1=poly_create(3,3,2,1,4,6,5); //3 terms: 6x^5 + 1x^4 + 3x^2 P2=poly_create(1,16,2); //1 term: 16x^2 P3=poly_create(0); // no terms printf("P0: "); poly_print(P0); printf("P1: "); poly_print(P1); printf("P2: "); poly_print(P2); printf("P3: "); poly_print(P3); M1=poly_scalar_mult(P1,2); printf("M1 (P1*2): "); poly_print(M1); M2=poly_scalar_mult(P3,4); printf("M2 (P3*4): "); poly_print(M2); P4=poly_duplicate(M1); printf("P4 (dup M1): "); poly_print(P4); P5=poly_add(P1,P0); printf("P5 (P1+P0): "); poly_print(P5); P6=poly_add(P3,P3); printf("P6 (P3+P3): "); poly_print(P6); P7=poly_add(P0,P3); printf("P7 (P0+P3): "); poly_print(P7); poly_free(&P1); if (P1==NULL) printf("P1: was freed\n"); else printf("P1: is not null!!\n"); poly *test1=poly_create(4,2,1,3,2,6,5,1,7); //4 terms: 1x^7 + 6x^5 + 3x^2 + 2x^1 poly *test2=poly_duplicate(test1); test2=poly_scalar_mult(test2, 5); printf("test1: ");poly_print(test1); printf("test2: ");poly_print(test2); //printf("%p, %p\n", test1, test2); poly_free(&test1); poly *MM1 = poly_create(5,1,1,2,2,3,3,4,4,5,5); poly *MM2 = poly_create(5,-1,1,-2,2,-3,3,-4,4,-5,5); printf("MM1: ");poly_print(MM1); printf("MM2: ");poly_print(MM2); poly *MM1_Plus_MM2 = poly_add(MM1, MM2); printf("(MM1+MM2): ");poly_print(MM1_Plus_MM2); poly *MM1_Multiplied_By_5 = poly_scalar_mult(MM1, 5); poly *MM2_Multiplied_By_10 = poly_scalar_mult(MM2, 10); printf("MM1*5: ");poly_print(MM1_Multiplied_By_5); printf("MM2*10: ");poly_print(MM2_Multiplied_By_10); printf("MM1: ");poly_print(MM1); printf("MM2: ");poly_print(MM2); }