Friday, January 25, 2013

Procedure for Creating Sparse Matrix

Procedure of Sparse Matrix

Procedure createsparematrix(A: array[1..30, 1..30] of Integers; M,N:Integers)
    B: array[0..30, 1..3] of Integers
    i,j,k : Integers
begin
     B[0,1]=M;
     B[0,2]=N;
     K=0;
     For i= 1 to m do
     Begin
        For j= 1 to n do
        Begin
             If A[i, j]<> 0 then
               Begin
                 K=k+1;
                 B[k,1]=i;
                 B[k,2]=j
                 B[k,3]= A[i,,j];
               end
        end
     end
 B[0,3]=k;
End


Program for Sparse Matrix

program
  A= array[1…30, 1…30] of integers;
  B= array[0 …30, 1…3] of integers;
   i,j,m.n; Integers
begin
   Writeln(“enter size of matrix”);
    Read(m,n)
   Writeln(“enter elements into sparse matrix”);
   For i= 1 to n
   For j= 1 to m
    Read(A[i,j]);
  
  Createsparematrix(A,B,m,n);
  Writeln(“3-tuples is as follows”);
  For i= 0 to B[0,3]
  For j = 1 to 3
    Begin
       Write (B[i, j],   “   “);
    End
    Writeln;
end

Sparse Matrices

Sparse Matrices

            A matrix were most of the elements are zeros is called sparse matrix.  Sparse matrix cannot be efficiently handled, when we are dealing with matrices of size 100 X 100 and if only 1000 entries are non-zeros and remaining location are filled with zeros.  These leads to huge amount of memory wastage.  The waste memory locations are 10000-1000=9000 i.e., 9000 memory location filled with zeros.  Hence huge amount of memory is wasted.  To avoid this we use another alternative representation, which will explicitly stores non-zero elements.  This representation is called 3-Tuples or Triples.

            In this representation, each element of the matrix is characterized by its row and column position say I, j.  We will store the matrix as a list of three tuples in the form of (I,  j, value).

Consider a sparse matrix shown below


         0    1   0   0
B=    2    0   0  -3
         0    0   -4  0
          

The 3- tuples representation of above matrix is as follows

B
Row
Column
value
0
3
4
4
1
1
2
1
2
2
1
2
3
2
4
-3
4
3
3
-4


The size of these 3-tuples B[0..t, 1..3] where t= no. of non-zero elements, which 4 in the above matrix.  The no. of columns of 3-tuples is always fixed as 3.  Hence it is called as 3-tuples.  The no. of rows in 3-tuples is equal to no. of non-zero elements of the sparse matrix + 1.  Here

B[0,1]= No. of rows of the sparse matrix.
B[0,2]= No. of column of the sparse matrix
B[0,3]= No. of non-zero elements of the sparse matrix

B[i,1]  represents row position of the non-zero element  (where i=1..t)
B[i,2]  represents column position of the non-zero element  (where i=1..t)
B[i,3]  represents actual non-zero elements in the matrix.

 Consider a sparse matrix shown below
 
         4    0   1   0   0   0
B=    0    0   0   0   0   9
         0    7   0   0   0   1
                            0    0  12  0   0   0
                            0    0    1  0   5   0
                            0   24   0  6   0   0



B
Row
Column
value
0
6
6
10
1
1
1
4
2
1
3
1
3
2
6
9
4
3
2
7
5
3
6
1
6
4
3
12
7
5
3
1
8
5
5
5
9
6
2
24
10
6
4
6


C Program to Multiply Two Polynomials

C program to multiply two Polynomial

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

int mult[20];
void display(int a[])
{
  int i;
  for(i=0; i<2*a[0]+1;i++)
  {
    printf("%d, ",a[i]);
  }
  printf("\n");
}

void addpoly(int a[], int b[],int c[])
{
   int i,j,k,temp;
   i=1;j=1;k=1;
   while ((i<=2*a[0]) && (j<=2*b[0])) /* both arrays are existing */
   {
     if (a[i]>b[j])
     {
            c[k]=a[i];
            c[k+1]=a[i+1];
            k=k+2;i=i+2;
     }
     else if (b[j]>a[i])
     {
            c[k]=b[j];
            c[k+1]=b[j+1];
            k=k+2;j=j+2;
     }
     else if (a[i]==b[j])
     {
            temp=a[i+1]+b[j+1];
            if (temp==0)
            {
             i=i+2; j=j+2;
            }
            else
            {
              c[k]=a[i];  /* either a[i] or b[j] can be assigned since both powers are equal */
              c[k+1]=temp;
              k=k+2; j=j+2; i=i+2;
            }
     }
   }
   /* A is not exhausted and B is Exhausted */
   while ( (i<=2*a[0]) && (j>=2*b[0]) )
   {
     c[k]=a[i];
     c[k+1]=a[i+1];
     i=i+2;
     k=k+2;
   }
   /* A is exhausted and B is not Exhausted */
   while ( (i>=2*a[0]) && (j<=2*b[0]) )
   {
     c[k]=b[j];
     c[k+1]=b[j+1];
     j=j+2;
     k=k+2;
   }
   c[0]=k/2;
}

