Frooster

Matrix Ops

May 17th, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.23 KB | None | 0 0
  1. /*
  2.   Name: MATRIX OPERATIONS PROGRAMME
  3.   Author: A.S.Ram Narayanan
  4.   Date: 10-05-15 13:47
  5.   Description: this programme does
  6.                
  7.                equality checking
  8.                addition
  9.                multiplication
  10.                subtraction
  11.                transpose
  12.                chexs for symmetry of matrix
  13.                determinant
  14.                adjoint
  15.                inverse
  16.                
  17.                
  18.     for 3 BY 3 ORDERED MATRIX          
  19. */
  20. #include<iostream>
  21. #include<conio.h>
  22. using namespace std ;
  23. void inverse();
  24. void adjoint();
  25. void matsub();
  26. void add();
  27. void matmult();
  28. void trans();
  29. void sym();
  30. void det();
  31. int A[3][3],B[3][3],r,c,d,C[3][3],i,j,k,D[3][3],T[3][3],E[3][3],f,f1,adj[3][3],F[3][3],T1[3][3],adjB[3][3];
  32. float determinant=0,determinantB=0;
  33.  
  34. int main()
  35. {
  36.  
  37. cout<<"enter the first matrix row"<<endl;
  38.  
  39.     /*input of data in matrix */
  40.    
  41.      
  42. for(r=0;r<3;r++)
  43. {
  44.    for(c=0;c<3;c++)
  45.   {
  46.      cin>>A[r][c];
  47.   }
  48. }
  49.    
  50. cout<<"enter the second matrix to check equality"<<endl;
  51. for(r=0;r<3;r++)
  52. {
  53.    for(c=0;c<3;c++)
  54.   {
  55.      cin>>B[r][c];
  56.   }
  57. }
  58.    
  59. for(r=0;r<3;r++)//checking the matrix equaltiy
  60. {
  61.    for(c=0;c<3;c++)
  62.   {
  63.      if(A[r][c]!=B[r][c])
  64.  
  65.   {
  66.        d=0;
  67.     }
  68.    
  69.    }//end of for loop(col)
  70.  
  71. }//end of row for loop
  72.        
  73. if(d!=0)
  74. cout<<"matrices are equal"<<endl;
  75. else
  76. cout<<"they arent equal"<<endl;
  77.  
  78.    
  79.   /* choice for matrix addtition or subtraction or multiplication */
  80.    
  81. int ch;
  82.  
  83. cout<<"enter your choice"<<endl;
  84. cout<<"1.subtraction"<<"\n"<<"2.addition"<< "\n" <<"3.multiplication"<<endl<<"4.transpose[FOR A and B]"<<endl<<"5.symmetric checking[FOR A and B]"<<endl<<"6.determinant[FOR A and B]"<<endl;
  85. cout<<"7.inverse(FOR A & B)"<<endl<<"8.adjoint(FOR A&B)"<<endl;
  86.    
  87. cin>>ch;
  88.    
  89.   if(ch==1)
  90.    
  91. {
  92.     matsub();//function call of matsub
  93.        
  94.         }
  95.    
  96.    else if(ch==2)
  97.     {
  98.    
  99.     add();//function addtion call
  100.        
  101.        }
  102.    
  103.     else if(ch==3)
  104.     {
  105.        matmult();//function matmult call
  106.    
  107.     }
  108.  
  109.  
  110.     else if(ch==4)
  111.      trans();//call of transpose fumction
  112.    
  113.     else if(ch==5)
  114.     sym();//call of sym function
  115.    
  116.     else if(ch==6)
  117.     det();//call of determinant function
  118.  
  119.    else if(ch==7)
  120.    {
  121.         //cout<<"the inverse of the matrix A is  ";
  122.    inverse();//call of inverse of matrix function
  123. }
  124.    else if(ch==8)
  125.    adjoint();//call of adjoint function
  126.    
  127.   cout<<endl;
  128.    
  129.   system("PAUSE");
  130.    
  131.    return 0;
  132.    
  133.  
  134.    
  135. }
  136.    
  137.  
  138. void matsub()//subtraction function
  139. {  
  140.    
  141. for(i=0;i<3;i++)
  142. {
  143.         for(j=0;j<3;j++)
  144.         {
  145.            C[i][j]=A[i][j]-B[i][j];
  146.            
  147.             }
  148.            
  149.          
  150.    }//end of subtraction operation loop
  151.    
  152.     for(i=0;i<3;i++)
  153.     {
  154.          cout<<"\n";
  155.            
  156.        for(j=0;j<3;j++)
  157.            
  158.         {
  159.                
  160.        cout<<" "<<C[i][j];
  161.                
  162.         }
  163.            
  164.        
  165.       }//end pf output loop
  166.    
  167.    
  168.    
  169. }//end of function
  170.  
  171.  
  172.    
  173.  
  174. void add()//addtiotn function
  175. {
  176.    
  177.    
  178.    for(i=0;i<3;i++)//addition operation
  179.     {
  180.        for(j=0;j<3;j++)
  181.       {
  182.        
  183.       C[i][j]=A[i][j]+B[i][j];
  184.        
  185.           }
  186.        
  187.         }
  188.  
  189. //end of input loop
  190.    
  191. for(i=0;i<3;i++)
  192.     {
  193.          cout<<"\n";
  194.        for(j=0;j<3;j++)
  195.        
  196.         {
  197.            
  198.           cout<<" "<<C[i][j];
  199.            
  200.         }
  201.  
  202.       }//end of output loop
  203.  
  204. }//end of function
  205.    
  206. void matmult()//function for matrix multiplication
  207. {
  208.    
  209.    
  210.     for(i=0;i<3;i++)//start of operation of multiplication
  211.     {
  212.         for(j=0;j<3;j++)
  213.         {
  214.            D[i][j]=0;
  215.            
  216.            for(k=0;k<3;k++)
  217.             {
  218.                D[i][j]=D[i][j]+A[i][k]*B[k][j];
  219.                
  220.                }
  221.                
  222.             }
  223.            
  224.      }//end of loop
  225.    
  226.   for(i=0;i<3;i++)
  227.    
  228.   {
  229.         for(j=0;j<3;j++)
  230.         {
  231.            cout<<" "<<D[i][j];
  232.            
  233.           }
  234.            
  235.         cout<<endl;
  236.        
  237.      }//end of output loop
  238.        
  239.        
  240.  
  241.   }//end of function matmult
  242.  
  243. void trans()//transpose function
  244. {
  245.       cout<<"matrix A"<<endl;
  246.       for(i=0;i<3;i++)//start of operation of transpose
  247.     {
  248.         E[3][3]=0;
  249.         for(j=0;j<3;j++)
  250.         {
  251.            T[j][i]=A[i][j];
  252.         }
  253.  
  254.        }
  255.  
  256.       for(i=0;i<3;i++)// output of transpose
  257.     {
  258.         for(j=0;j<3;j++)
  259.         {
  260.            
  261.           cout<<" "<<T[i][j];
  262.         }
  263.            
  264.           cout<<endl;
  265.  
  266.        }//end of loop for A
  267.        
  268.        
  269.    cout<<"matrix B"<<endl;
  270.       for(i=0;i<3;i++)//start of operation of transpose for B
  271.     {
  272.         E[3][3]=0;
  273.         for(j=0;j<3;j++)
  274.         {
  275.            T[j][i]=B[i][j];
  276.         }
  277.  
  278.        }
  279.  
  280.       for(i=0;i<3;i++)//output of transpose for B
  281.     {
  282.         for(j=0;j<3;j++)
  283.         {
  284.            
  285.           cout<<" "<<T[i][j];
  286.         }
  287.            
  288.           cout<<endl;    
  289.     }//END OF LOOP FOR B
  290.    
  291.  
  292. }//end of function trans
  293.  
  294.  
  295.  
  296. void sym()//symmetry
  297. {
  298. //matrix A  
  299.  
  300. cout<<" checking for matrix A"<<endl;  
  301. for(i=0;i<3;i++)//start of operation of transpose
  302.     {
  303.         for(j=0;j<3;j++)
  304.         {
  305.            T[j][i]=A[i][j];
  306.         }
  307.  
  308.        }
  309.  
  310.       for(i=0;i<3;i++)//start of operation of transpose
  311.     {
  312.         for(j=0;j<3;j++)
  313.         {
  314.            
  315.           cout<<" "<<T[i][j];
  316.         }
  317.            
  318.           cout<<endl;
  319.  
  320.        }
  321.      for(i=0;i<3;i++)//start of operation of transferinf ////elemts to matrix e
  322.     {
  323.         for(j=0;j<3;j++)
  324.         {
  325.            A[i][j]=E[i][j];
  326.         }
  327.  
  328.        }
  329.      
  330.    
  331.         for(i=0;i<3;i++)//start of chexkong of symmetr
  332.     {
  333.         for(j=0;j<3;j++)
  334.         {
  335.            if(T[i][j]!=E[i][j])
  336.             {
  337.               f=0;
  338.              }
  339.  
  340.        }  
  341.      
  342.      }//end of loop
  343.  
  344.    //if else for checking symmetricity  
  345.    
  346.     if(f!=0)
  347.     cout<<" its  a symmetric matrix";
  348.     else
  349.     cout<<"its not a symmetric";
  350.    
  351.     cout<<"\n";
  352.  
  353.  
  354. //matrix B
  355.    
  356. cout<<"checking for matrix B"<<endl;  
  357.    for(i=0;i<3;i++)//start of operation of transpose
  358.     {
  359.         for(j=0;j<3;j++)
  360.         {
  361.            T1[j][i]=B[i][j];
  362.         }
  363.  
  364.        }
  365.  
  366.       for(i=0;i<3;i++)//start of operation of transpose
  367.     {
  368.         for(j=0;j<3;j++)
  369.         {
  370.            
  371.           cout<<" "<<T1[i][j];
  372.         }
  373.            
  374.           cout<<endl;
  375.  
  376.        }
  377.      for(i=0;i<3;i++)//start of operation of transferinf ////elemts to matrix e
  378.     {
  379.         for(j=0;j<3;j++)
  380.         {
  381.            B[i][j]=F[i][j];
  382.         }
  383.  
  384.        }
  385.      
  386.    
  387.         for(i=0;i<3;i++)//start of chexkong of symmetr
  388.     {
  389.         for(j=0;j<3;j++)
  390.         {
  391.            if(T1[i][j]!=F[i][j])
  392.             {
  393.               f1=0;
  394.              }
  395.  
  396.        }  
  397.      
  398.      }//end of loop
  399.  
  400.    //if else for checking symmetricity  
  401.    
  402.     if(f1!=0)
  403.     cout<<" its  a symmetric matrix";
  404.     else
  405.     cout<<"its not a symmetric matrix";
  406.  
  407.    
  408.    
  409.    
  410.        
  411.   }//end of function sym    
  412.  
  413.    
  414.  void det()//det function
  415. {
  416.    determinant= A[0][0]*((A[1][1]*A[2][2])-(A[2][1]*A[1][2]))-A[0][1]*((A[1][0]*A[2][2])-(A[2][0]*A[1][2]))+A[0][2]*((A[1][0]*A[2][1])-(A[2][0]*A[1][1]));
  417.    determinantB=  B[0][0]*((B[1][1]*B[2][2])-(B[2][1]*B[1][2]))-B[0][1]*((B[1][0]*B[2][2])-(B[2][0]*B[1][2]))+B[0][2]*((B[1][0]*B[2][1])-(B[2][0]*B[1][1]));
  418. cout<<"the determinant of matrix A is "<<determinant<<endl;
  419. cout<<"the determinant of matrix B is "<<determinantB<<endl;
  420.  
  421. }//en d of function det
  422.  
  423.  
  424. void adjoint()//adjoint fumction
  425. {
  426.   //for matrix A  
  427.  
  428.   cout<<"adjoint of matrix A is "<<endl;
  429.   for(i=0;i<3;i++)
  430.   {
  431.        for(j=0;j<3;j++)
  432.        {
  433.                   adj[i][j]=0;                  
  434.                   adj[i][j]=(A[(i+1)%3][(j+1)%3]*A[(i+2)%3][(j+2)%3])-(A[(i+1)%3][(j+2)%3]*A[(i+2)%3][(j+1)%3]);
  435.                   }
  436.                   }
  437.  
  438.   for(i=0;i<3;i++)
  439.   {
  440.                   cout<<"\n";
  441.        for(j=0;j<3;j++)
  442.        {
  443.             cout<<" "<<adj[i][j];
  444.            
  445.             }
  446.            
  447.             }//end of loop for matrix a
  448.            
  449.   cout<<"\n"<<endl;          
  450.            
  451.  //for matrix B
  452.  cout<<"adjoint of matrix B is "<<endl;
  453.  for(i=0;i<3;i++)
  454.   {
  455.        for(j=0;j<3;j++)
  456.        {
  457.                   adjB[i][j]=0;
  458.                   adjB[i][j]=(B[(i+1)%3][(j+1)%3]*B[(i+2)%3][(j+2)%3])-(B[(i+1)%3][(j+2)%3]*B[(i+2)%3][(j+1)%3]);
  459.                  
  460.          }
  461.                  
  462.      }
  463.      
  464.    for(i=0;i<3;i++)
  465.   {
  466.                   cout<<"\n";
  467.        for(j=0;j<3;j++)
  468.        {
  469.             cout<<" "<<adjB[i][j];
  470.            
  471.             }
  472.            
  473.             }  
  474.      
  475. }//end of function adjoint
  476.  
  477.  
  478. void inverse()//inverse function
  479. {
  480.  
  481.  determinant= A[0][0]*((A[1][1]*A[2][2])-(A[2][1]*A[1][2]))-A[0][1]*((A[1][0]*A[2][2])-(A[2][0]*A[1][2]))+A[0][2]*((A[1][0]*A[2][1])-(A[2][0]*A[1][1]));
  482.  
  483.   if(determinant!=0)
  484.   {
  485.        cout<<"the inverse of the matrix A is "<<endl;      
  486.    for(i=0;i<3;i++)//output loop
  487.   {
  488.    cout<<"\n";
  489.        for(j=0;j<3;j++)
  490.        {
  491.               cout<<" "<<((A[(i+1)%3][(j+1)%3]*A[(i+2)%3][(j+2)%3])-(A[(i+1)%3][(j+2)%3]*A[(i+2)%3][(j+1)%3]))/determinant;
  492.               }
  493.               }
  494.        }
  495.        
  496.     else
  497.     {
  498.         cout<<"the inverse of this matrix A is not possible since the determinant is 0"<<endl;
  499.         }  
  500.   cout<<endl;      
  501.        
  502.   // for matrix B
  503.  
  504.  determinantB=  B[0][0]*((B[1][1]*B[2][2])-(B[2][1]*B[1][2]))-B[0][1]*((B[1][0]*B[2][2])-(B[2][0]*B[1][2]))+B[0][2]*((B[1][0]*B[2][1])-(B[2][0]*B[1][1]));
  505.   if(determinantB!=0)
  506.   {
  507.        cout<<"the inverse of the matrix B is "<<endl;      
  508.    for(i=0;i<3;i++)//output loop
  509.   {
  510.    cout<<"\n";
  511.        for(j=0;j<3;j++)
  512.        {
  513.               cout<<" "<<((B[(i+1)%3][(j+1)%3]*B[(i+2)%3][(j+2)%3])-(B[(i+1)%3][(j+2)%3]*B[(i+2)%3][(j+1)%3]))/determinant;
  514.               }
  515.               }
  516.        }
  517.        
  518.     else
  519.     {
  520.         cout<<"the inverse of this matrix B is not possible since the determinant is 0"<<endl;
  521.         }                                  
  522.  
  523.        
  524.        
  525. }//end of function
  526.  
  527.  
  528.  
  529. THE OUTPUT
Advertisement
Add Comment
Please, Sign In to add comment