Can anyone please help me that how this program works??
i have got this Program on internet and i want it to run.....

#include <cstdlib>
double** gauss(double **matrix, int dimension)
{
    double **inverse;
    inverse = (double**) malloc(dimension * sizeof (double *));
    for (int i = 0; i < dimension; i++)
        inverse[i] = (double*) malloc(dimension * sizeof (double));


    for (int i = 0; i < dimension; i++)
        for (int j = 0; j < dimension; j++)
            inverse[i][j] = 0;

    for (int i = 0; i < dimension; i++)
        inverse[i][i] = 1;

    for (int k = 0; k < dimension; k++)
    {
        for (int i = k; i < dimension; i++)
        {
            double valInv = 1.0 / matrix[i][k];
            for (int j = k; j < dimension; j++)
                matrix[i][j] *= valInv;
            for (int j = 0; j < dimension; j++)
                inverse[i][j] *= valInv;
        }
        for (int i = k + 1; i < dimension; i++)
        {
            for (int j = k; j < dimension; j++)
                matrix[i][j] -= matrix[k][j];
            for (int j = 0; j < dimension; j++)
                inverse[i][j] -= inverse[k][j];
        }
    }

    for (int i = dimension - 2; i >= 0; i--)
    {
        for (int j = dimension - 1; j > i; j--)
        {
            for (int k = 0; k < dimension; k++)
                inverse[i][k] -= matrix[i][j] * inverse[j][k]; 
            for (int k = 0; k < dimension; k++)
                matrix[i][k] -= matrix[i][j] * matrix[j][k]; 
        }
    }
    return inverse;
}

This is not a program, it is a function. What it does is to compute the inverse of a matrix through a Gaussian elimination. The implementation itself is very naive, meaning that it is inefficient, numerically unstable, and also programmed in bad style, not to mention that this is basically in C.

The function first allocates the memory for the resulting matrix. Then, it initializes the resulting matrix to the identity matrix (as the right-hand-side of the linear system of equations). Then, it does a number of row reductions in order to end up with an upper-triangular matrix. And finally, it performs a back-substitution to obtain the inverse matrix.

I suggest you read up on Gaussian elimination algorithm, because this function is a pretty straight-forward implementation of it. If you understand Gaussian elimination, you should have no problems understanding that function.

Thnkz for the reply......
how to pass the values to this function.....?

Well, you need to have a matrix that you need to invert. For a simple test program:

#include <iostream>

void printMatrix(double** M, int dim) {
  for(int i = 0; i < dim; ++i) {
    for(int j = 0; j < dim; ++j)
      std::cout << M[i][j] << " ";
    std::cout << std::endl;
  };
};

int main() {
  double** A = new double*[3];
  for(int i = 0; i < 3; ++i)
    A[i] = new double[3];
  // fill the matrix with values:
  A[0][0] = 3; A[0][1] = 4;  A[0][2] = 5;
  A[1][0] = 6; A[1][1] = 7;  A[1][2] = 8;
  A[2][0] = 9; A[2][1] = 10; A[2][2] = 11;

  std::cout << " A = " << std::endl;
  printMatrix(A,3);

  double** A_inv = gauss(A,3);

  // now, A_inv points to the inverse of A!
  std::cout << " A_inv = " << std::endl;
  printMatrix(A_inv,3);

  // never forget to clean up when you are done:
  for(int i = 0; i < 3; ++i)
    delete[] A[i];
  delete[] A;
  for(int i = 0; i < 3; ++i)
    free(A_inv[i]);
  free(A_inv);
  return 0;
};

Just a remark, if you didn't know how to call a function, or how to create such a matrix representation, then what are you doing searching to web for matrix inversion code? The learning process is a sequential process, you must get the basics first and build up from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.