Created
November 3, 2010 17:34
-
-
Save justjkk/661402 to your computer and use it in GitHub Desktop.
C Library for Polynomials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdlib.h> | |
typedef struct | |
{ | |
int order; | |
int *coeff; | |
}poly; | |
poly clone(poly a) | |
{ | |
poly c; | |
int i, n; | |
c.order = a.order; | |
for(i = 0; i <= c.order; i++ ) | |
{ | |
c.coeff[i] = a.coeff[i]; | |
} | |
return c; | |
} | |
poly multiply(poly a, poly b) | |
{ | |
poly res; | |
int i, o1, o2, n; | |
res.order = o1 + o2; | |
res.coeff = (int *)malloc(sizeof(int * (res.order + 1))); | |
for(i = 0; i <= res.order; i++) | |
res.coeff[i] = 0; | |
for(i = 0; i <= o1; i++) | |
for(j = 0; j <= o2; j++) | |
res.coeff[i+j] += a.coeff[i] * b.coeff[j]; | |
return res; | |
} | |
poly add(poly a, poly b) | |
{ | |
int o1, o2, i; | |
poly res; | |
res.order = (o1>o2)?o1:o2; | |
res.coeff = (int *)malloc(sizeof(int * (res.order + 1))); | |
for(i = 0; i <= o1 && i <= o2; i++) | |
{ | |
res.coeff[i] = a.coeff[i] + b.coeff[i]; | |
} | |
while(i <= o1) | |
{ | |
res.coeff[i] = a.coeff[i]; | |
i++; | |
} | |
while(i <= o2) | |
{ | |
res.coeff[i] = b.coeff[i]; | |
i++; | |
} | |
res.order = i-1; | |
return res; | |
} | |
poly subtract(poly a, poly b) | |
{ | |
int o1, o2, i; | |
poly res; | |
res.order = (o1>o2)?o1:o2; | |
res.coeff = (int *)malloc(sizeof(int * (res.order + 1))); | |
for(i = 0; i <= o1 && i <= o2; i++) | |
{ | |
res.coeff[i] = a.coeff[i] - b.coeff[i]; | |
} | |
while(i <= o1) | |
{ | |
res.coeff[i] = a.coeff[i]; | |
i++; | |
} | |
while(i <= o2) | |
{ | |
res.coeff[i] = - b.coeff[i]; | |
i++; | |
} | |
res.order = i-1; | |
return res; | |
} | |
poly generate_node_x() | |
{ | |
poly new_node; | |
new_node.order = 1; | |
new_node.coeff = (int *)malloc(sizeof(int*2)); | |
new_node.coeff[0] = 0; | |
new_node.coeff[1] = 1; | |
return new_node; | |
} | |
#define AND(a,b) multiply(a,b) | |
#define OR(a,b) add(a,subtract(b,multiply(a,b))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment