How can I define this algorithm to calculate the matrix that has uneven nr of rows and columns?
//==============================================================================
//
// Linear System Solution by Gauss method
//
//
//
//==============================================================================
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <cmath>
//==============================================================================
void VectorPrint(int nDim, double* pfVect)
{
int i;
//==============================================================================
// return 1 if system not solving
// nDim - system dimension
// pfMatr - matrix with coefficients
// pfVect - vector with free members
// pfSolution - vector with system solution
// pfMatr becames trianglular after function call
// pfVect changes after function call
//
//
//
//==============================================================================
int LinearEquationsSolving(int nDim, double* pfMatr, double* pfVect, double* pfSolution)
{
double fMaxElem;
double fAcc;
int i , j, k, m;
for(k=0; k<(nDim-1); k++) // base row of matrix
{
// search of line with max element
fMaxElem= fabs(pfMatr[k*nDim + k]);
m = k;
for(i=k+1; i<nDim; i++)
{
if(fMaxElem < fabs(pfMatr[i*nDim + k]) )
{
fMaxElem = pfMatr[i*nDim + k];
m = i;
}
}
// permutation of base line (index k) and max element line(index m)
if(m != k)
{
for(i=k; i<nDim; i++)
{
fAcc = pfMatr[k*nDim + i];
pfMatr[k*nDim + i] = pfMatr[m*nDim + i];
pfMatr[m*nDim + i] = fAcc;
}
fAcc = pfVect[k];
pfVect[k] = pfVect[m];
pfVect[m] = fAcc;
}
return 0;
}
//==============================================================================
// testing of function
//==============================================================================
Not 100% sure what the question is, but I'll have a go:
First some nitpicking:
* Very difficult to read the code if it isn't in code tags.
* Why include <iostream> if you are only going to use printf?
* The values in fMatr don't fill the memory allocated for the matrix (8 x 11 instead of 11 x 11).
* No values for fVec were given (needs 11 values).
1) If the number of rows/columns is an odd number ("uneven"), Gaussian elimination still works (where the inverse of the matrix exists).
or, what I think you actually meant...
2) If the number of rows is different from the number of columns (ie, the matrix is not square) then consider the general problem:
Solve A x = b, for the unknown x, where A is an [m x n] matrix, x is a n-vector, and b is a m-vector.
There are 3 possibilities:
i) m == n: the number of equations is equal to the number of unknowns, and a unique solution may exist (depends if the matrix is nonsingular). Gaussian elimination or LU decomposition is appropriate. Example: Solving a set of linear simultaneous equations.
ii) m < n: the number of equations is less than the number of unknowns, in which case the system is underdetermined. There exists no unique solution, and a space of solutions exist, all which satisfy the original equation. Gaussian elimination will not solve this problem. QR decomposition or the Singular Value Decomposition may be used to find the range/null space of the problem, but again, no unique solution necessarily exists. Example: Describing the remaining degrees of freedom for a mechanical system, where constraints have been imposed.
iii) m > n: the number of equations is more than the number of unknowns, in which case the system is overdetermined. In general no solution exists, but is it common to find a solution that minimises the least squared error ||A x - b||_2^2. Gaussian elimination will not help here also. QR decomposition is the usual way to "solve" this problem. Example: Finding the coefficients of a polynomial given a set of data.
Cases ii and iii usually represent vastly different physical problems to case i, so I suggest you think carefully about the question you are trying to ask.
If you don't know what I'm talking about, I would suggest reading:
Matrix Computations 3rd Edition, Golub and van Loan, 1996.
This book describes how to solve all the various problems I listed above, and gives algorithms in a MATLAB-like pseudocode, which is relatively easy to translate into C/C++.