1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
//inverse function
void Inverse(double A[][maxSize], double B[][maxSize], int N)
{
int row, column, step, i;
double mult, C[maxSize][2*maxSize] = {{0}};
for(row=0; row<N; row++)
for(column=0; column<N; column++)
C[row][column] = A[row][column];
for(i=0; i<N; i++)
C[i][N+i] = 1;
for(step=0; step<N-1; step++)
{
for(row=step+1; row<N; row++)
{
mult = C[row][step] / C[step][step];
for (column=step;column<2*N; column++)
C[row][column]-= mult*C[step][column];
}
}
for(step=1; step<=N-1; step++)
{
for(row=N-step-1; row>=0; row--)
{
mult = C[row][N-step] / C[N-step][N-step];
for (column=N; column<2*N; column++)
C[row][column]-=
mult*C[N-step][column];
}
}
for(row=0; row<N; row++)
for(column=0; column<N; column++)
B[row][column] = C[row][N+column] / C[row][row];
}
// solve function
void matrix::Solve()
{
double B[][columns];
double b[];
double x[];
int N;
int row, column, step, iStep, i;
double sum, maxElement, temp, mult;
double A[columns][columns+1];
for(row=0; row<N; row++)
{
for(column=0; column<N; column++)
A[row][column] = B[row][column];
A[row][N] = b[row];
}
for (step=0; step<N-1; step++)
{
maxElement=A[step][step];
iStep=step;
for ( row=step+1; row<N; row++ )
if(fabs(maxElement)< fabs(A[row][step]))
{
maxElement=A[row][step];
iStep=row;
}
for (column=step; column<=N; column++)
{
temp=A[step][column];
A[step][column]=A[iStep][column];
A[iStep][column]=temp;
}
for (row=step+1; row<N; row++)
{
mult=A[row][step]/A[step][step];
for (column=step; column<=N; column++)
A[row][column]-= mult*A[step][column];
}
}
x[N-1]=A[N-1][N]/A[N-1][N-1];
for (row=N-2; row>=0; row--)
{
sum=0;
for (column=row+1; column<N; column++)
sum+= A[row][column]*x[column];
x[row]=(A[row][N] - sum)/A[row][row];
}
}
//determinant function
double Determinant(double A[][maxSize], int N)
{
int row, column, step, i;
double mult;
for(step=0; step<N-1; step++)
{
for(row=step+1; row<N; row++)
{
mult = A[row][step] / A[step][step];
for (column=step; column<N; column++)
A[row][column]-= mult*A[step][column];
}
}
mult = 1;
for(row=0; row<N; row++)
mult = mult * A[row][row];
return mult;
}
|