Hello, there:

I am trying to calculate to the inverse of matrix A.

How to call function to main part? How to write the code for a matrix?

Can someone help me modify the errors?

Thank you very much!

--------

#include "matrix.h"
Matrix solve(const Matrix& A, const Matrix& B){
/*
solve A x = B and return the answer.
It is assumed that A can be factored without pivoting.
The factors are not saved
*/
if( A.row!=A.col || A.row!=B.row )
error("Matrix solve: incompatible sizes\n");
Matrix C(A),X(B); // local copies of A and B
int i,j,k,cr=A.row,xc=B.col;
double a,b;
// First preprocess C
for(i=0; i<cr; i++){ // for each row of C
if( C.m[i*cr+i] == 0.0 ) error(" zero divide in solve ");
a = (C.m[i*cr+i] = 1./C.m[i*cr+i]); 
for( j=i+1; j<cr; j++ ) // normalize the current row
C.m[i*cr+j] *=a;
for( k=i+1; k<cr; k++ ){// for each row after i modify the row
if((b = C.m[k*cr+i])!=0.0){ // but only if it is needed.
for( j=i+1; j<cr; j++ ) 
C.m[k*cr+j] -= b*C.m[i*cr+j];
}
}
}
// now solve the equations
for(k=0; k<xc; k++){ // for each column of X
for(i=0; i<cr; i++ ){ // forward solution
for( j=0; j<i; j++ )
X.m[i*xc+k] -= C.m[i*cr+j]*X.m[j*xc+k];
X.m[i*xc+k] *= C.m[i*cr+i];
}
for(i=cr-2; i>=0; i--) // back solution
for(j=i+1; j<cr;j++ )
X.m[i*xc+k] -= C.m[i*cr+j]*X.m[j*xc+k];
}
return X;
}
main() {
double A[3][3] = {{0, 0, 6}, {1/3, 0, 0}, {0, 1/2, 0}};
double B[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};

A beginner

Please format your code and use whitespace. It's practically unreadable in in't present form

#include "matrix.h"
Matrix solve (const Matrix& A, const Matrix& B)
{
/*  solve A x = B and return the answer.
    It is assumed that A can be factored without pivoting.
    The factors are not saved
*/
    if (A.row != A.col  ||  A.row != B.row )
        error ("Matrix solve: incompatible sizes\n");

    Matrix C(A), X(B);         // local copies of A and B
    int i, j, k, cr=A.row, xc=B.col;
    double a, b;

    // First preprocess C
    for (i = 0; i < cr; i++)
    {                        // for each row of C
        if (C.m[i*cr+i] == 0.0 ) error(" zero divide in solve ");
        a = (C.m[i*cr+i] = 1./C.m[i*cr+i]); 
        for (j = i+1; j < cr; j++ )     // normalize the current row
[b]etc...[/b]
Member Avatar for iamthwee

Why don't you take it a step at a time.

So do you know how to call a function and say pass in a 2d array?

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.