void polymult(int a[], int b[],int mult[])
{
  int d[20], i,j;
  mult[0]=1;mult[1]=-32768;mult[2]=-32768;
  d[0]=1;i=1;j=1;
  display(mult);
  display(d);
  while (i<=2*a[0])
  {
    j=1;
    while (j<=2*b[0])
    {
      d[1]=a[i]+b[j];
      d[2]=a[i+1]*b[j+1];
      j=j+2;
      addpoly(mult,d,mult);
      display(d);
      display(mult);
    }
    i=i+2;
  }
 // c[0]=c[0];
}


void main()
{
  int a[20],b[20],c[20],i;
  clrscr();
  printf("Enter no. of terms in first polynomial\n");
  scanf("%d",&a[0]);
  printf("enter elements in first polynomials i.e. power and coff.\n");
  for(i=1;i<2*a[0]+1; i++)
  {
    scanf("%d",&a[i]);
  }
  printf("Enter no. of terms in Second polynomial\n");
  scanf("%d",&b[0]);
  printf("enter elements in second polynomials i.e. power and coff.\n");
  for(i=1;i<2*b[0]+1; i++)
  {
    scanf("%d",&b[i]);
  }
  display(a);
  display(b);
  /*addpoly(a,b,c);
  printf("Result of Addition of Two Polynomials= \n");
  display(c);*/
  polymult(a,b,mult);
  printf("Result of Multiplication of Twol Polynomials=\n");
  display(mult);
  getch();
}

C Program to Add Polynomials

C program for adding of polynomials is illustrated here

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

void display(int a[])
{
  int i;
  for(i=0; i<2*a[0]+1;i++)
  {
    printf("%d, ",a[i]);
  }
  printf("\n");
}

void addpoly(int a[], int b[],int c[])
{
   int i,j,k,temp;
   i=1;j=1;k=1;
   while ((i<=2*a[0]) && (j<=2*b[0])) /* both arrays are existing */
   {
     if (a[i]>b[j])
     {
            c[k]=a[i];
            c[k+1]=a[i+1];
            k=k+2;i=i+2;
     }
     else if (b[j]>a[i])
     {
            c[k]=b[j];
            c[k+1]=b[j+1];
            k=k+2;j=j+2;
     }
     else if (a[i]==b[j])
     {
            temp=a[i+1]+b[j+1];
            if (temp==0)
            {
             i=i+2; j=j+2;
            }
            else
            {
              c[k]=a[i];  /* either a[i] or b[j] can be assigned since both powers are equal */
              c[k+1]=temp;
              k=k+2; j=j+2; i=i+2;
            }
     }
   }
   /* A is not exhausted and B is Exhausted */
   while ( (i<=2*a[0]) && (j>=2*b[0]) )
   {
     c[k]=a[i];
     c[k+1]=a[i+1];
     i=i+2;
     k=k+2;
   }
   /* A is exhausted and B is not Exhausted */
   while ( (i>=2*a[0]) && (j<=2*b[0]) )
   {
     c[k]=b[j];
     c[k+1]=b[j+1];
     j=j+2;
     k=k+2;
   }
   c[0]=k/2;
}


void main()
{
  int a[20],b[20],c[20],i;
  clrscr();
  printf("Enter no. of terms in first polynomial\n");
  scanf("%d",&a[0]);
  printf("enter elements in first polynomials i.e. power and coff.\n");
  for(i=1;i<2*a[0]+1; i++)
  {
    scanf("%d",&a[i]);
  }
  printf("Enter no. of terms in Second polynomial\n");
  scanf("%d",&b[0]);
  printf("enter elements in second polynomials i.e. power and coff.\n");
  for(i=1;i<2*b[0]+1; i++)
  {
    scanf("%d",&b[i]);
  }
  display(a);
  display(b);
  addpoly(a,b,c);
  printf("Result of Two Polynomials= \n");
  display(c);
  getch();
}