

Introduction
Matrixes are useful for so many things. My code demonstrates a small part of it, like solving sets of many equations, finding connections between vectors, multiplying, adding matrixes, and more. I thought that it will be most useful if matrixes wouldl come with the ability to handle complex numbers as well as rationals. I have split the code to the following headers to make it more simple: the main part RationalComlpexMatrix
is based on the RationalComplex
class, that is based on the Rational
class, and the power of RationalComplexMatrix
class is demonstrated in the RationalComplexMatrixCalculator
class. The full documentation is included.
Background
Basics of matrixes:
The entire matrixes operations are based on the Gauss ranking method, here is a simple example for a set of equations:
x1+2x2=4, 2x1=3 can be written:
{ 1 2 4}
{ 2 0 3}
x1 x2 b
As you can see, the first column is x1
's, the second is x2
's, and the third (b
) belongs to the solution part. Gauss said that one can use elementary rows or cols actions on cols and rows, and still keep the basic form of the equation set. I may multiply row, col with a scalar value (rational non-zero value number), add a row to another row, or col to other col. Let's do r2(row 2) /-2
-> now r2
is: (-1, 0, -3/2)
and add it to r1
. Now, we'll do r2*(-1)
:
{1 0 3/2}
Next, we will do r1 /2
or r1*(1/2)
now r1= {0, 1, 5/4}
:
{0 1 5/4}
{1 0 3/2}
Take a look at the first row and solve col(third). We see that x1=3/2
and that x2=5/4
. To make it more easy to see, we can arrange the rows so that the solution of x1
will be in r1
and x2
's solution will be at r2
. To do so, we will arrange the matrix so that the row with most "zeros" from the left will be at the bottom and so on... in our case: we will swap r1
with r2
(r1<->r2
):
{0 1 5/4}
How nice is that... :-)
As you can see, we ignored the solution col(vector). But one may ask when to stop ranking (that's how this process is called), the answer �16:02 �28/�04/�2004 is: one should stop ranking when every Gauss's action, he can do that to null a number, will cause adding a new number. For example, if I'll do now r2+r1
into r1
, not only will I null nothing (not needed action in its own), I will also destroy the zero I have in the first row second column and replace it with 1.
As you can see, we are ignoring the solution vector. All we care about are the variable vectors, but the ordinary rows and cols actions will work on them too due to the fact that they are part of the row. That might seems useless in our case but try to solve a set of 5 equations with 4 variables without ranking the matrix (it can take a while). And what about 300 equations with 400 variables.
Using the code
As written above, the main class is RationalComplexMatrix
. It is a most useful class and the RationalComplexMatrix
merely shows its abilities and usages. Simply open the source code zip file, compile, and link. If you're using VC++ 6, make sure you have the service pack 5 and later (a bug when using friend functions). For your convenience, I've also included a "clean" directory of the RationalComplexMatrix
class without the RationalComplexMatrix
Calculator files. In addition, I have put in an expanded version of the RationalComplexMatrix
. The expanded version can not deal with complex numbers, but it can deal with large scale matrixes. Accuracy cannot be promised when using large scale matrixes (6 x 6.. and above) with the Standard version. The accuracy at the expanded version of the RationalComplexMatrix
is gained thanks to another class, named Mint (multi-integer). The Mint class works just like the regular int
object (32 bits); only it has 337 bits!!!. For further information, read the documentations included. You might like to take a good look at the header of the RationalComplexMatrix
class:
class RationalComplexMatrix
{
short rows, columns;
char *MatrixName;
RationalComplex **matrix;
RationalComplex Vmulmatrix(RationalComplexMatrix leftmatrix ,
RationalComplexMatrix ob ,
int row, int col);
bool vAmIPivot(short CurrantRow,
RationalComplexMatrix *MyParalalMatrix);
void PivotNullity(RationalComplexMatrix *MyParalalMatrix);
void SwapMatrixRows(short RowA,short RowB,
RationalComplexMatrix *Myparalelmatrix);
void SwapMatrixCols(short ColA, short ColB);
void vBreakDownRow(RationalComplexMatrix *MyParalalmatrix);
void vCutDownRow(short CurrentRow,short CurrentCol,
RationalComplexMatrix *MyParalalmatrix);
void vMoveZerosDown(RationalComplexMatrix *Myparalelmatrix);
void RemoveEqual(short rowtodelete,
RationalComplexMatrix *MyParalalmatrix);
void CheckDuplicate(short row,
RationalComplexMatrix *MyParalalmatrix);
bool AmIAllZeros(short CurrentRow) const;
void SwapMatrix(RationalComplexMatrix *MyParalalmatrix);
bool DoIHaveAllZeroLine() const;
public:
RationalComplexMatrix();
RationalComplexMatrix(int m_NumberOfRow,
int n_NumberOfColumn);
RationalComplexMatrix(const RationalComplexMatrix &ob);
~RationalComplexMatrix();
RationalComplex &operator()(int RowNumber, int ColumnNumber);
friend std::ostream &operator <<(std::ostream &,
RationalComplexMatrix &);
friend std::istream &operator >>(std::istream &,
RationalComplexMatrix &);
bool operator==(const RationalComplexMatrix &ob) const;
bool operator!=(RationalComplexMatrix &ob) const;
RationalComplexMatrix &operator=(RationalComplexMatrix &ob) ;
RationalComplexMatrix operator+(RationalComplexMatrix &ob) const;
RationalComplexMatrix operator-(RationalComplexMatrix &ob) const;
RationalComplexMatrix operator*(RationalComplexMatrix ob);
void vSetMatrixName(const char *name);
const char* vGetMatrixName() const;
int vGetMatrixColumns() const;
int vGetMatrixRows() const;
void vFillMatrix();
void MakeSimpler();
void TurnSingularMatrix();
void TurnTransposeMatrix();
void vArrangeMatrix(RationalComplexMatrix &MyParalalmatrix);
void vArrangeMatrix();
void SwapMatrixDir();
bool IsSingular() const;
void TurnInverseMatrix ();
void RemoveCol(short ColNumber);
void RemoveRow(short RowNumber);
};
Points of Interest
This is the first "real" code I'm writing without getting any help from any source, I'm very proud of this piece. The real nice part was to implement the ranking algorithm, and the most time consuming was the debugging and optimizing part. You might be interested to know that I've built a class that implements the simplex optimization method, and it should be here soon.
History
This is the third and improved version of the RationalComplexMatrix
Calculator.
- The ranking algorithm is working faster than ever, with minimum iterations.
- Far better documentation.
- Several bugs fixed.