Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Matrix inverse in C [req]

Status
Not open for further replies.

anoop12

Member level 5
Member level 5
Joined
Nov 29, 2006
Messages
89
Helped
6
Reputation
12
Reaction score
3
Trophy points
1,288
Activity points
1,867
c program to find inverse of a matrix

hello everyone,
I am trying to write C program to find inverse of a matrix.
Do anyone have it?
Thanks
 

c program to find the inverse of a matrix

use numerical recipes in C!!!

enjoy
 

matrix inverse in c

Hope This will help...

/************************************************************************/
/* Please Note: */
/* */
/* (1) This computer program is written by Tao Pang in conjunction with */
/* his book, "An Introduction to Computational Physics," published */
/* by Cambridge University Press in 1997. */
/* */
/* (2) No warranties, express or implied, are made for this program. */
/* */
/************************************************************************/

#include <stdio.h>
#include <math.h>
#define NMAX 100

void migs (a,n,x,indx)

/* Function to invert matrix a[][] with the inverse stored
in x[][] in the output. Copyright (c) Tao Pang 2001. */
int n;
int indx[];
double a[NMAX][NMAX],x[NMAX][NMAX];
{
int i,j,k;
double b[NMAX][NMAX];
void elgs();

if (n > NMAX)
{
printf("The matrix dimension is too large.\n");
exit(1);
}

for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
b[j] = 0;
}
}
for (i = 0; i < n; ++i)
{
b = 1;
}

elgs (a,n,indx);

for (i = 0; i < n-1; ++i)
{
for (j = i+1; j < n; ++j)
{
for (k = 0; k < n; ++k)
{
b[indx[j]][k] = b[indx[j]][k]-a[indx[j]]*b[indx][k];
}
}
}

for (i = 0; i < n; ++i)
{
x[n-1] = b[indx[n-1]]/a[indx[n-1]][n-1];
for (j = n-2; j >= 0; j = j-1)
{
x[j] = b[indx[j]];
for (k = j+1; k < n; ++k)
{
x[j] = x[j]-a[indx[j]][k]*x[k];
}
x[j] = x[j]/a[indx[j]][j];
}
}
}

void elgs (a,n,indx)

/* Function to perform the partial-pivoting Gaussian elimination.
a[][] is the original matrix in the input and transformed
matrix plus the pivoting element ratios below the diagonal
in the output. indx[] records the pivoting order.
Copyright (c) Tao Pang 2001. */

int n;
int indx[];
double a[NMAX][NMAX];
{
int i, j, k, itmp;
double c1, pi, pi1, pj;
double c[NMAX];

if (n > NMAX)
{
printf("The matrix dimension is too large.\n");
exit(1);
}

/* Initialize the index */

for (i = 0; i < n; ++i)
{
indx = i;
}

/* Find the rescaling factors, one from each row */

for (i = 0; i < n; ++i)
{
c1 = 0;
for (j = 0; j < n; ++j)
{
if (fabs(a[j]) > c1) c1 = fabs(a[j]);
}
c = c1;
}

/* Search the pivoting (largest) element from each column */

for (j = 0; j < n-1; ++j)
{
pi1 = 0;
for (i = j; i < n; ++i)
{
pi = fabs(a[indx][j])/c[indx];
if (pi > pi1)
{
pi1 = pi;
k = i;
}
}

/* Interchange the rows via indx[] to record pivoting order */

itmp = indx[j];
indx[j] = indx[k];
indx[k] = itmp;
for (i = j+1; i < n; ++i)
{
pj = a[indx][j]/a[indx[j]][j];

/* Record pivoting ratios below the diagonal */

a[indx][j] = pj;

/* Modify other elements accordingly */

for (k = j+1; k < n; ++k)
{
a[indx][k] = a[indx][k]-pj*a[indx[j]][k];
}
}
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top