Monday, 4 September 2017

Matrix Programs - P8 Unit/Identity Matrix in C Language

Introduction

The identity matrix is a the simplest nontrivial diagonal matrix, defined such that
1 x (X) = X

for all vectors x. An identity matrix may be denoted 1,I,E (the latter being an abbreviation for the German term "Einheitsmatrix"; Courant and Hilbert 1989, p. 7), or occasionally , with a subscript sometimes used to indicate the dimension of the matrix. Identity matrices are sometimes also known as unit matrices (Akivis and Goldberg 1972, p. 71).[1]
 I=[1 0 ... 0; 0 1 ... 0; | | ... |; 0 0 ... 1].



identity-matrix.png
Image Courtesy: wikipedia.org
Programs:
#include <stdio.h>
#define ID 15

int main(){
   int i,j;
   int identity=ID+1;
   int a[ID][ID];
   /*Loop until the user enter's the data correctly */
   while(identity>ID){
       printf("Enter the value for identity Matrix<%d:", ID);
       scanf("%d", &identity);
   }
   /*Input the value for identity matrix in array */
   for(i=0;i<identity;i++){
       for(j=0;j<identity;j++){
           /* if i=j then the value is always 1 in identity matrix */
           if(i==j){
               a[i][j]=1;
           }
           /* if i!=j then the value is always 0 in identity matrix */
           else{
               a[i][j]=0;
           }
       }
   }
   printf("\n");
   /* Print the entire identity matrix */
   for(i=0;i<identity;i++){
       printf("\t|");
       for(j=0;j<identity;j++){
           printf(" %d ", a[i][j]);
       }
       printf("|\n");
   }
}
Output:
Enter the value for identity Matrix<15:5

       | 1  0  0  0  0 |
       | 0  1  0  0  0 |
       | 0  0  1  0  0 |
       | 0  0  0  1  0 |
       | 0  0  0  0  1 |

Process returned 5 (0x5)   execution time : 4.578 s
Press any key to continue.

References:

  1. Akivis, M. A. and Goldberg, V. V. An Introduction to Linear Algebra and Tensors. New York: Dover, 1972.

  2. Ayres, F. Jr. Schaum's Outline of Theory and Problems of Matrices. New York: Schaum, p. 10, 1962.

  3. Courant, R. and Hilbert, D. Methods of Mathematical Physics, Vol. 1. New York: Wiley, 1989.

  4. Weisstein, Eric W. "Identity Matrix." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/IdentityMatrix.html

Cite this as:

Bhagchandani, Niraj. “Matrix Programs - P8 Unit/Identity Matrix in C Language.” Everything Data Structure in C Language, Blogger, 4 Sept. 2017, edatastructure.blogspot.in/2017/09/matrix-programs-p8-unitidentity-matrix.html.

About This article:

No comments:

Post a Comment