Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

## Sparse Matrix representation for polynomials

August 26, 2012 Comments Off on Sparse Matrix representation for polynomials By admin

Sparse Matrix representation for polynomials

Program to represent two polynomials using Sparse Matrix representation using arrays, compute sum of two polynomials and represent the result using sparse matrix:

/* Sparse Matrix Representation for Polynomials */

#include <stdio.h>
#include <stdlib.h>

#define MAX 15

int main(){
int poly1[MAX][MAX]={0},poly2[MAX][MAX]={0},poly3[MAX][MAX]={0};
int i,j,k,m,n,nrows,nrows1,nrows2,nrows3,ncols,ct=0,ct1=0,ct2=0;
int spmat1[MAX][MAX]={0},spmat2[MAX][MAX]={0},spmat3[MAX][MAX]={0};
int deg,deg1,deg2,deg3;
printf(“nEnter degree of first polynomial?”);
scanf(“%d”,&deg1);
printf(“nEnter degree of second polynomial?”);
scanf(“%d”,&deg2);
deg3=(deg1>deg2)?deg1:deg2;

printf(“nFor first polynomial:”);
for(i=0;i<=deg1;i++){
printf(“nEnter coefficient for exponent %d> “,i);
scanf(“%d”,&poly1[0][i]);
if(poly1[0][i] != 0)
ct1++;
}
nrows1 = ct1 + 1;
ncols = 3;
spmat1[0][0] = 1;
spmat1[0][1] = deg1 + 1;
spmat1[0][2] = ct1;
m=1;
n=0;
for(i=0;i<1;i++){
for(j=0;j<deg1+1;j++){
if(poly1[i][j] != 0){
spmat1[m][n++]=i;
spmat1[m][n++]=j;
spmat1[m][n]=poly1[i][j];
m++;
n=0;
}
}
}
for(i=0;i<nrows1;i++){
for(j=0;j<ncols;j++)
printf(“%dt”,spmat1[i][j]);
printf(“n”);
}
ct2=0;
printf(“nFor second polynomial:”);
for(i=0;i<=deg2;i++){
printf(“nEnter coefficient for exponent %d> “,i);
scanf(“%d”,&poly2[0][i]);
if(poly2[0][i] != 0)
ct2++;
}
nrows2 = ct2 + 1;
ncols = 3;
spmat2[0][0] = 1;
spmat2[0][1] = deg2+1;
spmat2[0][2] = ct2;
m=1;
n=0;
for(i=0;i<1;i++){
for(j=0;j<deg2+1;j++){
if(poly2[i][j] != 0){
spmat2[m][n++]=i;
spmat2[m][n++]=j;
spmat2[m][n]=poly2[i][j];
m++;
n=0;
}
}
}
for(i=0;i<nrows2;i++){
for(j=0;j<ncols;j++)
printf(“%dt”,spmat2[i][j]);
printf(“n”);
}
printf(“nGenerating sum of polynomials…!”);
printf(“nPress any key to continue…”);

deg = deg1<deg2?deg1:deg2;
for(i=0;i<=deg;i++){
poly3[0][i]=poly1[0][i]+poly2[0][i];
ct++;
}
for(;i<=deg1;i++){
poly3[0][i]=poly1[0][i];
ct++;
}
for(;i<=deg2;i++){
poly3[0][i]=poly2[0][i];
ct++;
}
nrows = ct+1;
ncols = 3;
spmat3[0][0]=1;
spmat3[0][1]=ct;
spmat3[0][2]=ct;
m=1;
n=0;
for(i=0;i<1;i++){
for(j=0;j<deg+1;j++){
if(poly3[i][j] != 0){
spmat3[m][n++]=i;
spmat3[m][n++]=j;
spmat3[m][n]=poly3[i][j];
m++;
n=0;
}
}
}
printf(“nnAddition Result:nn”);
for(i=0;i<nrows;i++){
for(j=0;j<3;j++)
printf(“%dt”,spmat3[i][j]);
printf(“n”);
}
for(i=0;i<=deg1;i++)
printf(“(%d*x^%d)->”,poly1[0][i],i);

printf(“bb n”);

for(i=0;i<=deg2;i++)
printf(“(%d*x^%d)->”,poly2[0][i],i);

printf(“bb n”);

for(i=0;i<=deg3;i++)
printf(“(%d*x^%d)->”,poly3[0][i],i);

printf(“bb n”);

}

Comments are closed

x Close

## Like Us On Facebook

You cannot copy content of this page.