nodeptr add_poly(nodeptr first, nodeptr second)
{   float x;
    nodeptr head;
    int flag = 0;
    nodeptr t1 = first->link;
    nodeptr t2 = second->link;
    nodeptr t;
    head=getnode();
    head->link = head;

    while(t1!=first) //Inserting the nodes that have coeffs in both lists, and the distinct coeff nodes of list 1
    {
        t2 = second->link;
        flag = 0;
        for(t2;t2!=second;t2=t2->link)
        {
        if(t1->px==t2->px)
        {
            flag = 1;
            x = t1->cf + t2->cf;
            head = create_poly(x, t1->px,head);
        }
        }

        if(flag==0)
       head = create_poly(t1->cf,t1->px,head);

        t1 = t1->link;
    }


   t2 = second->link;
    while(t2!=second) //Inserting distinct coeff nodes of list 2
    {
        t1 = first->link;
        flag = 0;
        for(t1;t1!=first;t1=t1->link)
        if(t2->px==t1->px)flag = 1;


        if(flag==0)
        head = create_poly(t2->cf,t2->px,head);

        t2 = t2->link;
    }

        t = head->link;
      while(t!=head)
    {
         printf("%f x^%d  +  ", t->cf, t->px);
        t= t->link;
    }

}
Beispiel #2
0
Poly* legendre(size_t index)
{
    size_t i;
    Poly* res = NULL;
    poly_add_inp(&res, create_poly(1, 2));
    poly_add_inp(&res, create_poly(-1, 0));
    poly_pow_inp(&res, index);

    for (i = 0; i < index; i++)
        ;//poly_diff_inp(&res);

    poly_mul_cons_inp(&res, 1.0 / pow(2, index));
    poly_mul_cons_inp(&res, 1.0 / factorial(index));
    return res;
}
Beispiel #3
0
/*
 *description: the direct method of 
 *   best square approximation.
*/
Poly* bsa_direct(func f, double a, double b, size_t n)
{
    size_t i, j;
    Poly* res = NULL;
    Matrix* H = create_matrix_n(n + 1);
    Matrix* D = create_vector(n + 1);
    Matrix* A;
    for (i = 0; i < H->m; i++) {
        for (j = 0; j < H->n; j++) {
            double h = pow(b, i + j + 1) - pow(a, i + j + 1);
            H->mem[i][j] = h / (i + j + 1);
        }
    }
    for (i = 0; i < D->m; i++)
        D->mem[i][0] = integration(f, a, b, 1e-6);
    A = me_gauss_elim(H, D, false);
    
    for (i = 0; i < A->m; i++)
        poly_add_inp(&res, create_poly(A->mem[i][0], i));
    
    destroy_matrix(&H);
    destroy_matrix(&D);
    destroy_matrix(&A);
    return res;
}
int main()
{
    nodeptr one,two,sum;
    int x;
    int y=0;
    float c;
    one=getnode();
    two=getnode();
    sum=getnode();
    one->link=one;
    two->link=two;
    sum->link=sum;

    printf("1.enter first poly 2.enter second poly 3. add and display 4.exit");
    while(y!=4)
    {
        printf("enter choice");
        scanf("%d",&y);
        switch(y)
        {
          case 1:   printf("enter coefficient of x");
                    scanf("%f",&c);
                    printf("enter power of x");
                    scanf("%d",&x);
                    one=create_poly(c,x,one);
                    break;

        case 2:     printf("enter coefficient of x");
                    scanf("%f",&c);
                    printf("enter power of x");
                    scanf("%d",&x);
                    two=create_poly(c,x,two);
                    break;

        case 3:     sum=add_poly(one,two);



        }

    }

    return 0;
}
Beispiel #5
0
Datei: poly.c Projekt: wgtdkp/mym
/**
 *negative index is not allowed
 *@return: stored quotient in plhs
 */
void poly_div_inp(Poly** plhs, const Poly* rhs)
{
    Poly* lhs = *plhs;
    Poly odd = {.next = lhs};
    Poly* res = NULL;
    if (NULL == lhs || NULL == rhs)
        return;
    if (*plhs == rhs) { //lhs and rhs are the same poly
        destroy_poly(plhs);
        *plhs = create_poly(1, 0);
        return;
    }
    for (; NULL != lhs && lhs->index >= rhs->index;) {
        Poly *p1, *q1;
        const Poly* p2;
        Poly* item = create_poly(lhs->coeff / rhs->coeff,
             lhs->index - rhs->index);
        poly_add_inp(&res, poly_copy(item));
        //printf("res: \n");
        //print_poly(res);
        for (p1 = lhs, q1 = &odd, p2 = rhs; NULL != p1 && NULL != p2;) {
            //print_poly(p2);
            if (p1->index - p2->index == item->index) {
                p1->coeff -= p2->coeff * item->coeff;   //coeff may be zero
                if (DOUBLE_EQUAL(p1->coeff, 0)) {
                    destroy_poly_head(&p1); //move forward
                    q1->next = p1;
                }
                p2 = p2->next;
            } else if (p1->index - p2->index > item->index) {
                q1 = p1;
                p1 = p1->next;
            } else
                p2 = p2->next;
        }
        lhs = odd.next;
        //printf("lhs: \n");
        //print_poly(lhs);
    }
    *plhs = odd.next;
    destroy_poly(plhs);
    *plhs = res;
}
Beispiel #6
0
Datei: poly.c Projekt: wgtdkp/mym
Poly* poly_copy(const Poly* poly)
{
    Poly* p;
    const Poly* q;
    Poly odd = {.next = NULL};
    for (p = &odd, q = poly; NULL != q;) {
        p->next = create_poly(q->coeff, q->index);
        p = p->next;
        q = q->next;
    }
    return odd.next;
}
Beispiel #7
0
Datei: poly.c Projekt: wgtdkp/mym
void poly_pow_inp(Poly** poly, int n)
{
    int i;
    Poly* origin;
    if (0 == n) {
        *poly = create_poly(1, 0);
        return;
    }
    origin = poly_copy(*poly);
    for (i = 0; i < n - 1; i++)
        poly_mul_inp(poly, origin);
    destroy_poly(&origin);
}
Beispiel #8
0
int main()
{
	polynomial poly1 = create_poly();
	insert_node(poly1,10,1000);
	insert_node(poly1,5,14);
	insert_node(poly1,1,0);
	print_poly(poly1);

	polynomial poly2 = create_poly();
	insert_node(poly2,3,1990);
	insert_node(poly2,-2,14);
	print_poly(poly2);
	
	polynomial poly3 = create_poly();

	add_polynomial(poly1,poly2,poly3);

	print_poly(poly3);
	/*free the memory*/
	dispose_poly(poly1);
	dispose_poly(poly2);
	dispose_poly(poly3);
}
Beispiel #9
0
void init_region() {
   regions[0]=create_poly(p0k,p0l,p0A,p0b,p0center);
   regions[1]=create_poly(p1k,p1l,p1A,p1b,p1center);
   regions[2]=create_poly(p2k,p2l,p2A,p2b,p2center);
   regions[3]=create_poly(p3k,p3l,p3A,p3b,p3center);
   regions[4]=create_poly(p4k,p4l,p4A,p4b,p4center);
   regions[5]=create_poly(p5k,p5l,p5A,p5b,p5center);
   regions[6]=create_poly(p6k,p6l,p6A,p6b,p6center);
   regions[7]=create_poly(p7k,p7l,p7A,p7b,p7center);
   regions[8]=create_poly(p8k,p8l,p8A,p8b,p8center);
   regions[9]=create_poly(p9k,p9l,p9A,p9b,p9center);
   regions[10]=create_poly(p10k,p10l,p10A,p10b,p10center);
   regions[11]=create_poly(p11k,p11l,p11A,p11b,p11center);
   regions[12]=create_poly(p12k,p12l,p12A,p12b,p12center);
   regions[13]=create_poly(p13k,p13l,p13A,p13b,p13center);
   regions[14]=create_poly(p14k,p14l,p14A,p14b,p14center);
   regions[15]=create_poly(p15k,p15l,p15A,p15b,p15center);
   regions[16]=create_poly(p16k,p16l,p16A,p16b,p16center);
   regions[17]=create_poly(p17k,p17l,p17A,p17b,p17center);
   regions[18]=create_poly(p18k,p18l,p18A,p18b,p18center);
   regions[19]=create_poly(p19k,p19l,p19A,p19b,p19center);
};
Beispiel #10
0
void init_input_bound(void){
   input_bound=create_poly(puk,pul,puA,pub,pucenter);
};